| Issue |
237855
|
| Summary |
[InstCombine] floor/ceil of a known subnormal folded to -1.0/+1.0 ignoring denormal_fpenv(preservesign)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
20000419
|
With `denormal_fpenv(preservesign)` (what `-fdenormal-fp-math=preserve-sign` emits), InstCombine folds `floor(x)` to `-1.0` when `x` is known to be a negative subnormal, and `ceil(x)` to `+1.0` when `x` is known to be a positive subnormal. Under that mode the input is flushed to a signed zero first, so the correct results are `-0.0` and `+0.0`. The fold fires when there is a demanded-class query that reaches the call, e.g. a `nofpclass(nan)` return (what `-fno-honor-nans` emits). Without that, nothing is folded. With the default IEEE mode the fold is correct. It also happens with `dynamic` and `positivezero`.
Reproducer (`opt -S -passes=instcombine`):
```llvm
define nofpclass(nan) float @floor_nsub(float %x) denormal_fpenv(preservesign) {
entry:
%c = call i1 @llvm.is.fpclass.f32(float %x, i32 16) ; negative subnormal
br i1 %c, label %t, label %e
t:
%r = call float @llvm.floor.f32(float %x)
ret float %r
e:
ret float 4.200000e+01
}
define nofpclass(nan) float @ceil_psub(float %x) denormal_fpenv(preservesign) {
entry:
%c = call i1 @llvm.is.fpclass.f32(float %x, i32 128) ; positive subnormal
br i1 %c, label %t, label %e
t:
%r = call float @llvm.ceil.f32(float %x)
ret float %r
e:
ret float 4.200000e+01
}
```
Output: `ret float -1.000000e+00` in `@floor_nsub` and `ret float 1.000000e+00` in `@ceil_psub`. Expected: `-0.0` and `+0.0`, or no fold. `is.fpclass` does not flush, so the branch is taken for a real subnormal.
LangRef (`denormal_fpenv`): "If the input mode is `preservesign`, or `positivezero`, a floating-point operation must treat any input denormal value as zero." For `%x = 0x80000001`, `floor` sees `-0.0` and must return `-0.0`.
https://godbolt.org/z/hhfWccKGE
Self-checking C reproducer (x86-64; sets DAZ/FTZ in MXCSR to match `-fdenormal-fp-math=preserve-sign`, exits 1 on the wrong result):
```c
#include <stdio.h>
#include <string.h>
#include <math.h>
static unsigned bits(float f) { unsigned u; memcpy(&u, &f, 4); return u; }
__attribute__((noinline, optnone)) static float src(void) {
unsigned u = 0x80000001u; /* negative min subnormal */
float f; memcpy(&f, &u, 4); return f;
}
static float floor_nsub(float x) {
return __builtin_isfpclass(x, 16) ? floorf(x) : 42.0f;
}
int main(void) {
unsigned m;
__asm__ volatile("stmxcsr %0" : "=m"(m));
m |= 0x8040u; /* FTZ|DAZ */
__asm__ volatile("ldmxcsr %0" :: "m"(m));
unsigned g = bits(floor_nsub(src()));
printf("got %08x expected 80000000\n", g);
return g != 0x80000000u;
}
```
`clang -O2 -fdenormal-fp-math=preserve-sign -fno-honor-nans t.c -lm`
Results (`80000000` = `-0.0` is correct, `bf800000` = `-1.0` is wrong):
- 17.0.6, 18.1.0, 19.1.7, 20.1.8, 21.1.8, 22.1.8: correct at -O0/-O1/-O2/-O3.
- 23.1.5 and trunk (cf44dd9e317e): correct at -O0, **wrong at -O1 and above**. No diagnostic.
- The IR reproducer also folds with `opt` 22.1.8 (with the older `"denormal-fp-math"="preserve-sign,preserve-sign"` spelling); 21.1.8 does not fold. In 22 the C select form is not affected.
- UBSan and ASan are clean.
https://godbolt.org/z/nv8rv1Pds (clang trunk / 23.1.0 / 22.1.0, `-emit-llvm`: 23 and trunk give `select nnan i1 %2, float -1.000000e+00, float 4.200000e+01`, 22 keeps `llvm.floor`)
Cause: `InstCombinerImpl::SimplifyDemandedUseFPClass`, rounding-intrinsic case (`InstCombineSimplifyDemanded.cpp:3419-3423` on main):
```cpp
if (IID == Intrinsic::floor && KnownSrc.isKnownAlways(fcNegSubnormal))
return ConstantFP::get(VTy, -1.0);
if (IID == Intrinsic::ceil && KnownSrc.isKnownAlways(fcPosSubnormal))
return ConstantFP::get(VTy, 1.0);
```
These were added in #174842 and don't check the input denormal mode. #219700 (for #217412) made `KnownFPClass::roundToIntegral` and the demanded masks DAZ-aware but left these two returns. The `-0.0` fold just above (lines 3415-3417) has the same problem under `positivezero`: a negative subnormal reads as `+0.0`, so `ceil` should give `+0.0`, but it is folded to `-0.0`.
I searched open and closed issues and found no report of this. Nearby but different: #100661, #185253, #229988.
Possible fix (not tested): only do the ±1.0 / -0.0 folds when the function's input denormal mode is IEEE.
cc @arsenm
Disclaimer: This test case was found with LLM-assisted targeted testing, and the report was written with LLM assistance.
Assisted-by: Grok
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs