diff options
author | Vedant Kumar <vsk@apple.com> | 2020-04-13 16:15:37 -0700 |
---|---|---|
committer | Vedant Kumar <vsk@apple.com> | 2020-04-22 17:03:39 -0700 |
commit | 2a5675f11d3bc803a245c0e2a3b47491c8f8a065 (patch) | |
tree | c23de1280f407f61fc4960e858ea272036114971 /llvm/lib/Transforms/Utils/Debugify.cpp | |
parent | b424b0bf731d1fb338180553a86f912746f640ad (diff) | |
download | llvm-2a5675f11d3bc803a245c0e2a3b47491c8f8a065.zip llvm-2a5675f11d3bc803a245c0e2a3b47491c8f8a065.tar.gz llvm-2a5675f11d3bc803a245c0e2a3b47491c8f8a065.tar.bz2 |
[MachineDebugify] Insert synthetic DBG_VALUE instructions
Summary:
Teach MachineDebugify how to insert DBG_VALUE instructions. This can
help find bugs causing CodeGen differences when debug info is present.
DBG_VALUE instructions are only emitted when -debugify-level is set to
locations+variables.
There is essentially no attempt made to match up DBG_VALUE register
operands with the local variables they ought to correspond to. I'm not
sure how to improve the situation. In some cases (MachineMemOperand?)
it's possible to find the IR instruction a MachineInstr corresponds to,
but in general this seems to call for "undoing" the work done by ISel.
Reviewers: dsanders, aprantl
Subscribers: hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78135
Diffstat (limited to 'llvm/lib/Transforms/Utils/Debugify.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/Debugify.cpp | 40 |
1 files changed, 31 insertions, 9 deletions
diff --git a/llvm/lib/Transforms/Utils/Debugify.cpp b/llvm/lib/Transforms/Utils/Debugify.cpp index 57f1740..f2739a8 100644 --- a/llvm/lib/Transforms/Utils/Debugify.cpp +++ b/llvm/lib/Transforms/Utils/Debugify.cpp @@ -75,6 +75,7 @@ bool llvm::applyDebugifyMetadata( DIBuilder DIB(M); LLVMContext &Ctx = M.getContext(); + auto *Int32Ty = Type::getInt32Ty(Ctx); // Get a DIType which corresponds to Ty. DenseMap<uint64_t, DIType *> TypeCache; @@ -99,6 +100,7 @@ bool llvm::applyDebugifyMetadata( if (isFunctionSkipped(F)) continue; + bool InsertedDbgVal = false; auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None)); DISubprogram::DISPFlags SPFlags = DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized; @@ -107,6 +109,23 @@ bool llvm::applyDebugifyMetadata( auto SP = DIB.createFunction(CU, F.getName(), F.getName(), File, NextLine, SPType, NextLine, DINode::FlagZero, SPFlags); F.setSubprogram(SP); + + // Helper that inserts a dbg.value before \p InsertBefore, copying the + // location (and possibly the type, if it's non-void) from \p TemplateInst. + auto insertDbgVal = [&](Instruction &TemplateInst, + Instruction *InsertBefore) { + std::string Name = utostr(NextVar++); + Value *V = &TemplateInst; + if (TemplateInst.getType()->isVoidTy()) + V = ConstantInt::get(Int32Ty, 0); + const DILocation *Loc = TemplateInst.getDebugLoc().get(); + auto LocalVar = DIB.createAutoVariable(SP, Name, File, Loc->getLine(), + getCachedDIType(V->getType()), + /*AlwaysPreserve=*/true); + DIB.insertDbgValueIntrinsic(V, LocalVar, DIB.createExpression(), Loc, + InsertBefore); + }; + for (BasicBlock &BB : F) { // Attach debug locations. for (Instruction &I : BB) @@ -141,15 +160,19 @@ bool llvm::applyDebugifyMetadata( if (!isa<PHINode>(I) && !I->isEHPad()) InsertBefore = I->getNextNode(); - std::string Name = utostr(NextVar++); - const DILocation *Loc = I->getDebugLoc().get(); - auto LocalVar = DIB.createAutoVariable(SP, Name, File, Loc->getLine(), - getCachedDIType(I->getType()), - /*AlwaysPreserve=*/true); - DIB.insertDbgValueIntrinsic(I, LocalVar, DIB.createExpression(), Loc, - InsertBefore); + insertDbgVal(*I, InsertBefore); + InsertedDbgVal = true; } } + // Make sure we emit at least one dbg.value, otherwise MachineDebugify may + // not have anything to work with as it goes about inserting DBG_VALUEs. + // (It's common for MIR tests to be written containing skeletal IR with + // empty functions -- we're still interested in debugifying the MIR within + // those tests, and this helps with that.) + if (DebugifyLevel == Level::LocationsAndVariables && !InsertedDbgVal) { + auto *Term = findTerminatingInstruction(F.getEntryBlock()); + insertDbgVal(*Term, Term); + } if (ApplyToMF) ApplyToMF(DIB, F); DIB.finalizeSubprogram(SP); @@ -158,10 +181,9 @@ bool llvm::applyDebugifyMetadata( // Track the number of distinct lines and variables. NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.debugify"); - auto *IntTy = Type::getInt32Ty(Ctx); auto addDebugifyOperand = [&](unsigned N) { NMD->addOperand(MDNode::get( - Ctx, ValueAsMetadata::getConstant(ConstantInt::get(IntTy, N)))); + Ctx, ValueAsMetadata::getConstant(ConstantInt::get(Int32Ty, N)))); }; addDebugifyOperand(NextLine - 1); // Original number of lines. addDebugifyOperand(NextVar - 1); // Original number of variables. |