Issue 244790
Summary [mlir][MemRef][fuzzer-found] `memref-elide-reinterpret-cast` loads wrong address on source with a nonzero offset (silent miscompilation)
Labels mlir
Assignees
Reporter pingshiyu
    ## LLVM Version and Environment
Optimized LLVM Git `main` build at commit c4fb621a96d8caeb7d272a64047edf87dec68501 from 11/Nov/2026.

## Reproducer
`--memref-elide-reinterpret-cast` changes the result of this program from `42` to `9`:

```mlir
module {
  memref.global "private" constant @g : memref<3xi32> = dense<[42, 7, 9]>

  func.func @main() -> i32 {
    %g = memref.get_global @g : memref<3xi32>
    %subview = memref.subview %g[2] [1] [1] : memref<3xi32> to memref<1xi32, strided<[1], offset: 2>>
    %view = memref.reinterpret_cast %subview to offset: [0], sizes: [1], strides: [1] : memref<1xi32, strided<[1], offset: 2>> to memref<1xi32, strided<[1]>>
    %c0 = arith.constant 0 : index
    %value = memref.load %view[%c0] : memref<1xi32, strided<[1]>>
    return %value : i32
  }
}
```

## Expected Behaviour
Lowering the program as follows produces 42:
```console
$ mlir-opt repro.mlir --expand-strided-metadata --convert-index-to-llvm=index-bitwidth=64 --convert-arith-to-llvm --finalize-memref-to-llvm --convert-func-to-llvm --reconcile-unrealized-casts -o control.mlir
$ mlir-runner control.mlir -e main -entry-point-result=i32
42
```

## Actual Behaviour
Adding only the affected pass changes the result:
```console
$ mlir-opt repro.mlir --memref-elide-reinterpret-cast --expand-strided-metadata --convert-index-to-llvm=index-bitwidth=64 --convert-arith-to-llvm --finalize-memref-to-llvm --convert-func-to-llvm --reconcile-unrealized-casts -o affected.mlir
$ mlir-runner affected.mlir -e main -entry-point-result=i32
9
```

The mismatch also remains if one `--canonicalize` is placed before or after `--memref-elide-reinterpret-cast`.

## What went wrong
`%subview` has offset two, but `%view` resets the offset to zero, so `%view[0]` must load the first global element, `42`. 

The IR emitted by the faulty pass replaces the load with `%view` with:
```mlir
%value = memref.load %subview[%c0] : memref<1xi32, strided<[1], offset: 2>>
```
which is of course the wrong view.

## AI Declaration
The bug was found via a non-LLM, program generation and differential-testing based fuzzer. LLM was used for test case reduction together with C-Reduce.

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

Reply via email to