Issue 239711
Summary [mlir][vector] `vector.fma` lowers to `llvm.fmuladd` despite documented fused semantics
Labels mlir
Assignees
Reporter lin334141-cell
    ## Summary

The ODS definition of `vector.fma` in `mlir/include/mlir/Dialect/Vector/IR/VectorOps.td` explicitly states:
> *"The semantics of this operation correspond to those of the `llvm.fma` intrinsic."*

However, `VectorFMAOp1DConversion` in `mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp` actually emits `LLVM::FMulAddOp` (mapping to `llvm.fmuladd.*`):

```cpp
rewriter.replaceOpWithNewOp<LLVM::FMulAddOp>(
 fmaOp, adaptor.getLhs(), adaptor.getRhs(), adaptor.getAcc());
```

Unlike `llvm.fma`, which guarantees strict IEEE-754 single-rounding fused multiply-add, `llvm.fmuladd` is an optimization hint that allows the backend to perform separate multiplication and addition with intermediate rounding if a hardware FMA instruction is not available.

Consequently, `vector.fma` does not preserve the single-rounding guarantee promised by its specification.

## Minimal Reproducer

```mlir
// min.mlir
func.func @test_fma(%a: vector<4xf32>, %b: vector<4xf32>, %c: vector<4xf32>) -> vector<4xf32> {
  %r = vector.fma %a, %b, %c : vector<4xf32>
  return %r : vector<4xf32>
}
```

Run lowering:
```bash
mlir-opt min.mlir --convert-vector-to-llvm
```

### Actual Lowered IR
The lowering emits `llvm.intr.fmuladd`:
```mlir
%0 = llvm.intr.fmuladd(%arg0, %arg1, %arg2) : (vector<4xf32>, vector<4xf32>, vector<4xf32>) -> vector<4xf32>
```

### Expected Lowering
Per the ODS specification, the conversion should emit `LLVM::FMAOp` (`llvm.intr.fma`):
```mlir
%0 = llvm.intr.fma(%arg0, %arg1, %arg2) : (vector<4xf32>, vector<4xf32>, vector<4xf32>) -> vector<4xf32>
```

## Why This Matters (Precision Discrepancy)

Consider the following `binary32` inputs for `a * b + c`:
* `a = 0x3f800001` ($1 + 2^{-23}$)
* `b = 0x3f7ffffe` ($1 - 2^{-23}$)
* `c = 0xbf800000` ($-1.0$)

The exact mathematical result is $-2^{-46}$.

* **With fused single-rounding (`llvm.fma`)**:
  Evaluates to `0xa8800000` ($-2^{-46}$).
* **With unfused multiply-add (`llvm.fmuladd` fallback)**:
  `a * b` intermediate rounds to `1.0`, yielding `1.0 + (-1.0) = 0x00000000` (`+0.0`).

When targeting architectures or subtargets without hardware FMA instructions, `llvm.fmuladd` permits the second result, violating the documented contract of `vector.fma`.

## Suggested Resolution

There is a semantic mismatch between the documentation and the lowering:

1. **Option A (Align implementation with docs)**:
   Change `VectorFMAOp1DConversion` to emit `LLVM::FMAOp` instead of `LLVM::FMulAddOp`, and update the corresponding expectations in `mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir`.
2. **Option B (Align docs with implementation)**:
   If `fmuladd` was chosen intentionally to avoid software emulation penalties on targets lacking native vector FMA instructions, update `VectorOps.td` to clarify that `vector.fma` allows contraction/unfused rounding (similar to `llvm.fmuladd`), and point users who require strict IEEE-754 single-rounding to `math.fma`.

Clarifying this contract would avoid unexpected precision regressions in numerical workloads.

## Tested Versions
- LLVM `main` at commit `c1856996d677c1b3578752e6781d0df46617fd68`
- LLVM `24.0.0git` (`67005b724c917e1bdca50c82ecaa1b21a69244ed`)

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to