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(); + assert((useInsertValue || LLVM::isCompatibleVectorType(rootType)) && + "unrecognized aggregate type"); Value root = bEntry.create(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(i)}); - root = bEntry.create(UnknownLoc::get(context), rootType, - root, elementValue, indexAttr); + if (useInsertValue) { + ArrayAttr indexAttr = bEntry.getI32ArrayAttr({static_cast(i)}); + root = bEntry.create(UnknownLoc::get(context), rootType, + root, elementValue, indexAttr); + } else { + Attribute indexAttr = bEntry.getI32IntegerAttr(static_cast(i)); + Value indexValue = bEntry.create( + unknownLoc, bEntry.getI32Type(), indexAttr); + if (!indexValue) + return nullptr; + root = bEntry.create( + 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> +; CHECK: %[[C1:.+]] = llvm.mlir.null : !llvm.ptr> +; CHECK: %[[ROOT:.+]] = llvm.mlir.undef : !llvm.vec<2 x ptr>> +; CHECK: %[[P0:.+]] = llvm.mlir.constant(0 : i32) : i32 +; CHECK: %[[CHAIN0:.+]] = llvm.insertelement %[[C1]], %[[ROOT]][%[[P0]] : i32] : !llvm.vec<2 x ptr>> +; CHECK: %[[P1:.+]] = llvm.mlir.constant(1 : i32) : i32 +; CHECK: %[[CHAIN1:.+]] = llvm.insertelement %[[C0]], %[[CHAIN0]][%[[P1]] : i32] : !llvm.vec<2 x ptr>> +; CHECK: llvm.return %[[CHAIN1]] : !llvm.vec<2 x ptr>> +@vectorAgg = global <2 x %SimpleAggType*> <%SimpleAggType* null, %SimpleAggType* null>