Issue 239446
Summary [ConstantFold][SelectionDAG] fpext of a subnormal float constant ignores denormal_fpenv input flush
Labels new issue
Assignees
Reporter 20000419
    With `denormal_fpenv(ieee|preservesign)` (or `positivezero`), `fpext` of the subnormal float constant 2^-127 is folded to the normal double 2^-127 (`0x3800000000000000`). `fpext` is a floating-point operation, so under input flush its subnormal float input must be treated as zero and the result must be `+0.0`. The result is a normal double, so the output mode does not explain it. Without the attribute the fold is correct.

```llvm
define double @fpext() denormal_fpenv(ieee|preservesign) {
  %r = fpext float 0x3800000000000000 to double ; 2^-127
  ret double %r
}
```

`opt -S -passes=instsimplify t.ll` (also SCCP/IPSCCP and `-O2`):

```
  ret double f0x3800000000000000
```

Expected: `ret double 0.0`.

https://godbolt.org/z/K6h9xr5WY (opt trunk, `-S -passes=instsimplify`)

The backend has the same fold. `llc -mtriple=x86_64-linux-gnu t.ll` without any IR pass loads `.quad 0x3800000000000000`, and `-mtriple=aarch64-linux-gnu` materializes `0x3800000000000000`. The instruction is still unfolded when it reaches ISel (`-stop-after=codegenprepare`), so the fold is in `SelectionDAG::getNode`, `case ISD::FP_EXTEND`. https://godbolt.org/z/jqa9nG7v6 (llc trunk)

End to end (x86-64):

```c
#include <stdio.h>
#include <string.h>
#include <xmmintrin.h>
__attribute__((constructor)) static void d(void) { _mm_setcsr(_mm_getcsr() | 0x8040); }
int main(void) {
  volatile float o = 0x1p-127f;
  float c = 0x1p-127f;
  double a = o, b = c;
  unsigned long long x, y;
  memcpy(&x, &a, 8); memcpy(&y, &b, 8);
  printf("%016llx %016llx\n", x, y); /* expected: both 0000000000000000 */
  return 0;
}
```

`clang -fdenormal-fp-math=preserve-sign t.c`:

- `-O0`: `0000000000000000 0000000000000000`
- `-O2`: `0000000000000000 3800000000000000`

The runtime `cvtss2sd` with DAZ gives the expected `+0.0`; only the folded constant differs. https://godbolt.org/z/q7fP6dM5E (clang trunk, `-O2`, executed)

Versions: `opt`/`llc`/`clang` 19.1.7, 20.1.8, 21.1.8, 22.1.8 (with `"denormal-fp-math"="ieee,preserve-sign"`), 23.1.5 and 24.0.0 (cf44dd9e317e, `denormal_fpenv`) all fold to `0x3800000000000000`; also wrong on godbolt trunk (rechecked 2026-11-06). `opt` 15.0.6–17.0.6 wrong too with the string attribute. At `-O0` the C test is correct on 19–24.

Suspected source: the cast folds do not get the function's denormal mode. `ConstantFoldCastInstruction` in `llvm/lib/IR/ConstantFold.cpp` (`case Instruction::FPExt`, ~line 231) and the fall-through from `ConstantFoldCastOperand` in `llvm/lib/Analysis/ConstantFolding.cpp` (~line 1682) convert the raw APFloat; in SelectionDAG it is `getNode` `case ISD::FP_EXTEND` (~line 7926 of `SelectionDAG.cpp`). `fptrunc` of a subnormal double has the matching problem in principle, but I only found cases where the IEEE and flushed results are the same.

Possible fix (not tested): flush the source constant with the input mode of the source type before converting (and give up for `dynamic`), in both the IR cast fold and the DAG fold.

Related but different: #219300 and #236656 fix `fpext`/`fptrunc` in KnownFPClass and SimplifyDemandedFPClass, not the constant fold. #229111 and #236172 add a denormal-aware `tryFoldFP` helper, but its `FPOp` list is only Add/Sub/Mul/Div, the six min/max kinds, FMA and Ceil/Floor/Trunc/Round/RoundEven, plus compares; neither patch touches `fpext`/`FP_EXTEND`. #238620 is the same class of bug for `ldexp`/`frexp`/`sqrt`/`pow`/`powi` in ConstantFolding, not casts.

cc @arsenm @LewisCrawford

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

Reply via email to