[Mlir-commits] [mlir] [MLIR][Vector]: Generalize conversion of `vector.insert` to LLVM in line with `vector.extract` (PR #128915)

Kunwar Grover llvmlistbot at llvm.org
Wed Feb 26 11:52:40 PST 2025


================
@@ -1233,53 +1225,74 @@ class VectorInsertOpConversion
     SmallVector<OpFoldResult> positionVec = getMixedValues(
         adaptor.getStaticPosition(), adaptor.getDynamicPosition(), rewriter);
 
-    // Overwrite entire vector with value. Should be handled by folder, but
-    // just to be safe.
-    ArrayRef<OpFoldResult> position(positionVec);
-    if (position.empty()) {
-      rewriter.replaceOp(insertOp, adaptor.getSource());
-      return success();
-    }
-
-    // One-shot insertion of a vector into an array (only requires insertvalue).
-    if (isa<VectorType>(sourceType)) {
-      if (insertOp.hasDynamicPosition())
-        return failure();
-
-      Value inserted = rewriter.create<LLVM::InsertValueOp>(
-          loc, adaptor.getDest(), adaptor.getSource(), getAsIntegers(position));
-      rewriter.replaceOp(insertOp, inserted);
-      return success();
+    // The logic in this pattern mirrors VectorExtractOpConversion. Refer to
+    // its explanatory comment about how N-D vectors are converted as nested
+    // aggregates (llvm.array's) of 1D vectors.
+    //
+    // There are 3 steps here, vs 2 in VectorExtractOpConversion:
+    // - Extraction of a 1D vector from the nested aggregate: llvm.extractvalue.
+    // - Insertion into the 1D vector: llvm.insertelement.
+    // - Insertion of the 1D vector into the nested aggregate: llvm.insertvalue.
----------------
Groverkss wrote:

I think I would reword this to:

```
The innermost dimension of the destination vector, when converted to a nested aggregate form, will always be a 1D vector.

- If the insertion is happening into the innermost dimension of the destination vector:
   - If the destination is a nested aggregate, extract a 1D vector out of the aggregate. This can be done using llvm.extractvalue. The destination is now guranteed to be a 1D vector, to which we are inserting.
   - Do the insertion on the 1D destination vector, and make the result the new source nested aggregate. This can be done using llvm.insertelement.
 - Insert the source nested aggregate into the destination nested aggregate.
```

it makes it easier to understand the possible cases.

https://github.com/llvm/llvm-project/pull/128915


More information about the Mlir-commits mailing list