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