Debugify.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file In the `synthetic` mode, the `-debugify` attaches synthetic debug info
  10. /// to everything. It can be used to create targeted tests for debug info
  11. /// preservation. In addition, when using the `original` mode, it can check
  12. /// original debug info preservation. The `synthetic` mode is default one.
  13. ///
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/Transforms/Utils/Debugify.h"
  16. #include "llvm/ADT/BitVector.h"
  17. #include "llvm/ADT/StringExtras.h"
  18. #include "llvm/IR/DIBuilder.h"
  19. #include "llvm/IR/DebugInfo.h"
  20. #include "llvm/IR/InstIterator.h"
  21. #include "llvm/IR/Instructions.h"
  22. #include "llvm/IR/IntrinsicInst.h"
  23. #include "llvm/IR/Module.h"
  24. #include "llvm/IR/PassInstrumentation.h"
  25. #include "llvm/Pass.h"
  26. #include "llvm/Support/CommandLine.h"
  27. #include "llvm/Support/FileSystem.h"
  28. #include "llvm/Support/JSON.h"
  29. #include <optional>
  30. #define DEBUG_TYPE "debugify"
  31. using namespace llvm;
  32. namespace {
  33. cl::opt<bool> Quiet("debugify-quiet",
  34. cl::desc("Suppress verbose debugify output"));
  35. cl::opt<uint64_t> DebugifyFunctionsLimit(
  36. "debugify-func-limit",
  37. cl::desc("Set max number of processed functions per pass."),
  38. cl::init(UINT_MAX));
  39. enum class Level {
  40. Locations,
  41. LocationsAndVariables
  42. };
  43. cl::opt<Level> DebugifyLevel(
  44. "debugify-level", cl::desc("Kind of debug info to add"),
  45. cl::values(clEnumValN(Level::Locations, "locations", "Locations only"),
  46. clEnumValN(Level::LocationsAndVariables, "location+variables",
  47. "Locations and Variables")),
  48. cl::init(Level::LocationsAndVariables));
  49. raw_ostream &dbg() { return Quiet ? nulls() : errs(); }
  50. uint64_t getAllocSizeInBits(Module &M, Type *Ty) {
  51. return Ty->isSized() ? M.getDataLayout().getTypeAllocSizeInBits(Ty) : 0;
  52. }
  53. bool isFunctionSkipped(Function &F) {
  54. return F.isDeclaration() || !F.hasExactDefinition();
  55. }
  56. /// Find the basic block's terminating instruction.
  57. ///
  58. /// Special care is needed to handle musttail and deopt calls, as these behave
  59. /// like (but are in fact not) terminators.
  60. Instruction *findTerminatingInstruction(BasicBlock &BB) {
  61. if (auto *I = BB.getTerminatingMustTailCall())
  62. return I;
  63. if (auto *I = BB.getTerminatingDeoptimizeCall())
  64. return I;
  65. return BB.getTerminator();
  66. }
  67. } // end anonymous namespace
  68. bool llvm::applyDebugifyMetadata(
  69. Module &M, iterator_range<Module::iterator> Functions, StringRef Banner,
  70. std::function<bool(DIBuilder &DIB, Function &F)> ApplyToMF) {
  71. // Skip modules with debug info.
  72. if (M.getNamedMetadata("llvm.dbg.cu")) {
  73. dbg() << Banner << "Skipping module with debug info\n";
  74. return false;
  75. }
  76. DIBuilder DIB(M);
  77. LLVMContext &Ctx = M.getContext();
  78. auto *Int32Ty = Type::getInt32Ty(Ctx);
  79. // Get a DIType which corresponds to Ty.
  80. DenseMap<uint64_t, DIType *> TypeCache;
  81. auto getCachedDIType = [&](Type *Ty) -> DIType * {
  82. uint64_t Size = getAllocSizeInBits(M, Ty);
  83. DIType *&DTy = TypeCache[Size];
  84. if (!DTy) {
  85. std::string Name = "ty" + utostr(Size);
  86. DTy = DIB.createBasicType(Name, Size, dwarf::DW_ATE_unsigned);
  87. }
  88. return DTy;
  89. };
  90. unsigned NextLine = 1;
  91. unsigned NextVar = 1;
  92. auto File = DIB.createFile(M.getName(), "/");
  93. auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C, File, "debugify",
  94. /*isOptimized=*/true, "", 0);
  95. // Visit each instruction.
  96. for (Function &F : Functions) {
  97. if (isFunctionSkipped(F))
  98. continue;
  99. bool InsertedDbgVal = false;
  100. auto SPType =
  101. DIB.createSubroutineType(DIB.getOrCreateTypeArray(std::nullopt));
  102. DISubprogram::DISPFlags SPFlags =
  103. DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized;
  104. if (F.hasPrivateLinkage() || F.hasInternalLinkage())
  105. SPFlags |= DISubprogram::SPFlagLocalToUnit;
  106. auto SP = DIB.createFunction(CU, F.getName(), F.getName(), File, NextLine,
  107. SPType, NextLine, DINode::FlagZero, SPFlags);
  108. F.setSubprogram(SP);
  109. // Helper that inserts a dbg.value before \p InsertBefore, copying the
  110. // location (and possibly the type, if it's non-void) from \p TemplateInst.
  111. auto insertDbgVal = [&](Instruction &TemplateInst,
  112. Instruction *InsertBefore) {
  113. std::string Name = utostr(NextVar++);
  114. Value *V = &TemplateInst;
  115. if (TemplateInst.getType()->isVoidTy())
  116. V = ConstantInt::get(Int32Ty, 0);
  117. const DILocation *Loc = TemplateInst.getDebugLoc().get();
  118. auto LocalVar = DIB.createAutoVariable(SP, Name, File, Loc->getLine(),
  119. getCachedDIType(V->getType()),
  120. /*AlwaysPreserve=*/true);
  121. DIB.insertDbgValueIntrinsic(V, LocalVar, DIB.createExpression(), Loc,
  122. InsertBefore);
  123. };
  124. for (BasicBlock &BB : F) {
  125. // Attach debug locations.
  126. for (Instruction &I : BB)
  127. I.setDebugLoc(DILocation::get(Ctx, NextLine++, 1, SP));
  128. if (DebugifyLevel < Level::LocationsAndVariables)
  129. continue;
  130. // Inserting debug values into EH pads can break IR invariants.
  131. if (BB.isEHPad())
  132. continue;
  133. // Find the terminating instruction, after which no debug values are
  134. // attached.
  135. Instruction *LastInst = findTerminatingInstruction(BB);
  136. assert(LastInst && "Expected basic block with a terminator");
  137. // Maintain an insertion point which can't be invalidated when updates
  138. // are made.
  139. BasicBlock::iterator InsertPt = BB.getFirstInsertionPt();
  140. assert(InsertPt != BB.end() && "Expected to find an insertion point");
  141. Instruction *InsertBefore = &*InsertPt;
  142. // Attach debug values.
  143. for (Instruction *I = &*BB.begin(); I != LastInst; I = I->getNextNode()) {
  144. // Skip void-valued instructions.
  145. if (I->getType()->isVoidTy())
  146. continue;
  147. // Phis and EH pads must be grouped at the beginning of the block.
  148. // Only advance the insertion point when we finish visiting these.
  149. if (!isa<PHINode>(I) && !I->isEHPad())
  150. InsertBefore = I->getNextNode();
  151. insertDbgVal(*I, InsertBefore);
  152. InsertedDbgVal = true;
  153. }
  154. }
  155. // Make sure we emit at least one dbg.value, otherwise MachineDebugify may
  156. // not have anything to work with as it goes about inserting DBG_VALUEs.
  157. // (It's common for MIR tests to be written containing skeletal IR with
  158. // empty functions -- we're still interested in debugifying the MIR within
  159. // those tests, and this helps with that.)
  160. if (DebugifyLevel == Level::LocationsAndVariables && !InsertedDbgVal) {
  161. auto *Term = findTerminatingInstruction(F.getEntryBlock());
  162. insertDbgVal(*Term, Term);
  163. }
  164. if (ApplyToMF)
  165. ApplyToMF(DIB, F);
  166. DIB.finalizeSubprogram(SP);
  167. }
  168. DIB.finalize();
  169. // Track the number of distinct lines and variables.
  170. NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.debugify");
  171. auto addDebugifyOperand = [&](unsigned N) {
  172. NMD->addOperand(MDNode::get(
  173. Ctx, ValueAsMetadata::getConstant(ConstantInt::get(Int32Ty, N))));
  174. };
  175. addDebugifyOperand(NextLine - 1); // Original number of lines.
  176. addDebugifyOperand(NextVar - 1); // Original number of variables.
  177. assert(NMD->getNumOperands() == 2 &&
  178. "llvm.debugify should have exactly 2 operands!");
  179. // Claim that this synthetic debug info is valid.
  180. StringRef DIVersionKey = "Debug Info Version";
  181. if (!M.getModuleFlag(DIVersionKey))
  182. M.addModuleFlag(Module::Warning, DIVersionKey, DEBUG_METADATA_VERSION);
  183. return true;
  184. }
  185. static bool
  186. applyDebugify(Function &F,
  187. enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
  188. DebugInfoPerPass *DebugInfoBeforePass = nullptr,
  189. StringRef NameOfWrappedPass = "") {
  190. Module &M = *F.getParent();
  191. auto FuncIt = F.getIterator();
  192. if (Mode == DebugifyMode::SyntheticDebugInfo)
  193. return applyDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
  194. "FunctionDebugify: ", /*ApplyToMF*/ nullptr);
  195. assert(DebugInfoBeforePass);
  196. return collectDebugInfoMetadata(M, M.functions(), *DebugInfoBeforePass,
  197. "FunctionDebugify (original debuginfo)",
  198. NameOfWrappedPass);
  199. }
  200. static bool
  201. applyDebugify(Module &M,
  202. enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
  203. DebugInfoPerPass *DebugInfoBeforePass = nullptr,
  204. StringRef NameOfWrappedPass = "") {
  205. if (Mode == DebugifyMode::SyntheticDebugInfo)
  206. return applyDebugifyMetadata(M, M.functions(),
  207. "ModuleDebugify: ", /*ApplyToMF*/ nullptr);
  208. return collectDebugInfoMetadata(M, M.functions(), *DebugInfoBeforePass,
  209. "ModuleDebugify (original debuginfo)",
  210. NameOfWrappedPass);
  211. }
  212. bool llvm::stripDebugifyMetadata(Module &M) {
  213. bool Changed = false;
  214. // Remove the llvm.debugify and llvm.mir.debugify module-level named metadata.
  215. NamedMDNode *DebugifyMD = M.getNamedMetadata("llvm.debugify");
  216. if (DebugifyMD) {
  217. M.eraseNamedMetadata(DebugifyMD);
  218. Changed = true;
  219. }
  220. if (auto *MIRDebugifyMD = M.getNamedMetadata("llvm.mir.debugify")) {
  221. M.eraseNamedMetadata(MIRDebugifyMD);
  222. Changed = true;
  223. }
  224. // Strip out all debug intrinsics and supporting metadata (subprograms, types,
  225. // variables, etc).
  226. Changed |= StripDebugInfo(M);
  227. // Strip out the dead dbg.value prototype.
  228. Function *DbgValF = M.getFunction("llvm.dbg.value");
  229. if (DbgValF) {
  230. assert(DbgValF->isDeclaration() && DbgValF->use_empty() &&
  231. "Not all debug info stripped?");
  232. DbgValF->eraseFromParent();
  233. Changed = true;
  234. }
  235. // Strip out the module-level Debug Info Version metadata.
  236. // FIXME: There must be an easier way to remove an operand from a NamedMDNode.
  237. NamedMDNode *NMD = M.getModuleFlagsMetadata();
  238. if (!NMD)
  239. return Changed;
  240. SmallVector<MDNode *, 4> Flags(NMD->operands());
  241. NMD->clearOperands();
  242. for (MDNode *Flag : Flags) {
  243. auto *Key = cast<MDString>(Flag->getOperand(1));
  244. if (Key->getString() == "Debug Info Version") {
  245. Changed = true;
  246. continue;
  247. }
  248. NMD->addOperand(Flag);
  249. }
  250. // If we left it empty we might as well remove it.
  251. if (NMD->getNumOperands() == 0)
  252. NMD->eraseFromParent();
  253. return Changed;
  254. }
  255. bool llvm::collectDebugInfoMetadata(Module &M,
  256. iterator_range<Module::iterator> Functions,
  257. DebugInfoPerPass &DebugInfoBeforePass,
  258. StringRef Banner,
  259. StringRef NameOfWrappedPass) {
  260. LLVM_DEBUG(dbgs() << Banner << ": (before) " << NameOfWrappedPass << '\n');
  261. if (!M.getNamedMetadata("llvm.dbg.cu")) {
  262. dbg() << Banner << ": Skipping module without debug info\n";
  263. return false;
  264. }
  265. uint64_t FunctionsCnt = DebugInfoBeforePass.DIFunctions.size();
  266. // Visit each instruction.
  267. for (Function &F : Functions) {
  268. // Use DI collected after previous Pass (when -debugify-each is used).
  269. if (DebugInfoBeforePass.DIFunctions.count(&F))
  270. continue;
  271. if (isFunctionSkipped(F))
  272. continue;
  273. // Stop collecting DI if the Functions number reached the limit.
  274. if (++FunctionsCnt >= DebugifyFunctionsLimit)
  275. break;
  276. // Collect the DISubprogram.
  277. auto *SP = F.getSubprogram();
  278. DebugInfoBeforePass.DIFunctions.insert({&F, SP});
  279. if (SP) {
  280. LLVM_DEBUG(dbgs() << " Collecting subprogram: " << *SP << '\n');
  281. for (const DINode *DN : SP->getRetainedNodes()) {
  282. if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
  283. DebugInfoBeforePass.DIVariables[DV] = 0;
  284. }
  285. }
  286. }
  287. for (BasicBlock &BB : F) {
  288. // Collect debug locations (!dbg) and debug variable intrinsics.
  289. for (Instruction &I : BB) {
  290. // Skip PHIs.
  291. if (isa<PHINode>(I))
  292. continue;
  293. // Cllect dbg.values and dbg.declare.
  294. if (DebugifyLevel > Level::Locations) {
  295. if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I)) {
  296. if (!SP)
  297. continue;
  298. // Skip inlined variables.
  299. if (I.getDebugLoc().getInlinedAt())
  300. continue;
  301. // Skip undef values.
  302. if (DVI->isKillLocation())
  303. continue;
  304. auto *Var = DVI->getVariable();
  305. DebugInfoBeforePass.DIVariables[Var]++;
  306. continue;
  307. }
  308. }
  309. // Skip debug instructions other than dbg.value and dbg.declare.
  310. if (isa<DbgInfoIntrinsic>(&I))
  311. continue;
  312. LLVM_DEBUG(dbgs() << " Collecting info for inst: " << I << '\n');
  313. DebugInfoBeforePass.InstToDelete.insert({&I, &I});
  314. const DILocation *Loc = I.getDebugLoc().get();
  315. bool HasLoc = Loc != nullptr;
  316. DebugInfoBeforePass.DILocations.insert({&I, HasLoc});
  317. }
  318. }
  319. }
  320. return true;
  321. }
  322. // This checks the preservation of original debug info attached to functions.
  323. static bool checkFunctions(const DebugFnMap &DIFunctionsBefore,
  324. const DebugFnMap &DIFunctionsAfter,
  325. StringRef NameOfWrappedPass,
  326. StringRef FileNameFromCU, bool ShouldWriteIntoJSON,
  327. llvm::json::Array &Bugs) {
  328. bool Preserved = true;
  329. for (const auto &F : DIFunctionsAfter) {
  330. if (F.second)
  331. continue;
  332. auto SPIt = DIFunctionsBefore.find(F.first);
  333. if (SPIt == DIFunctionsBefore.end()) {
  334. if (ShouldWriteIntoJSON)
  335. Bugs.push_back(llvm::json::Object({{"metadata", "DISubprogram"},
  336. {"name", F.first->getName()},
  337. {"action", "not-generate"}}));
  338. else
  339. dbg() << "ERROR: " << NameOfWrappedPass
  340. << " did not generate DISubprogram for " << F.first->getName()
  341. << " from " << FileNameFromCU << '\n';
  342. Preserved = false;
  343. } else {
  344. auto SP = SPIt->second;
  345. if (!SP)
  346. continue;
  347. // If the function had the SP attached before the pass, consider it as
  348. // a debug info bug.
  349. if (ShouldWriteIntoJSON)
  350. Bugs.push_back(llvm::json::Object({{"metadata", "DISubprogram"},
  351. {"name", F.first->getName()},
  352. {"action", "drop"}}));
  353. else
  354. dbg() << "ERROR: " << NameOfWrappedPass << " dropped DISubprogram of "
  355. << F.first->getName() << " from " << FileNameFromCU << '\n';
  356. Preserved = false;
  357. }
  358. }
  359. return Preserved;
  360. }
  361. // This checks the preservation of the original debug info attached to
  362. // instructions.
  363. static bool checkInstructions(const DebugInstMap &DILocsBefore,
  364. const DebugInstMap &DILocsAfter,
  365. const WeakInstValueMap &InstToDelete,
  366. StringRef NameOfWrappedPass,
  367. StringRef FileNameFromCU,
  368. bool ShouldWriteIntoJSON,
  369. llvm::json::Array &Bugs) {
  370. bool Preserved = true;
  371. for (const auto &L : DILocsAfter) {
  372. if (L.second)
  373. continue;
  374. auto Instr = L.first;
  375. // In order to avoid pointer reuse/recycling, skip the values that might
  376. // have been deleted during a pass.
  377. auto WeakInstrPtr = InstToDelete.find(Instr);
  378. if (WeakInstrPtr != InstToDelete.end() && !WeakInstrPtr->second)
  379. continue;
  380. auto FnName = Instr->getFunction()->getName();
  381. auto BB = Instr->getParent();
  382. auto BBName = BB->hasName() ? BB->getName() : "no-name";
  383. auto InstName = Instruction::getOpcodeName(Instr->getOpcode());
  384. auto InstrIt = DILocsBefore.find(Instr);
  385. if (InstrIt == DILocsBefore.end()) {
  386. if (ShouldWriteIntoJSON)
  387. Bugs.push_back(llvm::json::Object({{"metadata", "DILocation"},
  388. {"fn-name", FnName.str()},
  389. {"bb-name", BBName.str()},
  390. {"instr", InstName},
  391. {"action", "not-generate"}}));
  392. else
  393. dbg() << "WARNING: " << NameOfWrappedPass
  394. << " did not generate DILocation for " << *Instr
  395. << " (BB: " << BBName << ", Fn: " << FnName
  396. << ", File: " << FileNameFromCU << ")\n";
  397. Preserved = false;
  398. } else {
  399. if (!InstrIt->second)
  400. continue;
  401. // If the instr had the !dbg attached before the pass, consider it as
  402. // a debug info issue.
  403. if (ShouldWriteIntoJSON)
  404. Bugs.push_back(llvm::json::Object({{"metadata", "DILocation"},
  405. {"fn-name", FnName.str()},
  406. {"bb-name", BBName.str()},
  407. {"instr", InstName},
  408. {"action", "drop"}}));
  409. else
  410. dbg() << "WARNING: " << NameOfWrappedPass << " dropped DILocation of "
  411. << *Instr << " (BB: " << BBName << ", Fn: " << FnName
  412. << ", File: " << FileNameFromCU << ")\n";
  413. Preserved = false;
  414. }
  415. }
  416. return Preserved;
  417. }
  418. // This checks the preservation of original debug variable intrinsics.
  419. static bool checkVars(const DebugVarMap &DIVarsBefore,
  420. const DebugVarMap &DIVarsAfter,
  421. StringRef NameOfWrappedPass, StringRef FileNameFromCU,
  422. bool ShouldWriteIntoJSON, llvm::json::Array &Bugs) {
  423. bool Preserved = true;
  424. for (const auto &V : DIVarsBefore) {
  425. auto VarIt = DIVarsAfter.find(V.first);
  426. if (VarIt == DIVarsAfter.end())
  427. continue;
  428. unsigned NumOfDbgValsAfter = VarIt->second;
  429. if (V.second > NumOfDbgValsAfter) {
  430. if (ShouldWriteIntoJSON)
  431. Bugs.push_back(llvm::json::Object(
  432. {{"metadata", "dbg-var-intrinsic"},
  433. {"name", V.first->getName()},
  434. {"fn-name", V.first->getScope()->getSubprogram()->getName()},
  435. {"action", "drop"}}));
  436. else
  437. dbg() << "WARNING: " << NameOfWrappedPass
  438. << " drops dbg.value()/dbg.declare() for " << V.first->getName()
  439. << " from "
  440. << "function " << V.first->getScope()->getSubprogram()->getName()
  441. << " (file " << FileNameFromCU << ")\n";
  442. Preserved = false;
  443. }
  444. }
  445. return Preserved;
  446. }
  447. // Write the json data into the specifed file.
  448. static void writeJSON(StringRef OrigDIVerifyBugsReportFilePath,
  449. StringRef FileNameFromCU, StringRef NameOfWrappedPass,
  450. llvm::json::Array &Bugs) {
  451. std::error_code EC;
  452. raw_fd_ostream OS_FILE{OrigDIVerifyBugsReportFilePath, EC,
  453. sys::fs::OF_Append | sys::fs::OF_TextWithCRLF};
  454. if (EC) {
  455. errs() << "Could not open file: " << EC.message() << ", "
  456. << OrigDIVerifyBugsReportFilePath << '\n';
  457. return;
  458. }
  459. if (auto L = OS_FILE.lock()) {
  460. OS_FILE << "{\"file\":\"" << FileNameFromCU << "\", ";
  461. StringRef PassName =
  462. NameOfWrappedPass != "" ? NameOfWrappedPass : "no-name";
  463. OS_FILE << "\"pass\":\"" << PassName << "\", ";
  464. llvm::json::Value BugsToPrint{std::move(Bugs)};
  465. OS_FILE << "\"bugs\": " << BugsToPrint;
  466. OS_FILE << "}\n";
  467. }
  468. OS_FILE.close();
  469. }
  470. bool llvm::checkDebugInfoMetadata(Module &M,
  471. iterator_range<Module::iterator> Functions,
  472. DebugInfoPerPass &DebugInfoBeforePass,
  473. StringRef Banner, StringRef NameOfWrappedPass,
  474. StringRef OrigDIVerifyBugsReportFilePath) {
  475. LLVM_DEBUG(dbgs() << Banner << ": (after) " << NameOfWrappedPass << '\n');
  476. if (!M.getNamedMetadata("llvm.dbg.cu")) {
  477. dbg() << Banner << ": Skipping module without debug info\n";
  478. return false;
  479. }
  480. // Map the debug info holding DIs after a pass.
  481. DebugInfoPerPass DebugInfoAfterPass;
  482. // Visit each instruction.
  483. for (Function &F : Functions) {
  484. if (isFunctionSkipped(F))
  485. continue;
  486. // Don't process functions without DI collected before the Pass.
  487. if (!DebugInfoBeforePass.DIFunctions.count(&F))
  488. continue;
  489. // TODO: Collect metadata other than DISubprograms.
  490. // Collect the DISubprogram.
  491. auto *SP = F.getSubprogram();
  492. DebugInfoAfterPass.DIFunctions.insert({&F, SP});
  493. if (SP) {
  494. LLVM_DEBUG(dbgs() << " Collecting subprogram: " << *SP << '\n');
  495. for (const DINode *DN : SP->getRetainedNodes()) {
  496. if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
  497. DebugInfoAfterPass.DIVariables[DV] = 0;
  498. }
  499. }
  500. }
  501. for (BasicBlock &BB : F) {
  502. // Collect debug locations (!dbg) and debug variable intrinsics.
  503. for (Instruction &I : BB) {
  504. // Skip PHIs.
  505. if (isa<PHINode>(I))
  506. continue;
  507. // Collect dbg.values and dbg.declares.
  508. if (DebugifyLevel > Level::Locations) {
  509. if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I)) {
  510. if (!SP)
  511. continue;
  512. // Skip inlined variables.
  513. if (I.getDebugLoc().getInlinedAt())
  514. continue;
  515. // Skip undef values.
  516. if (DVI->isKillLocation())
  517. continue;
  518. auto *Var = DVI->getVariable();
  519. DebugInfoAfterPass.DIVariables[Var]++;
  520. continue;
  521. }
  522. }
  523. // Skip debug instructions other than dbg.value and dbg.declare.
  524. if (isa<DbgInfoIntrinsic>(&I))
  525. continue;
  526. LLVM_DEBUG(dbgs() << " Collecting info for inst: " << I << '\n');
  527. const DILocation *Loc = I.getDebugLoc().get();
  528. bool HasLoc = Loc != nullptr;
  529. DebugInfoAfterPass.DILocations.insert({&I, HasLoc});
  530. }
  531. }
  532. }
  533. // TODO: The name of the module could be read better?
  534. StringRef FileNameFromCU =
  535. (cast<DICompileUnit>(M.getNamedMetadata("llvm.dbg.cu")->getOperand(0)))
  536. ->getFilename();
  537. auto DIFunctionsBefore = DebugInfoBeforePass.DIFunctions;
  538. auto DIFunctionsAfter = DebugInfoAfterPass.DIFunctions;
  539. auto DILocsBefore = DebugInfoBeforePass.DILocations;
  540. auto DILocsAfter = DebugInfoAfterPass.DILocations;
  541. auto InstToDelete = DebugInfoBeforePass.InstToDelete;
  542. auto DIVarsBefore = DebugInfoBeforePass.DIVariables;
  543. auto DIVarsAfter = DebugInfoAfterPass.DIVariables;
  544. bool ShouldWriteIntoJSON = !OrigDIVerifyBugsReportFilePath.empty();
  545. llvm::json::Array Bugs;
  546. bool ResultForFunc =
  547. checkFunctions(DIFunctionsBefore, DIFunctionsAfter, NameOfWrappedPass,
  548. FileNameFromCU, ShouldWriteIntoJSON, Bugs);
  549. bool ResultForInsts = checkInstructions(
  550. DILocsBefore, DILocsAfter, InstToDelete, NameOfWrappedPass,
  551. FileNameFromCU, ShouldWriteIntoJSON, Bugs);
  552. bool ResultForVars = checkVars(DIVarsBefore, DIVarsAfter, NameOfWrappedPass,
  553. FileNameFromCU, ShouldWriteIntoJSON, Bugs);
  554. bool Result = ResultForFunc && ResultForInsts && ResultForVars;
  555. StringRef ResultBanner = NameOfWrappedPass != "" ? NameOfWrappedPass : Banner;
  556. if (ShouldWriteIntoJSON && !Bugs.empty())
  557. writeJSON(OrigDIVerifyBugsReportFilePath, FileNameFromCU, NameOfWrappedPass,
  558. Bugs);
  559. if (Result)
  560. dbg() << ResultBanner << ": PASS\n";
  561. else
  562. dbg() << ResultBanner << ": FAIL\n";
  563. // In the case of the `debugify-each`, no need to go over all the instructions
  564. // again in the collectDebugInfoMetadata(), since as an input we can use
  565. // the debugging information from the previous pass.
  566. DebugInfoBeforePass = DebugInfoAfterPass;
  567. LLVM_DEBUG(dbgs() << "\n\n");
  568. return Result;
  569. }
  570. namespace {
  571. /// Return true if a mis-sized diagnostic is issued for \p DVI.
  572. bool diagnoseMisSizedDbgValue(Module &M, DbgValueInst *DVI) {
  573. // The size of a dbg.value's value operand should match the size of the
  574. // variable it corresponds to.
  575. //
  576. // TODO: This, along with a check for non-null value operands, should be
  577. // promoted to verifier failures.
  578. // For now, don't try to interpret anything more complicated than an empty
  579. // DIExpression. Eventually we should try to handle OP_deref and fragments.
  580. if (DVI->getExpression()->getNumElements())
  581. return false;
  582. Value *V = DVI->getVariableLocationOp(0);
  583. if (!V)
  584. return false;
  585. Type *Ty = V->getType();
  586. uint64_t ValueOperandSize = getAllocSizeInBits(M, Ty);
  587. std::optional<uint64_t> DbgVarSize = DVI->getFragmentSizeInBits();
  588. if (!ValueOperandSize || !DbgVarSize)
  589. return false;
  590. bool HasBadSize = false;
  591. if (Ty->isIntegerTy()) {
  592. auto Signedness = DVI->getVariable()->getSignedness();
  593. if (Signedness && *Signedness == DIBasicType::Signedness::Signed)
  594. HasBadSize = ValueOperandSize < *DbgVarSize;
  595. } else {
  596. HasBadSize = ValueOperandSize != *DbgVarSize;
  597. }
  598. if (HasBadSize) {
  599. dbg() << "ERROR: dbg.value operand has size " << ValueOperandSize
  600. << ", but its variable has size " << *DbgVarSize << ": ";
  601. DVI->print(dbg());
  602. dbg() << "\n";
  603. }
  604. return HasBadSize;
  605. }
  606. bool checkDebugifyMetadata(Module &M,
  607. iterator_range<Module::iterator> Functions,
  608. StringRef NameOfWrappedPass, StringRef Banner,
  609. bool Strip, DebugifyStatsMap *StatsMap) {
  610. // Skip modules without debugify metadata.
  611. NamedMDNode *NMD = M.getNamedMetadata("llvm.debugify");
  612. if (!NMD) {
  613. dbg() << Banner << ": Skipping module without debugify metadata\n";
  614. return false;
  615. }
  616. auto getDebugifyOperand = [&](unsigned Idx) -> unsigned {
  617. return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0))
  618. ->getZExtValue();
  619. };
  620. assert(NMD->getNumOperands() == 2 &&
  621. "llvm.debugify should have exactly 2 operands!");
  622. unsigned OriginalNumLines = getDebugifyOperand(0);
  623. unsigned OriginalNumVars = getDebugifyOperand(1);
  624. bool HasErrors = false;
  625. // Track debug info loss statistics if able.
  626. DebugifyStatistics *Stats = nullptr;
  627. if (StatsMap && !NameOfWrappedPass.empty())
  628. Stats = &StatsMap->operator[](NameOfWrappedPass);
  629. BitVector MissingLines{OriginalNumLines, true};
  630. BitVector MissingVars{OriginalNumVars, true};
  631. for (Function &F : Functions) {
  632. if (isFunctionSkipped(F))
  633. continue;
  634. // Find missing lines.
  635. for (Instruction &I : instructions(F)) {
  636. if (isa<DbgValueInst>(&I))
  637. continue;
  638. auto DL = I.getDebugLoc();
  639. if (DL && DL.getLine() != 0) {
  640. MissingLines.reset(DL.getLine() - 1);
  641. continue;
  642. }
  643. if (!isa<PHINode>(&I) && !DL) {
  644. dbg() << "WARNING: Instruction with empty DebugLoc in function ";
  645. dbg() << F.getName() << " --";
  646. I.print(dbg());
  647. dbg() << "\n";
  648. }
  649. }
  650. // Find missing variables and mis-sized debug values.
  651. for (Instruction &I : instructions(F)) {
  652. auto *DVI = dyn_cast<DbgValueInst>(&I);
  653. if (!DVI)
  654. continue;
  655. unsigned Var = ~0U;
  656. (void)to_integer(DVI->getVariable()->getName(), Var, 10);
  657. assert(Var <= OriginalNumVars && "Unexpected name for DILocalVariable");
  658. bool HasBadSize = diagnoseMisSizedDbgValue(M, DVI);
  659. if (!HasBadSize)
  660. MissingVars.reset(Var - 1);
  661. HasErrors |= HasBadSize;
  662. }
  663. }
  664. // Print the results.
  665. for (unsigned Idx : MissingLines.set_bits())
  666. dbg() << "WARNING: Missing line " << Idx + 1 << "\n";
  667. for (unsigned Idx : MissingVars.set_bits())
  668. dbg() << "WARNING: Missing variable " << Idx + 1 << "\n";
  669. // Update DI loss statistics.
  670. if (Stats) {
  671. Stats->NumDbgLocsExpected += OriginalNumLines;
  672. Stats->NumDbgLocsMissing += MissingLines.count();
  673. Stats->NumDbgValuesExpected += OriginalNumVars;
  674. Stats->NumDbgValuesMissing += MissingVars.count();
  675. }
  676. dbg() << Banner;
  677. if (!NameOfWrappedPass.empty())
  678. dbg() << " [" << NameOfWrappedPass << "]";
  679. dbg() << ": " << (HasErrors ? "FAIL" : "PASS") << '\n';
  680. // Strip debugify metadata if required.
  681. if (Strip)
  682. return stripDebugifyMetadata(M);
  683. return false;
  684. }
  685. /// ModulePass for attaching synthetic debug info to everything, used with the
  686. /// legacy module pass manager.
  687. struct DebugifyModulePass : public ModulePass {
  688. bool runOnModule(Module &M) override {
  689. return applyDebugify(M, Mode, DebugInfoBeforePass, NameOfWrappedPass);
  690. }
  691. DebugifyModulePass(enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
  692. StringRef NameOfWrappedPass = "",
  693. DebugInfoPerPass *DebugInfoBeforePass = nullptr)
  694. : ModulePass(ID), NameOfWrappedPass(NameOfWrappedPass),
  695. DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode) {}
  696. void getAnalysisUsage(AnalysisUsage &AU) const override {
  697. AU.setPreservesAll();
  698. }
  699. static char ID; // Pass identification.
  700. private:
  701. StringRef NameOfWrappedPass;
  702. DebugInfoPerPass *DebugInfoBeforePass;
  703. enum DebugifyMode Mode;
  704. };
  705. /// FunctionPass for attaching synthetic debug info to instructions within a
  706. /// single function, used with the legacy module pass manager.
  707. struct DebugifyFunctionPass : public FunctionPass {
  708. bool runOnFunction(Function &F) override {
  709. return applyDebugify(F, Mode, DebugInfoBeforePass, NameOfWrappedPass);
  710. }
  711. DebugifyFunctionPass(
  712. enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
  713. StringRef NameOfWrappedPass = "",
  714. DebugInfoPerPass *DebugInfoBeforePass = nullptr)
  715. : FunctionPass(ID), NameOfWrappedPass(NameOfWrappedPass),
  716. DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode) {}
  717. void getAnalysisUsage(AnalysisUsage &AU) const override {
  718. AU.setPreservesAll();
  719. }
  720. static char ID; // Pass identification.
  721. private:
  722. StringRef NameOfWrappedPass;
  723. DebugInfoPerPass *DebugInfoBeforePass;
  724. enum DebugifyMode Mode;
  725. };
  726. /// ModulePass for checking debug info inserted by -debugify, used with the
  727. /// legacy module pass manager.
  728. struct CheckDebugifyModulePass : public ModulePass {
  729. bool runOnModule(Module &M) override {
  730. if (Mode == DebugifyMode::SyntheticDebugInfo)
  731. return checkDebugifyMetadata(M, M.functions(), NameOfWrappedPass,
  732. "CheckModuleDebugify", Strip, StatsMap);
  733. return checkDebugInfoMetadata(
  734. M, M.functions(), *DebugInfoBeforePass,
  735. "CheckModuleDebugify (original debuginfo)", NameOfWrappedPass,
  736. OrigDIVerifyBugsReportFilePath);
  737. }
  738. CheckDebugifyModulePass(
  739. bool Strip = false, StringRef NameOfWrappedPass = "",
  740. DebugifyStatsMap *StatsMap = nullptr,
  741. enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
  742. DebugInfoPerPass *DebugInfoBeforePass = nullptr,
  743. StringRef OrigDIVerifyBugsReportFilePath = "")
  744. : ModulePass(ID), NameOfWrappedPass(NameOfWrappedPass),
  745. OrigDIVerifyBugsReportFilePath(OrigDIVerifyBugsReportFilePath),
  746. StatsMap(StatsMap), DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode),
  747. Strip(Strip) {}
  748. void getAnalysisUsage(AnalysisUsage &AU) const override {
  749. AU.setPreservesAll();
  750. }
  751. static char ID; // Pass identification.
  752. private:
  753. StringRef NameOfWrappedPass;
  754. StringRef OrigDIVerifyBugsReportFilePath;
  755. DebugifyStatsMap *StatsMap;
  756. DebugInfoPerPass *DebugInfoBeforePass;
  757. enum DebugifyMode Mode;
  758. bool Strip;
  759. };
  760. /// FunctionPass for checking debug info inserted by -debugify-function, used
  761. /// with the legacy module pass manager.
  762. struct CheckDebugifyFunctionPass : public FunctionPass {
  763. bool runOnFunction(Function &F) override {
  764. Module &M = *F.getParent();
  765. auto FuncIt = F.getIterator();
  766. if (Mode == DebugifyMode::SyntheticDebugInfo)
  767. return checkDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
  768. NameOfWrappedPass, "CheckFunctionDebugify",
  769. Strip, StatsMap);
  770. return checkDebugInfoMetadata(
  771. M, make_range(FuncIt, std::next(FuncIt)), *DebugInfoBeforePass,
  772. "CheckFunctionDebugify (original debuginfo)", NameOfWrappedPass,
  773. OrigDIVerifyBugsReportFilePath);
  774. }
  775. CheckDebugifyFunctionPass(
  776. bool Strip = false, StringRef NameOfWrappedPass = "",
  777. DebugifyStatsMap *StatsMap = nullptr,
  778. enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
  779. DebugInfoPerPass *DebugInfoBeforePass = nullptr,
  780. StringRef OrigDIVerifyBugsReportFilePath = "")
  781. : FunctionPass(ID), NameOfWrappedPass(NameOfWrappedPass),
  782. OrigDIVerifyBugsReportFilePath(OrigDIVerifyBugsReportFilePath),
  783. StatsMap(StatsMap), DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode),
  784. Strip(Strip) {}
  785. void getAnalysisUsage(AnalysisUsage &AU) const override {
  786. AU.setPreservesAll();
  787. }
  788. static char ID; // Pass identification.
  789. private:
  790. StringRef NameOfWrappedPass;
  791. StringRef OrigDIVerifyBugsReportFilePath;
  792. DebugifyStatsMap *StatsMap;
  793. DebugInfoPerPass *DebugInfoBeforePass;
  794. enum DebugifyMode Mode;
  795. bool Strip;
  796. };
  797. } // end anonymous namespace
  798. void llvm::exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map) {
  799. std::error_code EC;
  800. raw_fd_ostream OS{Path, EC};
  801. if (EC) {
  802. errs() << "Could not open file: " << EC.message() << ", " << Path << '\n';
  803. return;
  804. }
  805. OS << "Pass Name" << ',' << "# of missing debug values" << ','
  806. << "# of missing locations" << ',' << "Missing/Expected value ratio" << ','
  807. << "Missing/Expected location ratio" << '\n';
  808. for (const auto &Entry : Map) {
  809. StringRef Pass = Entry.first;
  810. DebugifyStatistics Stats = Entry.second;
  811. OS << Pass << ',' << Stats.NumDbgValuesMissing << ','
  812. << Stats.NumDbgLocsMissing << ',' << Stats.getMissingValueRatio() << ','
  813. << Stats.getEmptyLocationRatio() << '\n';
  814. }
  815. }
  816. ModulePass *createDebugifyModulePass(enum DebugifyMode Mode,
  817. llvm::StringRef NameOfWrappedPass,
  818. DebugInfoPerPass *DebugInfoBeforePass) {
  819. if (Mode == DebugifyMode::SyntheticDebugInfo)
  820. return new DebugifyModulePass();
  821. assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
  822. return new DebugifyModulePass(Mode, NameOfWrappedPass, DebugInfoBeforePass);
  823. }
  824. FunctionPass *
  825. createDebugifyFunctionPass(enum DebugifyMode Mode,
  826. llvm::StringRef NameOfWrappedPass,
  827. DebugInfoPerPass *DebugInfoBeforePass) {
  828. if (Mode == DebugifyMode::SyntheticDebugInfo)
  829. return new DebugifyFunctionPass();
  830. assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
  831. return new DebugifyFunctionPass(Mode, NameOfWrappedPass, DebugInfoBeforePass);
  832. }
  833. PreservedAnalyses NewPMDebugifyPass::run(Module &M, ModuleAnalysisManager &) {
  834. if (Mode == DebugifyMode::SyntheticDebugInfo)
  835. applyDebugifyMetadata(M, M.functions(),
  836. "ModuleDebugify: ", /*ApplyToMF*/ nullptr);
  837. else
  838. collectDebugInfoMetadata(M, M.functions(), *DebugInfoBeforePass,
  839. "ModuleDebugify (original debuginfo)",
  840. NameOfWrappedPass);
  841. return PreservedAnalyses::all();
  842. }
  843. ModulePass *createCheckDebugifyModulePass(
  844. bool Strip, StringRef NameOfWrappedPass, DebugifyStatsMap *StatsMap,
  845. enum DebugifyMode Mode, DebugInfoPerPass *DebugInfoBeforePass,
  846. StringRef OrigDIVerifyBugsReportFilePath) {
  847. if (Mode == DebugifyMode::SyntheticDebugInfo)
  848. return new CheckDebugifyModulePass(Strip, NameOfWrappedPass, StatsMap);
  849. assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
  850. return new CheckDebugifyModulePass(false, NameOfWrappedPass, nullptr, Mode,
  851. DebugInfoBeforePass,
  852. OrigDIVerifyBugsReportFilePath);
  853. }
  854. FunctionPass *createCheckDebugifyFunctionPass(
  855. bool Strip, StringRef NameOfWrappedPass, DebugifyStatsMap *StatsMap,
  856. enum DebugifyMode Mode, DebugInfoPerPass *DebugInfoBeforePass,
  857. StringRef OrigDIVerifyBugsReportFilePath) {
  858. if (Mode == DebugifyMode::SyntheticDebugInfo)
  859. return new CheckDebugifyFunctionPass(Strip, NameOfWrappedPass, StatsMap);
  860. assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
  861. return new CheckDebugifyFunctionPass(false, NameOfWrappedPass, nullptr, Mode,
  862. DebugInfoBeforePass,
  863. OrigDIVerifyBugsReportFilePath);
  864. }
  865. PreservedAnalyses NewPMCheckDebugifyPass::run(Module &M,
  866. ModuleAnalysisManager &) {
  867. if (Mode == DebugifyMode::SyntheticDebugInfo)
  868. checkDebugifyMetadata(M, M.functions(), NameOfWrappedPass,
  869. "CheckModuleDebugify", Strip, StatsMap);
  870. else
  871. checkDebugInfoMetadata(
  872. M, M.functions(), *DebugInfoBeforePass,
  873. "CheckModuleDebugify (original debuginfo)", NameOfWrappedPass,
  874. OrigDIVerifyBugsReportFilePath);
  875. return PreservedAnalyses::all();
  876. }
  877. static bool isIgnoredPass(StringRef PassID) {
  878. return isSpecialPass(PassID, {"PassManager", "PassAdaptor",
  879. "AnalysisManagerProxy", "PrintFunctionPass",
  880. "PrintModulePass", "BitcodeWriterPass",
  881. "ThinLTOBitcodeWriterPass", "VerifierPass"});
  882. }
  883. void DebugifyEachInstrumentation::registerCallbacks(
  884. PassInstrumentationCallbacks &PIC) {
  885. PIC.registerBeforeNonSkippedPassCallback([this](StringRef P, Any IR) {
  886. if (isIgnoredPass(P))
  887. return;
  888. if (const auto **F = any_cast<const Function *>(&IR))
  889. applyDebugify(*const_cast<Function *>(*F),
  890. Mode, DebugInfoBeforePass, P);
  891. else if (const auto **M = any_cast<const Module *>(&IR))
  892. applyDebugify(*const_cast<Module *>(*M),
  893. Mode, DebugInfoBeforePass, P);
  894. });
  895. PIC.registerAfterPassCallback([this](StringRef P, Any IR,
  896. const PreservedAnalyses &PassPA) {
  897. if (isIgnoredPass(P))
  898. return;
  899. if (const auto **CF = any_cast<const Function *>(&IR)) {
  900. auto &F = *const_cast<Function *>(*CF);
  901. Module &M = *F.getParent();
  902. auto It = F.getIterator();
  903. if (Mode == DebugifyMode::SyntheticDebugInfo)
  904. checkDebugifyMetadata(M, make_range(It, std::next(It)), P,
  905. "CheckFunctionDebugify", /*Strip=*/true, DIStatsMap);
  906. else
  907. checkDebugInfoMetadata(
  908. M, make_range(It, std::next(It)), *DebugInfoBeforePass,
  909. "CheckModuleDebugify (original debuginfo)",
  910. P, OrigDIVerifyBugsReportFilePath);
  911. } else if (const auto **CM = any_cast<const Module *>(&IR)) {
  912. auto &M = *const_cast<Module *>(*CM);
  913. if (Mode == DebugifyMode::SyntheticDebugInfo)
  914. checkDebugifyMetadata(M, M.functions(), P, "CheckModuleDebugify",
  915. /*Strip=*/true, DIStatsMap);
  916. else
  917. checkDebugInfoMetadata(
  918. M, M.functions(), *DebugInfoBeforePass,
  919. "CheckModuleDebugify (original debuginfo)",
  920. P, OrigDIVerifyBugsReportFilePath);
  921. }
  922. });
  923. }
  924. char DebugifyModulePass::ID = 0;
  925. static RegisterPass<DebugifyModulePass> DM("debugify",
  926. "Attach debug info to everything");
  927. char CheckDebugifyModulePass::ID = 0;
  928. static RegisterPass<CheckDebugifyModulePass>
  929. CDM("check-debugify", "Check debug info from -debugify");
  930. char DebugifyFunctionPass::ID = 0;
  931. static RegisterPass<DebugifyFunctionPass> DF("debugify-function",
  932. "Attach debug info to a function");
  933. char CheckDebugifyFunctionPass::ID = 0;
  934. static RegisterPass<CheckDebugifyFunctionPass>
  935. CDF("check-debugify-function", "Check debug info from -debugify-function");