Earlier quoted context omitted.
These annotations are really only of interest in performance-critical computations. It’s another knob for library writers to use to make the libraries you use magically faster for their users. And, should be quite rare outside of libraries.
And even then they should be handled with extreme care, as they can trigger UB if used incorrectly.
C++ Attribute: Likely, Unlikely
61–70 of 70 posts
Re: C++ Attribute: Likely, Unlikely
#62See also: https://blog.aaronballman.com/2020/08/dont-use-the-likely-or... tl;dr: these attributes are absolutely full of footguns because the standard is not explicit about precedence and nesting, and you should probably avoid them and prefer to spend time investing in PGO. It’s very easy to make sane-looking code containing these attributes which does the exact opposite of what you intended. Note that this issue doe…
Re: C++ Attribute: Likely, Unlikely
#63I often write my code pessimally in this regard so have a note in the back of my mind to someday use these in a few hot paths. When I say “pessimally” I mean I usually check the unlikely cases right away and then put the normal case last: Blah foo (something& arg) { if (is_invalid (arg)) return blah(0); if (is_inactive (arg)) return blah(1); // ok do all the normal stuff } It makes the code clearer but slightly slowe…
> I usually check the unlikely cases right away and then put the normal case last. Don't we all?
Re: C++ Attribute: Likely, Unlikely
#64What everybody should read before using these (Aaron Ballman is a Senior Staff Compiler Engineer for Intel and is the lead maintainer of the Clang open source compiler): https://blog.aaronballman.com/2020/08/dont-use-the-likely-or...
PGO is a mixed blessing and detracts a bit from the thrust of the article. The more obvious conclusion is to continue using builtin_expect (on the boolean guard of a branch) which works great and has done for ages.
Re: C++ Attribute: Likely, Unlikely
#65HGO, Hunch-guided Optimisation
Re: C++ Attribute: Likely, Unlikely
#66Re: C++ Attribute: Likely, Unlikely
#67What everybody should read before using these (Aaron Ballman is a Senior Staff Compiler Engineer for Intel and is the lead maintainer of the Clang open source compiler): https://blog.aaronballman.com/2020/08/dont-use-the-likely-or...
This is a pretty coherent argument that that new feature is broken and best ignored. Hopefully that's the approach clang will take. PGO is a mixed blessing and detracts a bit from the thrust of the article. The more obvious conclusion is to continue using builtin_expect (on the boolean guard of a branch) which works great and has done for ages.
However better not give data to the function that contradicts the condition if you don't want to figth nasal daemons.
Re: C++ Attribute: Likely, Unlikely
#68Earlier quoted context omitted.
> I could always write a conditional for the hot path up front, but code is for human readers, right? Does C or C++ actually make any promises that it’ll assume the “true” branch of the conditional will be taken? I always assumed that the compiler could make whatever weird decision it wants for that sort of thing. TBH I’d probably just write normal stuff as a function, and then call that function directly in cases wh…
No. The traditional compiler heuristic is to assume backwards branches are taken (loops) and forward branches are not.
Re: C++ Attribute: Likely, Unlikely
#69Earlier quoted context omitted.
This is a pretty coherent argument that that new feature is broken and best ignored. Hopefully that's the approach clang will take. PGO is a mixed blessing and detracts a bit from the thrust of the article. The more obvious conclusion is to continue using builtin_expect (on the boolean guard of a branch) which works great and has done for ages.
That is also covered by new C++ attributes, namely [[assume(expr)]]. However better not give data to the function that contradicts the condition if you don't want to figth nasal daemons.
Edit. It's because assume does not map to expect, it maps to builtin_assume. So that's just another way to write undefined c++.
Re: C++ Attribute: Likely, Unlikely
#70Earlier quoted context omitted.
That is also covered by new C++ attributes, namely [[assume(expr)]]. However better not give data to the function that contradicts the condition if you don't want to figth nasal daemons.
Glad assume(expr) is available, if it maps onto builtin-expect but confused by the UB reference. Why would taking the less likely path be undefined behaviour? Edit. It's because assume does not map to expect, it maps to builtin_assume. So that's just another way to write undefined c++.