-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core/compiler_hints: add assume() hint #19354
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Do you have any good example for testing where the use of assume()
makes a difference, or do you plan to switch some initial hot candidates over in the original PR already?
ee6ffd9
to
c1f63ca
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we name this macro assume, what will a macro pointing to https://clang.llvm.org/docs/AttributeReference.html#id468 / https://en.cppreference.com/w/cpp/language/attributes/assume ,which do not check the assumption, be called?
maybe our assume is just safer in the debug case?
If compilers provide any specialized form, it'd be possible to implement the (non-debug ... in debug mode it's probably helpful to check) assume function through their builtins. But outside corner cases, I doubt they'd make any difference: The one provided here is only evaluated if there's an external function or side effect invoked (why would one do that in an assume?), otherwise it may look like evaluating something, but by the outcome going into the unreachable, the compiler doesn't have to (and won't) emit code but just use the learned properties of the affected variables. |
wouldn't the preprocessor rewrite https://godbolt.org/z/xojhj9ab8 |
Ah, thanks for the example. I find it highly weird (but at the same time
very consistent) that these new attributes (are they really becoming a
thing in C?) are not protected against preprocessor.
But well.
Is there any good reason why this can't be a static inline function in
the first place? (assert can't be one, for it may not want to evaluate,
but that's not an issue here for the assumed expression is always
"evaluated").
|
+1 for giving IMO we should also provide a semantic patch to let the CI insist on |
The reason why a define is nicer is that it doesn't care about types. If I have a But this is not a mayor thing, if you really prefer the |
OK, let's have a define. It would be good to have this as a drop-in replacement as much as possible. |
05bc635
to
a5bc1b3
Compare
bors merge |
Build succeeded:
|
Contribution description
This behaves like an
assert()
withDEVELHELP=1
, but whenDEVELHELP=0
it still gives the compiler a hint that the condition is guaranteed to be true and that it can optimize the code accordingly.Testing procedure
Issues/PRs references
alternative to #19337