Issue 243065
Summary [mlir][math] `math.ceil` expansion on f8E8M0FNU adds 2^-127 for byte 0x00 (miscompile)
Labels mlir
Assignees
Reporter wildoranges
    `f8E8M0FNU` has no zero: the all-zero byte `0x00` encodes `2^-127`. #238377 fixed the arith expansion of `f8E8M0FNU`, which had turned that byte into zero. The math expansion in `convertCeilOp` still relies on a `0.00` constant of the operand type as an additive identity. It creates `zero = createFloatConst(..., 0.00, ...)`, selects `incrValue = select(gtCheck, one, zero)`, and adds `incrValue` to `fpFixedConvert`.

https://github.com/llvm/llvm-project/blob/8445b527ed0da1f9156f27be8c193624d0292253/mlir/lib/Dialect/Math/Transforms/ExpandOps.cpp#L282-L353

On the reproducer, `math.ceil` receives byte `0x00` through a memref load, so it is not folded before the pattern runs. For that operand, `fpFixedConvert` is also `0x00` (`2^-127`), `gtCheck` is false, the selected increment is `0x00`, and the sum is `2^-127 + 2^-127 = 2^-126` (byte `0x01`). The correct `ceil(2^-127)` is `1.0` (byte `0x7F`). The pipeline `--math-expand-ops --canonicalize --convert-to-llvm` prints `1`, while `--canonicalize --convert-to-llvm` prints `127`.

`run.mlir`:
```mlir
memref.global "private" constant @g : memref<1xi8> = dense<[0]>
func.func @main() {
 %c0 = arith.constant 0 : index
  %g = memref.get_global @g : memref<1xi8>
 %b = memref.load %g[%c0] : memref<1xi8>
  %x = arith.bitcast %b : i8 to f8E8M0FNU
  %r = math.ceil %x : f8E8M0FNU
  %rb = arith.bitcast %r : f8E8M0FNU to i8
  %i = arith.extui %rb : i8 to i32
  vector.print %i : i32
  return
}
```

```console
$ mlir-opt run.mlir -o /dev/null && echo verifies
verifies
$ RUN="mlir-runner -e main -entry-point-result=void -shared-libs=$LLVM_BUILD/lib/libmlir_c_runner_utils.so,$LLVM_BUILD/lib/libmlir_runner_utils.so"
$ mlir-opt run.mlir --canonicalize --convert-to-llvm | $RUN; echo
127
$ mlir-opt run.mlir --math-expand-ops --canonicalize --convert-to-llvm | $RUN; echo
1
```
(`$LLVM_BUILD` is the LLVM/MLIR build directory.)


Suggested fix: Special-case no-zero types in `convertCeilOp`. For `Float8E8M0FNU`, detect the minimum encoding (`0x00`) and select `1.0` for it, and express the non-minimum increment without a `0.00` constant, for example by selecting between `fpFixedConvert + 1.0` and `fpFixedConvert`, following the `isMin`/select handling used in #238377.

**Version**: llvm-project main @ 8445b527ed0da1f9156f27be8c193624d0292253 (2026-11-16), Release + `LLVM_ENABLE_ASSERTIONS=ON`, x86_64 Linux. Also reproduced on main @ a60677dca3a8536a0cf5bec1b846610014a983fd (2026-11-12).

*Disclosure: I wrote this report with the help of Claude Code. I reproduced and reviewed everything in this report myself.*

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

Reply via email to