diff --git a/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp b/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp --- a/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp +++ b/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp @@ -26,6 +26,7 @@ #include "llvm/ADT/TypeSwitch.h" #include "llvm/IR/Attributes.h" #include "llvm/IR/Constants.h" +#include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/Function.h" #include "llvm/IR/InlineAsm.h" @@ -188,11 +189,9 @@ /// placeholder that will be remapped later if this is an instruction that /// has not yet been visited. Value processValue(llvm::Value *value); - /// Create the most accurate Location possible using a llvm::DebugLoc and - /// possibly an llvm::Instruction to narrow the Location if debug information - /// is unavailable. - Location processDebugLoc(const llvm::DebugLoc &loc, - llvm::Instruction *inst = nullptr); + // Translate the debug location to a FileLineColLoc, if `loc` is non-null. + // Otherwise, return UnknownLoc. + Location processDebugLoc(llvm::DILocation *loc); /// `br` branches to `target`. Append the block arguments to attach to the /// generated branch op to `blockArguments`. These should be in the same order /// as the PHIs in `target`. @@ -249,14 +248,12 @@ }; } // namespace -Location Importer::processDebugLoc(const llvm::DebugLoc &loc, - llvm::Instruction *inst) { +Location Importer::processDebugLoc(llvm::DILocation *loc) { if (!loc) - return unknownLoc; + return UnknownLoc::get(context); - // FIXME: Obtain the filename from DILocationInfo. - return FileLineColLoc::get(context, "imported-bitcode", loc.getLine(), - loc.getCol()); + return FileLineColLoc::get(context, loc->getFilename(), loc->getLine(), + loc->getColumn()); } Type Importer::processType(llvm::Type *type) { @@ -812,7 +809,7 @@ LogicalResult Importer::processInstruction(llvm::Instruction *inst) { // FIXME: Support uses of SubtargetData. Currently inbounds GEPs, fast-math // flags and call / operand attributes are not supported. - Location loc = processDebugLoc(inst->getDebugLoc(), inst); + Location loc = processDebugLoc(inst->getDebugLoc()); assert(!instMap.count(inst) && "processInstruction must be called only once per instruction!"); switch (inst->getOpcode()) { @@ -993,7 +990,7 @@ if (!type) return failure(); instMap[inst] = b.getInsertionBlock()->addArgument( - type, processDebugLoc(inst->getDebugLoc(), inst)); + type, processDebugLoc(inst->getDebugLoc())); return success(); } case llvm::Instruction::Call: { 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 @@ -2,7 +2,6 @@ ; RUN: mlir-translate -import-llvm -mlir-print-debuginfo %s | FileCheck %s --check-prefix=CHECK-DBG ; CHECK-DBG: #[[UNKNOWNLOC:.+]] = loc(unknown) -; CHECK-DBG: #[[ZEROLOC:.+]] = loc("imported-bitcode":0:0) %struct.t = type {} %struct.s = type { %struct.t, i64 } @@ -151,7 +150,7 @@ %bb = ptrtoint double* @g2 to i64 %cc = getelementptr double, double* @g2, i32 2 ; CHECK: %[[b:[0-9]+]] = llvm.trunc %arg0 : i64 to i32 -; CHECK-DBG: llvm.trunc %arg0 : i64 to i32 loc(#[[ZEROLOC]]) +; CHECK-DBG: llvm.trunc %arg0 : i64 to i32 loc(#[[UNKNOWNLOC]]) %b = trunc i64 %a to i32 ; CHECK: %[[c:[0-9]+]] = llvm.call @fe(%[[b]]) : (i32) -> f32 %c = call float @fe(i32 %b) diff --git a/mlir/test/Target/LLVMIR/Import/debug-info.ll b/mlir/test/Target/LLVMIR/Import/debug-info.ll new file mode 100644 --- /dev/null +++ b/mlir/test/Target/LLVMIR/Import/debug-info.ll @@ -0,0 +1,42 @@ +; RUN: mlir-translate -import-llvm -mlir-print-debuginfo -split-input-file %s | FileCheck %s + +; CHECK: #[[$UNKNOWNLOC:.+]] = loc(unknown) +; CHECK-LABEL: @unknown( +define i32 @unknown(i32 %0) { +entry: + br label %next +end: + ; CHECK: ^bb1(%{{.+}}: i32 loc(unknown)): + %1 = phi i32 [ %2, %next ] + ret i32 %1 +next: + ; CHECK: = llvm.mul %{{.+}}, %{{.+}} : i32 loc(#[[$UNKNOWNLOC:.+]]) + %2 = mul i32 %0, %0 + br label %end +} + +; // ----- + +; CHECK-LABEL: @known_loc( +define i32 @known_loc(i32 %0) { +entry: + br label %next +end: + ; CHECK: ^bb1(%{{.+}}: i32 loc("known_loc.cpp":5:2)): + %1 = phi i32 [ %2, %next ], !dbg !4 + ret i32 %1 +next: + ; CHECK: = llvm.mul %{{.+}}, %{{.+}} : i32 loc(#[[LOC:.+]]) + %2 = mul i32 %0, %0, !dbg !5 + br label %end +} +; CHECK: #[[LOC]] = loc("known_loc.cpp":8:3) + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!1} +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !2) +!1 = !{i32 2, !"Debug Info Version", i32 3} +!2 = !DIFile(filename: "known_loc.cpp", directory: "/") +!3 = distinct !DISubprogram(name: "known_loc", scope: !0, file: !2, line: 1, scopeLine: 1, unit: !0) +!4 = !DILocation(line: 5, column: 2, scope: !3) +!5 = !DILocation(line: 8, column: 3, scope: !3)