diff --git a/mlir/lib/Target/LLVMIR/DebugImporter.h b/mlir/lib/Target/LLVMIR/DebugImporter.h --- a/mlir/lib/Target/LLVMIR/DebugImporter.h +++ b/mlir/lib/Target/LLVMIR/DebugImporter.h @@ -35,8 +35,9 @@ /// Translates the given LLVM debug location to an MLIR location. Location translateLoc(llvm::DILocation *loc); - /// Translates the debug information for the given function. - void translate(llvm::Function *func, LLVMFuncOp funcOp); + /// Translates the debug information for the given function into a Location. + /// Returns UnknownLoc if `func` has no debug information attached to it. + Location translateFuncLocation(llvm::Function *func); /// Translates the given LLVM debug metadata to MLIR. DINodeAttr translate(llvm::DINode *node); diff --git a/mlir/lib/Target/LLVMIR/DebugImporter.cpp b/mlir/lib/Target/LLVMIR/DebugImporter.cpp --- a/mlir/lib/Target/LLVMIR/DebugImporter.cpp +++ b/mlir/lib/Target/LLVMIR/DebugImporter.cpp @@ -23,14 +23,14 @@ using namespace mlir::LLVM; using namespace mlir::LLVM::detail; -void DebugImporter::translate(llvm::Function *func, LLVMFuncOp funcOp) { +Location DebugImporter::translateFuncLocation(llvm::Function *func) { if (!func->getSubprogram()) - return; + return UnknownLoc::get(context); // Add a fused location to link the subprogram information. StringAttr name = StringAttr::get(context, func->getSubprogram()->getName()); - funcOp->setLoc(FusedLocWith::get( - {NameLoc::get(name)}, translate(func->getSubprogram()), context)); + return FusedLocWith::get( + {NameLoc::get(name)}, translate(func->getSubprogram()), context); } //===----------------------------------------------------------------------===// @@ -237,7 +237,7 @@ Location DebugImporter::translateLoc(llvm::DILocation *loc) { if (!loc) - return mlirModule.getLoc(); + return UnknownLoc::get(context); // Get the file location of the instruction. Location result = FileLineColLoc::get(context, loc->getFilename(), diff --git a/mlir/lib/Target/LLVMIR/ModuleImport.cpp b/mlir/lib/Target/LLVMIR/ModuleImport.cpp --- a/mlir/lib/Target/LLVMIR/ModuleImport.cpp +++ b/mlir/lib/Target/LLVMIR/ModuleImport.cpp @@ -594,13 +594,13 @@ if (globalVar.getName() == getGlobalCtorsVarName() || globalVar.getName() == getGlobalDtorsVarName()) { if (failed(convertGlobalCtorsAndDtors(&globalVar))) { - return emitError(mlirModule.getLoc()) + return emitError(UnknownLoc::get(context)) << "unhandled global variable: " << diag(globalVar); } continue; } if (failed(convertGlobal(&globalVar))) { - return emitError(mlirModule.getLoc()) + return emitError(UnknownLoc::get(context)) << "unhandled global variable: " << diag(globalVar); } } @@ -1019,7 +1019,7 @@ } FailureOr ModuleImport::convertConstant(llvm::Constant *constant) { - Location loc = mlirModule.getLoc(); + Location loc = UnknownLoc::get(context); // Convert constants that can be represented as attributes. if (Attribute attr = getConstantAsAttr(constant)) { @@ -1188,7 +1188,7 @@ if (auto *constant = dyn_cast(value)) return convertConstantExpr(constant); - Location loc = mlirModule.getLoc(); + Location loc = UnknownLoc::get(context); if (auto *inst = dyn_cast(value)) loc = translateLoc(inst->getDebugLoc()); return emitError(loc) << "unhandled value: " << diag(*value); @@ -1721,13 +1721,11 @@ OpBuilder::InsertionGuard guard(builder); builder.setInsertionPoint(mlirModule.getBody(), mlirModule.getBody()->end()); + Location loc = debugImporter->translateFuncLocation(func); LLVMFuncOp funcOp = builder.create( - mlirModule.getLoc(), func->getName(), functionType, + loc, func->getName(), functionType, convertLinkageFromLLVM(func->getLinkage()), dsoLocal, cconv); - // Set the function debug information if available. - debugImporter->translate(func, funcOp); - convertParameterAttributes(func, funcOp, builder); if (FlatSymbolRefAttr personality = getPersonalityAsAttr(func)) diff --git a/mlir/test/Target/LLVMIR/Import/basic.ll b/mlir/test/Target/LLVMIR/Import/basic.ll --- a/mlir/test/Target/LLVMIR/Import/basic.ll +++ b/mlir/test/Target/LLVMIR/Import/basic.ll @@ -1,7 +1,7 @@ ; RUN: mlir-translate -import-llvm %s | FileCheck %s ; RUN: mlir-translate -import-llvm -mlir-print-debuginfo %s | FileCheck %s --check-prefix=CHECK-DBG -; CHECK-DBG: #[[MODULELOC:.+]] = loc({{.*}}basic.ll{{.*}}:0:0) +; CHECK-DBG: #[[UNKNOWN_LOC:.+]] = loc(unknown) @global = external global double, align 8 @@ -9,7 +9,7 @@ declare float @fe(i32) ; CHECK-LABEL: llvm.func internal @f1(%arg0: i64) -> i32 attributes {dso_local, passthrough = ["norecurse"]} { -; CHECK-DBG: llvm.func internal @f1(%arg0: i64 loc({{.*}}basic.ll{{.*}}:0:0)) -> i32 attributes {dso_local, passthrough = ["norecurse"]} { +; CHECK-DBG: llvm.func internal @f1(%arg0: i64 loc(unknown)) -> i32 attributes {dso_local, passthrough = ["norecurse"]} { ; CHECK: %[[c2:[0-9]+]] = llvm.mlir.constant(2 : i32) : i32 ; CHECK: %[[c1:[0-9]+]] = llvm.mlir.constant(true) : i1 ; CHECK: %[[c43:[0-9]+]] = llvm.mlir.constant(43 : i32) : i32 @@ -18,7 +18,7 @@ entry: ; CHECK: %{{[0-9]+}} = llvm.inttoptr %arg0 : i64 to !llvm.ptr %aa = inttoptr i64 %a to ptr -; CHECK-DBG: llvm.mlir.addressof @global : !llvm.ptr loc(#[[MODULELOC]]) +; CHECK-DBG: llvm.mlir.addressof @global : !llvm.ptr loc(#[[UNKNOWN_LOC]]) ; %[[addrof:[0-9]+]] = llvm.mlir.addressof @global : !llvm.ptr ; %[[addrof2:[0-9]+]] = llvm.mlir.addressof @global : !llvm.ptr ; %{{[0-9]+}} = llvm.inttoptr %arg0 : i64 to !llvm.ptr @@ -27,13 +27,12 @@ %bb = ptrtoint ptr @global to i64 %cc = getelementptr double, ptr @global, i32 3 ; CHECK: %[[b:[0-9]+]] = llvm.trunc %arg0 : i64 to i32 -; CHECK-DBG: llvm.trunc %arg0 : i64 to i32 loc(#[[MODULELOC]]) +; CHECK-DBG: llvm.trunc %arg0 : i64 to i32 loc(#[[UNKNOWN_LOC]]) %b = trunc i64 %a to i32 ; CHECK: %[[c:[0-9]+]] = llvm.call @fe(%[[b]]) : (i32) -> f32 %c = call float @fe(i32 %b) ; CHECK: %[[d:[0-9]+]] = llvm.fptosi %[[c]] : f32 to i32 %d = fptosi float %c to i32 -; FIXME: icmp should return i1. ; CHECK: %[[e:[0-9]+]] = llvm.icmp "ne" %[[d]], %[[c2]] : i32 %e = icmp ne i32 %d, 2 ; CHECK: llvm.cond_br %[[e]], ^bb1, ^bb2 @@ -51,7 +50,7 @@ ; CHECK: llvm.return %[[c43]] ret i32 43 } -; CHECK-DBG: } loc(#[[MODULELOC]]) +; CHECK-DBG: } loc(#[[UNKNOWN_LOC]]) ; CHECK-LABEL: @hasGCFunction ; CHECK-SAME: garbageCollector = "statepoint-example" @@ -59,7 +58,7 @@ ret void } -;CHECK-LABEL: @useFreezeOp +; CHECK-LABEL: @useFreezeOp define i32 @useFreezeOp(i32 %x) { ;CHECK: %{{[0-9]+}} = llvm.freeze %{{[0-9a-z]+}} : i32 %1 = freeze i32 %x diff --git a/mlir/test/Target/LLVMIR/Import/debug-info.ll b/mlir/test/Target/LLVMIR/Import/debug-info.ll --- a/mlir/test/Target/LLVMIR/Import/debug-info.ll +++ b/mlir/test/Target/LLVMIR/Import/debug-info.ll @@ -1,17 +1,17 @@ ; RUN: mlir-translate -import-llvm -mlir-print-debuginfo -split-input-file %s | FileCheck %s -; CHECK: #[[$MODULELOC:.+]] = loc({{.*}}debug-info.ll{{.*}}:0:0) +; CHECK: #[[$UNKNOWN_LOC:.+]] = loc(unknown) ; CHECK-LABEL: @module_loc( define i32 @module_loc(i32 %0) { entry: br label %next end: - ; CHECK: ^{{.*}}(%{{.+}}: i32 loc({{.*}}debug-info.ll{{.*}}:0:0)): + ; CHECK: ^{{.*}}(%{{.+}}: i32 loc(unknown)): %1 = phi i32 [ %2, %next ] ret i32 %1 next: - ; CHECK: = llvm.mul %{{.+}}, %{{.+}} : i32 loc(#[[$MODULELOC]]) + ; CHECK: = llvm.mul %{{.+}}, %{{.+}} : i32 loc(#[[$UNKNOWN_LOC]]) %2 = mul i32 %0, %0 br label %end } diff --git a/mlir/test/Target/LLVMIR/Import/import-failure.ll b/mlir/test/Target/LLVMIR/Import/import-failure.ll --- a/mlir/test/Target/LLVMIR/Import/import-failure.ll +++ b/mlir/test/Target/LLVMIR/Import/import-failure.ll @@ -1,6 +1,6 @@ ; RUN: not mlir-translate -import-llvm -emit-expensive-warnings -split-input-file %s 2>&1 | FileCheck %s -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: error: unhandled instruction: indirectbr ptr %dst, [label %bb1, label %bb2] define i32 @unhandled_instruction(ptr %dst) { indirectbr ptr %dst, [label %bb1, label %bb2] @@ -12,7 +12,7 @@ ; // ----- -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: error: unhandled value: ptr asm "bswap $0", "=r,r" define i32 @unhandled_value(i32 %arg1) { %1 = call i32 asm "bswap $0", "=r,r"(i32 %arg1) @@ -21,9 +21,9 @@ ; // ----- -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: unhandled constant: ptr blockaddress(@unhandled_constant, %bb1) since blockaddress(...) is unsupported -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: error: unhandled instruction: ret ptr blockaddress(@unhandled_constant, %bb1) define ptr @unhandled_constant() { br label %bb1 @@ -33,9 +33,9 @@ ; // ----- -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: unhandled constant: ptr blockaddress(@unhandled_global, %bb1) since blockaddress(...) is unsupported -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: error: unhandled global variable: @private = private global ptr blockaddress(@unhandled_global, %bb1) @private = private global ptr blockaddress(@unhandled_global, %bb1) @@ -49,7 +49,7 @@ declare void @llvm.gcroot(ptr %arg1, ptr %arg2) -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: error: unhandled intrinsic: call void @llvm.gcroot(ptr %arg1, ptr null) define void @unhandled_intrinsic() gc "example" { %arg1 = alloca ptr @@ -83,7 +83,7 @@ ; // ----- ; global_dtors with non-null data fields cannot be represented in MLIR. -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: error: unhandled global variable: @llvm.global_dtors @llvm.global_dtors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @foo, ptr @foo }] @@ -132,9 +132,9 @@ ; // ----- -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: expected all loop properties to be either debug locations or metadata nodes -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, i32 42} define void @invalid_loop_node(i64 %n, ptr %A) { entry: @@ -147,9 +147,9 @@ ; // ----- -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: cannot import empty loop property -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1} define void @invalid_loop_node(i64 %n, ptr %A) { entry: @@ -163,9 +163,9 @@ ; // ----- -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: cannot import loop property without a name -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1} define void @invalid_loop_node(i64 %n, ptr %A) { entry: @@ -179,9 +179,9 @@ ; // ----- -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: cannot import loop properties with duplicated names llvm.loop.disable_nonforced -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1, !1} define void @unsupported_loop_annotation(i64 %n, ptr %A) { entry: @@ -195,9 +195,9 @@ ; // ----- -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: expected metadata node llvm.loop.disable_nonforced to hold no value -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1} define void @unsupported_loop_annotation(i64 %n, ptr %A) { entry: @@ -211,9 +211,9 @@ ; // ----- -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: expected metadata nodes llvm.loop.unroll.enable and llvm.loop.unroll.disable to be mutually exclusive -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1, !2} define void @unsupported_loop_annotation(i64 %n, ptr %A) { entry: @@ -228,9 +228,9 @@ ; // ----- -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: expected metadata node llvm.loop.vectorize.enable to hold a boolean value -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1} define void @unsupported_loop_annotation(i64 %n, ptr %A) { entry: @@ -244,9 +244,9 @@ ; // ----- -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: expected metadata node llvm.loop.vectorize.width to hold an i32 value -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1} define void @unsupported_loop_annotation(i64 %n, ptr %A) { entry: @@ -260,9 +260,9 @@ ; // ----- -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: expected metadata node llvm.loop.vectorize.followup_all to hold an MDNode -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1} define void @unsupported_loop_annotation(i64 %n, ptr %A) { entry: @@ -276,9 +276,9 @@ ; // ----- -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: expected metadata node llvm.loop.parallel_accesses to hold one or multiple MDNodes -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1} define void @unsupported_loop_annotation(i64 %n, ptr %A) { entry: @@ -292,9 +292,9 @@ ; // ----- -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: unknown loop annotation llvm.loop.typo -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: unhandled metadata: !0 = distinct !{!0, !1, !2} define void @unsupported_loop_annotation(i64 %n, ptr %A) { entry: @@ -309,7 +309,7 @@ ; // ----- -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: could not lookup access group define void @unused_access_group(ptr %arg) { entry: @@ -326,7 +326,7 @@ ; // ----- -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: expected function_entry_count to be attached to a function ; CHECK: warning: unhandled metadata: !0 = !{!"function_entry_count", i64 42} define void @cond_br(i1 %arg) { @@ -342,7 +342,7 @@ ; // ----- -; CHECK: import-failure.ll +; CHECK: ; CHECK-SAME: warning: dropped instruction: call void @llvm.experimental.noalias.scope.decl(metadata !0) define void @unused_scope() { call void @llvm.experimental.noalias.scope.decl(metadata !0)