Issue 240012
Summary [mlir][ValueBounds] Wrong bound or assertion failure for integer constants that do not fit in int64_t with allowIntegerType
Labels mlir
Assignees
Reporter ggeorgakoudis
    With `ValueBoundsOptions::allowIntegerType` (#196082), `ValueBoundsConstraintSet` accepts integer values of any width, but converts integer constants to `int64_t` with an unchecked `APInt::getSExtValue()`. For a constant that does not fit in `int64_t`, builds with assertions abort. Builds without assertions use the low 64 bits, which gives a wrong exact bound and, in `-affine-loop-normalize`, wrong code.

### Reproducer

The constant is 2^70:

```mlir
func.func @f() -> index {
  %c = arith.constant 1180591620717411303424 : i128
  %0 = "test.reify_bound"(%c) {allow_integer_type} : (i128) -> (index)
  return %0 : index
}
```

```
mlir-opt repro.mlir --pass-pipeline='builtin.module(func.func(test-affine-reify-value-bounds))'
```

- Without assertions (Compiler Explorer trunk, https://godbolt.org/z/4GWao4vvW): the bound is reified as `arith.constant 0 : index`, the low 64 bits of 2^70.
- With assertions (a100bd4d1e8841a7b71c85a24dfb46680a3ea968, Release, `LLVM_ENABLE_ASSERTIONS=ON`), the assertion fires in `ValueBoundsConstraintSet::Variable::Variable(OpFoldResult, std::optional<int64_t>)`:
  ```
  APInt.h:1585: int64_t llvm::APInt::getSExtValue() const: Assertion `getSignificantBits() <= 64 && "Too many bits for int64_t"' failed.
  ```

With `5 : i128`, which fits in 64 bits, the result is correct. A wide constant used as an operand, as in `arith.addi %x, %c : i128`, aborts the same way through `getExpr`.

### Wrong code in `-affine-loop-normalize`

`-affine-loop-normalize='use-expensive-math=1'` sets `allowIntegerType`. The `scf.for` below runs zero times because its upper bound, -2^64 + 5, is below its lower bound, so `%n` is 0:

```mlir
func.func @f() {
  %lb = arith.constant 0 : i128
  %ub = arith.constant -18446744073709551611 : i128
  %st = arith.constant 1 : i128
  %c0 = arith.constant 0 : index
  %c1 = arith.constant 1 : index
 %n = scf.for %i = %lb to %ub step %st iter_args(%a = %c0) -> (index) : i128 {
    %a1 = arith.addi %a, %c1 : index
    scf.yield %a1 : index
  }
 affine.for %j = 0 to %n {
  }
  return
}
```

Without assertions (Compiler Explorer trunk, https://godbolt.org/z/joKzqcaKP), the pass replaces `%n` with 5, the low 64 bits of `%ub`, so the loop runs 5 times instead of zero:

```mlir
affine.for %arg0 = 0 to 5 {
}
```

### Cause

The unchecked conversion is in the file-local helper `getConstantIntValue`, used by the `Variable` constructor and by the `getExpr(Value, std::optional<int64_t>)` and `getExpr(OpFoldResult)` overloads:

https://github.com/llvm/llvm-project/blob/a100bd4d1e8841a7b71c85a24dfb46680a3ea968/mlir/lib/Interfaces/ValueBoundsOpInterface.cpp#L93-L106

Before `allowIntegerType`, only `index` values were supported, and their constants always fit in 64 bits. The `arith.constant` model has the same unchecked conversion (`attr.getInt()`), so fixing only the helper is not enough:

https://github.com/llvm/llvm-project/blob/a100bd4d1e8841a7b71c85a24dfb46680a3ea968/mlir/lib/Dialect/Arith/IR/ValueBoundsOpInterfaceImpl.cpp#L28-L29

A related problem outside ValueBounds: the public `mlir::getConstantIntValue` in `StaticValueUtils.cpp` makes the same unchecked conversion. With assertions, `mlir-opt` aborts while verifying an `i128` `scf.for` whose lower bound is 2^70, through `isZeroInteger` in `constantTripCount`, called from `scf::ForOp::getSuccessorRegions`. I can file that as a separate issue.

I'm happy to be assigned and fix this once the issue is confirmed.

Assisted-by: Claude Code (Claude Opus 5.5)

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

Reply via email to