Issue 239311
Summary [MLIR][Affine] `affine-data-copy-generate` crashes on Presburger arithmetic
Labels mlir
Assignees
Reporter pingshiyu
    Reproduced with the optimized assertions-enabled main build at `fb288ec4774f8664b165f6e302db5be8b30a41da` (from 2026-11-04).

This canonicalized code contains an uncalled private function and one affine load:
```mlir
module {
  func.func private @test(%data: memref<1xi32>, %i: index) -> i32 {
    %v = affine.load %data[(symbol(%i) ceildiv 3 - 4611686018427387904) ceildiv 2] : memref<1xi32>
    return %v : i32
 }
}
```

The data-copy generation pass aborts:
```console
$ mlir-opt repro.mlir '--pass-pipeline=builtin.module(func.func(affine-data-copy-generate{fast-mem-space=0}))' -o /dev/null
mlir-opt: .../APInt.h:1585: int64_t llvm::APInt::getSExtValue() const: Assertion `getSignificantBits() <= 64 && "Too many bits for int64_t"' failed.
```

The stack includes:
```text
llvm::APInt::getSExtValue
mlir::presburger::getInt64Vec
mlir::FlatLinearConstraints::getConstantBoundOnDimSize
mlir::affine::MemRefRegion::getConstantBoundingSizeAndShape
```

If its bounding-box representation cannot express the intermediate constraint row, it could conservatively decline this data-copy transformation. Changing the constant `4611686018427387904` to `1` makes the pass succeed.

## LLM-Suggested Cause

The data-copy analysis reaches [`MemRefRegion::getConstantBoundingSizeAndShape`](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Dialect/Affine/Analysis/Utils.cpp#L1189-L1213), which calls `FlatLinearConstraints::getConstantBoundOnDimSize`. Its local [`getAsExpr`](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Analysis/FlatLinearValueConstraints.cpp#L920-L950) helper calls [`IntegerRelation::getInequality64`](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/include/mlir/Analysis/Presburger/IntegerRelation.h#L254-L268) on an arbitrary-precision Presburger row. That `int64_t` wrapper is explicitly documented as unsafe and asserts if a value does not fit. Projection/bound construction for the nested ceil divisions creates such a wide row, and `getInt64Vec` reaches `APInt::getSExtValue` without a checked fallback.

A checked conversion that returns analysis failure, or an arbitrary-precision path that avoids narrowing the row, would prevent the compiler abort. I have not tested a patch.

## Related cases

[#206621](https://github.com/llvm/llvm-project/issues/206621) has the same final APInt assertion for extreme loop bounds, but its stack goes through `ValueBoundsConstraintSet::computeConstantBound` and `IntegerRelation::getConstantBound64`, converting the result of a value-bounds query. This case converts a constraint row through `FlatLinearConstraints::getAsExpr/getInequality64` while `affine-data-copy-generate` computes a memory-region extent. The caller, converted value, and first unsafe conversion are different.

[#238630](https://github.com/llvm/llvm-project/issues/238630) is another `affine-data-copy-generate` issue, but it emits unconditional copies for an unreachable out-of-bounds access and fails at runtime. This case aborts during compile-time Presburger analysis before a copy is emitted.

## LLM Declaration

LLM was used to prepare the bug report, and the bug itself was found and verified by a human.

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

Reply via email to