1193 lines · cpp
1//===- Debugify.cpp - Check debug info preservation in optimizations ------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8///9/// \file In the `synthetic` mode, the `-debugify` attaches synthetic debug info10/// to everything. It can be used to create targeted tests for debug info11/// preservation. In addition, when using the `original` mode, it can check12/// original debug info preservation. The `synthetic` mode is default one.13///14//===----------------------------------------------------------------------===//15 16#include "llvm/Transforms/Utils/Debugify.h"17#include "llvm/ADT/BitVector.h"18#include "llvm/ADT/StringExtras.h"19#include "llvm/Config/llvm-config.h"20#include "llvm/IR/DIBuilder.h"21#include "llvm/IR/DebugInfo.h"22#include "llvm/IR/DebugInfoMetadata.h"23#include "llvm/IR/DebugLoc.h"24#include "llvm/IR/InstIterator.h"25#include "llvm/IR/Instructions.h"26#include "llvm/IR/Module.h"27#include "llvm/IR/PassInstrumentation.h"28#include "llvm/Pass.h"29#include "llvm/Support/CommandLine.h"30#include "llvm/Support/FileSystem.h"31#include "llvm/Support/JSON.h"32#include <optional>33#if LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN34// We need the Signals header to operate on stacktraces if we're using DebugLoc35// origin-tracking.36#include "llvm/Support/Signals.h"37#endif38 39#define DEBUG_TYPE "debugify"40 41using namespace llvm;42 43namespace {44 45cl::opt<bool> ApplyAtomGroups("debugify-atoms", cl::init(false));46 47cl::opt<bool> Quiet("debugify-quiet",48 cl::desc("Suppress verbose debugify output"));49 50cl::opt<uint64_t> DebugifyFunctionsLimit(51 "debugify-func-limit",52 cl::desc("Set max number of processed functions per pass."),53 cl::init(UINT_MAX));54 55enum class Level {56 Locations,57 LocationsAndVariables58};59 60cl::opt<Level> DebugifyLevel(61 "debugify-level", cl::desc("Kind of debug info to add"),62 cl::values(clEnumValN(Level::Locations, "locations", "Locations only"),63 clEnumValN(Level::LocationsAndVariables, "location+variables",64 "Locations and Variables")),65 cl::init(Level::LocationsAndVariables));66 67raw_ostream &dbg() { return Quiet ? nulls() : errs(); }68 69#if LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN70// These maps refer to addresses in the current LLVM process, so we can reuse71// them everywhere - therefore, we store them at file scope.72static SymbolizedAddressMap SymbolizedAddrs;73static AddressSet UnsymbolizedAddrs;74 75std::string symbolizeStackTrace(const Instruction *I) {76 // We flush the set of unsymbolized addresses at the latest possible moment,77 // i.e. now.78 if (!UnsymbolizedAddrs.empty()) {79 sys::symbolizeAddresses(UnsymbolizedAddrs, SymbolizedAddrs);80 UnsymbolizedAddrs.clear();81 }82 const DbgLocOrigin::StackTracesTy &OriginStackTraces =83 I->getDebugLoc().getOriginStackTraces();84 std::string Result;85 raw_string_ostream OS(Result);86 for (size_t TraceIdx = 0; TraceIdx < OriginStackTraces.size(); ++TraceIdx) {87 if (TraceIdx != 0)88 OS << "========================================\n";89 auto &[Depth, StackTrace] = OriginStackTraces[TraceIdx];90 unsigned VirtualFrameNo = 0;91 for (int Frame = 0; Frame < Depth; ++Frame) {92 assert(SymbolizedAddrs.contains(StackTrace[Frame]) &&93 "Expected each address to have been symbolized.");94 for (std::string &SymbolizedFrame : SymbolizedAddrs[StackTrace[Frame]]) {95 OS << right_justify(formatv("#{0}", VirtualFrameNo++).str(),96 std::log10(Depth) + 2)97 << ' ' << SymbolizedFrame << '\n';98 }99 }100 }101 return Result;102}103void collectStackAddresses(Instruction &I) {104 auto &OriginStackTraces = I.getDebugLoc().getOriginStackTraces();105 for (auto &[Depth, StackTrace] : OriginStackTraces) {106 for (int Frame = 0; Frame < Depth; ++Frame) {107 void *Addr = StackTrace[Frame];108 if (!SymbolizedAddrs.contains(Addr))109 UnsymbolizedAddrs.insert(Addr);110 }111 }112}113#else114void collectStackAddresses(Instruction &I) {}115#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN116 117uint64_t getAllocSizeInBits(Module &M, Type *Ty) {118 return Ty->isSized() ? M.getDataLayout().getTypeAllocSizeInBits(Ty) : 0;119}120 121bool isFunctionSkipped(Function &F) {122 return F.isDeclaration() || !F.hasExactDefinition();123}124 125/// Find the basic block's terminating instruction.126///127/// Special care is needed to handle musttail and deopt calls, as these behave128/// like (but are in fact not) terminators.129Instruction *findTerminatingInstruction(BasicBlock &BB) {130 if (auto *I = BB.getTerminatingMustTailCall())131 return I;132 if (auto *I = BB.getTerminatingDeoptimizeCall())133 return I;134 return BB.getTerminator();135}136} // end anonymous namespace137 138bool llvm::applyDebugifyMetadata(139 Module &M, iterator_range<Module::iterator> Functions, StringRef Banner,140 std::function<bool(DIBuilder &DIB, Function &F)> ApplyToMF) {141 // Skip modules with debug info.142 if (M.getNamedMetadata("llvm.dbg.cu")) {143 dbg() << Banner << "Skipping module with debug info\n";144 return false;145 }146 147 DIBuilder DIB(M);148 LLVMContext &Ctx = M.getContext();149 auto *Int32Ty = Type::getInt32Ty(Ctx);150 151 // Get a DIType which corresponds to Ty.152 DenseMap<uint64_t, DIType *> TypeCache;153 auto getCachedDIType = [&](Type *Ty) -> DIType * {154 uint64_t Size = getAllocSizeInBits(M, Ty);155 DIType *&DTy = TypeCache[Size];156 if (!DTy) {157 std::string Name = "ty" + utostr(Size);158 DTy = DIB.createBasicType(Name, Size, dwarf::DW_ATE_unsigned);159 }160 return DTy;161 };162 163 unsigned NextLine = 1;164 unsigned NextVar = 1;165 auto File = DIB.createFile(M.getName(), "/");166 auto CU = DIB.createCompileUnit(DISourceLanguageName(dwarf::DW_LANG_C), File,167 "debugify", /*isOptimized=*/true, "", 0);168 169 // Visit each instruction.170 for (Function &F : Functions) {171 if (isFunctionSkipped(F))172 continue;173 174 bool InsertedDbgVal = false;175 auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray({}));176 DISubprogram::DISPFlags SPFlags =177 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized;178 if (F.hasPrivateLinkage() || F.hasInternalLinkage())179 SPFlags |= DISubprogram::SPFlagLocalToUnit;180 auto SP = DIB.createFunction(CU, F.getName(), F.getName(), File, NextLine,181 SPType, NextLine, DINode::FlagZero, SPFlags,182 nullptr, nullptr, nullptr, nullptr, "",183 /*UseKeyInstructions*/ ApplyAtomGroups);184 F.setSubprogram(SP);185 186 // Helper that inserts a dbg.value before \p InsertBefore, copying the187 // location (and possibly the type, if it's non-void) from \p TemplateInst.188 auto insertDbgVal = [&](Instruction &TemplateInst,189 BasicBlock::iterator InsertPt) {190 std::string Name = utostr(NextVar++);191 Value *V = &TemplateInst;192 if (TemplateInst.getType()->isVoidTy())193 V = ConstantInt::get(Int32Ty, 0);194 const DILocation *Loc = TemplateInst.getDebugLoc().get();195 auto LocalVar = DIB.createAutoVariable(SP, Name, File, Loc->getLine(),196 getCachedDIType(V->getType()),197 /*AlwaysPreserve=*/true);198 DIB.insertDbgValueIntrinsic(V, LocalVar, DIB.createExpression(), Loc,199 InsertPt);200 };201 202 for (BasicBlock &BB : F) {203 // Attach debug locations.204 for (Instruction &I : BB) {205 uint64_t AtomGroup = ApplyAtomGroups ? NextLine : 0;206 uint8_t AtomRank = ApplyAtomGroups ? 1 : 0;207 uint64_t Line = NextLine++;208 I.setDebugLoc(DILocation::get(Ctx, Line, 1, SP, nullptr, false,209 AtomGroup, AtomRank));210 }211 212 if (DebugifyLevel < Level::LocationsAndVariables)213 continue;214 215 // Inserting debug values into EH pads can break IR invariants.216 if (BB.isEHPad())217 continue;218 219 // Find the terminating instruction, after which no debug values are220 // attached.221 Instruction *LastInst = findTerminatingInstruction(BB);222 assert(LastInst && "Expected basic block with a terminator");223 224 // Maintain an insertion point which can't be invalidated when updates225 // are made.226 BasicBlock::iterator InsertPt = BB.getFirstInsertionPt();227 assert(InsertPt != BB.end() && "Expected to find an insertion point");228 229 // Insert after existing debug values to preserve order.230 InsertPt.setHeadBit(false);231 232 // Attach debug values.233 for (Instruction *I = &*BB.begin(); I != LastInst; I = I->getNextNode()) {234 // Skip void-valued instructions.235 if (I->getType()->isVoidTy())236 continue;237 238 // Phis and EH pads must be grouped at the beginning of the block.239 // Only advance the insertion point when we finish visiting these.240 if (!isa<PHINode>(I) && !I->isEHPad())241 InsertPt = std::next(I->getIterator());242 243 insertDbgVal(*I, InsertPt);244 InsertedDbgVal = true;245 }246 }247 // Make sure we emit at least one dbg.value, otherwise MachineDebugify may248 // not have anything to work with as it goes about inserting DBG_VALUEs.249 // (It's common for MIR tests to be written containing skeletal IR with250 // empty functions -- we're still interested in debugifying the MIR within251 // those tests, and this helps with that.)252 if (DebugifyLevel == Level::LocationsAndVariables && !InsertedDbgVal) {253 auto *Term = findTerminatingInstruction(F.getEntryBlock());254 insertDbgVal(*Term, Term->getIterator());255 }256 if (ApplyToMF)257 ApplyToMF(DIB, F);258 }259 DIB.finalize();260 261 // Track the number of distinct lines and variables.262 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.debugify");263 auto addDebugifyOperand = [&](unsigned N) {264 NMD->addOperand(MDNode::get(265 Ctx, ValueAsMetadata::getConstant(ConstantInt::get(Int32Ty, N))));266 };267 addDebugifyOperand(NextLine - 1); // Original number of lines.268 addDebugifyOperand(NextVar - 1); // Original number of variables.269 assert(NMD->getNumOperands() == 2 &&270 "llvm.debugify should have exactly 2 operands!");271 272 // Claim that this synthetic debug info is valid.273 StringRef DIVersionKey = "Debug Info Version";274 if (!M.getModuleFlag(DIVersionKey))275 M.addModuleFlag(Module::Warning, DIVersionKey, DEBUG_METADATA_VERSION);276 277 return true;278}279 280static bool applyDebugify(Function &F, enum DebugifyMode Mode,281 DebugInfoPerPass *DebugInfoBeforePass,282 StringRef NameOfWrappedPass = "") {283 Module &M = *F.getParent();284 auto FuncIt = F.getIterator();285 if (Mode == DebugifyMode::SyntheticDebugInfo)286 return applyDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),287 "FunctionDebugify: ", /*ApplyToMF*/ nullptr);288 assert(DebugInfoBeforePass && "Missing debug info metadata");289 return collectDebugInfoMetadata(M, M.functions(), *DebugInfoBeforePass,290 "FunctionDebugify (original debuginfo)",291 NameOfWrappedPass);292}293 294static bool applyDebugify(Module &M, enum DebugifyMode Mode,295 DebugInfoPerPass *DebugInfoBeforePass,296 StringRef NameOfWrappedPass = "") {297 if (Mode == DebugifyMode::SyntheticDebugInfo)298 return applyDebugifyMetadata(M, M.functions(),299 "ModuleDebugify: ", /*ApplyToMF*/ nullptr);300 assert(DebugInfoBeforePass && "Missing debug info metadata");301 return collectDebugInfoMetadata(M, M.functions(), *DebugInfoBeforePass,302 "ModuleDebugify (original debuginfo)",303 NameOfWrappedPass);304}305 306bool llvm::stripDebugifyMetadata(Module &M) {307 bool Changed = false;308 309 // Remove the llvm.debugify and llvm.mir.debugify module-level named metadata.310 NamedMDNode *DebugifyMD = M.getNamedMetadata("llvm.debugify");311 if (DebugifyMD) {312 M.eraseNamedMetadata(DebugifyMD);313 Changed = true;314 }315 316 if (auto *MIRDebugifyMD = M.getNamedMetadata("llvm.mir.debugify")) {317 M.eraseNamedMetadata(MIRDebugifyMD);318 Changed = true;319 }320 321 // Strip out all debug intrinsics and supporting metadata (subprograms, types,322 // variables, etc).323 Changed |= StripDebugInfo(M);324 325 // Strip out the dead dbg.value prototype.326 Function *DbgValF = M.getFunction("llvm.dbg.value");327 if (DbgValF) {328 assert(DbgValF->isDeclaration() && DbgValF->use_empty() &&329 "Not all debug info stripped?");330 DbgValF->eraseFromParent();331 Changed = true;332 }333 334 // Strip out the module-level Debug Info Version metadata.335 // FIXME: There must be an easier way to remove an operand from a NamedMDNode.336 NamedMDNode *NMD = M.getModuleFlagsMetadata();337 if (!NMD)338 return Changed;339 SmallVector<MDNode *, 4> Flags(NMD->operands());340 NMD->clearOperands();341 for (MDNode *Flag : Flags) {342 auto *Key = cast<MDString>(Flag->getOperand(1));343 if (Key->getString() == "Debug Info Version") {344 Changed = true;345 continue;346 }347 NMD->addOperand(Flag);348 }349 // If we left it empty we might as well remove it.350 if (NMD->getNumOperands() == 0)351 NMD->eraseFromParent();352 353 return Changed;354}355 356bool hasLoc(const Instruction &I) {357 const DILocation *Loc = I.getDebugLoc().get();358#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE359 DebugLocKind Kind = I.getDebugLoc().getKind();360 return Loc || Kind != DebugLocKind::Normal;361#else362 return Loc;363#endif364}365 366bool llvm::collectDebugInfoMetadata(Module &M,367 iterator_range<Module::iterator> Functions,368 DebugInfoPerPass &DebugInfoBeforePass,369 StringRef Banner,370 StringRef NameOfWrappedPass) {371 LLVM_DEBUG(dbgs() << Banner << ": (before) " << NameOfWrappedPass << '\n');372 373 if (!M.getNamedMetadata("llvm.dbg.cu")) {374 dbg() << Banner << ": Skipping module without debug info\n";375 return false;376 }377 378 uint64_t FunctionsCnt = DebugInfoBeforePass.DIFunctions.size();379 // Visit each instruction.380 for (Function &F : Functions) {381 // Use DI collected after previous Pass (when -debugify-each is used).382 if (DebugInfoBeforePass.DIFunctions.count(&F))383 continue;384 385 if (isFunctionSkipped(F))386 continue;387 388 // Stop collecting DI if the Functions number reached the limit.389 if (++FunctionsCnt >= DebugifyFunctionsLimit)390 break;391 // Collect the DISubprogram.392 auto *SP = F.getSubprogram();393 DebugInfoBeforePass.DIFunctions.insert({&F, SP});394 if (SP) {395 LLVM_DEBUG(dbgs() << " Collecting subprogram: " << *SP << '\n');396 for (const DINode *DN : SP->getRetainedNodes()) {397 if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {398 DebugInfoBeforePass.DIVariables[DV] = 0;399 }400 }401 }402 403 for (BasicBlock &BB : F) {404 // Collect debug locations (!dbg) and debug variable intrinsics.405 for (Instruction &I : BB) {406 // Skip PHIs.407 if (isa<PHINode>(I))408 continue;409 410 // Cllect dbg.values and dbg.declare.411 if (DebugifyLevel > Level::Locations) {412 auto HandleDbgVariable = [&](DbgVariableRecord *DbgVar) {413 if (!SP)414 return;415 // Skip inlined variables.416 if (DbgVar->getDebugLoc().getInlinedAt())417 return;418 // Skip undef values.419 if (DbgVar->isKillLocation())420 return;421 422 auto *Var = DbgVar->getVariable();423 DebugInfoBeforePass.DIVariables[Var]++;424 };425 for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange()))426 HandleDbgVariable(&DVR);427 }428 429 LLVM_DEBUG(dbgs() << " Collecting info for inst: " << I << '\n');430 DebugInfoBeforePass.InstToDelete.insert({&I, &I});431 432 // Track the addresses to symbolize, if the feature is enabled.433 collectStackAddresses(I);434 DebugInfoBeforePass.DILocations.insert({&I, hasLoc(I)});435 }436 }437 }438 439 return true;440}441 442// This checks the preservation of original debug info attached to functions.443static bool checkFunctions(const DebugFnMap &DIFunctionsBefore,444 const DebugFnMap &DIFunctionsAfter,445 StringRef NameOfWrappedPass,446 StringRef FileNameFromCU, bool ShouldWriteIntoJSON,447 llvm::json::Array &Bugs) {448 bool Preserved = true;449 for (const auto &F : DIFunctionsAfter) {450 if (F.second)451 continue;452 auto SPIt = DIFunctionsBefore.find(F.first);453 if (SPIt == DIFunctionsBefore.end()) {454 if (ShouldWriteIntoJSON)455 Bugs.push_back(llvm::json::Object({{"metadata", "DISubprogram"},456 {"name", F.first->getName()},457 {"action", "not-generate"}}));458 else459 dbg() << "ERROR: " << NameOfWrappedPass460 << " did not generate DISubprogram for " << F.first->getName()461 << " from " << FileNameFromCU << '\n';462 Preserved = false;463 } else {464 auto SP = SPIt->second;465 if (!SP)466 continue;467 // If the function had the SP attached before the pass, consider it as468 // a debug info bug.469 if (ShouldWriteIntoJSON)470 Bugs.push_back(llvm::json::Object({{"metadata", "DISubprogram"},471 {"name", F.first->getName()},472 {"action", "drop"}}));473 else474 dbg() << "ERROR: " << NameOfWrappedPass << " dropped DISubprogram of "475 << F.first->getName() << " from " << FileNameFromCU << '\n';476 Preserved = false;477 }478 }479 480 return Preserved;481}482 483// This checks the preservation of the original debug info attached to484// instructions.485static bool checkInstructions(const DebugInstMap &DILocsBefore,486 const DebugInstMap &DILocsAfter,487 const WeakInstValueMap &InstToDelete,488 StringRef NameOfWrappedPass,489 StringRef FileNameFromCU,490 bool ShouldWriteIntoJSON,491 llvm::json::Array &Bugs) {492 bool Preserved = true;493 for (const auto &L : DILocsAfter) {494 if (L.second)495 continue;496 auto Instr = L.first;497 498 // In order to avoid pointer reuse/recycling, skip the values that might499 // have been deleted during a pass.500 auto WeakInstrPtr = InstToDelete.find(Instr);501 if (WeakInstrPtr != InstToDelete.end() && !WeakInstrPtr->second)502 continue;503 504 auto FnName = Instr->getFunction()->getName();505 auto BB = Instr->getParent();506 auto BBName = BB->hasName() ? BB->getName() : "no-name";507 auto InstName = Instruction::getOpcodeName(Instr->getOpcode());508 509 auto CreateJSONBugEntry = [&](const char *Action) {510 Bugs.push_back(llvm::json::Object({511 {"metadata", "DILocation"},512 {"fn-name", FnName.str()},513 {"bb-name", BBName.str()},514 {"instr", InstName},515 {"action", Action},516#if LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN517 {"origin", symbolizeStackTrace(Instr)},518#endif519 }));520 };521 522 auto InstrIt = DILocsBefore.find(Instr);523 if (InstrIt == DILocsBefore.end()) {524 if (ShouldWriteIntoJSON)525 CreateJSONBugEntry("not-generate");526 else527 dbg() << "WARNING: " << NameOfWrappedPass528 << " did not generate DILocation for " << *Instr529 << " (BB: " << BBName << ", Fn: " << FnName530 << ", File: " << FileNameFromCU << ")\n";531 Preserved = false;532 } else {533 if (!InstrIt->second)534 continue;535 // If the instr had the !dbg attached before the pass, consider it as536 // a debug info issue.537 if (ShouldWriteIntoJSON)538 CreateJSONBugEntry("drop");539 else540 dbg() << "WARNING: " << NameOfWrappedPass << " dropped DILocation of "541 << *Instr << " (BB: " << BBName << ", Fn: " << FnName542 << ", File: " << FileNameFromCU << ")\n";543 Preserved = false;544 }545 }546 547 return Preserved;548}549 550// This checks the preservation of original debug variable intrinsics.551static bool checkVars(const DebugVarMap &DIVarsBefore,552 const DebugVarMap &DIVarsAfter,553 StringRef NameOfWrappedPass, StringRef FileNameFromCU,554 bool ShouldWriteIntoJSON, llvm::json::Array &Bugs) {555 bool Preserved = true;556 for (const auto &V : DIVarsBefore) {557 auto VarIt = DIVarsAfter.find(V.first);558 if (VarIt == DIVarsAfter.end())559 continue;560 561 unsigned NumOfDbgValsAfter = VarIt->second;562 563 if (V.second > NumOfDbgValsAfter) {564 if (ShouldWriteIntoJSON)565 Bugs.push_back(llvm::json::Object(566 {{"metadata", "dbg-var-intrinsic"},567 {"name", V.first->getName()},568 {"fn-name", V.first->getScope()->getSubprogram()->getName()},569 {"action", "drop"}}));570 else571 dbg() << "WARNING: " << NameOfWrappedPass572 << " drops dbg.value()/dbg.declare() for " << V.first->getName()573 << " from "574 << "function " << V.first->getScope()->getSubprogram()->getName()575 << " (file " << FileNameFromCU << ")\n";576 Preserved = false;577 }578 }579 580 return Preserved;581}582 583// Write the json data into the specifed file.584static void writeJSON(StringRef OrigDIVerifyBugsReportFilePath,585 StringRef FileNameFromCU, StringRef NameOfWrappedPass,586 llvm::json::Array &Bugs) {587 std::error_code EC;588 raw_fd_ostream OS_FILE{OrigDIVerifyBugsReportFilePath, EC,589 sys::fs::OF_Append | sys::fs::OF_TextWithCRLF};590 if (EC) {591 errs() << "Could not open file: " << EC.message() << ", "592 << OrigDIVerifyBugsReportFilePath << '\n';593 return;594 }595 596 if (auto L = OS_FILE.lock()) {597 OS_FILE << "{\"file\":\"" << FileNameFromCU << "\", ";598 599 StringRef PassName =600 NameOfWrappedPass != "" ? NameOfWrappedPass : "no-name";601 OS_FILE << "\"pass\":\"" << PassName << "\", ";602 603 llvm::json::Value BugsToPrint{std::move(Bugs)};604 OS_FILE << "\"bugs\": " << BugsToPrint;605 606 OS_FILE << "}\n";607 }608 OS_FILE.close();609}610 611bool llvm::checkDebugInfoMetadata(Module &M,612 iterator_range<Module::iterator> Functions,613 DebugInfoPerPass &DebugInfoBeforePass,614 StringRef Banner, StringRef NameOfWrappedPass,615 StringRef OrigDIVerifyBugsReportFilePath) {616 LLVM_DEBUG(dbgs() << Banner << ": (after) " << NameOfWrappedPass << '\n');617 618 if (!M.getNamedMetadata("llvm.dbg.cu")) {619 dbg() << Banner << ": Skipping module without debug info\n";620 return false;621 }622 623 // Map the debug info holding DIs after a pass.624 DebugInfoPerPass DebugInfoAfterPass;625 626 // Visit each instruction.627 for (Function &F : Functions) {628 if (isFunctionSkipped(F))629 continue;630 631 // Don't process functions without DI collected before the Pass.632 if (!DebugInfoBeforePass.DIFunctions.count(&F))633 continue;634 // TODO: Collect metadata other than DISubprograms.635 // Collect the DISubprogram.636 auto *SP = F.getSubprogram();637 DebugInfoAfterPass.DIFunctions.insert({&F, SP});638 639 if (SP) {640 LLVM_DEBUG(dbgs() << " Collecting subprogram: " << *SP << '\n');641 for (const DINode *DN : SP->getRetainedNodes()) {642 if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {643 DebugInfoAfterPass.DIVariables[DV] = 0;644 }645 }646 }647 648 for (BasicBlock &BB : F) {649 // Collect debug locations (!dbg) and debug variable intrinsics.650 for (Instruction &I : BB) {651 // Skip PHIs.652 if (isa<PHINode>(I))653 continue;654 655 // Collect dbg.values and dbg.declares.656 if (DebugifyLevel > Level::Locations) {657 auto HandleDbgVariable = [&](DbgVariableRecord *DbgVar) {658 if (!SP)659 return;660 // Skip inlined variables.661 if (DbgVar->getDebugLoc().getInlinedAt())662 return;663 // Skip undef values.664 if (DbgVar->isKillLocation())665 return;666 667 auto *Var = DbgVar->getVariable();668 DebugInfoAfterPass.DIVariables[Var]++;669 };670 for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange()))671 HandleDbgVariable(&DVR);672 }673 674 LLVM_DEBUG(dbgs() << " Collecting info for inst: " << I << '\n');675 676 // Track the addresses to symbolize, if the feature is enabled.677 collectStackAddresses(I);678 DebugInfoAfterPass.DILocations.insert({&I, hasLoc(I)});679 }680 }681 }682 683 // TODO: The name of the module could be read better?684 StringRef FileNameFromCU =685 (cast<DICompileUnit>(M.getNamedMetadata("llvm.dbg.cu")->getOperand(0)))686 ->getFilename();687 688 auto DIFunctionsBefore = DebugInfoBeforePass.DIFunctions;689 auto DIFunctionsAfter = DebugInfoAfterPass.DIFunctions;690 691 auto DILocsBefore = DebugInfoBeforePass.DILocations;692 auto DILocsAfter = DebugInfoAfterPass.DILocations;693 694 auto InstToDelete = DebugInfoBeforePass.InstToDelete;695 696 auto DIVarsBefore = DebugInfoBeforePass.DIVariables;697 auto DIVarsAfter = DebugInfoAfterPass.DIVariables;698 699 bool ShouldWriteIntoJSON = !OrigDIVerifyBugsReportFilePath.empty();700 llvm::json::Array Bugs;701 702 bool ResultForFunc =703 checkFunctions(DIFunctionsBefore, DIFunctionsAfter, NameOfWrappedPass,704 FileNameFromCU, ShouldWriteIntoJSON, Bugs);705 bool ResultForInsts = checkInstructions(706 DILocsBefore, DILocsAfter, InstToDelete, NameOfWrappedPass,707 FileNameFromCU, ShouldWriteIntoJSON, Bugs);708 709#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE710 // If we are tracking DebugLoc coverage, replace each empty DebugLoc with an711 // annotated location now so that it does not show up in future passes even if712 // it is propagated to other instructions.713 for (auto &L : DILocsAfter)714 if (!L.second)715 const_cast<Instruction *>(L.first)->setDebugLoc(DebugLoc::getUnknown());716#endif717 718 bool ResultForVars = checkVars(DIVarsBefore, DIVarsAfter, NameOfWrappedPass,719 FileNameFromCU, ShouldWriteIntoJSON, Bugs);720 721 bool Result = ResultForFunc && ResultForInsts && ResultForVars;722 723 StringRef ResultBanner = NameOfWrappedPass != "" ? NameOfWrappedPass : Banner;724 if (ShouldWriteIntoJSON && !Bugs.empty())725 writeJSON(OrigDIVerifyBugsReportFilePath, FileNameFromCU, NameOfWrappedPass,726 Bugs);727 728 if (Result)729 dbg() << ResultBanner << ": PASS\n";730 else731 dbg() << ResultBanner << ": FAIL\n";732 733 // In the case of the `debugify-each`, no need to go over all the instructions734 // again in the collectDebugInfoMetadata(), since as an input we can use735 // the debugging information from the previous pass.736 DebugInfoBeforePass = DebugInfoAfterPass;737 738 LLVM_DEBUG(dbgs() << "\n\n");739 return Result;740}741 742namespace {743/// Return true if a mis-sized diagnostic is issued for \p DbgVal.744template <typename DbgValTy>745bool diagnoseMisSizedDbgValue(Module &M, DbgValTy *DbgVal) {746 // The size of a dbg.value's value operand should match the size of the747 // variable it corresponds to.748 //749 // TODO: This, along with a check for non-null value operands, should be750 // promoted to verifier failures.751 752 // For now, don't try to interpret anything more complicated than an empty753 // DIExpression. Eventually we should try to handle OP_deref and fragments.754 if (DbgVal->getExpression()->getNumElements())755 return false;756 757 Value *V = DbgVal->getVariableLocationOp(0);758 if (!V)759 return false;760 761 Type *Ty = V->getType();762 uint64_t ValueOperandSize = getAllocSizeInBits(M, Ty);763 std::optional<uint64_t> DbgVarSize = DbgVal->getFragmentSizeInBits();764 if (!ValueOperandSize || !DbgVarSize)765 return false;766 767 bool HasBadSize = false;768 if (Ty->isIntegerTy()) {769 auto Signedness = DbgVal->getVariable()->getSignedness();770 if (Signedness == DIBasicType::Signedness::Signed)771 HasBadSize = ValueOperandSize < *DbgVarSize;772 } else {773 HasBadSize = ValueOperandSize != *DbgVarSize;774 }775 776 if (HasBadSize) {777 dbg() << "ERROR: dbg.value operand has size " << ValueOperandSize778 << ", but its variable has size " << *DbgVarSize << ": ";779 DbgVal->print(dbg());780 dbg() << "\n";781 }782 return HasBadSize;783}784 785bool checkDebugifyMetadata(Module &M,786 iterator_range<Module::iterator> Functions,787 StringRef NameOfWrappedPass, StringRef Banner,788 bool Strip, DebugifyStatsMap *StatsMap) {789 // Skip modules without debugify metadata.790 NamedMDNode *NMD = M.getNamedMetadata("llvm.debugify");791 if (!NMD) {792 dbg() << Banner << ": Skipping module without debugify metadata\n";793 return false;794 }795 796 auto getDebugifyOperand = [&](unsigned Idx) -> unsigned {797 return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0))798 ->getZExtValue();799 };800 assert(NMD->getNumOperands() == 2 &&801 "llvm.debugify should have exactly 2 operands!");802 unsigned OriginalNumLines = getDebugifyOperand(0);803 unsigned OriginalNumVars = getDebugifyOperand(1);804 bool HasErrors = false;805 806 // Track debug info loss statistics if able.807 DebugifyStatistics *Stats = nullptr;808 if (StatsMap && !NameOfWrappedPass.empty())809 Stats = &StatsMap->operator[](NameOfWrappedPass);810 811 BitVector MissingLines{OriginalNumLines, true};812 BitVector MissingVars{OriginalNumVars, true};813 for (Function &F : Functions) {814 if (isFunctionSkipped(F))815 continue;816 817 // Find missing lines.818 for (Instruction &I : instructions(F)) {819 auto DL = I.getDebugLoc();820 if (DL && DL.getLine() != 0) {821 MissingLines.reset(DL.getLine() - 1);822 continue;823 }824 825 if (!isa<PHINode>(&I) && !DL) {826 dbg() << "WARNING: Instruction with empty DebugLoc in function ";827 dbg() << F.getName() << " --";828 I.print(dbg());829 dbg() << "\n";830 }831 }832 833 // Find missing variables and mis-sized debug values.834 auto CheckForMisSized = [&](auto *DbgVal) {835 unsigned Var = ~0U;836 (void)to_integer(DbgVal->getVariable()->getName(), Var, 10);837 assert(Var <= OriginalNumVars && "Unexpected name for DILocalVariable");838 bool HasBadSize = diagnoseMisSizedDbgValue(M, DbgVal);839 if (!HasBadSize)840 MissingVars.reset(Var - 1);841 HasErrors |= HasBadSize;842 };843 for (Instruction &I : instructions(F)) {844 for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange()))845 if (DVR.isDbgValue() || DVR.isDbgAssign())846 CheckForMisSized(&DVR);847 }848 }849 850 // Print the results.851 for (unsigned Idx : MissingLines.set_bits())852 dbg() << "WARNING: Missing line " << Idx + 1 << "\n";853 854 for (unsigned Idx : MissingVars.set_bits())855 dbg() << "WARNING: Missing variable " << Idx + 1 << "\n";856 857 // Update DI loss statistics.858 if (Stats) {859 Stats->NumDbgLocsExpected += OriginalNumLines;860 Stats->NumDbgLocsMissing += MissingLines.count();861 Stats->NumDbgValuesExpected += OriginalNumVars;862 Stats->NumDbgValuesMissing += MissingVars.count();863 }864 865 dbg() << Banner;866 if (!NameOfWrappedPass.empty())867 dbg() << " [" << NameOfWrappedPass << "]";868 dbg() << ": " << (HasErrors ? "FAIL" : "PASS") << '\n';869 870 // Strip debugify metadata if required.871 bool Ret = false;872 if (Strip)873 Ret = stripDebugifyMetadata(M);874 875 return Ret;876}877 878/// ModulePass for attaching synthetic debug info to everything, used with the879/// legacy module pass manager.880struct DebugifyModulePass : public ModulePass {881 bool runOnModule(Module &M) override {882 bool Result =883 applyDebugify(M, Mode, DebugInfoBeforePass, NameOfWrappedPass);884 return Result;885 }886 887 DebugifyModulePass(enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,888 StringRef NameOfWrappedPass = "",889 DebugInfoPerPass *DebugInfoBeforePass = nullptr)890 : ModulePass(ID), NameOfWrappedPass(NameOfWrappedPass),891 DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode) {}892 893 void getAnalysisUsage(AnalysisUsage &AU) const override {894 AU.setPreservesAll();895 }896 897 static char ID; // Pass identification.898 899private:900 StringRef NameOfWrappedPass;901 DebugInfoPerPass *DebugInfoBeforePass;902 enum DebugifyMode Mode;903};904 905/// FunctionPass for attaching synthetic debug info to instructions within a906/// single function, used with the legacy module pass manager.907struct DebugifyFunctionPass : public FunctionPass {908 bool runOnFunction(Function &F) override {909 bool Result =910 applyDebugify(F, Mode, DebugInfoBeforePass, NameOfWrappedPass);911 return Result;912 }913 914 DebugifyFunctionPass(915 enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,916 StringRef NameOfWrappedPass = "",917 DebugInfoPerPass *DebugInfoBeforePass = nullptr)918 : FunctionPass(ID), NameOfWrappedPass(NameOfWrappedPass),919 DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode) {}920 921 void getAnalysisUsage(AnalysisUsage &AU) const override {922 AU.setPreservesAll();923 }924 925 static char ID; // Pass identification.926 927private:928 StringRef NameOfWrappedPass;929 DebugInfoPerPass *DebugInfoBeforePass;930 enum DebugifyMode Mode;931};932 933/// ModulePass for checking debug info inserted by -debugify, used with the934/// legacy module pass manager.935struct CheckDebugifyModulePass : public ModulePass {936 bool runOnModule(Module &M) override {937 bool Result;938 if (Mode == DebugifyMode::SyntheticDebugInfo)939 Result = checkDebugifyMetadata(M, M.functions(), NameOfWrappedPass,940 "CheckModuleDebugify", Strip, StatsMap);941 else942 Result = checkDebugInfoMetadata(943 M, M.functions(), *DebugInfoBeforePass,944 "CheckModuleDebugify (original debuginfo)", NameOfWrappedPass,945 OrigDIVerifyBugsReportFilePath);946 947 return Result;948 }949 950 CheckDebugifyModulePass(951 bool Strip = false, StringRef NameOfWrappedPass = "",952 DebugifyStatsMap *StatsMap = nullptr,953 enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,954 DebugInfoPerPass *DebugInfoBeforePass = nullptr,955 StringRef OrigDIVerifyBugsReportFilePath = "")956 : ModulePass(ID), NameOfWrappedPass(NameOfWrappedPass),957 OrigDIVerifyBugsReportFilePath(OrigDIVerifyBugsReportFilePath),958 StatsMap(StatsMap), DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode),959 Strip(Strip) {}960 961 void getAnalysisUsage(AnalysisUsage &AU) const override {962 AU.setPreservesAll();963 }964 965 static char ID; // Pass identification.966 967private:968 StringRef NameOfWrappedPass;969 StringRef OrigDIVerifyBugsReportFilePath;970 DebugifyStatsMap *StatsMap;971 DebugInfoPerPass *DebugInfoBeforePass;972 enum DebugifyMode Mode;973 bool Strip;974};975 976/// FunctionPass for checking debug info inserted by -debugify-function, used977/// with the legacy module pass manager.978struct CheckDebugifyFunctionPass : public FunctionPass {979 bool runOnFunction(Function &F) override {980 Module &M = *F.getParent();981 auto FuncIt = F.getIterator();982 bool Result;983 if (Mode == DebugifyMode::SyntheticDebugInfo)984 Result = checkDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),985 NameOfWrappedPass, "CheckFunctionDebugify",986 Strip, StatsMap);987 else988 Result = checkDebugInfoMetadata(989 M, make_range(FuncIt, std::next(FuncIt)), *DebugInfoBeforePass,990 "CheckFunctionDebugify (original debuginfo)", NameOfWrappedPass,991 OrigDIVerifyBugsReportFilePath);992 993 return Result;994 }995 996 CheckDebugifyFunctionPass(997 bool Strip = false, StringRef NameOfWrappedPass = "",998 DebugifyStatsMap *StatsMap = nullptr,999 enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,1000 DebugInfoPerPass *DebugInfoBeforePass = nullptr,1001 StringRef OrigDIVerifyBugsReportFilePath = "")1002 : FunctionPass(ID), NameOfWrappedPass(NameOfWrappedPass),1003 OrigDIVerifyBugsReportFilePath(OrigDIVerifyBugsReportFilePath),1004 StatsMap(StatsMap), DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode),1005 Strip(Strip) {}1006 1007 void getAnalysisUsage(AnalysisUsage &AU) const override {1008 AU.setPreservesAll();1009 }1010 1011 static char ID; // Pass identification.1012 1013private:1014 StringRef NameOfWrappedPass;1015 StringRef OrigDIVerifyBugsReportFilePath;1016 DebugifyStatsMap *StatsMap;1017 DebugInfoPerPass *DebugInfoBeforePass;1018 enum DebugifyMode Mode;1019 bool Strip;1020};1021 1022} // end anonymous namespace1023 1024void llvm::exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map) {1025 std::error_code EC;1026 raw_fd_ostream OS{Path, EC};1027 if (EC) {1028 errs() << "Could not open file: " << EC.message() << ", " << Path << '\n';1029 return;1030 }1031 1032 OS << "Pass Name" << ',' << "# of missing debug values" << ','1033 << "# of missing locations" << ',' << "Missing/Expected value ratio" << ','1034 << "Missing/Expected location ratio" << '\n';1035 for (const auto &Entry : Map) {1036 StringRef Pass = Entry.first;1037 DebugifyStatistics Stats = Entry.second;1038 1039 OS << Pass << ',' << Stats.NumDbgValuesMissing << ','1040 << Stats.NumDbgLocsMissing << ',' << Stats.getMissingValueRatio() << ','1041 << Stats.getEmptyLocationRatio() << '\n';1042 }1043}1044 1045ModulePass *createDebugifyModulePass(enum DebugifyMode Mode,1046 llvm::StringRef NameOfWrappedPass,1047 DebugInfoPerPass *DebugInfoBeforePass) {1048 if (Mode == DebugifyMode::SyntheticDebugInfo)1049 return new DebugifyModulePass();1050 assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");1051 return new DebugifyModulePass(Mode, NameOfWrappedPass, DebugInfoBeforePass);1052}1053 1054FunctionPass *1055createDebugifyFunctionPass(enum DebugifyMode Mode,1056 llvm::StringRef NameOfWrappedPass,1057 DebugInfoPerPass *DebugInfoBeforePass) {1058 if (Mode == DebugifyMode::SyntheticDebugInfo)1059 return new DebugifyFunctionPass();1060 assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");1061 return new DebugifyFunctionPass(Mode, NameOfWrappedPass, DebugInfoBeforePass);1062}1063 1064PreservedAnalyses NewPMDebugifyPass::run(Module &M, ModuleAnalysisManager &) {1065 if (Mode == DebugifyMode::SyntheticDebugInfo)1066 applyDebugifyMetadata(M, M.functions(),1067 "ModuleDebugify: ", /*ApplyToMF*/ nullptr);1068 else1069 collectDebugInfoMetadata(M, M.functions(), *DebugInfoBeforePass,1070 "ModuleDebugify (original debuginfo)",1071 NameOfWrappedPass);1072 1073 PreservedAnalyses PA;1074 PA.preserveSet<CFGAnalyses>();1075 return PA;1076}1077 1078ModulePass *createCheckDebugifyModulePass(1079 bool Strip, StringRef NameOfWrappedPass, DebugifyStatsMap *StatsMap,1080 enum DebugifyMode Mode, DebugInfoPerPass *DebugInfoBeforePass,1081 StringRef OrigDIVerifyBugsReportFilePath) {1082 if (Mode == DebugifyMode::SyntheticDebugInfo)1083 return new CheckDebugifyModulePass(Strip, NameOfWrappedPass, StatsMap);1084 assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");1085 return new CheckDebugifyModulePass(false, NameOfWrappedPass, nullptr, Mode,1086 DebugInfoBeforePass,1087 OrigDIVerifyBugsReportFilePath);1088}1089 1090FunctionPass *createCheckDebugifyFunctionPass(1091 bool Strip, StringRef NameOfWrappedPass, DebugifyStatsMap *StatsMap,1092 enum DebugifyMode Mode, DebugInfoPerPass *DebugInfoBeforePass,1093 StringRef OrigDIVerifyBugsReportFilePath) {1094 if (Mode == DebugifyMode::SyntheticDebugInfo)1095 return new CheckDebugifyFunctionPass(Strip, NameOfWrappedPass, StatsMap);1096 assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");1097 return new CheckDebugifyFunctionPass(false, NameOfWrappedPass, nullptr, Mode,1098 DebugInfoBeforePass,1099 OrigDIVerifyBugsReportFilePath);1100}1101 1102PreservedAnalyses NewPMCheckDebugifyPass::run(Module &M,1103 ModuleAnalysisManager &) {1104 if (Mode == DebugifyMode::SyntheticDebugInfo)1105 checkDebugifyMetadata(M, M.functions(), NameOfWrappedPass,1106 "CheckModuleDebugify", Strip, StatsMap);1107 else1108 checkDebugInfoMetadata(1109 M, M.functions(), *DebugInfoBeforePass,1110 "CheckModuleDebugify (original debuginfo)", NameOfWrappedPass,1111 OrigDIVerifyBugsReportFilePath);1112 1113 return PreservedAnalyses::all();1114}1115 1116static bool isIgnoredPass(StringRef PassID) {1117 return isSpecialPass(PassID, {"PassManager", "PassAdaptor",1118 "AnalysisManagerProxy", "PrintFunctionPass",1119 "PrintModulePass", "BitcodeWriterPass",1120 "ThinLTOBitcodeWriterPass", "VerifierPass"});1121}1122 1123void DebugifyEachInstrumentation::registerCallbacks(1124 PassInstrumentationCallbacks &PIC, ModuleAnalysisManager &MAM) {1125 PIC.registerBeforeNonSkippedPassCallback([this, &MAM](StringRef P, Any IR) {1126 if (isIgnoredPass(P))1127 return;1128 PreservedAnalyses PA;1129 PA.preserveSet<CFGAnalyses>();1130 if (const auto **CF = llvm::any_cast<const Function *>(&IR)) {1131 Function &F = *const_cast<Function *>(*CF);1132 applyDebugify(F, Mode, DebugInfoBeforePass, P);1133 MAM.getResult<FunctionAnalysisManagerModuleProxy>(*F.getParent())1134 .getManager()1135 .invalidate(F, PA);1136 } else if (const auto **CM = llvm::any_cast<const Module *>(&IR)) {1137 Module &M = *const_cast<Module *>(*CM);1138 applyDebugify(M, Mode, DebugInfoBeforePass, P);1139 MAM.invalidate(M, PA);1140 }1141 });1142 PIC.registerAfterPassCallback(1143 [this, &MAM](StringRef P, Any IR, const PreservedAnalyses &PassPA) {1144 if (isIgnoredPass(P))1145 return;1146 PreservedAnalyses PA;1147 PA.preserveSet<CFGAnalyses>();1148 if (const auto **CF = llvm::any_cast<const Function *>(&IR)) {1149 auto &F = *const_cast<Function *>(*CF);1150 Module &M = *F.getParent();1151 auto It = F.getIterator();1152 if (Mode == DebugifyMode::SyntheticDebugInfo)1153 checkDebugifyMetadata(M, make_range(It, std::next(It)), P,1154 "CheckFunctionDebugify", /*Strip=*/true,1155 DIStatsMap);1156 else1157 checkDebugInfoMetadata(M, make_range(It, std::next(It)),1158 *DebugInfoBeforePass,1159 "CheckModuleDebugify (original debuginfo)",1160 P, OrigDIVerifyBugsReportFilePath);1161 MAM.getResult<FunctionAnalysisManagerModuleProxy>(*F.getParent())1162 .getManager()1163 .invalidate(F, PA);1164 } else if (const auto **CM = llvm::any_cast<const Module *>(&IR)) {1165 Module &M = *const_cast<Module *>(*CM);1166 if (Mode == DebugifyMode::SyntheticDebugInfo)1167 checkDebugifyMetadata(M, M.functions(), P, "CheckModuleDebugify",1168 /*Strip=*/true, DIStatsMap);1169 else1170 checkDebugInfoMetadata(M, M.functions(), *DebugInfoBeforePass,1171 "CheckModuleDebugify (original debuginfo)",1172 P, OrigDIVerifyBugsReportFilePath);1173 MAM.invalidate(M, PA);1174 }1175 });1176}1177 1178char DebugifyModulePass::ID = 0;1179static RegisterPass<DebugifyModulePass> DM("debugify",1180 "Attach debug info to everything");1181 1182char CheckDebugifyModulePass::ID = 0;1183static RegisterPass<CheckDebugifyModulePass>1184 CDM("check-debugify", "Check debug info from -debugify");1185 1186char DebugifyFunctionPass::ID = 0;1187static RegisterPass<DebugifyFunctionPass> DF("debugify-function",1188 "Attach debug info to a function");1189 1190char CheckDebugifyFunctionPass::ID = 0;1191static RegisterPass<CheckDebugifyFunctionPass>1192 CDF("check-debugify-function", "Check debug info from -debugify-function");1193