This is an archive of the discontinued LLVM Phabricator instance.

[libcxx] Add flag to disable __builtin_assume in _LIBCPP_ASSERT
ClosedPublic

Authored by aeubanks on Apr 5 2022, 7:20 PM.

Details

Summary

Introduce _LIBCPP_ASSERTIONS_DISABLE_ASSUME which makes _LIBCPP_ASSERT
not call __builtin_assume when _LIBCPP_ENABLE_ASSERTIONS == 0.

Calling __builtin_assume was introduced in D122397.

__builtin_assume is generally supposed to improve optimizations, but can
cause regressions when LLVM has trouble handling the calls to
llvm.assume() (see comments in D122397).

Diff Detail

Event Timeline

aeubanks created this revision.Apr 5 2022, 7:20 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 5 2022, 7:20 PM
aeubanks requested review of this revision.Apr 5 2022, 7:20 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 5 2022, 7:20 PM
Herald added a reviewer: Restricted Project. · View Herald Transcript
philnik added inline comments.
libcxx/include/__assert
36–45

I think the disable things flags are normally 'define it to disable this part', so the flag should be _LIBCPP_ASSERTIONS_DISABLE_ASSUME. That would allow you to remove this whole block and just have !defined(_LIBCPP_ASSERTIONS_DISABLE_ASSUME) in line 51.

Thanks for the patch @aeubanks. I agree with @philnik's suggestion, the macro should be "defined => no assume, undefined => assume". That way, it would be defined by default but we'd still have the escape hatch until we figure out issues with the builtin. Do you have an ETA for that (it's more than just this libc++ issue -- if Clang has a broken builtin, we need to fix it ASAP).

aeubanks updated this revision to Diff 420929.Apr 6 2022, 9:42 AM

use disable define

Thanks for the patch @aeubanks. I agree with @philnik's suggestion, the macro should be "defined => no assume, undefined => assume". That way, it would be defined by default but we'd still have the escape hatch until we figure out issues with the builtin. Do you have an ETA for that (it's more than just this libc++ issue -- if Clang has a broken builtin, we need to fix it ASAP).

I don't have an ETA, so far I have one patch that only partially solves the very reduced case I was looking at, it still needs followup work and also followup investigation to see if there are any remaining issues.

aeubanks retitled this revision from [libcxx] Add flag to turn on/off __builtin_assume in _LIBCPP_ASSERT to [libcxx] Add flag to disable __builtin_assume in _LIBCPP_ASSERT.Apr 6 2022, 10:53 AM
aeubanks edited the summary of this revision. (Show Details)
philnik accepted this revision as: philnik.Apr 7 2022, 8:41 AM

LGTM

This revision was not accepted when it landed; it landed in state Needs Review.Apr 7 2022, 9:03 AM
This revision was automatically updated to reflect the committed changes.

@aeubanks Hi, I just came across this again, did you ever get around to fixing the broken builtin? It would be nice to remove _LIBCPP_ASSERTIONS_DISABLE_ASSUME.

@aeubanks Hi, I just came across this again, did you ever get around to fixing the broken builtin? It would be nice to remove _LIBCPP_ASSERTIONS_DISABLE_ASSUME.

sorry, I never fixed the regressions, I'll try to find some time to take a look at this again

I was talking to some people about this and due to the way assumes are handled in the IR, it's not an automatic win to sprinkle assumes everywhere. There is inherently a tradeoff when you add assumes (assumes are instructions and are control flow dependent, adding assumes can prevent some optimizations) so they should be added carefully. Was there a concrete speedup seen when you initially landed this? But either way I think there needs to be a way to opt out of this behavior.

I was talking to some people about this and due to the way assumes are handled in the IR, it's not an automatic win to sprinkle assumes everywhere. There is inherently a tradeoff when you add assumes (assumes are instructions and are control flow dependent, adding assumes can prevent some optimizations) so they should be added carefully. Was there a concrete speedup seen when you initially landed this? But either way I think there needs to be a way to opt out of this behavior.

No, see https://discourse.llvm.org/t/llvm-assume-blocks-optimization/71609/11?u=ldionne.

I'll disable them by default -- this is nuts!

See D153968 for the patch that stops using __builtin_assume unconditionally.