Issue 240187
Summary [mlir][Vector] `--test-scalar-vector-transfer-lowering` moves a read through an intervening write, producing wrong binary
Labels mlir
Assignees
Reporter pingshiyu
    Reproduced with the optimized LLVM Git `main` build at commit fb288ec4774f8664b165f6e302db5be8b30a41da from 04/Nov/2026.

This valid program should return `42`:
```mlir
module {
  func.func @main() -> i32 {
    %c0 = arith.constant 0 : index
    %c42 = arith.constant 42 : i32
 %c7 = arith.constant 7 : i32
    %pad = arith.constant 0 : i32
    %alloc = memref.alloc() : memref<1xi32>
    memref.store %c42, %alloc[%c0] : memref<1xi32>
    %v = vector.transfer_read %alloc[%c0], %pad : memref<1xi32>, vector<i32>
    memref.store %c7, %alloc[%c0] : memref<1xi32>
    %x = vector.extract %v[] : i32 from vector<i32>
 memref.dealloc %alloc : memref<1xi32>
    return %x : i32
 }
}
```

The transfer reads `42` before the second store and produces a vector SSA value. The later extract must therefore return `42`, even though the memref subsequently contains `7`.

Using the usual MemRef/Vector-to-LLVM lowering passes, we get areturn of `42`:
```console
$ mlir-opt repro.mlir --convert-vector-to-llvm --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
```

However, adding the scalar transfer lowering pass changes the result to `7`:
```console
$ mlir-opt repro.mlir --test-scalar-vector-transfer-lowering --convert-vector-to-llvm --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
7
```

Looking at where it went wrong:
```console
$ mlir-opt repro.mlir --test-scalar-vector-transfer-lowering
```

```mlir
memref.store %c42_i32, %alloc[%c0] : memref<1xi32>
memref.store %c7_i32, %alloc[%c0] : memref<1xi32>
%0 = memref.load %alloc[%c0] : memref<1xi32>
```

Scalar transfer lowering replaced `vector.extract(vector.transfer_read)` with a `memref.load` at the extract's position. This moved the memory read from before the store (of `7`) to after it.

## Related cases

[LLVM issue #237580](https://github.com/llvm/llvm-project/issues/237580) is an open Affine data-copy miscompile that inserts a copy-out `affine.store` after an existing `memref.dealloc`. Both cases can produce a compiler-inserted access after deallocation, but that issue does so in `affine-data-copy-generate`; this case rematerializes an earlier transfer read at a later `vector.extract`. The direct reduction here does not run the Affine pass or contain a use-after-free. It is related by symptom but distinct in pass, helper, access kind, and first incorrect transformation.

[LLVM PR #229296](https://github.com/llvm/llvm-project/pull/229296) was merged on 15/Oct/2026 and fixed invalid insertion points when allocation-liveness moves a deallocation. This case does not run allocation-liveness: the store remains in place, and the Vector pattern moves the read across it. It is related by the retained use-after-free composition but is not a duplicate.

[LLVM PR #215331](https://github.com/llvm/llvm-project/pull/215331) was merged on 25/Aug/2026 and fixed allocation-liveness failing to follow a `memref.transpose` alias. This case has no view operation and fails in the scalar transfer rewrite itself. It is contextual and unrelated to the first incorrect transformation.

[LLVM issue #239710](https://github.com/llvm/llvm-project/issues/239710) concerns permuted transfer-mask indexing in VectorToSCF. This case is unmasked, rank zero, and minor identity; its first incorrect action is a memory read moved across a store. It is unrelated.

## LLM-Suggested Cause and Fix

[`RewriteScalarExtractOfTransferRead`](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp#L1025-L1113) matches `vector.extract(vector.transfer_read)` but calls `replaceOpWithNewOp<memref::LoadOp>` on the extract. This creates the scalar memory read at the extract's position rather than where the transfer originally observed memory, without checking intervening memory effects.

A likely correction is to insert the scalar load at the original transfer-read position and replace the extract with that earlier SSA value. If a dynamic extract offset does not dominate the transfer, the pattern should decline the rewrite or prove that moving the read is safe using conservative alias and memory-effect analysis. A regression test should place a store between the transfer and extract and check that scalarization preserves the pre-store value.

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