Index: mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp =================================================================== --- mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp +++ mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp @@ -465,15 +465,14 @@ if (!type) return nullptr; if (auto symbolRef = attr.dyn_cast()) - return instMap[c] = bEntry.create(unknownLoc, type, - symbolRef.getValue()); - return instMap[c] = bEntry.create(unknownLoc, type, attr); + return bEntry.create(unknownLoc, type, symbolRef.getValue()); + return bEntry.create(unknownLoc, type, attr); } if (auto *cn = dyn_cast(c)) { Type type = processType(cn->getType()); if (!type) return nullptr; - return instMap[c] = bEntry.create(unknownLoc, type); + return bEntry.create(unknownLoc, type); } if (auto *gv = dyn_cast(c)) return bEntry.create(UnknownLoc::get(context), @@ -498,13 +497,13 @@ // Remove this zombie LLVM instruction now, leaving us only with the MLIR // op. i->deleteValue(); - return instMap[c] = value; + return value; } if (auto *ue = dyn_cast(c)) { Type type = processType(ue->getType()); if (!type) return nullptr; - return instMap[c] = bEntry.create(UnknownLoc::get(context), type); + return bEntry.create(UnknownLoc::get(context), type); } if (isa(c) || isa(c)) { Index: mlir/test/Target/LLVMIR/Import/incorrect-constant-caching.ll =================================================================== --- /dev/null +++ mlir/test/Target/LLVMIR/Import/incorrect-constant-caching.ll @@ -0,0 +1,34 @@ +; RUN: mlir-translate --import-llvm %s | FileCheck %s + +; Testing the fix for issue where llvm.getelementptr translated from the second +; ConstantExpr-GEP tried to use llvm.constant(0: i32)-s that were below itself, +; because it's reusing the cached ConstantInt-type zero value translated earlier. + +; This test is primarily used to make sure an verification is passed. Thus, we +; only wrote minimum level of checks. + +%my_struct = type {i32, i8*} +; CHECK: llvm.mlir.constant(0 : i32) : i32 +; CHECK: llvm.mlir.constant(0 : i32) : i32 +; CHECK: llvm.mlir.addressof @str1 : !llvm.ptr> +; CHECK: llvm.getelementptr +; CHECK: llvm.mlir.constant(7 : i32) : i32 +; CHECK: llvm.mlir.undef : !llvm.struct<"my_struct", (i32, ptr)> +; CHECK: llvm.insertvalue +; CHECK: llvm.insertvalue +; CHECK: llvm.mlir.constant(0 : i32) : i32 +; CHECK: llvm.mlir.constant(0 : i32) : i32 +; CHECK: llvm.mlir.addressof @str0 : !llvm.ptr> +; CHECK: llvm.getelementptr +; CHECK: llvm.mlir.constant(8 : i32) : i32 +; CHECK: llvm.mlir.undef : !llvm.struct<"my_struct", (i32, ptr)> +; CHECK: llvm.insertvalue +; CHECK: llvm.insertvalue +; CHECK: llvm.mlir.undef : !llvm.array<2 x struct<"my_struct", (i32, ptr)>> +; CHECK: llvm.insertvalue +; CHECK: llvm.insertvalue +; CHECK: llvm.return +@str0 = private unnamed_addr constant [5 x i8] c"aaaa\00" +@str1 = private unnamed_addr constant [5 x i8] c"bbbb\00" +@g = global [2 x %my_struct] [%my_struct {i32 8, i8* getelementptr ([5 x i8], [5 x i8]* @str0, i32 0, i32 0)}, %my_struct {i32 7, i8* getelementptr ([5 x i8], [5 x i8]* @str1, i32 0, i32 0)}] +