Issue 238200
Summary [MLIR][MemRef] `memref-elide-reinterpret-cast` fails on a load through nested redundant casts
Labels mlir
Assignees
Reporter pingshiyu
    Reproduced with the official LLVM/MLIR 23.1.4 optimized Linux x86-64 build.

This program loads through two zero-offset identity reinterpret casts:

```mlir
module {
  func.func @load_through_nested_reinterpret_casts(
      %source: memref<i32>) -> i32 {
    %inner = memref.reinterpret_cast %source
      to offset: [0], sizes: [], strides: []
      : memref<i32> to memref<i32>
    %outer = memref.reinterpret_cast %inner
      to offset: [0], sizes: [], strides: []
      : memref<i32> to memref<i32>
    %value = memref.load %outer[] : memref<i32>
    return %value : i32
  }
}
```

The pass fails:
```console
mlir-opt reduced.mlir \
 '--pass-pipeline=builtin.module(func.func(memref-elide-reinterpret-cast))' \
  -o /dev/null
```

```text
error: failed to legalize operation 'memref.load' that was explicitly marked illegal
```

One reinterpret cast followed by the same load succeeds and the cast is elided. We would expect two nested supported casts to also compile, with the load rewritten directly to `%source[]`.

The same failure occurs with the casting chain `memref<1xi32> -> memref<1x1xi32> -> memref<1x1x1xi32>`.

## LLM-Suggested Cause

`RewriteLoadFromReinterpretCast` replaces the load through the outer cast with a load from the immediate `rc.getSource()`. With nested casts, that source is the inner reinterpret cast, so the replacement is another load that the dynamic legality callback marks illegal. `applyPartialConversion` then fails instead of applying the same rewrite for the second layer.

The mechanism is still present by source inspection on `main` at `384a71b8a2d99b5deb82792c23202c2b82b8ed82`: the [one-layer replacement](https://github.com/llvm/llvm-project/blob/384a71b8a2d99b5deb82792c23202c2b82b8ed82/mlir/lib/Dialect/MemRef/Transforms/ElideReinterpretCast.cpp#L617-L624) and [dynamic legality check](https://github.com/llvm/llvm-project/blob/384a71b8a2d99b5deb82792c23202c2b82b8ed82/mlir/lib/Dialect/MemRef/Transforms/ElideReinterpretCast.cpp#L648-L653) remain unchanged. I did not have a current-main binary for executable confirmation.

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