| Issue |
233728
|
| Summary |
[mlir][arith] ValueBounds model of arith.maxui adds `result >= lhs` when only lhs is non-negative -> affine-simplify-min-max miscompile
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
wildoranges
|
The `ValueBoundsOpInterface` model of `arith.maxui` adds `result >= operand` for every operand that is provably non-negative. `ValueBoundsConstraintSet` models values as **signed** integers, so this is unsound: for `maxui(x, y)` with `y >= 0` and `x < 0` (signed), the unsigned maximum is `x`, which is *smaller* than `y` in the signed model.
The `minui` model in the same file explains why one non-negative operand is enough for `minui`. The `maxui` model says "See MinUIOpInterface comment" and applies the same per-operand rule, but the argument does not carry over to max. This came in with #207701.
`maxui.mlir`:
```mlir
func.func @f(%x: index) -> index {
%c5 = arith.constant 5 : index
%m = arith.maxui %x, %c5 : index
%r = affine.max affine_map<(d0) -> (d0, 5)>(%m)
return %r : index
}
func.func @main() {
%x = arith.constant -1 : index
%r = func.call @f(%x) : (index) -> index
vector.print %r : index
return
}
```
(`$LLVM_BUILD` below is the LLVM/MLIR build directory.)
For `x = -1`, `%m = maxui(-1, 5) = -1` (all ones is the largest unsigned value), so `affine.max(-1, 5) = 5`.
```console
$ LOWER="-convert-vector-to-scf -convert-scf-to-cf -convert-vector-to-llvm -convert-arith-to-llvm -convert-index-to-llvm -convert-cf-to-llvm -convert-func-to-llvm -finalize-memref-to-llvm -convert-ub-to-llvm -reconcile-unrealized-casts"
$ RUN="mlir-runner -e main -entry-point-result=void -shared-libs=$LLVM_BUILD/lib/libmlir_runner_utils.so,$LLVM_BUILD/lib/libmlir_c_runner_utils.so"
$ mlir-opt maxui.mlir -lower-affine | mlir-opt $LOWER | $RUN
5
$ mlir-opt maxui.mlir --pass-pipeline='builtin.module(func.func(affine-simplify-min-max))' | mlir-opt -lower-affine | mlir-opt $LOWER | $RUN
18446744073709551615
```
The second run prints -1 (as an unsigned `index`) instead of 5. `affine-simplify-min-max` drops the `affine.max` entirely, because the constraint set "proves" `%m >= 5`:
```console
$ mlir-opt maxui.mlir --pass-pipeline='builtin.module(func.func(affine-simplify-min-max))'
module {
func.func @f(%arg0: index) -> index {
%c5 = arith.constant 5 : index
%0 = arith.maxui %arg0, %c5 : index
return %0 : index
}
...
```
https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/Arith/IR/ValueBoundsOpInterfaceImpl.cpp#L388-L410
For comparison, the `minui` rationale is at
https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/Arith/IR/ValueBoundsOpInterfaceImpl.cpp#L356-L359
The input verifies and is UB-free: `maxui` and `affine.max` are total.
A possible fix: for `maxui`, only add `result >= lhs` and `result >= rhs` when **both** operands are provably non-negative. In that case unsigned and signed max agree.
**Version**: llvm-project main @ 018b722b2d48cf42dccded46e1226f1d9c29f339 (2026-10-17), Release + assertions, x86_64 Linux. Also reproduced on main @ 4c407e843c975298b6892d50afd9e72cfb0cbe46 (2026-10-20).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs