LLVM 22.0.0git
Debugify.cpp
Go to the documentation of this file.
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
17#include "llvm/ADT/BitVector.h"
19#include "llvm/Config/llvm-config.h"
20#include "llvm/IR/DIBuilder.h"
21#include "llvm/IR/DebugInfo.h"
23#include "llvm/IR/DebugLoc.h"
26#include "llvm/IR/Module.h"
28#include "llvm/Pass.h"
31#include "llvm/Support/JSON.h"
32#include <optional>
33#if LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN
34// We need the Signals header to operate on stacktraces if we're using DebugLoc
35// origin-tracking.
37#endif
38
39#define DEBUG_TYPE "debugify"
40
41using namespace llvm;
42
43namespace {
44
45cl::opt<bool> ApplyAtomGroups("debugify-atoms", cl::init(false));
46
47cl::opt<bool> Quiet("debugify-quiet",
48 cl::desc("Suppress verbose debugify output"));
49
50cl::opt<uint64_t> DebugifyFunctionsLimit(
51 "debugify-func-limit",
52 cl::desc("Set max number of processed functions per pass."),
53 cl::init(UINT_MAX));
54
55enum class Level {
56 Locations,
57 LocationsAndVariables
58};
59
60cl::opt<Level> DebugifyLevel(
61 "debugify-level", cl::desc("Kind of debug info to add"),
62 cl::values(clEnumValN(Level::Locations, "locations", "Locations only"),
63 clEnumValN(Level::LocationsAndVariables, "location+variables",
64 "Locations and Variables")),
65 cl::init(Level::LocationsAndVariables));
66
67raw_ostream &dbg() { return Quiet ? nulls() : errs(); }
68
69#if LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN
70// These maps refer to addresses in the current LLVM process, so we can reuse
71// them everywhere - therefore, we store them at file scope.
72static SymbolizedAddressMap SymbolizedAddrs;
73static AddressSet UnsymbolizedAddrs;
74
75std::string symbolizeStackTrace(const Instruction *I) {
76 // We flush the set of unsymbolized addresses at the latest possible moment,
77 // i.e. now.
78 if (!UnsymbolizedAddrs.empty()) {
79 sys::symbolizeAddresses(UnsymbolizedAddrs, SymbolizedAddrs);
80 UnsymbolizedAddrs.clear();
81 }
82 const DbgLocOrigin::StackTracesTy &OriginStackTraces =
83 I->getDebugLoc().getOriginStackTraces();
84 std::string Result;
85 raw_string_ostream OS(Result);
86 for (size_t TraceIdx = 0; TraceIdx < OriginStackTraces.size(); ++TraceIdx) {
87 if (TraceIdx != 0)
88 OS << "========================================\n";
89 auto &[Depth, StackTrace] = OriginStackTraces[TraceIdx];
90 unsigned VirtualFrameNo = 0;
91 for (int Frame = 0; Frame < Depth; ++Frame) {
92 assert(SymbolizedAddrs.contains(StackTrace[Frame]) &&
93 "Expected each address to have been symbolized.");
94 for (std::string &SymbolizedFrame : SymbolizedAddrs[StackTrace[Frame]]) {
95 OS << right_justify(formatv("#{0}", VirtualFrameNo++).str(),
96 std::log10(Depth) + 2)
97 << ' ' << SymbolizedFrame << '\n';
98 }
99 }
100 }
101 return Result;
102}
103void collectStackAddresses(Instruction &I) {
104 auto &OriginStackTraces = I.getDebugLoc().getOriginStackTraces();
105 for (auto &[Depth, StackTrace] : OriginStackTraces) {
106 for (int Frame = 0; Frame < Depth; ++Frame) {
107 void *Addr = StackTrace[Frame];
108 if (!SymbolizedAddrs.contains(Addr))
109 UnsymbolizedAddrs.insert(Addr);
110 }
111 }
112}
113#else
114void collectStackAddresses(Instruction &I) {}
115#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN
116
117uint64_t getAllocSizeInBits(Module &M, Type *Ty) {
118 return Ty->isSized() ? M.getDataLayout().getTypeAllocSizeInBits(Ty) : 0;
119}
120
121bool isFunctionSkipped(Function &F) {
122 return F.isDeclaration() || !F.hasExactDefinition();
123}
124
125/// Find the basic block's terminating instruction.
126///
127/// Special care is needed to handle musttail and deopt calls, as these behave
128/// like (but are in fact not) terminators.
129Instruction *findTerminatingInstruction(BasicBlock &BB) {
130 if (auto *I = BB.getTerminatingMustTailCall())
131 return I;
132 if (auto *I = BB.getTerminatingDeoptimizeCall())
133 return I;
134 return BB.getTerminator();
135}
136} // end anonymous namespace
137
140 std::function<bool(DIBuilder &DIB, Function &F)> ApplyToMF) {
141 // Skip modules with debug info.
142 if (M.getNamedMetadata("llvm.dbg.cu")) {
143 dbg() << Banner << "Skipping module with debug info\n";
144 return false;
145 }
146
147 DIBuilder DIB(M);
148 LLVMContext &Ctx = M.getContext();
149 auto *Int32Ty = Type::getInt32Ty(Ctx);
150
151 // Get a DIType which corresponds to Ty.
153 auto getCachedDIType = [&](Type *Ty) -> DIType * {
154 uint64_t Size = getAllocSizeInBits(M, Ty);
155 DIType *&DTy = TypeCache[Size];
156 if (!DTy) {
157 std::string Name = "ty" + utostr(Size);
158 DTy = DIB.createBasicType(Name, Size, dwarf::DW_ATE_unsigned);
159 }
160 return DTy;
161 };
162
163 unsigned NextLine = 1;
164 unsigned NextVar = 1;
165 auto File = DIB.createFile(M.getName(), "/");
166 auto CU = DIB.createCompileUnit(DISourceLanguageName(dwarf::DW_LANG_C), File,
167 "debugify", /*isOptimized=*/true, "", 0);
168
169 // Visit each instruction.
170 for (Function &F : Functions) {
171 if (isFunctionSkipped(F))
172 continue;
173
174 bool InsertedDbgVal = false;
175 auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray({}));
177 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized;
178 if (F.hasPrivateLinkage() || F.hasInternalLinkage())
179 SPFlags |= DISubprogram::SPFlagLocalToUnit;
180 auto SP = DIB.createFunction(CU, F.getName(), F.getName(), File, NextLine,
181 SPType, NextLine, DINode::FlagZero, SPFlags,
182 nullptr, nullptr, nullptr, nullptr, "",
183 /*UseKeyInstructions*/ ApplyAtomGroups);
184 F.setSubprogram(SP);
185
186 // Helper that inserts a dbg.value before \p InsertBefore, copying the
187 // location (and possibly the type, if it's non-void) from \p TemplateInst.
188 auto insertDbgVal = [&](Instruction &TemplateInst,
189 BasicBlock::iterator InsertPt) {
190 std::string Name = utostr(NextVar++);
191 Value *V = &TemplateInst;
192 if (TemplateInst.getType()->isVoidTy())
193 V = ConstantInt::get(Int32Ty, 0);
194 const DILocation *Loc = TemplateInst.getDebugLoc().get();
195 auto LocalVar = DIB.createAutoVariable(SP, Name, File, Loc->getLine(),
196 getCachedDIType(V->getType()),
197 /*AlwaysPreserve=*/true);
198 DIB.insertDbgValueIntrinsic(V, LocalVar, DIB.createExpression(), Loc,
199 InsertPt);
200 };
201
202 for (BasicBlock &BB : F) {
203 // Attach debug locations.
204 for (Instruction &I : BB) {
205 uint64_t AtomGroup = ApplyAtomGroups ? NextLine : 0;
206 uint8_t AtomRank = ApplyAtomGroups ? 1 : 0;
207 uint64_t Line = NextLine++;
208 I.setDebugLoc(DILocation::get(Ctx, Line, 1, SP, nullptr, false,
209 AtomGroup, AtomRank));
210 }
211
212 if (DebugifyLevel < Level::LocationsAndVariables)
213 continue;
214
215 // Inserting debug values into EH pads can break IR invariants.
216 if (BB.isEHPad())
217 continue;
218
219 // Find the terminating instruction, after which no debug values are
220 // attached.
221 Instruction *LastInst = findTerminatingInstruction(BB);
222 assert(LastInst && "Expected basic block with a terminator");
223
224 // Maintain an insertion point which can't be invalidated when updates
225 // are made.
226 BasicBlock::iterator InsertPt = BB.getFirstInsertionPt();
227 assert(InsertPt != BB.end() && "Expected to find an insertion point");
228
229 // Insert after existing debug values to preserve order.
230 InsertPt.setHeadBit(false);
231
232 // Attach debug values.
233 for (Instruction *I = &*BB.begin(); I != LastInst; I = I->getNextNode()) {
234 // Skip void-valued instructions.
235 if (I->getType()->isVoidTy())
236 continue;
237
238 // Phis and EH pads must be grouped at the beginning of the block.
239 // Only advance the insertion point when we finish visiting these.
240 if (!isa<PHINode>(I) && !I->isEHPad())
241 InsertPt = std::next(I->getIterator());
242
243 insertDbgVal(*I, InsertPt);
244 InsertedDbgVal = true;
245 }
246 }
247 // Make sure we emit at least one dbg.value, otherwise MachineDebugify may
248 // not have anything to work with as it goes about inserting DBG_VALUEs.
249 // (It's common for MIR tests to be written containing skeletal IR with
250 // empty functions -- we're still interested in debugifying the MIR within
251 // those tests, and this helps with that.)
252 if (DebugifyLevel == Level::LocationsAndVariables && !InsertedDbgVal) {
253 auto *Term = findTerminatingInstruction(F.getEntryBlock());
254 insertDbgVal(*Term, Term->getIterator());
255 }
256 if (ApplyToMF)
257 ApplyToMF(DIB, F);
258 }
259 DIB.finalize();
260
261 // Track the number of distinct lines and variables.
262 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.debugify");
263 auto addDebugifyOperand = [&](unsigned N) {
265 Ctx, ValueAsMetadata::getConstant(ConstantInt::get(Int32Ty, N))));
266 };
267 addDebugifyOperand(NextLine - 1); // Original number of lines.
268 addDebugifyOperand(NextVar - 1); // Original number of variables.
269 assert(NMD->getNumOperands() == 2 &&
270 "llvm.debugify should have exactly 2 operands!");
271
272 // Claim that this synthetic debug info is valid.
273 StringRef DIVersionKey = "Debug Info Version";
274 if (!M.getModuleFlag(DIVersionKey))
275 M.addModuleFlag(Module::Warning, DIVersionKey, DEBUG_METADATA_VERSION);
276
277 return true;
278}
279
281 DebugInfoPerPass *DebugInfoBeforePass,
282 StringRef NameOfWrappedPass = "") {
283 Module &M = *F.getParent();
284 auto FuncIt = F.getIterator();
286 return applyDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
287 "FunctionDebugify: ", /*ApplyToMF*/ nullptr);
288 assert(DebugInfoBeforePass && "Missing debug info metadata");
289 return collectDebugInfoMetadata(M, M.functions(), *DebugInfoBeforePass,
290 "FunctionDebugify (original debuginfo)",
291 NameOfWrappedPass);
292}
293
295 DebugInfoPerPass *DebugInfoBeforePass,
296 StringRef NameOfWrappedPass = "") {
298 return applyDebugifyMetadata(M, M.functions(),
299 "ModuleDebugify: ", /*ApplyToMF*/ nullptr);
300 assert(DebugInfoBeforePass && "Missing debug info metadata");
301 return collectDebugInfoMetadata(M, M.functions(), *DebugInfoBeforePass,
302 "ModuleDebugify (original debuginfo)",
303 NameOfWrappedPass);
304}
305
307 bool Changed = false;
308
309 // Remove the llvm.debugify and llvm.mir.debugify module-level named metadata.
310 NamedMDNode *DebugifyMD = M.getNamedMetadata("llvm.debugify");
311 if (DebugifyMD) {
312 M.eraseNamedMetadata(DebugifyMD);
313 Changed = true;
314 }
315
316 if (auto *MIRDebugifyMD = M.getNamedMetadata("llvm.mir.debugify")) {
317 M.eraseNamedMetadata(MIRDebugifyMD);
318 Changed = true;
319 }
320
321 // Strip out all debug intrinsics and supporting metadata (subprograms, types,
322 // variables, etc).
324
325 // Strip out the dead dbg.value prototype.
326 Function *DbgValF = M.getFunction("llvm.dbg.value");
327 if (DbgValF) {
328 assert(DbgValF->isDeclaration() && DbgValF->use_empty() &&
329 "Not all debug info stripped?");
330 DbgValF->eraseFromParent();
331 Changed = true;
332 }
333
334 // Strip out the module-level Debug Info Version metadata.
335 // FIXME: There must be an easier way to remove an operand from a NamedMDNode.
336 NamedMDNode *NMD = M.getModuleFlagsMetadata();
337 if (!NMD)
338 return Changed;
340 NMD->clearOperands();
341 for (MDNode *Flag : Flags) {
342 auto *Key = cast<MDString>(Flag->getOperand(1));
343 if (Key->getString() == "Debug Info Version") {
344 Changed = true;
345 continue;
346 }
347 NMD->addOperand(Flag);
348 }
349 // If we left it empty we might as well remove it.
350 if (NMD->getNumOperands() == 0)
351 NMD->eraseFromParent();
352
353 return Changed;
354}
355
356bool hasLoc(const Instruction &I) {
357 const DILocation *Loc = I.getDebugLoc().get();
358#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
359 DebugLocKind Kind = I.getDebugLoc().getKind();
360 return Loc || Kind != DebugLocKind::Normal;
361#else
362 return Loc;
363#endif
364}
365
368 DebugInfoPerPass &DebugInfoBeforePass,
369 StringRef Banner,
370 StringRef NameOfWrappedPass) {
371 LLVM_DEBUG(dbgs() << Banner << ": (before) " << NameOfWrappedPass << '\n');
372
373 if (!M.getNamedMetadata("llvm.dbg.cu")) {
374 dbg() << Banner << ": Skipping module without debug info\n";
375 return false;
376 }
377
378 uint64_t FunctionsCnt = DebugInfoBeforePass.DIFunctions.size();
379 // Visit each instruction.
380 for (Function &F : Functions) {
381 // Use DI collected after previous Pass (when -debugify-each is used).
382 if (DebugInfoBeforePass.DIFunctions.count(&F))
383 continue;
384
385 if (isFunctionSkipped(F))
386 continue;
387
388 // Stop collecting DI if the Functions number reached the limit.
389 if (++FunctionsCnt >= DebugifyFunctionsLimit)
390 break;
391 // Collect the DISubprogram.
392 auto *SP = F.getSubprogram();
393 DebugInfoBeforePass.DIFunctions.insert({&F, SP});
394 if (SP) {
395 LLVM_DEBUG(dbgs() << " Collecting subprogram: " << *SP << '\n');
396 for (const DINode *DN : SP->getRetainedNodes()) {
397 if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
398 DebugInfoBeforePass.DIVariables[DV] = 0;
399 }
400 }
401 }
402
403 for (BasicBlock &BB : F) {
404 // Collect debug locations (!dbg) and debug variable intrinsics.
405 for (Instruction &I : BB) {
406 // Skip PHIs.
407 if (isa<PHINode>(I))
408 continue;
409
410 // Cllect dbg.values and dbg.declare.
411 if (DebugifyLevel > Level::Locations) {
412 auto HandleDbgVariable = [&](DbgVariableRecord *DbgVar) {
413 if (!SP)
414 return;
415 // Skip inlined variables.
416 if (DbgVar->getDebugLoc().getInlinedAt())
417 return;
418 // Skip undef values.
419 if (DbgVar->isKillLocation())
420 return;
421
422 auto *Var = DbgVar->getVariable();
423 DebugInfoBeforePass.DIVariables[Var]++;
424 };
425 for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange()))
426 HandleDbgVariable(&DVR);
427 }
428
429 LLVM_DEBUG(dbgs() << " Collecting info for inst: " << I << '\n');
430 DebugInfoBeforePass.InstToDelete.insert({&I, &I});
431
432 // Track the addresses to symbolize, if the feature is enabled.
433 collectStackAddresses(I);
434 DebugInfoBeforePass.DILocations.insert({&I, hasLoc(I)});
435 }
436 }
437 }
438
439 return true;
440}
441
442// This checks the preservation of original debug info attached to functions.
443static bool checkFunctions(const DebugFnMap &DIFunctionsBefore,
444 const DebugFnMap &DIFunctionsAfter,
445 StringRef NameOfWrappedPass,
446 StringRef FileNameFromCU, bool ShouldWriteIntoJSON,
447 llvm::json::Array &Bugs) {
448 bool Preserved = true;
449 for (const auto &F : DIFunctionsAfter) {
450 if (F.second)
451 continue;
452 auto SPIt = DIFunctionsBefore.find(F.first);
453 if (SPIt == DIFunctionsBefore.end()) {
454 if (ShouldWriteIntoJSON)
455 Bugs.push_back(llvm::json::Object({{"metadata", "DISubprogram"},
456 {"name", F.first->getName()},
457 {"action", "not-generate"}}));
458 else
459 dbg() << "ERROR: " << NameOfWrappedPass
460 << " did not generate DISubprogram for " << F.first->getName()
461 << " from " << FileNameFromCU << '\n';
462 Preserved = false;
463 } else {
464 auto SP = SPIt->second;
465 if (!SP)
466 continue;
467 // If the function had the SP attached before the pass, consider it as
468 // a debug info bug.
469 if (ShouldWriteIntoJSON)
470 Bugs.push_back(llvm::json::Object({{"metadata", "DISubprogram"},
471 {"name", F.first->getName()},
472 {"action", "drop"}}));
473 else
474 dbg() << "ERROR: " << NameOfWrappedPass << " dropped DISubprogram of "
475 << F.first->getName() << " from " << FileNameFromCU << '\n';
476 Preserved = false;
477 }
478 }
479
480 return Preserved;
481}
482
483// This checks the preservation of the original debug info attached to
484// instructions.
485static bool checkInstructions(const DebugInstMap &DILocsBefore,
486 const DebugInstMap &DILocsAfter,
487 const WeakInstValueMap &InstToDelete,
488 StringRef NameOfWrappedPass,
489 StringRef FileNameFromCU,
490 bool ShouldWriteIntoJSON,
491 llvm::json::Array &Bugs) {
492 bool Preserved = true;
493 for (const auto &L : DILocsAfter) {
494 if (L.second)
495 continue;
496 auto Instr = L.first;
497
498 // In order to avoid pointer reuse/recycling, skip the values that might
499 // have been deleted during a pass.
500 auto WeakInstrPtr = InstToDelete.find(Instr);
501 if (WeakInstrPtr != InstToDelete.end() && !WeakInstrPtr->second)
502 continue;
503
504 auto FnName = Instr->getFunction()->getName();
505 auto BB = Instr->getParent();
506 auto BBName = BB->hasName() ? BB->getName() : "no-name";
507 auto InstName = Instruction::getOpcodeName(Instr->getOpcode());
508
509 auto CreateJSONBugEntry = [&](const char *Action) {
511 {"metadata", "DILocation"},
512 {"fn-name", FnName.str()},
513 {"bb-name", BBName.str()},
514 {"instr", InstName},
515 {"action", Action},
516#if LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN
517 {"origin", symbolizeStackTrace(Instr)},
518#endif
519 }));
520 };
521
522 auto InstrIt = DILocsBefore.find(Instr);
523 if (InstrIt == DILocsBefore.end()) {
524 if (ShouldWriteIntoJSON)
525 CreateJSONBugEntry("not-generate");
526 else
527 dbg() << "WARNING: " << NameOfWrappedPass
528 << " did not generate DILocation for " << *Instr
529 << " (BB: " << BBName << ", Fn: " << FnName
530 << ", File: " << FileNameFromCU << ")\n";
531 Preserved = false;
532 } else {
533 if (!InstrIt->second)
534 continue;
535 // If the instr had the !dbg attached before the pass, consider it as
536 // a debug info issue.
537 if (ShouldWriteIntoJSON)
538 CreateJSONBugEntry("drop");
539 else
540 dbg() << "WARNING: " << NameOfWrappedPass << " dropped DILocation of "
541 << *Instr << " (BB: " << BBName << ", Fn: " << FnName
542 << ", File: " << FileNameFromCU << ")\n";
543 Preserved = false;
544 }
545 }
546
547 return Preserved;
548}
549
550// This checks the preservation of original debug variable intrinsics.
551static bool checkVars(const DebugVarMap &DIVarsBefore,
552 const DebugVarMap &DIVarsAfter,
553 StringRef NameOfWrappedPass, StringRef FileNameFromCU,
554 bool ShouldWriteIntoJSON, llvm::json::Array &Bugs) {
555 bool Preserved = true;
556 for (const auto &V : DIVarsBefore) {
557 auto VarIt = DIVarsAfter.find(V.first);
558 if (VarIt == DIVarsAfter.end())
559 continue;
560
561 unsigned NumOfDbgValsAfter = VarIt->second;
562
563 if (V.second > NumOfDbgValsAfter) {
564 if (ShouldWriteIntoJSON)
566 {{"metadata", "dbg-var-intrinsic"},
567 {"name", V.first->getName()},
568 {"fn-name", V.first->getScope()->getSubprogram()->getName()},
569 {"action", "drop"}}));
570 else
571 dbg() << "WARNING: " << NameOfWrappedPass
572 << " drops dbg.value()/dbg.declare() for " << V.first->getName()
573 << " from "
574 << "function " << V.first->getScope()->getSubprogram()->getName()
575 << " (file " << FileNameFromCU << ")\n";
576 Preserved = false;
577 }
578 }
579
580 return Preserved;
581}
582
583// Write the json data into the specifed file.
584static void writeJSON(StringRef OrigDIVerifyBugsReportFilePath,
585 StringRef FileNameFromCU, StringRef NameOfWrappedPass,
586 llvm::json::Array &Bugs) {
587 std::error_code EC;
588 raw_fd_ostream OS_FILE{OrigDIVerifyBugsReportFilePath, EC,
590 if (EC) {
591 errs() << "Could not open file: " << EC.message() << ", "
592 << OrigDIVerifyBugsReportFilePath << '\n';
593 return;
594 }
595
596 if (auto L = OS_FILE.lock()) {
597 OS_FILE << "{\"file\":\"" << FileNameFromCU << "\", ";
598
600 NameOfWrappedPass != "" ? NameOfWrappedPass : "no-name";
601 OS_FILE << "\"pass\":\"" << PassName << "\", ";
602
603 llvm::json::Value BugsToPrint{std::move(Bugs)};
604 OS_FILE << "\"bugs\": " << BugsToPrint;
605
606 OS_FILE << "}\n";
607 }
608 OS_FILE.close();
609}
610
613 DebugInfoPerPass &DebugInfoBeforePass,
614 StringRef Banner, StringRef NameOfWrappedPass,
615 StringRef OrigDIVerifyBugsReportFilePath) {
616 LLVM_DEBUG(dbgs() << Banner << ": (after) " << NameOfWrappedPass << '\n');
617
618 if (!M.getNamedMetadata("llvm.dbg.cu")) {
619 dbg() << Banner << ": Skipping module without debug info\n";
620 return false;
621 }
622
623 // Map the debug info holding DIs after a pass.
624 DebugInfoPerPass DebugInfoAfterPass;
625
626 // Visit each instruction.
627 for (Function &F : Functions) {
628 if (isFunctionSkipped(F))
629 continue;
630
631 // Don't process functions without DI collected before the Pass.
632 if (!DebugInfoBeforePass.DIFunctions.count(&F))
633 continue;
634 // TODO: Collect metadata other than DISubprograms.
635 // Collect the DISubprogram.
636 auto *SP = F.getSubprogram();
637 DebugInfoAfterPass.DIFunctions.insert({&F, SP});
638
639 if (SP) {
640 LLVM_DEBUG(dbgs() << " Collecting subprogram: " << *SP << '\n');
641 for (const DINode *DN : SP->getRetainedNodes()) {
642 if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
643 DebugInfoAfterPass.DIVariables[DV] = 0;
644 }
645 }
646 }
647
648 for (BasicBlock &BB : F) {
649 // Collect debug locations (!dbg) and debug variable intrinsics.
650 for (Instruction &I : BB) {
651 // Skip PHIs.
652 if (isa<PHINode>(I))
653 continue;
654
655 // Collect dbg.values and dbg.declares.
656 if (DebugifyLevel > Level::Locations) {
657 auto HandleDbgVariable = [&](DbgVariableRecord *DbgVar) {
658 if (!SP)
659 return;
660 // Skip inlined variables.
661 if (DbgVar->getDebugLoc().getInlinedAt())
662 return;
663 // Skip undef values.
664 if (DbgVar->isKillLocation())
665 return;
666
667 auto *Var = DbgVar->getVariable();
668 DebugInfoAfterPass.DIVariables[Var]++;
669 };
670 for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange()))
671 HandleDbgVariable(&DVR);
672 }
673
674 LLVM_DEBUG(dbgs() << " Collecting info for inst: " << I << '\n');
675
676 // Track the addresses to symbolize, if the feature is enabled.
677 collectStackAddresses(I);
678 DebugInfoAfterPass.DILocations.insert({&I, hasLoc(I)});
679 }
680 }
681 }
682
683 // TODO: The name of the module could be read better?
684 StringRef FileNameFromCU =
685 (cast<DICompileUnit>(M.getNamedMetadata("llvm.dbg.cu")->getOperand(0)))
686 ->getFilename();
687
688 auto DIFunctionsBefore = DebugInfoBeforePass.DIFunctions;
689 auto DIFunctionsAfter = DebugInfoAfterPass.DIFunctions;
690
691 auto DILocsBefore = DebugInfoBeforePass.DILocations;
692 auto DILocsAfter = DebugInfoAfterPass.DILocations;
693
694 auto InstToDelete = DebugInfoBeforePass.InstToDelete;
695
696 auto DIVarsBefore = DebugInfoBeforePass.DIVariables;
697 auto DIVarsAfter = DebugInfoAfterPass.DIVariables;
698
699 bool ShouldWriteIntoJSON = !OrigDIVerifyBugsReportFilePath.empty();
701
702 bool ResultForFunc =
703 checkFunctions(DIFunctionsBefore, DIFunctionsAfter, NameOfWrappedPass,
704 FileNameFromCU, ShouldWriteIntoJSON, Bugs);
705 bool ResultForInsts = checkInstructions(
706 DILocsBefore, DILocsAfter, InstToDelete, NameOfWrappedPass,
707 FileNameFromCU, ShouldWriteIntoJSON, Bugs);
708
709#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
710 // If we are tracking DebugLoc coverage, replace each empty DebugLoc with an
711 // annotated location now so that it does not show up in future passes even if
712 // it is propagated to other instructions.
713 for (auto &L : DILocsAfter)
714 if (!L.second)
715 const_cast<Instruction *>(L.first)->setDebugLoc(DebugLoc::getUnknown());
716#endif
717
718 bool ResultForVars = checkVars(DIVarsBefore, DIVarsAfter, NameOfWrappedPass,
719 FileNameFromCU, ShouldWriteIntoJSON, Bugs);
720
721 bool Result = ResultForFunc && ResultForInsts && ResultForVars;
722
723 StringRef ResultBanner = NameOfWrappedPass != "" ? NameOfWrappedPass : Banner;
724 if (ShouldWriteIntoJSON && !Bugs.empty())
725 writeJSON(OrigDIVerifyBugsReportFilePath, FileNameFromCU, NameOfWrappedPass,
726 Bugs);
727
728 if (Result)
729 dbg() << ResultBanner << ": PASS\n";
730 else
731 dbg() << ResultBanner << ": FAIL\n";
732
733 // In the case of the `debugify-each`, no need to go over all the instructions
734 // again in the collectDebugInfoMetadata(), since as an input we can use
735 // the debugging information from the previous pass.
736 DebugInfoBeforePass = DebugInfoAfterPass;
737
738 LLVM_DEBUG(dbgs() << "\n\n");
739 return Result;
740}
741
742namespace {
743/// Return true if a mis-sized diagnostic is issued for \p DbgVal.
744template <typename DbgValTy>
745bool diagnoseMisSizedDbgValue(Module &M, DbgValTy *DbgVal) {
746 // The size of a dbg.value's value operand should match the size of the
747 // variable it corresponds to.
748 //
749 // TODO: This, along with a check for non-null value operands, should be
750 // promoted to verifier failures.
751
752 // For now, don't try to interpret anything more complicated than an empty
753 // DIExpression. Eventually we should try to handle OP_deref and fragments.
754 if (DbgVal->getExpression()->getNumElements())
755 return false;
756
757 Value *V = DbgVal->getVariableLocationOp(0);
758 if (!V)
759 return false;
760
761 Type *Ty = V->getType();
762 uint64_t ValueOperandSize = getAllocSizeInBits(M, Ty);
763 std::optional<uint64_t> DbgVarSize = DbgVal->getFragmentSizeInBits();
764 if (!ValueOperandSize || !DbgVarSize)
765 return false;
766
767 bool HasBadSize = false;
768 if (Ty->isIntegerTy()) {
769 auto Signedness = DbgVal->getVariable()->getSignedness();
770 if (Signedness == DIBasicType::Signedness::Signed)
771 HasBadSize = ValueOperandSize < *DbgVarSize;
772 } else {
773 HasBadSize = ValueOperandSize != *DbgVarSize;
774 }
775
776 if (HasBadSize) {
777 dbg() << "ERROR: dbg.value operand has size " << ValueOperandSize
778 << ", but its variable has size " << *DbgVarSize << ": ";
779 DbgVal->print(dbg());
780 dbg() << "\n";
781 }
782 return HasBadSize;
783}
784
785bool checkDebugifyMetadata(Module &M,
787 StringRef NameOfWrappedPass, StringRef Banner,
788 bool Strip, DebugifyStatsMap *StatsMap) {
789 // Skip modules without debugify metadata.
790 NamedMDNode *NMD = M.getNamedMetadata("llvm.debugify");
791 if (!NMD) {
792 dbg() << Banner << ": Skipping module without debugify metadata\n";
793 return false;
794 }
795
796 auto getDebugifyOperand = [&](unsigned Idx) -> unsigned {
798 ->getZExtValue();
799 };
800 assert(NMD->getNumOperands() == 2 &&
801 "llvm.debugify should have exactly 2 operands!");
802 unsigned OriginalNumLines = getDebugifyOperand(0);
803 unsigned OriginalNumVars = getDebugifyOperand(1);
804 bool HasErrors = false;
805
806 // Track debug info loss statistics if able.
807 DebugifyStatistics *Stats = nullptr;
808 if (StatsMap && !NameOfWrappedPass.empty())
809 Stats = &StatsMap->operator[](NameOfWrappedPass);
810
811 BitVector MissingLines{OriginalNumLines, true};
812 BitVector MissingVars{OriginalNumVars, true};
813 for (Function &F : Functions) {
814 if (isFunctionSkipped(F))
815 continue;
816
817 // Find missing lines.
818 for (Instruction &I : instructions(F)) {
819 auto DL = I.getDebugLoc();
820 if (DL && DL.getLine() != 0) {
821 MissingLines.reset(DL.getLine() - 1);
822 continue;
823 }
824
825 if (!isa<PHINode>(&I) && !DL) {
826 dbg() << "WARNING: Instruction with empty DebugLoc in function ";
827 dbg() << F.getName() << " --";
828 I.print(dbg());
829 dbg() << "\n";
830 }
831 }
832
833 // Find missing variables and mis-sized debug values.
834 auto CheckForMisSized = [&](auto *DbgVal) {
835 unsigned Var = ~0U;
836 (void)to_integer(DbgVal->getVariable()->getName(), Var, 10);
837 assert(Var <= OriginalNumVars && "Unexpected name for DILocalVariable");
838 bool HasBadSize = diagnoseMisSizedDbgValue(M, DbgVal);
839 if (!HasBadSize)
840 MissingVars.reset(Var - 1);
841 HasErrors |= HasBadSize;
842 };
843 for (Instruction &I : instructions(F)) {
844 for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange()))
845 if (DVR.isDbgValue() || DVR.isDbgAssign())
846 CheckForMisSized(&DVR);
847 }
848 }
849
850 // Print the results.
851 for (unsigned Idx : MissingLines.set_bits())
852 dbg() << "WARNING: Missing line " << Idx + 1 << "\n";
853
854 for (unsigned Idx : MissingVars.set_bits())
855 dbg() << "WARNING: Missing variable " << Idx + 1 << "\n";
856
857 // Update DI loss statistics.
858 if (Stats) {
859 Stats->NumDbgLocsExpected += OriginalNumLines;
860 Stats->NumDbgLocsMissing += MissingLines.count();
861 Stats->NumDbgValuesExpected += OriginalNumVars;
862 Stats->NumDbgValuesMissing += MissingVars.count();
863 }
864
865 dbg() << Banner;
866 if (!NameOfWrappedPass.empty())
867 dbg() << " [" << NameOfWrappedPass << "]";
868 dbg() << ": " << (HasErrors ? "FAIL" : "PASS") << '\n';
869
870 // Strip debugify metadata if required.
871 bool Ret = false;
872 if (Strip)
874
875 return Ret;
876}
877
878/// ModulePass for attaching synthetic debug info to everything, used with the
879/// legacy module pass manager.
880struct DebugifyModulePass : public ModulePass {
881 bool runOnModule(Module &M) override {
882 bool Result =
883 applyDebugify(M, Mode, DebugInfoBeforePass, NameOfWrappedPass);
884 return Result;
885 }
886
887 DebugifyModulePass(enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
888 StringRef NameOfWrappedPass = "",
889 DebugInfoPerPass *DebugInfoBeforePass = nullptr)
890 : ModulePass(ID), NameOfWrappedPass(NameOfWrappedPass),
891 DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode) {}
892
893 void getAnalysisUsage(AnalysisUsage &AU) const override {
894 AU.setPreservesAll();
895 }
896
897 static char ID; // Pass identification.
898
899private:
900 StringRef NameOfWrappedPass;
901 DebugInfoPerPass *DebugInfoBeforePass;
902 enum DebugifyMode Mode;
903};
904
905/// FunctionPass for attaching synthetic debug info to instructions within a
906/// single function, used with the legacy module pass manager.
907struct DebugifyFunctionPass : public FunctionPass {
908 bool runOnFunction(Function &F) override {
909 bool Result =
910 applyDebugify(F, Mode, DebugInfoBeforePass, NameOfWrappedPass);
911 return Result;
912 }
913
914 DebugifyFunctionPass(
916 StringRef NameOfWrappedPass = "",
917 DebugInfoPerPass *DebugInfoBeforePass = nullptr)
918 : FunctionPass(ID), NameOfWrappedPass(NameOfWrappedPass),
919 DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode) {}
920
921 void getAnalysisUsage(AnalysisUsage &AU) const override {
922 AU.setPreservesAll();
923 }
924
925 static char ID; // Pass identification.
926
927private:
928 StringRef NameOfWrappedPass;
929 DebugInfoPerPass *DebugInfoBeforePass;
930 enum DebugifyMode Mode;
931};
932
933/// ModulePass for checking debug info inserted by -debugify, used with the
934/// legacy module pass manager.
935struct CheckDebugifyModulePass : public ModulePass {
936 bool runOnModule(Module &M) override {
937 bool Result;
939 Result = checkDebugifyMetadata(M, M.functions(), NameOfWrappedPass,
940 "CheckModuleDebugify", Strip, StatsMap);
941 else
943 M, M.functions(), *DebugInfoBeforePass,
944 "CheckModuleDebugify (original debuginfo)", NameOfWrappedPass,
945 OrigDIVerifyBugsReportFilePath);
946
947 return Result;
948 }
949
950 CheckDebugifyModulePass(
951 bool Strip = false, StringRef NameOfWrappedPass = "",
952 DebugifyStatsMap *StatsMap = nullptr,
954 DebugInfoPerPass *DebugInfoBeforePass = nullptr,
955 StringRef OrigDIVerifyBugsReportFilePath = "")
956 : ModulePass(ID), NameOfWrappedPass(NameOfWrappedPass),
957 OrigDIVerifyBugsReportFilePath(OrigDIVerifyBugsReportFilePath),
958 StatsMap(StatsMap), DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode),
959 Strip(Strip) {}
960
961 void getAnalysisUsage(AnalysisUsage &AU) const override {
962 AU.setPreservesAll();
963 }
964
965 static char ID; // Pass identification.
966
967private:
968 StringRef NameOfWrappedPass;
969 StringRef OrigDIVerifyBugsReportFilePath;
970 DebugifyStatsMap *StatsMap;
971 DebugInfoPerPass *DebugInfoBeforePass;
972 enum DebugifyMode Mode;
973 bool Strip;
974};
975
976/// FunctionPass for checking debug info inserted by -debugify-function, used
977/// with the legacy module pass manager.
978struct CheckDebugifyFunctionPass : public FunctionPass {
979 bool runOnFunction(Function &F) override {
980 Module &M = *F.getParent();
981 auto FuncIt = F.getIterator();
982 bool Result;
984 Result = checkDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
985 NameOfWrappedPass, "CheckFunctionDebugify",
986 Strip, StatsMap);
987 else
989 M, make_range(FuncIt, std::next(FuncIt)), *DebugInfoBeforePass,
990 "CheckFunctionDebugify (original debuginfo)", NameOfWrappedPass,
991 OrigDIVerifyBugsReportFilePath);
992
993 return Result;
994 }
995
996 CheckDebugifyFunctionPass(
997 bool Strip = false, StringRef NameOfWrappedPass = "",
998 DebugifyStatsMap *StatsMap = nullptr,
1000 DebugInfoPerPass *DebugInfoBeforePass = nullptr,
1001 StringRef OrigDIVerifyBugsReportFilePath = "")
1002 : FunctionPass(ID), NameOfWrappedPass(NameOfWrappedPass),
1003 OrigDIVerifyBugsReportFilePath(OrigDIVerifyBugsReportFilePath),
1004 StatsMap(StatsMap), DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode),
1005 Strip(Strip) {}
1006
1007 void getAnalysisUsage(AnalysisUsage &AU) const override {
1008 AU.setPreservesAll();
1009 }
1010
1011 static char ID; // Pass identification.
1012
1013private:
1014 StringRef NameOfWrappedPass;
1015 StringRef OrigDIVerifyBugsReportFilePath;
1016 DebugifyStatsMap *StatsMap;
1017 DebugInfoPerPass *DebugInfoBeforePass;
1018 enum DebugifyMode Mode;
1019 bool Strip;
1020};
1021
1022} // end anonymous namespace
1023
1025 std::error_code EC;
1026 raw_fd_ostream OS{Path, EC};
1027 if (EC) {
1028 errs() << "Could not open file: " << EC.message() << ", " << Path << '\n';
1029 return;
1030 }
1031
1032 OS << "Pass Name" << ',' << "# of missing debug values" << ','
1033 << "# of missing locations" << ',' << "Missing/Expected value ratio" << ','
1034 << "Missing/Expected location ratio" << '\n';
1035 for (const auto &Entry : Map) {
1036 StringRef Pass = Entry.first;
1037 DebugifyStatistics Stats = Entry.second;
1038
1039 OS << Pass << ',' << Stats.NumDbgValuesMissing << ','
1040 << Stats.NumDbgLocsMissing << ',' << Stats.getMissingValueRatio() << ','
1041 << Stats.getEmptyLocationRatio() << '\n';
1042 }
1043}
1044
1046 llvm::StringRef NameOfWrappedPass,
1047 DebugInfoPerPass *DebugInfoBeforePass) {
1049 return new DebugifyModulePass();
1050 assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
1051 return new DebugifyModulePass(Mode, NameOfWrappedPass, DebugInfoBeforePass);
1052}
1053
1056 llvm::StringRef NameOfWrappedPass,
1057 DebugInfoPerPass *DebugInfoBeforePass) {
1059 return new DebugifyFunctionPass();
1060 assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
1061 return new DebugifyFunctionPass(Mode, NameOfWrappedPass, DebugInfoBeforePass);
1062}
1063
1066 applyDebugifyMetadata(M, M.functions(),
1067 "ModuleDebugify: ", /*ApplyToMF*/ nullptr);
1068 else
1069 collectDebugInfoMetadata(M, M.functions(), *DebugInfoBeforePass,
1070 "ModuleDebugify (original debuginfo)",
1071 NameOfWrappedPass);
1072
1075 return PA;
1076}
1077
1079 bool Strip, StringRef NameOfWrappedPass, DebugifyStatsMap *StatsMap,
1080 enum DebugifyMode Mode, DebugInfoPerPass *DebugInfoBeforePass,
1081 StringRef OrigDIVerifyBugsReportFilePath) {
1083 return new CheckDebugifyModulePass(Strip, NameOfWrappedPass, StatsMap);
1084 assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
1085 return new CheckDebugifyModulePass(false, NameOfWrappedPass, nullptr, Mode,
1086 DebugInfoBeforePass,
1087 OrigDIVerifyBugsReportFilePath);
1088}
1089
1091 bool Strip, StringRef NameOfWrappedPass, DebugifyStatsMap *StatsMap,
1092 enum DebugifyMode Mode, DebugInfoPerPass *DebugInfoBeforePass,
1093 StringRef OrigDIVerifyBugsReportFilePath) {
1095 return new CheckDebugifyFunctionPass(Strip, NameOfWrappedPass, StatsMap);
1096 assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
1097 return new CheckDebugifyFunctionPass(false, NameOfWrappedPass, nullptr, Mode,
1098 DebugInfoBeforePass,
1099 OrigDIVerifyBugsReportFilePath);
1100}
1101
1105 checkDebugifyMetadata(M, M.functions(), NameOfWrappedPass,
1106 "CheckModuleDebugify", Strip, StatsMap);
1107 else
1109 M, M.functions(), *DebugInfoBeforePass,
1110 "CheckModuleDebugify (original debuginfo)", NameOfWrappedPass,
1111 OrigDIVerifyBugsReportFilePath);
1112
1113 return PreservedAnalyses::all();
1114}
1115
1116static bool isIgnoredPass(StringRef PassID) {
1117 return isSpecialPass(PassID, {"PassManager", "PassAdaptor",
1118 "AnalysisManagerProxy", "PrintFunctionPass",
1119 "PrintModulePass", "BitcodeWriterPass",
1120 "ThinLTOBitcodeWriterPass", "VerifierPass"});
1121}
1122
1125 PIC.registerBeforeNonSkippedPassCallback([this, &MAM](StringRef P, Any IR) {
1126 if (isIgnoredPass(P))
1127 return;
1130 if (const auto **CF = llvm::any_cast<const Function *>(&IR)) {
1131 Function &F = *const_cast<Function *>(*CF);
1132 applyDebugify(F, Mode, DebugInfoBeforePass, P);
1133 MAM.getResult<FunctionAnalysisManagerModuleProxy>(*F.getParent())
1134 .getManager()
1135 .invalidate(F, PA);
1136 } else if (const auto **CM = llvm::any_cast<const Module *>(&IR)) {
1137 Module &M = *const_cast<Module *>(*CM);
1138 applyDebugify(M, Mode, DebugInfoBeforePass, P);
1139 MAM.invalidate(M, PA);
1140 }
1141 });
1142 PIC.registerAfterPassCallback(
1143 [this, &MAM](StringRef P, Any IR, const PreservedAnalyses &PassPA) {
1144 if (isIgnoredPass(P))
1145 return;
1148 if (const auto **CF = llvm::any_cast<const Function *>(&IR)) {
1149 auto &F = *const_cast<Function *>(*CF);
1150 Module &M = *F.getParent();
1151 auto It = F.getIterator();
1153 checkDebugifyMetadata(M, make_range(It, std::next(It)), P,
1154 "CheckFunctionDebugify", /*Strip=*/true,
1155 DIStatsMap);
1156 else
1157 checkDebugInfoMetadata(M, make_range(It, std::next(It)),
1158 *DebugInfoBeforePass,
1159 "CheckModuleDebugify (original debuginfo)",
1160 P, OrigDIVerifyBugsReportFilePath);
1161 MAM.getResult<FunctionAnalysisManagerModuleProxy>(*F.getParent())
1162 .getManager()
1163 .invalidate(F, PA);
1164 } else if (const auto **CM = llvm::any_cast<const Module *>(&IR)) {
1165 Module &M = *const_cast<Module *>(*CM);
1167 checkDebugifyMetadata(M, M.functions(), P, "CheckModuleDebugify",
1168 /*Strip=*/true, DIStatsMap);
1169 else
1170 checkDebugInfoMetadata(M, M.functions(), *DebugInfoBeforePass,
1171 "CheckModuleDebugify (original debuginfo)",
1172 P, OrigDIVerifyBugsReportFilePath);
1173 MAM.invalidate(M, PA);
1174 }
1175 });
1176}
1177
1178char DebugifyModulePass::ID = 0;
1180 "Attach debug info to everything");
1181
1182char CheckDebugifyModulePass::ID = 0;
1184 CDM("check-debugify", "Check debug info from -debugify");
1185
1186char DebugifyFunctionPass::ID = 0;
1187static RegisterPass<DebugifyFunctionPass> DF("debugify-function",
1188 "Attach debug info to a function");
1189
1190char CheckDebugifyFunctionPass::ID = 0;
1192 CDF("check-debugify-function", "Check debug info from -debugify-function");
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
This file implements the BitVector class.
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
static RegisterPass< CheckDebugifyModulePass > CDM("check-debugify", "Check debug info from -debugify")
ModulePass * createDebugifyModulePass(enum DebugifyMode Mode, llvm::StringRef NameOfWrappedPass, DebugInfoPerPass *DebugInfoBeforePass)
bool hasLoc(const Instruction &I)
Definition Debugify.cpp:356
FunctionPass * createDebugifyFunctionPass(enum DebugifyMode Mode, llvm::StringRef NameOfWrappedPass, DebugInfoPerPass *DebugInfoBeforePass)
static bool isIgnoredPass(StringRef PassID)
static bool applyDebugify(Function &F, enum DebugifyMode Mode, DebugInfoPerPass *DebugInfoBeforePass, StringRef NameOfWrappedPass="")
Definition Debugify.cpp:280
ModulePass * createCheckDebugifyModulePass(bool Strip, StringRef NameOfWrappedPass, DebugifyStatsMap *StatsMap, enum DebugifyMode Mode, DebugInfoPerPass *DebugInfoBeforePass, StringRef OrigDIVerifyBugsReportFilePath)
static void writeJSON(StringRef OrigDIVerifyBugsReportFilePath, StringRef FileNameFromCU, StringRef NameOfWrappedPass, llvm::json::Array &Bugs)
Definition Debugify.cpp:584
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
static RegisterPass< DebugifyModulePass > DM("debugify", "Attach debug info to everything")
static bool checkFunctions(const DebugFnMap &DIFunctionsBefore, const DebugFnMap &DIFunctionsAfter, StringRef NameOfWrappedPass, StringRef FileNameFromCU, bool ShouldWriteIntoJSON, llvm::json::Array &Bugs)
Definition Debugify.cpp:443
static RegisterPass< CheckDebugifyFunctionPass > CDF("check-debugify-function", "Check debug info from -debugify-function")
FunctionPass * createCheckDebugifyFunctionPass(bool Strip, StringRef NameOfWrappedPass, DebugifyStatsMap *StatsMap, enum DebugifyMode Mode, DebugInfoPerPass *DebugInfoBeforePass, StringRef OrigDIVerifyBugsReportFilePath)
static bool checkVars(const DebugVarMap &DIVarsBefore, const DebugVarMap &DIVarsAfter, StringRef NameOfWrappedPass, StringRef FileNameFromCU, bool ShouldWriteIntoJSON, llvm::json::Array &Bugs)
Definition Debugify.cpp:551
static bool checkInstructions(const DebugInstMap &DILocsBefore, const DebugInstMap &DILocsAfter, const WeakInstValueMap &InstToDelete, StringRef NameOfWrappedPass, StringRef FileNameFromCU, bool ShouldWriteIntoJSON, llvm::json::Array &Bugs)
Definition Debugify.cpp:485
DebugifyMode
Used to check whether we track synthetic or original debug info.
Definition Debugify.h:98
@ SyntheticDebugInfo
Definition Debugify.h:98
@ OriginalDebugInfo
Definition Debugify.h:98
llvm::MapVector< const llvm::Function *, const llvm::DISubprogram * > DebugFnMap
Definition Debugify.h:29
llvm::MapVector< llvm::StringRef, DebugifyStatistics > DebugifyStatsMap
Map pass names to a per-pass DebugifyStatistics instance.
Definition Debugify.h:152
llvm::MapVector< const llvm::Instruction *, bool > DebugInstMap
Definition Debugify.h:31
llvm::MapVector< const llvm::Instruction *, llvm::WeakVH > WeakInstValueMap
Definition Debugify.h:33
llvm::MapVector< const llvm::DILocalVariable *, unsigned > DebugVarMap
Definition Debugify.h:32
static bool runOnFunction(Function &F, bool PostInlining)
static SmallString< 128 > getFilename(const DIScope *SP, vfs::FileSystem &VFS)
Extract a filename for a DIScope.
Module.h This file contains the declarations for the Module class.
This file supports working with JSON data.
Legalize the Machine IR a function s Machine IR
Definition Legalizer.cpp:80
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
block placement Basic Block Placement Stats
Machine Check Debug Module
#define P(N)
ModuleAnalysisManager MAM
PassInstrumentationCallbacks PIC
This file defines the Pass Instrumentation classes that provide instrumentation points into the pass ...
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static const char PassName[]
LLVM_ABI llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &AM)
LLVM_ABI llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &AM)
void setPreservesAll()
Set by analyses that do not transform their input at all.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
LLVM_ABI const CallInst * getTerminatingDeoptimizeCall() const
Returns the call instruction calling @llvm.experimental.deoptimize prior to the terminating return in...
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
LLVM_ABI const CallInst * getTerminatingMustTailCall() const
Returns the call instruction marked 'musttail' prior to the terminating return instruction of this ba...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
BitVector & reset()
Definition BitVector.h:411
size_type count() const
count - Returns the number of bits which are set.
Definition BitVector.h:181
iterator_range< const_set_bits_iterator > set_bits() const
Definition BitVector.h:159
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
LLVM_ABI void finalize()
Construct any deferred debug info descriptors.
Definition DIBuilder.cpp:62
LLVM_ABI DISubroutineType * createSubroutineType(DITypeRefArray ParameterTypes, DINode::DIFlags Flags=DINode::FlagZero, unsigned CC=0)
Create subroutine type.
LLVM_ABI DISubprogram * createFunction(DIScope *Scope, StringRef Name, StringRef LinkageName, DIFile *File, unsigned LineNo, DISubroutineType *Ty, unsigned ScopeLine, DINode::DIFlags Flags=DINode::FlagZero, DISubprogram::DISPFlags SPFlags=DISubprogram::SPFlagZero, DITemplateParameterArray TParams=nullptr, DISubprogram *Decl=nullptr, DITypeArray ThrownTypes=nullptr, DINodeArray Annotations=nullptr, StringRef TargetFuncName="", bool UseKeyInstructions=false)
Create a new descriptor for the specified subprogram.
LLVM_ABI DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val, DILocalVariable *VarInfo, DIExpression *Expr, const DILocation *DL, InsertPosition InsertPt)
Insert a new llvm.dbg.value intrinsic call.
LLVM_ABI DIBasicType * createBasicType(StringRef Name, uint64_t SizeInBits, unsigned Encoding, DINode::DIFlags Flags=DINode::FlagZero, uint32_t NumExtraInhabitants=0)
Create debugging information entry for a basic type.
LLVM_ABI DITypeRefArray getOrCreateTypeArray(ArrayRef< Metadata * > Elements)
Get a DITypeRefArray, create one if required.
LLVM_ABI DICompileUnit * createCompileUnit(DISourceLanguageName Lang, DIFile *File, StringRef Producer, bool isOptimized, StringRef Flags, unsigned RV, StringRef SplitName=StringRef(), DICompileUnit::DebugEmissionKind Kind=DICompileUnit::DebugEmissionKind::FullDebug, uint64_t DWOId=0, bool SplitDebugInlining=true, bool DebugInfoForProfiling=false, DICompileUnit::DebugNameTableKind NameTableKind=DICompileUnit::DebugNameTableKind::Default, bool RangesBaseAddress=false, StringRef SysRoot={}, StringRef SDK={})
A CompileUnit provides an anchor for all debugging information generated during this instance of comp...
LLVM_ABI DIExpression * createExpression(ArrayRef< uint64_t > Addr={})
Create a new descriptor for the specified variable which has a complex address expression for its add...
LLVM_ABI DILocalVariable * createAutoVariable(DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNo, DIType *Ty, bool AlwaysPreserve=false, DINode::DIFlags Flags=DINode::FlagZero, uint32_t AlignInBits=0)
Create a new descriptor for an auto variable.
LLVM_ABI DIFile * createFile(StringRef Filename, StringRef Directory, std::optional< DIFile::ChecksumInfo< StringRef > > Checksum=std::nullopt, std::optional< StringRef > Source=std::nullopt)
Create a file descriptor to hold debugging information for a file.
Tagged DWARF-like metadata node.
Wrapper structure that holds a language name and its version.
DISPFlags
Debug info subprogram flags.
Base class for types.
Record of a variable value-assignment, aka a non instruction representation of the dbg....
LLVM_ABI DILocation * get() const
Get the underlying DILocation.
Definition DebugLoc.cpp:50
static DebugLoc getUnknown()
Definition DebugLoc.h:162
LLVM_ABI void registerCallbacks(PassInstrumentationCallbacks &PIC, ModuleAnalysisManager &MAM)
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
const Function & getFunction() const
Definition Function.h:164
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition Function.cpp:448
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition Globals.cpp:328
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
const char * getOpcodeName() const
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Metadata node.
Definition Metadata.h:1078
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1442
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1569
size_type count(const KeyT &Key) const
Definition MapVector.h:145
iterator end()
Definition MapVector.h:67
iterator find(const KeyT &Key)
Definition MapVector.h:149
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition MapVector.h:119
size_type size() const
Definition MapVector.h:56
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:255
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
@ Warning
Emits a warning if two values disagree.
Definition Module.h:124
A tuple of MDNodes.
Definition Metadata.h:1757
LLVM_ABI void eraseFromParent()
Drop all references and remove the node from parent module.
LLVM_ABI MDNode * getOperand(unsigned i) const
LLVM_ABI unsigned getNumOperands() const
LLVM_ABI void clearOperands()
Drop all references to this node's operands.
iterator_range< op_iterator > operands()
Definition Metadata.h:1853
LLVM_ABI void addOperand(MDNode *M)
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:297
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
static ConstantAsMetadata * getConstant(Value *C)
Definition Metadata.h:480
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
bool use_empty() const
Definition Value.h:346
A range adaptor for a pair of iterators.
An Array is a JSON array, which contains heterogeneous JSON values.
Definition JSON.h:166
bool empty() const
Definition JSON.h:557
void push_back(const Value &E)
Definition JSON.h:562
An Object is a JSON object, which maps strings to heterogenous JSON values.
Definition JSON.h:98
A Value is an JSON value of unknown type.
Definition JSON.h:290
A raw_ostream that writes to a file descriptor.
void close()
Manually flush the stream and close the file.
Expected< sys::fs::FileLocker > lock()
Locks the underlying file.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
Changed
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:667
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition FileSystem.h:764
@ OF_Append
The file should be opened in append mode.
Definition FileSystem.h:767
This is an optimization pass for GlobalISel generic memory operations.
T any_cast(const Any &Value)
Definition Any.h:137
FormattedString right_justify(StringRef Str, unsigned Width)
right_justify - add spaces before string so total output is Width characters.
Definition Format.h:146
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:644
FunctionAddr VTableAddr uintptr_t uintptr_t Int32Ty
Definition InstrProf.h:296
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
std::string utostr(uint64_t X, bool isNeg=false)
LLVM_ABI bool stripDebugifyMetadata(Module &M)
Strip out all of the metadata and debug info inserted by debugify.
Definition Debugify.cpp:306
LLVM_ABI void exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map)
LLVM_ABI bool applyDebugifyMetadata(Module &M, iterator_range< Module::iterator > Functions, StringRef Banner, std::function< bool(DIBuilder &, Function &)> ApplyToMF)
Add synthesized debug information to a module.
LLVM_ABI bool collectDebugInfoMetadata(Module &M, iterator_range< Module::iterator > Functions, DebugInfoPerPass &DebugInfoBeforePass, StringRef Banner, StringRef NameOfWrappedPass)
Collect original debug information before a pass.
Definition Debugify.cpp:366
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
LLVM_ABI bool checkDebugInfoMetadata(Module &M, iterator_range< Module::iterator > Functions, DebugInfoPerPass &DebugInfoBeforePass, StringRef Banner, StringRef NameOfWrappedPass, StringRef OrigDIVerifyBugsReportFilePath)
Check original debug information after a pass.
Definition Debugify.cpp:611
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI bool isSpecialPass(StringRef PassID, const std::vector< StringRef > &Specials)
LLVM_ABI raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ABI bool StripDebugInfo(Module &M)
Strip debug info in the module if it exists.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:560
bool to_integer(StringRef S, N &Num, unsigned Base=0)
Convert the string S to an integer of the specified type using the radix Base. If Base is 0,...
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
@ DEBUG_METADATA_VERSION
Definition Metadata.h:54
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
#define N
Used to track the Debug Info Metadata information.
Definition Debugify.h:37
DebugInstMap DILocations
Definition Debugify.h:41
DebugFnMap DIFunctions
Definition Debugify.h:39
DebugVarMap DIVariables
Definition Debugify.h:46
WeakInstValueMap InstToDelete
Definition Debugify.h:44
Track how much debugify information (in the synthetic mode only) has been lost.
Definition Debugify.h:127
RegisterPass<t> template - This template class is used to notify the system that a Pass is available ...
Definition PassSupport.h:89