| Issue |
238186
|
| Summary |
[InstCombine] ldexp(x, K) folded to fmul by a subnormal 2^K under input denormal flush returns 0
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
20000419
|
With an input denormal mode of `preservesign`, `positivezero`, or `dynamic`, InstCombine folds `ldexp(x, K)` to `fmul x, 2^K` when `2^K` is subnormal. The multiply then flushes that constant to zero, so a normal result becomes a signed zero. `ldexpf(2.0f, -127)` is `2^-126`, the minimum normal (`0x00800000`). The folded `fmul` by `2^-127` (`0x00400000`) returns `+0`. The same split holds for the negative input (`0x80800000` vs `-0`) and for `ldexp(2.0, -1023)` in double. The default IEEE input mode keeps a correct fold. Output flush is not what changes this result.
Reproducer (`opt -S -passes=instcombine`):
```llvm
define float @scale(float %x) denormal_fpenv(preservesign) {
%r = call float @llvm.ldexp.f32.i32(float %x, i32 -127)
ret float %r
}
```
Output: `fmul float %x, f0x00400000`. Expected: the `llvm.ldexp` call, or an `fmul` by a normal power of two. `opt` 22.1.8 rejects the `denormal_fpenv` spelling and still folds the legacy `"denormal-fp-math"="preserve-sign,preserve-sign"` form to `fmul float %x, 0x3800000000000000` (the same `2^-127`). Executing that IR at `-O0` with MXCSR FTZ|DAZ returns `00000000` / `80000000`.
https://godbolt.org/z/96dobher1
LangRef `llvm.ldexp`: "This function multiplies the first argument by 2 raised to the second argument's power. ... If the result underflows a zero with the same sign is returned." The exponent is an integer. `2.0 * 2^-127` is the minimum normal, so this call does not underflow. Input flush applies to a denormal floating-point operand of the operation. Here that operand is the normal `±2.0`, so flushing it leaves the value unchanged. `KnownFPClass::ldexp` models the same split: it flushes the source operand, then scales.
LangRef `denormal_fpenv`: "If the input mode is `preservesign`, or `positivezero`, a floating-point operation must treat any input denormal value as zero." The folded `fmul` has a denormal constant operand, so that multiply is required to return zero. For `dynamic`, "Transformations which depend on the behavior of denormal values should not be performed." Equality of this fold depends on the subnormal constant surviving. Output `preservesign` only allows a denormal result to be flushed, and this result is normal. `-fdenormal-fp-math=preserve-sign,ieee` with FTZ and not DAZ stays correct. `-fdenormal-fp-math=ieee,preserve-sign` with DAZ|FTZ is wrong. The positive constant flushes to `+0` under both `preservesign` and `positive-zero`, so both modes return a signed zero.
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 <stdint.h>
#include <stdio.h>
#include <string.h>
static void daz(void) {
unsigned m;
__asm__ volatile("stmxcsr %0" : "=m"(m));
m |= 0x8040u;
__asm__ volatile("ldmxcsr %0" :: "m"(m));
}
static uint32_t bits(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
__attribute__((noinline)) static float scale(float x) {
return __builtin_ldexpf(x, -127);
}
__attribute__((noinline)) static float scale_n(float x) {
return __builtin_ldexpf(x, -126); /* 2^-126 is normal; stays correct */
}
int main(void) {
daz();
uint32_t a = bits(scale(2.f)), b = bits(scale(-2.f)), c = bits(scale_n(2.f));
if (a != 0x00800000u || b != 0x80800000u || c != 0x01000000u) {
printf("got %08x %08x %08x expected 00800000 80800000 01000000\n", a, b, c);
return 1;
}
return 0;
}
```
`clang -O2 -fno-math-errno -fdenormal-fp-math=preserve-sign t.c -lm`
`-fno-math-errno` is what forms `llvm.ldexp`. With the default `-fmath-errno`, clang keeps a libcall and every opt level matches. `-ffast-math -fdenormal-fp-math=preserve-sign` fails the same way. `-O2` assembly is `mulss` of `.long 0x00400000`. `-O0` is `callq ldexpf`.
Results (`00800000` / `80800000` correct, `00000000` / `80000000` wrong; the `-126` control stays `01000000`):
- 17.0.6, 19.1.7, 20.1.8, 21.1.8: correct at -O0/-O1/-O2/-O3. 21.1.8 still emits `llvm.ldexp`.
- 18.1.8: not tested. The binary does not start (`libtinfo.so.5`).
- 22.1.8, 23.1.5, and trunk 24.0.0 (cf44dd9e317e, 2026-10-31; also Compiler Explorer clang trunk on 2026-11-03): correct at -O0, wrong at -O1/-O2/-O3. No diagnostic.
- `positive-zero` and `dynamic` on 24.0.0: same split.
- double `ldexp(±2.0, -1023)` on 24.0.0: `0010000000000000` / `8010000000000000` at -O0, `+0` / `-0` at -O2 (`fmul` by `2^-1023`).
- gcc 14.2.0 -O0..-O3 (`-fno-math-errno`, MXCSR set in the program): correct.
- UBSan and ASan (`-fsanitize=undefined,address -fno-sanitize-recover=all`) on 23.1.5 and 24.0.0, -O0 and -O2: no diagnostic. -O2 still exits 1 on the wrong bits.
https://godbolt.org/z/s1YsTj1M8 (clang trunk vs 21.1.0, `-emit-llvm`: trunk gives `fmul nnan float %0, f0x00400000`, 21 keeps `llvm.ldexp.f32.i32(float %0, i32 -127)`; the trunk executor prints `got 00000000 80000000 01000000`)
Cause: `Intrinsic::ldexp` in `InstCombinerImpl::visitCallInst` (`llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp:3509-3521` on main at 2d7136ea92e9):
```cpp
// ldexp(x, K) -> fmul x, 2^K
if (match(Exp, m_ConstantInt(ConstExp))) {
APFloat Scaled = scalbn(APFloat::getOne(FPTy), (int)ConstExp, rmNearestTiesToEven);
if (!Scaled.isZero() && !Scaled.isInfinity()) { // subnormal 2^K is accepted
Constant *FPConst = ConstantFP::get(Src->getType(), Scaled);
return BinaryOperator::CreateFMulFMF(Src, FPConst, II);
}
}
```
Nothing reads the denormal mode. The guard only rejects a scale that is already 0 or inf.
History: #171731 (5eb2ec2179b2, 2025-12-11) added the fold with no range check, including `fmul` by 0 for a fully underflowing exponent. That failed an llvm-libc test that expects a subnormal result, and #171895 (757c5b3bc70c) reverted it. #171977 (6e47d4ef45f1, 2025-12-12) reapplied it and skipped `isZero()` / `isInfinity()` only. A subnormal power of two still folds. That is the code in 22.1.8 through trunk. In-tree `@ldexp_neg127` and `@ldexp_neg149` lock the IEEE fold in, with no denormal attribute.
I searched open and closed issues and PRs (rechecked 2026-11-03). No report or fix of this fold. Nearby but different: #229985 (demanded FP-class for `ldexp` under positive-zero), #237491 (NVPTX `ldexp.f16`), #214845 (KnownFPClass for `ldexp`), #230614 (`ppcf128` constant folding), #216414 and #66794 (the reverse `fmul`-by-pow2 to `ldexp` idea).
Possible fix (not tested): skip the `fmul` when `Scaled.isDenormal()` and the input mode may flush (`inputsMayBeZero()`, which covers `preservesign`, `positivezero`, and `dynamic`). Keep it for IEEE.
Related: #237855 (same denormal_fpenv input-flush theme, different fold)
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