Issue 237719
Summary [SelectionDAG][LegalizeTypes] Fast-math flags of one user leak onto a shared soft-promoted f16 value: `llvm.maximum(NaN, z)` returns z on RISC-V without Zfh
Labels backend:RISC-V, miscompilation, floating-point, llvm:SelectionDAG, generated by fuzzer
Assignees
Reporter YuanchengJiang
    The following code:

```cpp
/* [SelectionDAG][LegalizeTypes] fast-math flags of one user leak onto a shared soft-promoted operand
 * (f16 soft promotion: the fp16_to_fp(x) built for an `fcmp nnan` / `fmul nnan` user is re-used, through the
 * type legalizer's replaced-node remapping, by an unrelated precise user of x).
 *
 * f(): x = (_Float16)fx is used by a fast-math helper (x < y ? x*y : y, stored to *p) and by a precise
 * __builtin_elementwise_maximum(x, z) (= llvm.maximum.f16, must propagate NaN).  On RISC-V without Zfh, the
 * f32 extension of x that feeds llvm.maximum carries `nnan`, so lowerFMAXIMUM_FMINIMUM skips x's NaN check
 * and emits a bare fmax.s: maximum(NaN, 2.0) returns 2.0.
 *
 * Godbolt: x86-64 clang (trunk), flags: --target=riscv64-linux-gnu -march=rv64gc -O2 -DNOHDR
 *   asm of f: one `feq.s` (z's check) before `fmax.s`; correct code (-O0, or no pragma) has two.
 *   (Godbolt cannot execute RISC-V; run with qemu-riscv64 / on an RV64 host.)
 * EXPECTED (clang -O0, gcc):  maximum((_Float16)NaN, 2.0) = nan (bits 0x7e00), expected nan exit 0
 * BUGGY (clang trunk -O2):    maximum((_Float16)NaN, 2.0) = 2.000000 (bits 0x4000), expected nan   exit 1
 * Executed under an RV64 emulator: -O0 -> 32256 (0x7e00),
 * -O2 -> 16384 (0x4000).
 */
#ifdef NOHDR
int printf(const char *, ...);
void *memcpy(void *, const void *, unsigned long);
#define NAN __builtin_nanf("")
#else
#include <stdio.h>
#include <string.h>
#include <math.h>
#endif

static inline void side(_Float16 x, _Float16 y, _Float16 *p) {
#pragma float_control(precise, off)
  *p = (x < y) ? x * y : y;
}

/* precise code: llvm.maximum must propagate a NaN x */
__attribute__((noinline)) _Float16 f(float fx, _Float16 y, _Float16 z, _Float16 *p) {
  _Float16 x = (_Float16)fx;
  side(x, y, p);
  return __builtin_elementwise_maximum(x, z);
}

int main(void) {
  volatile float n = NAN;
  _Float16 tmp;
  _Float16 r = f(n, (_Float16)1.0f, (_Float16)2.0f, &tmp);
  unsigned short u;
  memcpy(&u, &r, 2);
 printf("maximum((_Float16)NaN, 2.0) = %f (bits 0x%04x), expected nan\n", (double)(float)r, u);
  return !((u & 0x7c00) == 0x7c00 && (u & 0x3ff));
}
```

Resulted in this output:
```
$ ./repro   # clang --target=riscv64-linux-gnu -march=rv64gc -O2, run in an RV64 emulator
maximum((_Float16)NaN, 2.0) = 2.000000 (bits 0x4000), expected nan (exit 1)
$ ./repro   # same with -O0
maximum((_Float16)NaN, 2.0) = nan (bits 0x7e00), expected nan           (exit 0)

; f at -O2: only z is NaN-checked before fmax.s; x's check is missing
	call	__extendhfsf2 ; z -> f32
	feq.s	a0, fa0, fa0
	bnez	a0, .LBB0_5
	fmv.s	fs1, fa0
.LBB0_5:
	fmax.s	fa0, fs1, fa0          ; fmax.s(NaN, 2.0) = 2.0
```

| x | z | expected maximum(x, z) | actual |
|---|---|------------------------|--------|
| NaN | 2.0 | NaN (0x7e00) | 2.0 (0x4000) |

Trunk: https://godbolt.org/z/MPoY81ErG (x86-64 clang trunk, `--target=riscv64-linux-gnu -march=rv64gc -O2 -DNOHDR`, assembly view; reproduces as of 2026-10-23)

Root cause: The type legalizer builds every node created while legalizing `N` under `FlagInserter(DAG, N->getFlags())` (llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.cpp). So the `fp16_to_fp(x')` that `SoftPromoteHalfOp_SETCC` builds for the `fcmp nnan` user gets `nnan`. The `fmul nnan` user builds a twin. `SelectionDAG::UpdateNodeOperands` returns the existing node without intersecting flags, and the twin is recorded in `ReplacedValues`. The later flag-less `llvm.maximum` user gets a CSE hit on the twin, and `DAGTypeLegalizer::RemapValue` hands it the original node, which still carries `nnan`. `isKnownNeverNaN` trusts that flag, so RISC-V `lowerFMAXIMUM_FMINIMUM` drops x's NaN check and emits a bare `fmax.s`. The leak occurs on every target that soft-promotes f16 (MIPS, ARM and PowerPC too), but RISC-V is where it was seen to produce wrong code.

Suggested fix: Make `SelectionDAG::UpdateNodeOperands` (all overloads) intersect the existing node's flags with `N`'s when it returns an existing node, and make `DAGTypeLegalizer::RemapValue` intersect the remapped node's flags with those of the node the user reached. Alternatively, stop applying the legalized node's flags to nodes built for its operands. The first approach is validated locally (a local tree carries it, and `f` then NaN-checks both operands).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to