| Issue |
243217
|
| Summary |
[SLPVectorizer] Miscompile: copyable @llvm.fma lane injects fma(-0.0, +0.0, x) on non-FP passthrough values.
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
lntue
|
After #240039 (cac5de14d5a7, `[SLP]Use fma as copyable main op without fmuladd lanes`), `SLPVectorizer` can bundle a single `@llvm.fma` lane with an arbitrary passthrough value `x` (such as a `load`, `phi`, or function argument) by synthesizing `@llvm.fma(-0.0, +0.0, x)` for the copyable lane.
Unlike absorbing an existing `fadd` or `fmul` lane, synthesizing a new FP arithmetic operation on a value that had **no** FP math operation in the source IR is not semantics-preserving under default LLVM FP rules.
### Minimal Reproducer
Run:
```bash
opt -mtriple=aarch64-unknown-linux-gnu -passes=slp-vectorizer -S repro.ll
```
Input (`repro.ll`):
```llvm
define void @test(ptr noalias %dst, double %a, double %b, ptr noalias %c) {
%c0 = load double, ptr %c, align 8
%c1_ptr = getelementptr inbounds double, ptr %c, i64 1
%c1 = load double, ptr %c1_ptr, align 8
%f0 = call double @llvm.fma.f64(double %a, double %b, double %c0)
store double %f0, ptr %dst, align 8
%d1_ptr = getelementptr inbounds double, ptr %dst, i64 1
store double %c1, ptr %d1_ptr, align 8
ret void
}
```
Output at current `main` (fbd4bcc8e9e3):
```llvm
define void @test(ptr noalias %dst, double %a, double %b, ptr noalias %c) {
%1 = load <2 x double>, ptr %c, align 8
%2 = insertelement <2 x double> <double poison, double -0.000000e+00>, double %a, i64 0
%3 = insertelement <2 x double> <double poison, double 0.000000e+00>, double %b, i64 0
%4 = call <2 x double> @llvm.fma.v2f64(<2 x double> %2, <2 x double> %3, <2 x double> %1)
store <2 x double> %4, ptr %dst, align 8
ret void
}
```
### Counterexamples on Lane 1 (`dst[1]`)
In the input IR, `%c1` has no floating-point operation (`load` -> `store`), so per LangRef ([Behavior of Floating-Point NaN values](https://llvm.org/docs/LangRef.html#behavior-of-floating-point-nan-values)), `dst[1]` must preserve all 64 representation bits of `c[1]` verbatim. In the transformed IR, lane 1 evaluates `@llvm.fma(-0.0, +0.0, %c1)`:
1. **Signaling NaN quieting (default FP mode):**
- Let `c[1] = 0x7ff0000000000001` (`sNaN`).
- Source stores `0x7ff0000000000001`; target (`fmla.2d` / `vfmadd213pd`) quiets the `sNaN` and stores `0x7ff8000000000001`.
2. **Quiet NaN sign and payload loss (default FP mode / Alive2):**
- Let `c[1] = 0xfff8000000000042` (`-qNaN` with payload).
- Source deterministically stores `0xfff8000000000042`. In target, `@llvm.fma` has a nondeterministic NaN output sign per LangRef (Alive2 rejects this transform for NaN inputs), and on targets with canonical NaN propagation (e.g. RISC-V) hardware rewrites it to `0x7ff8000000000000`.
3. **Subnormal flushing under `"denormal-fp-math"`:**
- Adding `"denormal-fp-math"="preserve-sign"` (or `"positive-zero"`) to `@test` still produces the same transformed IR.
- Let `c[1] = 0x0000000000000001` (`5e-324`): source stores `0x0000000000000001`, whereas target flushes the subnormal through `fma` to `+0.0` (`0x0000000000000000`).
4. *(Secondary, runtime rounding mode without `strictfp`)* **`+0.0` under `FE_DOWNWARD`:**
- If `c[1] = +0.0`, `fma(-0.0, +0.0, +0.0)` evaluates `(-0.0) + (+0.0)` which yields `-0.0` under IEEE-754 `roundTowardNegative` instead of `+0.0`.
*(Note: the same issue applies more generally whenever SLP turns a non-FP passthrough value `x` into an identity FP binop such as `fadd(x, -0.0)` or `fmul(x, 1.0)` without `nnan` and IEEE denormal mode.)*
### Expected Behavior
Synthesizing an identity `@llvm.fma` (or `fadd`/`fmul`/`fsub`) on a copyable lane that is **not** itself an absorbed FP instruction (`fadd`/`fmul`/`fsub`) should only be allowed when `nnan` holds on the vectorized operation and the function uses IEEE denormal handling (`"denormal-fp-math"="ieee"`).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs