Issue 240630
Summary [mlir][MemRef] `--normalize-memrefs` silently miscompiles a `memref.dim` result after normalizing `reinterpret_cast`
Labels mlir
Assignees
Reporter pingshiyu
    Reproduced with the optimized LLVM Git `main` build at commit fb288ec4774f8664b165f6e302db5be8b30a41da from 04/Nov/2026.

This valid program returns `1` because dimension zero of the reinterpret-cast view has size one:
```mlir
module {
  func.func @main() -> i32 {
    %alloc = memref.alloc() : memref<3xi32>
    %view = memref.reinterpret_cast %alloc to offset: [0], sizes: [1, 3], strides: [3, 1] : memref<3xi32> to memref<1x3xi32, strided<[3, 1]>>
    %c0 = arith.constant 0 : index
 %dim = memref.dim %view, %c0 : memref<1x3xi32, strided<[3, 1]>>
    %result = arith.index_cast %dim : index to i32
    memref.dealloc %alloc : memref<3xi32>
    return %result : i32
  }
}
```

Running `--normalize-memrefs` succeeds, but it changes the view to rank one while leaving the dimension operand unchanged:
```console
$ mlir-opt repro.mlir --normalize-memrefs -o normalized.mlir
```

```mlir
%view = memref.reinterpret_cast %alloc to offset: [0], sizes: [3], strides: [1] : memref<3xi32> to memref<3xi32>
%dim = memref.dim %view, %c0 : memref<3xi32>
```

The transformed program returns `3`. The expected behavior is for normalization to preserve the dimension and return `1`.

Note that later passes didn't cause this miscompile: here's the full pipeline. 
The errorneous behaviour comes after `--normalize-memrefs` is applied, and is not present without it.
```console
$ mlir-opt repro.mlir --lower-affine --convert-scf-to-cf --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
1
$ mlir-opt repro.mlir --normalize-memrefs --lower-affine --convert-scf-to-cf --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
3
$ mlir-opt repro.mlir --normalize-memrefs --canonicalize --lower-affine --convert-scf-to-cf --convert-index-to-llvm=index-bitwidth=64 --convert-arith-to-llvm --finalize-memref-to-llvm --convert-func-to-llvm --reconcile-unrealized-casts -o affected-canonicalized.mlir
$ mlir-runner affected-canonicalized.mlir -e main -entry-point-result=i32
3
```

## Related cases

[LLVM issue #237736](https://github.com/llvm/llvm-project/issues/237736) also involves `normalize-memrefs`, but it reports mutation through an incompatible cast/user and invalid IR or a compiler failure. This case produces valid IR and silently changes the result through a retained `memref.dim` index after a rank-changing rewrite, so the first incorrect action is different.

[LLVM issue #59896](https://github.com/llvm/llvm-project/issues/59896) concerns reinterpret-cast metadata in `expand-strided-metadata`, while this case is introduced directly by `normalize-memrefs` and specifically affects the logical dimension queried by `memref.dim`.

Open and closed LLVM issues and pull requests were searched on 09/Nov/2026 for `normalize-memrefs`, `normalizeMemRef`, `memref.dim`, `reinterpret_cast`, rank normalization, and wrong results. I did not find an exact report of this mechanism.

## LLM-Suggested Cause and Fix

[`isMemRefNormalizable`](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Dialect/MemRef/Transforms/NormalizeMemRefs.cpp#L95-L103) admits operations with the `MemRefsNormalizable` trait and contains `TODO: Extend this for DimOps.`. [`memref.dim` has that trait](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td#L640-L645).

[`normalizeFuncOpMemRefs`](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Dialect/MemRef/Transforms/NormalizeMemRefs.cpp#L341-L366) sends reinterpret casts to [`affine::normalizeMemRef(ReinterpretCastOp)`](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Dialect/Affine/Utils/Utils.cpp#L1776-L1858). That helper can construct a normalized type with a different rank, remaps dereferencing uses, allows non-dereferencing uses, and ultimately replaces remaining uses with the new view. A `memref.dim` dimension operand is not a memory subscript, so retaining the same numeric index does not preserve its meaning after dimensions are collapsed.

A conservative fix would reject a rank-changing normalization in the presence of `memref.dim` users unless each query can be proven equivalent. A fuller fix could rewrite such queries to the corresponding original logical extent. A regression test can use the `1x3` case above and check that dimension zero remains `1`.

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