Issue 240189
Summary [mlir][Vector] Contiguous gather/scatter canonicalization produces invalid IR on loads/stores of non-unit-stride memrefs
Labels mlir
Assignees
Reporter pingshiyu
    Reproduced with the optimized LLVM Git `main` build at commit fb288ec4774f8664b165f6e302db5be8b30a41da from 04/Nov/2026.

```mlir
func.func @gather_nonunit_stride(%base: memref<2xi32, strided<[3], offset: 0>>) -> vector<2xi32> {
  %c0 = arith.constant 0 : index
  %indices = arith.constant dense<[0, 1]> : vector<2xindex>
  %mask = arith.constant dense<true> : vector<2xi1>
 %pass_thru = arith.constant dense<0> : vector<2xi32>
  %result = vector.gather %base[%c0] [%indices], %mask, %pass_thru : memref<2xi32, strided<[3], offset: 0>>, vector<2xindex>, vector<2xi1>, vector<2xi32> into vector<2xi32>
  return %result : vector<2xi32>
}

func.func @scatter_nonunit_stride(%base: memref<2xi32, strided<[6], offset: 0>>, %value: vector<2xi32>) {
  %c0 = arith.constant 0 : index
  %indices = arith.constant dense<[0, 1]> : vector<2xindex>
  %mask = arith.constant dense<true> : vector<2xi1>
  vector.scatter %base[%c0] [%indices], %mask, %value : memref<2xi32, strided<[6], offset: 0>>, vector<2xindex>, vector<2xi1>, vector<2xi32>
  return
}
```

```console
mlir-opt repro.mlir --split-input-file --verify-each -o /dev/null
mlir-opt repro.mlir --split-input-file --verify-each --canonicalize -o /dev/null
```

Both split inputs verifies. The second command fails with:

```text
error: 'vector.load' op most minor memref dim must have unit stride
note: see current operation: ... "vector.load" ... memref<2xi32, strided<[3]>> ...
error: 'vector.store' op most minor memref dim must have unit stride
note: see current operation: ... "vector.store" ... memref<2xi32, strided<[6]>> ...
```

The programs above contains no UB - canonicalization should therefore not miscompile the IR into an invalid form.

## Related cases

[Issue #187215](https://github.com/llvm/llvm-project/issues/187215) remains open and discusses inconsistent `vector.gather` lowering semantics for strided memrefs, but its reproducer has unit most-minor stride and compares two successful paths with different runtime results. This case has non-unit most-minor strides and `canonicalize` creates operations that fail verification, independently of that issue's unresolved logical-versus-physical indexing discussion. [Issue #239720](https://github.com/llvm/llvm-project/issues/239720) is also distinct: a single-element `vector.load` is accepted by the Vector verifier and fails later in `VectorToLLVM`, whereas these two-element source operations are valid and the first invalid action is the gather/scatter canonicalization. [PR #117939](https://github.com/llvm/llvm-project/pull/117939) introduced the two contiguous-index folds.

## LLM-Suggested Cause and Fix

[`FoldContiguousGather`](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Dialect/Vector/IR/VectorOps.cpp#L6600-L6618) and the adjacent [`FoldContiguousScatter`](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Dialect/Vector/IR/VectorOps.cpp#L6690-L6720) check only the logical index sequence before creating `vector.maskedload`/`vector.maskedstore`. Their replacements are subject to [`verifyLoadStoreMemRefLayout`](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Dialect/Vector/IR/VectorOps.cpp#L6262-L6274), which requires a unit most-minor stride for multi-element vectors. Both patterns could share the verifier's layout predicate and decline the fold when it is false. Regression tests can check that positive non-unit-stride gather/scatter operations remain unchanged while unit-stride controls still fold. This is a proposed fix direction, not a tested patch.

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