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