Issue 238856
Summary [MLIR][MemRef] `memref-elide-reinterpret-cast` crashes on a program with unreachable OOB load
Labels mlir
Assignees
Reporter pingshiyu
    Reproduced with the optimized assertions-enabled LLVM `main` build at commit `fb288ec4774f8664b165f6e302db5be8b30a41da` (from 04/Nov/2026).

The following verifier-valid program has an out-of-bounds load inside a statically unreachable branch. The operation is never executed, and the normally lowered program returns 0.

```mlir
module {
  func.func @main() -> i32 {
    %src = "" : memref<1xi32>
    %false = arith.constant false
    scf.if %false {
      %view = memref.reinterpret_cast %src to offset: [0], sizes: [1, 1], strides: [1, 1] : memref<1xi32> to memref<1x1xi32, strided<[1, 1], offset: 0>>
      %c0 = arith.constant 0 : index
      %c2 = arith.constant 2 : index
      %unused = memref.load %view[%c2, %c0] : memref<1x1xi32, strided<[1, 1], offset: 0>>
 }
    %zero = arith.constant 0 : i32
    return %zero : i32
 }
}
```

```console
$ mlir-opt repro.mlir --verify-each -o /dev/null
$ mlir-opt repro.mlir '--pass-pipeline=builtin.module(func.func(memref-elide-reinterpret-cast))' -o /dev/null
mlir-opt: .../ElideReinterpretCast.cpp:592: ... RewriteLoadFromReinterpretCast::matchAndRewrite(...): Assertion `areIndicesInBounds(op) && "load from reinterpret_cast indexes out of bounds!"' failed.
Aborted
```

Running `canonicalize` first removes the false branch and makes the pass succeed, but the pass should not require dead-code elimination in order to avoid crashing. It could safely leave this cast/load unchanged.

## LLM-suggested cause

The conversion target marks a load through a supported reinterpret cast dynamically illegal whenever `getNonUnitDimMapping` succeeds. `RewriteLoadFromReinterpretCast` has the same shape check, then asserts the additional precondition `areIndicesInBounds(op)`. For a constant-OOB load, conversion requires a rewrite that the pattern cannot safely perform.

A conservative fix may be to make such loads dynamically legal and return pattern failure rather than asserting, so the cast/load remains unchanged. Changing only the assertion would cause `applyPartialConversion` to fail because the operation would remain explicitly illegal.

## Related cases

[#238200](https://github.com/llvm/llvm-project/issues/238200) has two nested reinterpret casts and an in-bounds load. A one-layer rewrite leaves another dynamically illegal load and the pass reports legalization failure. This case has one cast and aborts at the constant-index bounds assertion before replacement, so it appears distinct.

[#238630](https://github.com/llvm/llvm-project/issues/238630) concerns `affine-data-copy-generate` speculating unconditional copies from an unreachable out-of-bounds access and causing a runtime memory error. It does not involve this conversion target or assertion.

The assertion originated in the load-through-reinterpret-cast work around [#188459](https://github.com/llvm/llvm-project/pull/188459) and was retained when [#202683](https://github.com/llvm/llvm-project/pull/202683) generalized the rewrite. I found no issue matching this unreachable-code assertion.

## LLM declaration

LLM was used to prepare this report. It has not been posted upstream.

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

Reply via email to