Issue 239611
Summary [MLIR][MemRef] expand-realloc fails on a valid statically shrinking memref.realloc
Labels mlir
Assignees
Reporter lin334141-cell
    ## Summary

The `--expand-realloc` pass fails on a valid, statically shrinking `memref.realloc`, where a `memref<4xi32>` allocation is reallocated to `memref<2xi32>`.

The pass constructs an out-of-bounds `memref.subview` in the grow branch of an `scf.if`, even though that branch cannot be taken for the given static sizes.

The resulting verification failure prevents the pass from lowering the operation.

## Minimal Reproducer

Save as `min.mlir`:

```mlir
module {
  func.func @main() -> memref<2xi32> {
    %tmp = memref.alloc() : memref<4xi32>
    %resized = memref.realloc %tmp
        : memref<4xi32> to memref<2xi32>
    func.return %resized : memref<2xi32>
  }
}
```

Run:

```bash
mlir-opt min.mlir --mlir-disable-threading --expand-realloc -o /dev/null
```

## Actual Behavior

The pass exits with code 1:

```text
error: slice along dimension 0 runs out-of-bounds: 3 >= 2
```

The diagnostic identifies the generated operation:

```mlir
%7 = "memref.subview"(%6) <{
  static_offsets = array<i64: 0>,
  static_sizes = array<i64: 4>,
  static_strides = array<i64: 1>
}> : (memref<2xi32>) -> memref<4xi32, strided<[1]>>
```

The grow branch attempts to construct a four-element subview of a two-element allocation.

## Expected Behavior

The `memref.realloc` operation is valid. The MemRef dialect specification permits reallocation to a smaller size, preserving the contents up to the minimum of the old and new sizes when the allocation is moved.

The pass should successfully lower this operation to valid MLIR while preserving its defined behavior.

The source program does not access the old memref SSA value after reallocation.

## Root Cause

In `mlir/lib/Dialect/MemRef/Transforms/ExpandRealloc.cpp`, `ExpandReallocOpPattern::matchAndRewrite` constructs an `scf.if` with both regions.

The grow-region builder allocates a buffer of the result type and creates a `memref.subview` whose size is the original buffer size (`currSize`).

For this reproducer:

- Original buffer size: 4
- Requested result size: 2
- Generated subview size: 4
- Generated subview base allocation size: 2

The condition `currSize < targetSize` is false, so the grow region is unreachable at runtime. However, the region still contains a statically out-of-bounds `memref.subview`, which fails verification.

The transformation therefore cannot lower an otherwise valid static shrink.

## Tested Versions

Reproduced with stock `mlir-opt` on:

- `67005b724c917e1bdca50c82ecaa1b21a69244ed` — exit 1
- `61817b48e757f9a07f2a84e36326542052c12c22` — exit 1
- `c1856996d677c1b3578752e6781d0df46617fd68` (LLVM main, November 6, 2026) — exit 1, using a freshly built `mlir-opt`

The last executable has SHA-256:

`668ac06c408e3c797bffe3bb73420904050374ee03e2ddb66b6f28ced6b7e7f7`

The relevant `ExpandRealloc.cpp` implementation is byte-identical across these revisions. A later main snapshot was also inspected at source level, but not independently executed.

## Notes

This reproducer uses only standard MLIR operations and the stock `--expand-realloc` pass. It does not depend on a custom test harness, observer, or downstream pass pipeline.

The problem is a failure to lower valid source IR, not a runtime numerical miscompilation.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to