[Mlir-commits] [mlir] [mlir][Vector] Fold chains of `vector.extract` with dynamic positions (PR #228686)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Sat Oct 3 03:10:15 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir-vector
Author: Federico Bruzzone (FedericoBruzzone)
<details>
<summary>Changes</summary>
`foldExtractOpFromExtractChain` bailed out when any extract in the chain had a dynamic position.
We now _simply_ merge the positions as `OpFoldResult`s instead, then split them back into the `static_position` attribute and the dynamic operands with `decomposeMixedValues` to correctly set them.
---
Full diff: https://github.com/llvm/llvm-project/pull/228686.diff
2 Files Affected:
- (modified) mlir/lib/Dialect/Vector/IR/VectorOps.cpp (+20-18)
- (modified) mlir/test/Dialect/Vector/canonicalize.mlir (+28-4)
``````````diff
diff --git a/mlir/lib/Dialect/Vector/IR/VectorOps.cpp b/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
index 638f7017bc5819..b48ef2b9830e52 100644
--- a/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
+++ b/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
@@ -1482,32 +1482,34 @@ static SmallVector<IntType> extractVector(ArrayAttr arrayAttr) {
}
/// Fold the result of chains of ExtractOp in place by simply concatenating the
-/// positions.
+/// positions. Both static and dynamic positions are supported.
+///
+/// BEFORE:
+/// ```mlir
+/// %0 = vector.extract %v[1, 2] : vector<3x4x5xf32>
+/// %1 = vector.extract %0[%c0, 3] : vector<5xf32>
+/// ```
+///
+/// AFTER:
+/// ```mlir
+/// %1 = vector.extract %v[1, 2, %c0, 3] : vector<3x4x5xf32>
+/// ```
static LogicalResult foldExtractOpFromExtractChain(ExtractOp extractOp) {
if (!extractOp.getSource().getDefiningOp<ExtractOp>())
return failure();
- // TODO: Canonicalization for dynamic position not implemented yet.
- if (extractOp.hasDynamicPosition())
- return failure();
-
- SmallVector<int64_t> globalPosition;
+ SmallVector<OpFoldResult> globalPosition = extractOp.getMixedPosition();
ExtractOp currentOp = extractOp;
- ArrayRef<int64_t> extrPos = currentOp.getStaticPosition();
- globalPosition.append(extrPos.rbegin(), extrPos.rend());
while (ExtractOp nextOp = currentOp.getSource().getDefiningOp<ExtractOp>()) {
currentOp = nextOp;
- // TODO: Canonicalization for dynamic position not implemented yet.
- if (currentOp.hasDynamicPosition())
- return failure();
- ArrayRef<int64_t> extrPos = currentOp.getStaticPosition();
- globalPosition.append(extrPos.rbegin(), extrPos.rend());
+ SmallVector<OpFoldResult> pos = currentOp.getMixedPosition();
+ globalPosition.insert(globalPosition.begin(), pos.begin(), pos.end());
}
- extractOp.setOperand(0, currentOp.getSource());
- // OpBuilder is only used as a helper to build an I64ArrayAttr.
- OpBuilder b(extractOp.getContext());
- std::reverse(globalPosition.begin(), globalPosition.end());
- extractOp.setStaticPosition(globalPosition);
+ auto [staticPos, dynPos] = decomposeMixedValues(globalPosition);
+ Value source = currentOp.getSource();
+ extractOp->setOperands(
+ llvm::to_vector(llvm::concat<Value>(ValueRange(source), dynPos)));
+ extractOp.setStaticPosition(staticPos);
return success();
}
diff --git a/mlir/test/Dialect/Vector/canonicalize.mlir b/mlir/test/Dialect/Vector/canonicalize.mlir
index 6b271ee5cce19d..cdd10e8e1db183 100644
--- a/mlir/test/Dialect/Vector/canonicalize.mlir
+++ b/mlir/test/Dialect/Vector/canonicalize.mlir
@@ -834,6 +834,31 @@ func.func @fold_extracts(%a : vector<3x4x5x6xf32>) -> (f32, vector<4x5x6xf32>) {
// -----
+// CHECK-LABEL: fold_extracts_dynamic_position
+// CHECK-SAME: %[[A:[a-zA-Z0-9]*]]: vector<3x4x5x6xf32>
+// CHECK-SAME: %[[I:[a-zA-Z0-9]*]]: index
+// CHECK-SAME: %[[J:[a-zA-Z0-9]*]]: index
+func.func @fold_extracts_dynamic_position(%a : vector<3x4x5x6xf32>, %i : index, %j : index) -> (f32, f32, f32) {
+ // Dynamic position in the inner extract.
+ %b = vector.extract %a[%i] : vector<4x5x6xf32> from vector<3x4x5x6xf32>
+ // CHECK: vector.extract %[[A]][%[[I]], 1, 2, 3] : f32 from vector<3x4x5x6xf32>
+ %c = vector.extract %b[1, 2, 3] : f32 from vector<4x5x6xf32>
+
+ // Dynamic position in the outer extract.
+ %d = vector.extract %a[0, 1] : vector<5x6xf32> from vector<3x4x5x6xf32>
+ // CHECK: vector.extract %[[A]][0, 1, %[[J]], 3] : f32 from vector<3x4x5x6xf32>
+ %e = vector.extract %d[%j, 3] : f32 from vector<5x6xf32>
+
+ // Dynamic positions in both extracts.
+ %f = vector.extract %a[%i, 1] : vector<5x6xf32> from vector<3x4x5x6xf32>
+ // CHECK: vector.extract %[[A]][%[[I]], 1, %[[J]], 3] : f32 from vector<3x4x5x6xf32>
+ %g = vector.extract %f[%j, 3] : f32 from vector<5x6xf32>
+
+ return %c, %e, %g : f32, f32, f32
+}
+
+// -----
+
// CHECK-LABEL: fold_extract_transpose
// CHECK-SAME: %[[A:[a-zA-Z0-9]*]]: vector<3x4x5x6xf32>
// CHECK-SAME: %[[B:[a-zA-Z0-9]*]]: vector<3x6x5x6xf32>
@@ -2710,11 +2735,10 @@ func.func @extract_insert_chain(%a: vector<2x16xf32>, %b: vector<12x8x16xf32>, %
// -----
-// CHECK-LABEL: extract_from_extract_chain_should_not_fold_dynamic_extracts
+// CHECK-LABEL: extract_from_extract_chain_fold_dynamic_extracts
// CHECK-SAME: (%[[VEC:.*]]: vector<2x4xf32>, %[[IDX:.*]]: index)
-// CHECK: %[[A:.*]] = vector.extract %[[VEC]][%[[IDX]]] : vector<4xf32> from vector<2x4xf32>
-// CHECK: %[[B:.*]] = vector.extract %[[A]][1] : f32 from vector<4xf32>
-func.func @extract_from_extract_chain_should_not_fold_dynamic_extracts(%v: vector<2x4xf32>, %index: index) -> f32 {
+// CHECK: vector.extract %[[VEC]][%[[IDX]], 1] : f32 from vector<2x4xf32>
+func.func @extract_from_extract_chain_fold_dynamic_extracts(%v: vector<2x4xf32>, %index: index) -> f32 {
%0 = vector.extract %v[%index] : vector<4xf32> from vector<2x4xf32>
%1 = vector.extract %0[1] : f32 from vector<4xf32>
return %1 : f32
``````````
</details>
https://github.com/llvm/llvm-project/pull/228686
More information about the Mlir-commits
mailing list