[Host-dependent][ARM vs x86] ConstantFoldBinaryFP affects DCE consistency
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
When compiling for some(ARM) target, the behavior of ConstantFoldBinaryFP may differ depending on the host machine (x86 vs ARM). This is because constant folding is done on the host, and floating-point environment differences can trigger except on one host but not the other. This affects dead code elimination (DCE): code that could be removed on one host remains on another.
**Example code:**
Compilation command `clang -O2 test.c -lm`
```
// test.c
#include
#include
#include
#include
int main()
{
double a_pow, b_pow, c_x;
a_pow = 1.7976931348623157e+308;
c_x = -0.9999876894265599;
b_pow = pow(a_pow, c_x);
return 0;
}
```
**Built on x86 host machine, the ASM is as follows:**
```
main:
mov r0, #0
bx lr
```
**Built on arm host machine, the ASM is as follows:**
```
main:
push {r11, lr}
vldr d0, .LCPI0_0
vldr d1, .LCPI0_1
bl pow
mov r0, #0
pop {r11, pc}
.LCPI0_0:
.long 4294967295
.long 2146435071
.LCPI0_1:
.long 785361781
.long 3220176870
```
When compiling for the same target, llvm_fenv_testexcept produces inconsistent results depending on the host machine. This leads to host-dependent behavior in constant folding and dead code elimination.
Has the considered addressing this host-dependence? Alternatively, are there recommended strategies or best practices to ensure consistent floating-point folding across different host platforms?
https://github.com/llvm/llvm-project/blob/ca84e9e8260983d17f02a76de8b1ee51cd3ed896/llvm/lib/Analysis/ConstantFolding.cpp#L2153-L2160
Contributor guide
Assessment
This issue has not been assessed yet.