[Mlir-commits] [mlir] dcdd5d3 - [mlir][LLVMIR] Use insertelement if needed when translating ConstantAggregate

Min-Yih Hsu llvmlistbot at llvm.org
Wed Jun 15 14:33:56 PDT 2022


Author: Min-Yih Hsu
Date: 2022-06-15T14:33:47-07:00
New Revision: dcdd5d312f29356fc4329e4c2a9856db870ad617

URL: https://github.com/llvm/llvm-project/commit/dcdd5d312f29356fc4329e4c2a9856db870ad617
DIFF: https://github.com/llvm/llvm-project/commit/dcdd5d312f29356fc4329e4c2a9856db870ad617.diff

LOG: [mlir][LLVMIR] Use insertelement if needed when translating ConstantAggregate

When translating from a llvm::ConstantAggregate with vector type, we
should lower to insertelement operations (if needed) rather than using
insertvalue.

Differential Revision: https://reviews.llvm.org/D127534

Added: 
    

Modified: 
    mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
    mlir/test/Target/LLVMIR/Import/constant-aggregate.ll

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp b/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
index 53e0a3138489c..32237bd022e7b 100644
--- a/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
+++ b/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
@@ -533,15 +533,28 @@ Value Importer::processConstant(llvm::Constant *c) {
     Type rootType = processType(c->getType());
     if (!rootType)
       return nullptr;
+    bool useInsertValue = rootType.isa<LLVMArrayType, LLVMStructType>();
+    assert((useInsertValue || LLVM::isCompatibleVectorType(rootType)) &&
+           "unrecognized aggregate type");
     Value root = bEntry.create<UndefOp>(unknownLoc, rootType);
     for (unsigned i = 0; i < numElements; ++i) {
       llvm::Constant *element = getElement(i);
       Value elementValue = processConstant(element);
       if (!elementValue)
         return nullptr;
-      ArrayAttr indexAttr = bEntry.getI32ArrayAttr({static_cast<int32_t>(i)});
-      root = bEntry.create<InsertValueOp>(UnknownLoc::get(context), rootType,
-                                          root, elementValue, indexAttr);
+      if (useInsertValue) {
+        ArrayAttr indexAttr = bEntry.getI32ArrayAttr({static_cast<int32_t>(i)});
+        root = bEntry.create<InsertValueOp>(UnknownLoc::get(context), rootType,
+                                            root, elementValue, indexAttr);
+      } else {
+        Attribute indexAttr = bEntry.getI32IntegerAttr(static_cast<int32_t>(i));
+        Value indexValue = bEntry.create<ConstantOp>(
+            unknownLoc, bEntry.getI32Type(), indexAttr);
+        if (!indexValue)
+          return nullptr;
+        root = bEntry.create<InsertElementOp>(
+            UnknownLoc::get(context), rootType, root, elementValue, indexValue);
+      }
     }
     return root;
   }

diff  --git a/mlir/test/Target/LLVMIR/Import/constant-aggregate.ll b/mlir/test/Target/LLVMIR/Import/constant-aggregate.ll
index 3a8d270df09f2..71919a08b05e8 100644
--- a/mlir/test/Target/LLVMIR/Import/constant-aggregate.ll
+++ b/mlir/test/Target/LLVMIR/Import/constant-aggregate.ll
@@ -30,3 +30,12 @@
 %NestedAggType = type {%SimpleAggType, %SimpleAggType*}
 @nestedAgg = global %NestedAggType { %SimpleAggType{i32 1, i8 2, i16 3, i32 4}, %SimpleAggType* null }
 
+; CHECK: %[[C0:.+]] = llvm.mlir.null : !llvm.ptr<struct<"SimpleAggType", (i32, i8, i16, i32)>>
+; CHECK: %[[C1:.+]] = llvm.mlir.null : !llvm.ptr<struct<"SimpleAggType", (i32, i8, i16, i32)>>
+; CHECK: %[[ROOT:.+]] = llvm.mlir.undef : !llvm.vec<2 x ptr<struct<"SimpleAggType", (i32, i8, i16, i32)>>>
+; CHECK: %[[P0:.+]] = llvm.mlir.constant(0 : i32) : i32
+; CHECK: %[[CHAIN0:.+]] = llvm.insertelement %[[C1]], %[[ROOT]][%[[P0]] : i32] : !llvm.vec<2 x ptr<struct<"SimpleAggType", (i32, i8, i16, i32)>>>
+; CHECK: %[[P1:.+]] = llvm.mlir.constant(1 : i32) : i32
+; CHECK: %[[CHAIN1:.+]] = llvm.insertelement %[[C0]], %[[CHAIN0]][%[[P1]] : i32] : !llvm.vec<2 x ptr<struct<"SimpleAggType", (i32, i8, i16, i32)>>>
+; CHECK: llvm.return %[[CHAIN1]] : !llvm.vec<2 x ptr<struct<"SimpleAggType", (i32, i8, i16, i32)>>>
+ at vectorAgg = global <2 x %SimpleAggType*> <%SimpleAggType* null, %SimpleAggType* null>


        


More information about the Mlir-commits mailing list