[Mlir-commits] [mlir] [mlir][vector] Add `vector.from_elements` op (PR #95938)

Matthias Springer llvmlistbot at llvm.org
Wed Jun 19 00:27:03 PDT 2024


================
@@ -1836,6 +1836,34 @@ struct VectorDeinterleaveOpLowering
   }
 };
 
+/// Conversion pattern for a `vector.from_elements`.
+struct VectorFromElementsLowering
+    : public ConvertOpToLLVMPattern<vector::FromElementsOp> {
+  using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
+
+  LogicalResult
+  matchAndRewrite(vector::FromElementsOp fromElementsOp, OpAdaptor adaptor,
+                  ConversionPatternRewriter &rewriter) const override {
+    Location loc = fromElementsOp.getLoc();
+    VectorType vectorType = fromElementsOp.getType();
+    // TODO: Multi-dimensional vectors lower to !llvm.array<... x vector<>>.
+    // Such ops should be handled in the same way as vector.insert.
+    if (vectorType.getRank() > 1)
+      return rewriter.notifyMatchFailure(fromElementsOp,
+                                         "rank > 1 vectors are not supported");
+    Type llvmType = typeConverter->convertType(vectorType);
+    Value result = rewriter.create<LLVM::UndefOp>(loc, llvmType);
+    for (auto it : llvm::enumerate(adaptor.getElements())) {
+      result = rewriter.create<LLVM::InsertElementOp>(
+          loc, result, it.value(),
+          rewriter.create<LLVM::ConstantOp>(
+              loc, rewriter.getI64IntegerAttr(it.index())));
----------------
matthias-springer wrote:

Good point. `InsertElementOp` supports any integer type. I change the implementation to create `vector.insert`, which will then be lowered to `llvm.insertelement`. Then we are consistent and use whatever integer type the other lowerings use. (Presumably it can be configured through the type converter.)


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


More information about the Mlir-commits mailing list