Issue 242609
Summary [mlir][vector] extract_strided_slice insert-chain folder treats a partially overlapping insert as disjoint (wrong value)
Labels mlir
Assignees
Reporter wildoranges
    `foldExtractStridedOpFromInsertChain` walks an `insert_strided_slice` chain. For each dimension it checks only whether the extract's **start** offset lies inside the inserted interval. If it does not, the insert is classified as **disjoint** and skipped, even when the extract interval `[offset, offset + size)` overlaps the inserted interval from below:
https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/Vector/IR/VectorOps.cpp#L4630-L4644

The fold then forwards to an earlier insert, or to the original vector, and returns stale lanes.

`strided.mlir`:
```mlir
// No mem2reg involved: extract lanes [1,3) of (a with [1,3):=x, then [2,4):=y)
// must be [x0, y0] = [10, 30]. canonicalize folds it to %x = [10, 20].
func.func @chain(%a: vector<4xi32>, %x: vector<2xi32>, %y: vector<2xi32>) -> vector<2xi32> {
 %i1 = vector.insert_strided_slice %x, %a offsets = [1], strides = [1] : vector<2xi32> into vector<4xi32>
  %i2 = vector.insert_strided_slice %y, %i1 offsets = [2], strides = [1] : vector<2xi32> into vector<4xi32>
  %e = vector.extract_strided_slice %i2 offsets = [1], sizes = [2], strides = [1] : vector<4xi32> to vector<2xi32>
  return %e : vector<2xi32>
}
func.func @main() {
  %a = arith.constant dense<[1, 2, 3, 4]> : vector<4xi32>
  %x = arith.constant dense<[10, 20]> : vector<2xi32>
  %y = arith.constant dense<[30, 40]> : vector<2xi32>
  %r = func.call @chain(%a, %x, %y) : (vector<4xi32>, vector<2xi32>, vector<2xi32>) -> vector<2xi32>
 vector.print %r : vector<2xi32>
  return
}
```

The reference splits the inserts and the extract into separate functions, so the folder cannot see through them.

`strided_split.mlir`:
```mlir
// Same computation as strided.mlir, but the inserts and the extract are in
// different functions, so the insert-chain folder cannot see through them.
func.func @ins(%a: vector<4xi32>, %x: vector<2xi32>, %y: vector<2xi32>) -> vector<4xi32> {
 %i1 = vector.insert_strided_slice %x, %a offsets = [1], strides = [1] : vector<2xi32> into vector<4xi32>
  %i2 = vector.insert_strided_slice %y, %i1 offsets = [2], strides = [1] : vector<2xi32> into vector<4xi32>
  return %i2 : vector<4xi32>
}
func.func @ext(%v: vector<4xi32>) -> vector<2xi32> {
 %e = vector.extract_strided_slice %v offsets = [1], sizes = [2], strides = [1] : vector<4xi32> to vector<2xi32>
  return %e : vector<2xi32>
}
func.func @main() {
  %a = arith.constant dense<[1, 2, 3, 4]> : vector<4xi32>
  %x = arith.constant dense<[10, 20]> : vector<2xi32>
 %y = arith.constant dense<[30, 40]> : vector<2xi32>
  %i2 = func.call @ins(%a, %x, %y) : (vector<4xi32>, vector<2xi32>, vector<2xi32>) -> vector<4xi32>
  %r = func.call @ext(%i2) : (vector<4xi32>) -> vector<2xi32>
  vector.print %r : vector<2xi32>
 return
}
```

(`$LLVM_BUILD` is the LLVM/MLIR build directory.)

```console
$ LOWER="-convert-vector-to-scf -convert-scf-to-cf -convert-vector-to-llvm -convert-arith-to-llvm -convert-index-to-llvm -convert-cf-to-llvm -convert-func-to-llvm -finalize-memref-to-llvm -convert-ub-to-llvm -reconcile-unrealized-casts"
$ RUN="mlir-runner -e main -entry-point-result=void -shared-libs=$LLVM_BUILD/lib/libmlir_runner_utils.so,$LLVM_BUILD/lib/libmlir_c_runner_utils.so"
$ mlir-opt strided_split.mlir | mlir-opt $LOWER | $RUN
( 10, 30 )
$ mlir-opt strided.mlir | mlir-opt $LOWER | $RUN
( 10, 20 )
$ mlir-opt strided.mlir -canonicalize
module {
  func.func @chain(%arg0: vector<4xi32>, %arg1: vector<2xi32>, %arg2: vector<2xi32>) -> vector<2xi32> {
    return %arg1 : vector<2xi32>
  }
  ...
```

The lanes extracted from `[1, 3)` are `x[0]`, written by the first insert at lane 1, and `y[0]`, written by the second insert at lane 2. So the result must be `( 10, 30 )`. When the second insert `[2, 4)` is visited, the extract start (1) is outside `[2, 4)`, so the insert is wrongly treated as disjoint and the fold returns `%x`. This also happens without an explicit `-canonicalize`, because the lowering pipeline itself folds.

This looks related to, but distinct from, #233054. That issue is about the *containment* check producing an ill-typed `vector.shuffle`; this one is about the *disjointness* check producing a well-typed but wrong value.

Both inputs verify and are UB-free.

Possible fix: treat the insert as disjoint only if `offset + size <= start || offset >= end` in some dimension; otherwise (partial overlap) stop folding.

**Version**: llvm-project main @ 018b722b2d48cf42dccded46e1226f1d9c29f339 (2026-10-17), Release + assertions, x86_64 Linux. Also reproduced on main @ a60677dca3a8536a0cf5bec1b846610014a983fd (2026-11-12).

*Disclosure: I wrote this report with the help of Claude Code. I reproduced and reviewed everything in this report myself.*
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to