Issue 240186
Summary [mlir][Affine] `affine-simplify-with-bounds` crashes on a dimension of a shaped loop result
Labels mlir
Assignees
Reporter pingshiyu
    Reproduced with the optimized LLVM Git `main` build at commit fb288ec4774f8664b165f6e302db5be8b30a41da from 04/Nov/2026.

```mlir
#lb = affine_map<(d0) -> (0, d0)>
module {
  func.func @f(%t: tensor<?xi32>) -> index {
    %c0 = arith.constant 0 : index
    %r = affine.for %i = 0 to 2 iter_args(%a = %t) -> tensor<?xi32> {
      %size = tensor.dim %a, %c0 : tensor<?xi32>
      %next = tensor.empty(%size) : tensor<?xi32>
 affine.yield %next : tensor<?xi32>
    }
    %d = tensor.dim %r, %c0 : tensor<?xi32>
    %x = affine.for %j = max #lb(%d) to 2 iter_args(%v = %d) -> index {
      affine.yield %j : index
    }
    return %x : index
 }
}
```

```console
mlir-opt repro.mlir -o /dev/null
mlir-opt repro.mlir --affine-simplify-with-bounds -o /dev/null
```

The program is valid, but the second command crashes with:

```text
not implemented
UNREACHABLE executed at .../ValueBoundsOpInterface.h.inc:137!
```

## Related cases

[PR #199032](https://github.com/llvm/llvm-project/pull/199032) added the multi-result affine loop-bound pattern that exposes this path; it is not a report or fix for shaped `affine.for` results. [Issue #240012](https://github.com/llvm/llvm-project/issues/240012) is a distinct `ValueBoundsConstraintSet` problem caused by converting integer constants wider than 64 bits through `APInt::getSExtValue()` under `allowIntegerType`; this case uses ordinary index/tensor types and reaches an unimplemented shaped-dimension interface method instead. [Issue #224577](https://github.com/llvm/llvm-project/issues/224577) was a separate overflow in affine _expression_ flattening, fixed by [PR #228010](https://github.com/llvm/llvm-project/pull/228010).

## LLM-Suggested Cause and Fix

[`SimplifyAffineLoopBoundMap`](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Dialect/Affine/Transforms/SimplifyAffineWithBounds.cpp#L221-L275) compares the lower-bound results through `ValueBoundsConstraintSet`. Processing `%d` then asks the attached [`AffineForOpInterface`](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Dialect/Affine/IR/ValueBoundsOpInterfaceImpl.cpp#L68-L109) for a shaped-result dimension, but that model implements only index bounds, so dispatch reaches the [`ExternalModel` default](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/include/mlir/Interfaces/ValueBoundsOpInterface.td#L41-L58) and crashes. `AffineForOpInterface` could implement `populateBoundsForShapedValueDim` for shape-preserving iterated tensors, analogous to the SCF model, and conservatively add no constraint for unsupported shape changes. A regression test can cover a multi-result bound using `tensor.dim` of an `affine.for` result. This is a proposed fix direction, not a tested patch.

## 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