by toredo1729_2 · 1y ago · 0 comments
x > c ? y : 0.;
It annoyed me many times and it still does.
(Compilers obviously do this transformation, including GCC, but it is not always beneficial, especially on x86-64.)
[0] https://clang.llvm.org/docs/LanguageExtensions.html#builtin-...
https://www.godbolt.org/z/ffEvvjhz8
PS: and it also doesn't matter whether a ternary is used or a traditional if (as one would expect):
https://www.godbolt.org/z/zjb4KdqvK
(the float version also appears to not use branches: https://www.godbolt.org/z/98bdheKK4)
For such simple expression I would expect the compiler to pick the right output pattern based on the target CPU though...
Unfortunately, the heuristic that calculates the expense often gets things wrong. That is why OpenZFS passes -mllvm -x86-cmov-converter=false to Clang for certain files where the LLVM heuristic was found to do the wrong thing:
https://github.com/openzfs/zfs/commit/677c6f8457943fe5b56d7a...
There is an open LLVM issue regarding this:
https://github.com/llvm/llvm-project/issues/62790
The issue explains why __builtin_unpredictable() does not address the problem. In short, the metadata is dropped when an intermediate representation is generated inside clang since the IR does not have a way to preserve the information.