Skip to content

Commit 3589885

Browse files
committed
[mlir][llvm] Improve error messages during LLVM IR import.
Use the module location instead of unknown location if the imported LLVM IR module does not have more precise debug information. Additionally, use the diagMD function to print the metadata instead of just the metadata kind if the import fails. Reviewed By: definelicht Differential Revision: https://reviews.llvm.org/D141357
1 parent dbee203 commit 3589885

File tree

6 files changed

+84
-50
lines changed

6 files changed

+84
-50
lines changed

mlir/lib/Target/LLVMIR/DebugImporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ DINodeAttr DebugImporter::translate(llvm::DINode *node) {
191191

192192
Location DebugImporter::translateLoc(llvm::DILocation *loc) {
193193
if (!loc)
194-
return UnknownLoc::get(context);
194+
return mlirModule.getLoc();
195195

196196
// Get the file location of the instruction.
197197
Location result = FileLineColLoc::get(context, loc->getFilename(),

mlir/lib/Target/LLVMIR/DebugImporter.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ namespace detail {
2929

3030
class DebugImporter {
3131
public:
32-
DebugImporter(MLIRContext *context) : context(context) {}
32+
DebugImporter(ModuleOp mlirModule)
33+
: context(mlirModule.getContext()), mlirModule(mlirModule) {}
3334

3435
/// Translates the given LLVM debug location to an MLIR location.
3536
Location translateLoc(llvm::DILocation *loc);
@@ -69,6 +70,7 @@ class DebugImporter {
6970
DenseMap<llvm::DINode *, DINodeAttr> nodeToAttr;
7071

7172
MLIRContext *context;
73+
ModuleOp mlirModule;
7274
};
7375

7476
} // namespace detail

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ ModuleImport::ModuleImport(ModuleOp mlirModule,
354354
mlirModule(mlirModule), llvmModule(std::move(llvmModule)),
355355
iface(mlirModule->getContext()),
356356
typeTranslator(*mlirModule->getContext()),
357-
debugImporter(std::make_unique<DebugImporter>(mlirModule->getContext())) {
357+
debugImporter(std::make_unique<DebugImporter>(mlirModule)) {
358358
builder.setInsertionPointToStart(mlirModule.getBody());
359359
}
360360

@@ -636,13 +636,13 @@ LogicalResult ModuleImport::convertGlobals() {
636636
globalVar.getName() == getGlobalDtorsVarName()) {
637637
if (failed(convertGlobalCtorsAndDtors(&globalVar))) {
638638
return emitError(mlirModule.getLoc())
639-
<< "unhandled global variable " << diag(globalVar);
639+
<< "unhandled global variable: " << diag(globalVar);
640640
}
641641
continue;
642642
}
643643
if (failed(convertGlobal(&globalVar))) {
644644
return emitError(mlirModule.getLoc())
645-
<< "unhandled global variable " << diag(globalVar);
645+
<< "unhandled global variable: " << diag(globalVar);
646646
}
647647
}
648648
return success();
@@ -664,7 +664,9 @@ void ModuleImport::setNonDebugMetadataAttrs(llvm::Instruction *inst,
664664
continue;
665665
if (failed(iface.setMetadataAttrs(builder, kind, node, op, *this))) {
666666
Location loc = debugImporter->translateLoc(inst->getDebugLoc());
667-
emitWarning(loc) << "unhandled metadata (" << kind << ") " << diag(*inst);
667+
emitWarning(loc) << "unhandled metadata: "
668+
<< diagMD(node, llvmModule.get()) << " on "
669+
<< diag(*inst);
668670
}
669671
}
670672
}
@@ -967,8 +969,7 @@ ModuleImport::getConstantsToConvert(llvm::Constant *constant) {
967969
}
968970

969971
FailureOr<Value> ModuleImport::convertConstant(llvm::Constant *constant) {
970-
// Constants have no location attached.
971-
Location loc = UnknownLoc::get(context);
972+
Location loc = mlirModule.getLoc();
972973

973974
// Convert constants that can be represented as attributes.
974975
if (Attribute attr = getConstantAsAttr(constant)) {
@@ -1062,7 +1063,7 @@ FailureOr<Value> ModuleImport::convertConstant(llvm::Constant *constant) {
10621063
return root;
10631064
}
10641065

1065-
return emitError(loc) << "unhandled constant " << diag(*constant);
1066+
return emitError(loc) << "unhandled constant: " << diag(*constant);
10661067
}
10671068

10681069
FailureOr<Value> ModuleImport::convertConstantExpr(llvm::Constant *constant) {
@@ -1107,10 +1108,10 @@ FailureOr<Value> ModuleImport::convertValue(llvm::Value *value) {
11071108
if (auto *constant = dyn_cast<llvm::Constant>(value))
11081109
return convertConstantExpr(constant);
11091110

1110-
Location loc = UnknownLoc::get(context);
1111+
Location loc = mlirModule.getLoc();
11111112
if (auto *inst = dyn_cast<llvm::Instruction>(value))
11121113
loc = translateLoc(inst->getDebugLoc());
1113-
return emitError(loc) << "unhandled value " << diag(*value);
1114+
return emitError(loc) << "unhandled value: " << diag(*value);
11141115
}
11151116

11161117
FailureOr<SmallVector<Value>>
@@ -1187,7 +1188,7 @@ LogicalResult ModuleImport::convertIntrinsic(llvm::CallInst *inst) {
11871188
return success();
11881189

11891190
Location loc = translateLoc(inst->getDebugLoc());
1190-
return emitError(loc) << "unhandled intrinsic " << diag(*inst);
1191+
return emitError(loc) << "unhandled intrinsic: " << diag(*inst);
11911192
}
11921193

11931194
LogicalResult ModuleImport::convertInstruction(llvm::Instruction *inst) {
@@ -1364,7 +1365,7 @@ LogicalResult ModuleImport::convertInstruction(llvm::Instruction *inst) {
13641365
if (succeeded(convertInstructionImpl(builder, inst, *this)))
13651366
return success();
13661367

1367-
return emitError(loc) << "unhandled instruction " << diag(*inst);
1368+
return emitError(loc) << "unhandled instruction: " << diag(*inst);
13681369
}
13691370

13701371
LogicalResult ModuleImport::processInstruction(llvm::Instruction *inst) {
@@ -1432,7 +1433,7 @@ LogicalResult ModuleImport::processFunction(llvm::Function *func) {
14321433
builder.setInsertionPoint(mlirModule.getBody(), mlirModule.getBody()->end());
14331434

14341435
LLVMFuncOp funcOp = builder.create<LLVMFuncOp>(
1435-
UnknownLoc::get(context), func->getName(), functionType,
1436+
mlirModule.getLoc(), func->getName(), functionType,
14361437
convertLinkageFromLLVM(func->getLinkage()), dsoLocal, cconv);
14371438

14381439
// Set the function debug information if available.
@@ -1471,8 +1472,7 @@ LogicalResult ModuleImport::processFunction(llvm::Function *func) {
14711472
if (FlatSymbolRefAttr personality = getPersonalityAsAttr(func))
14721473
funcOp.setPersonalityAttr(personality);
14731474
else if (func->hasPersonalityFn())
1474-
emitWarning(UnknownLoc::get(context),
1475-
"could not deduce personality, skipping it");
1475+
emitWarning(funcOp.getLoc(), "could not deduce personality, skipping it");
14761476

14771477
if (func->hasGC())
14781478
funcOp.setGarbageCollector(StringRef(func->getGC()));
@@ -1487,8 +1487,9 @@ LogicalResult ModuleImport::processFunction(llvm::Function *func) {
14871487
if (!iface.isConvertibleMetadata(kind))
14881488
continue;
14891489
if (failed(iface.setMetadataAttrs(builder, kind, node, funcOp, *this))) {
1490-
emitWarning(funcOp->getLoc())
1491-
<< "unhandled function metadata (" << kind << ") " << diag(*func);
1490+
emitWarning(funcOp.getLoc())
1491+
<< "unhandled function metadata: " << diagMD(node, llvmModule.get())
1492+
<< " on " << diag(*func);
14921493
}
14931494
}
14941495

@@ -1536,7 +1537,7 @@ LogicalResult ModuleImport::processBasicBlock(llvm::BasicBlock *bb,
15361537
setNonDebugMetadataAttrs(&inst, op);
15371538
} else if (inst.getOpcode() != llvm::Instruction::PHI) {
15381539
Location loc = debugImporter->translateLoc(inst.getDebugLoc());
1539-
emitWarning(loc) << "dropped instruction " << diag(inst);
1540+
emitWarning(loc) << "dropped instruction: " << diag(inst);
15401541
}
15411542
}
15421543
return success();

mlir/test/Target/LLVMIR/Import/basic.ll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; RUN: mlir-translate -import-llvm %s | FileCheck %s
22
; RUN: mlir-translate -import-llvm -mlir-print-debuginfo %s | FileCheck %s --check-prefix=CHECK-DBG
33

4-
; CHECK-DBG: #[[UNKNOWNLOC:.+]] = loc(unknown)
4+
; CHECK-DBG: #[[MODULELOC:.+]] = loc({{.*}}basic.ll{{.*}}:0:0)
55

66
@global = external global double, align 8
77

@@ -10,7 +10,7 @@ declare float @fe(i32)
1010

1111
; FIXME: function attributes.
1212
; CHECK-LABEL: llvm.func internal @f1(%arg0: i64) -> i32 attributes {dso_local} {
13-
; CHECK-DBG: llvm.func internal @f1(%arg0: i64 loc(unknown)) -> i32 attributes {dso_local} {
13+
; CHECK-DBG: llvm.func internal @f1(%arg0: i64 loc({{.*}}basic.ll{{.*}}:0:0)) -> i32 attributes {dso_local} {
1414
; CHECK: %[[c2:[0-9]+]] = llvm.mlir.constant(2 : i32) : i32
1515
; CHECK: %[[c1:[0-9]+]] = llvm.mlir.constant(true) : i1
1616
; CHECK: %[[c43:[0-9]+]] = llvm.mlir.constant(43 : i32) : i32
@@ -19,7 +19,7 @@ define internal dso_local i32 @f1(i64 %a) norecurse {
1919
entry:
2020
; CHECK: %{{[0-9]+}} = llvm.inttoptr %arg0 : i64 to !llvm.ptr<i64>
2121
%aa = inttoptr i64 %a to i64*
22-
; CHECK-DBG: llvm.mlir.addressof @global : !llvm.ptr<f64> loc(#[[UNKNOWNLOC]])
22+
; CHECK-DBG: llvm.mlir.addressof @global : !llvm.ptr<f64> loc(#[[MODULELOC]])
2323
; %[[addrof:[0-9]+]] = llvm.mlir.addressof @global : !llvm.ptr<f64>
2424
; %[[addrof2:[0-9]+]] = llvm.mlir.addressof @global : !llvm.ptr<f64>
2525
; %{{[0-9]+}} = llvm.inttoptr %arg0 : i64 to !llvm.ptr<i64>
@@ -28,7 +28,7 @@ entry:
2828
%bb = ptrtoint double* @global to i64
2929
%cc = getelementptr double, double* @global, i32 3
3030
; CHECK: %[[b:[0-9]+]] = llvm.trunc %arg0 : i64 to i32
31-
; CHECK-DBG: llvm.trunc %arg0 : i64 to i32 loc(#[[UNKNOWNLOC]])
31+
; CHECK-DBG: llvm.trunc %arg0 : i64 to i32 loc(#[[MODULELOC]])
3232
%b = trunc i64 %a to i32
3333
; CHECK: %[[c:[0-9]+]] = llvm.call @fe(%[[b]]) : (i32) -> f32
3434
%c = call float @fe(i32 %b)
@@ -52,7 +52,7 @@ if.end:
5252
; CHECK: llvm.return %[[c43]]
5353
ret i32 43
5454
}
55-
; CHECK-DBG: } loc(#[[UNKNOWNLOC]])
55+
; CHECK-DBG: } loc(#[[MODULELOC]])
5656

5757

5858
@_ZTIi = external dso_local constant i8*

mlir/test/Target/LLVMIR/Import/debug-info.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
; RUN: mlir-translate -import-llvm -mlir-print-debuginfo -split-input-file %s | FileCheck %s
22

3-
; CHECK-LABEL: @unknown(
4-
define i32 @unknown(i32 %0) {
3+
; CHECK: #[[$MODULELOC:.+]] = loc({{.*}}debug-info.ll{{.*}}:0:0)
4+
5+
; CHECK-LABEL: @module_loc(
6+
define i32 @module_loc(i32 %0) {
57
entry:
68
br label %next
79
end:
8-
; CHECK: ^{{.*}}(%{{.+}}: i32 loc(unknown)):
10+
; CHECK: ^{{.*}}(%{{.+}}: i32 loc({{.*}}debug-info.ll{{.*}}:0:0)):
911
%1 = phi i32 [ %2, %next ]
1012
ret i32 %1
1113
next:
12-
; CHECK: = llvm.mul %{{.+}}, %{{.+}} : i32 loc(#[[UNKNOWNLOC:.+]])
14+
; CHECK: = llvm.mul %{{.+}}, %{{.+}} : i32 loc(#[[$MODULELOC]])
1315
%2 = mul i32 %0, %0
1416
br label %end
1517
}
1618

17-
; CHECK: #[[UNKNOWNLOC:.+]] = loc(unknown)
18-
1919
; // -----
2020

2121
; CHECK-LABEL: @instruction_loc

0 commit comments

Comments
 (0)