Issue 243888
Summary [mlir][arith] SubFOp::fold loses negative zero under downward rounding
Labels mlir
Assignees
Reporter lin334141-cell
    ## Summary

`arith::SubFOp::fold` unconditionally folds `subf(x, +0.0)` to `x`, even when the operation specifies downward rounding and does not have the `nsz` fast-math flag.

Under IEEE 754 roundTowardNegative, `+0.0 - +0.0` must produce `-0.0`. However, this fold replaces the subtraction with the original operand, producing `+0.0` instead.

This changes the observable sign of zero.

## Minimal Reproducer

```mlir
"builtin.module"() ({
  "func.func"() <{sym_name = "derived", function_type = (f32) -> f32}> ({
  ^bb0(%arg0: f32):
    %c = "arith.constant"() <{value = 0x00000000 : f32}> : () -> f32
    %0 = "arith.subf"(%arg0, %c) <{fastmath = #arith.fastmath<none>, roundingmode = 1 : i32}> : (f32, f32) -> f32
    "func.return"(%0) : (f32) -> ()
  }) : () -> ()
}) : () -> ()
```

Here, `roundingmode = 1 : i32` specifies downward rounding.

Run:

```bash
mlir-opt repro.mlir \
 --pass-pipeline='builtin.module(canonicalize)'
```

## Expected Behavior

For `%arg0 = +0.0` under downward rounding:

```text
+0.0 - (+0.0) = -0.0
Expected bits: 0x80000000
```

Without `nsz`, the sign of zero must be preserved. The subtraction cannot be replaced unconditionally by `%arg0`.

## Actual Behavior

`canonicalize` eliminates the subtraction and returns the original operand directly:

```mlir
"func.return"(%arg0) : (f32) -> ()
```

For `%arg0 = +0.0`, this produces:

```text
Actual result: +0.0
Actual bits:   0x00000000
```

The same fold is applied regardless of the specified rounding mode.

## Root Cause

In `mlir/lib/Dialect/Arith/IR/ArithOps.cpp`:

```cpp
OpFoldResult arith::SubFOp::fold(FoldAdaptor adaptor) {
  // subf(x, +0) -> x
  if (matchPattern(adaptor.getRhs(), m_PosZeroFloat()))
    return getLhs();
```

This fold does not check `getRoundingmode()` or require `FastMathFlags::nsz`.

In contrast, the nearby `AddFOp::fold` implementation guards its corresponding signed-zero-sensitive fold with `nsz`.

The subtraction fold should also account for rounding modes in which replacing the operation changes the sign of zero.

## Validation

Reproduced with the frozen LLVM 24.0.0 development build associated with source revision `8635f9042c2a42d15844b0bc4b2e8e27b06f3f54`.

- The generic-form IR parses and verifies successfully.
- The `roundingmode` property is preserved during an IR round-trip.
- `canonicalize` eliminates the subtraction.
- Differential testing observed `P = Q = +0.0` while the independent source reference returned `-0.0` for the affected input.
- The native test harness verified `FE_DOWNWARD` and MXCSR on each invocation.

The LLVM `main` source inspected on November 18, 2026 still contains the same unconditional fold. The reproducer has not yet been executed using a trunk-built `mlir-opt`.

## Related Issue

#241653 reports a related signed-zero/rounding-mode issue in LLVM's `InstSimplify`, involving `fsub(-0.0, fneg X) -> X`.

This report concerns a different fold, `subf(X, +0.0) -> X`, in the MLIR Arith dialect. The two defects occur in separate implementations.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to