llvm.org GIT mirror llvm / 32791b0
verify-di: Implement DebugInfoVerifier Implement DebugInfoVerifier, which steals verification relying on DebugInfoFinder from Verifier. - Adds LegacyDebugInfoVerifierPassPass, a ModulePass which wraps DebugInfoVerifier. Uses -verify-di command-line flag. - Change verifyModule() to invoke DebugInfoVerifier as well as Verifier. - Add a call to createDebugInfoVerifierPass() wherever there was a call to createVerifierPass(). This implementation as a module pass should sidestep efficiency issues, allowing us to turn debug info verification back on. <rdar://problem/15500563> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206300 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 5 years ago
10 changed file(s) with 139 addition(s) and 55 deletion(s). Raw diff Collapse all Expand all
2727
2828 class Function;
2929 class FunctionPass;
30 class ModulePass;
3031 class Module;
3132 class PreservedAnalyses;
3233 class raw_ostream;
5758 /// Note that this creates a pass suitable for the legacy pass manager. It has nothing to do with \c VerifierPass.
5859 FunctionPass *createVerifierPass(bool FatalErrors = true);
5960
61 /// \brief Create a debug-info verifier pass.
62 ///
63 /// Check a module for validity of debug info. This is essentially a pass
64 /// wrapped around the debug-info parts of \a verifyModule(). When the pass
65 /// detects a verification error it is always printed to stderr, and by default
66 /// they are fatal. You can override that by passing \c false to \p
67 /// FatalErrors.
68 ///
69 /// Note that this creates a pass suitable for the legacy pass manager. It has
70 /// nothing to do with \c VerifierPass.
71 ModulePass *createDebugInfoVerifierPass(bool FatalErrors = true);
72
6073 class VerifierPass {
6174 bool FatalErrors;
6275
102102 void initializeDCEPass(PassRegistry&);
103103 void initializeDSEPass(PassRegistry&);
104104 void initializeDebugIRPass(PassRegistry&);
105 void initializeDebugInfoVerifierLegacyPassPass(PassRegistry &);
105106 void initializeDeadInstEliminationPass(PassRegistry&);
106107 void initializeDeadMachineInstructionElimPass(PassRegistry&);
107108 void initializeDelinearizationPass(PassRegistry &);
383383
384384 // Before running any passes, run the verifier to determine if the input
385385 // coming from the front-end and/or optimizer is valid.
386 if (!DisableVerify)
386 if (!DisableVerify) {
387387 addPass(createVerifierPass());
388 addPass(createDebugInfoVerifierPass());
389 }
388390
389391 // Run loop strength reduction before anything else.
390392 if (getOptLevel() != CodeGenOpt::None && !DisableLSR) {
442444 void TargetPassConfig::addISelPrepare() {
443445 addPreISel();
444446
447 // Need to verify DebugInfo *before* creating the stack protector analysis.
448 // It's a function pass, and verifying between it and its users causes a
449 // crash.
450 if (!DisableVerify)
451 addPass(createDebugInfoVerifierPass());
452
445453 addPass(createStackProtectorPass(TM));
446454
447455 if (PrintISelInput)
6060 #include "llvm/IR/DerivedTypes.h"
6161 #include "llvm/IR/Dominators.h"
6262 #include "llvm/IR/InlineAsm.h"
63 #include "llvm/IR/InstIterator.h"
6364 #include "llvm/IR/InstVisitor.h"
6465 #include "llvm/IR/IntrinsicInst.h"
6566 #include "llvm/IR/LLVMContext.h"
159160 /// personality function.
160161 const Value *PersonalityFn;
161162
162 /// \brief Finder keeps track of all debug info MDNodes in a Module.
163 DebugInfoFinder Finder;
164
165163 public:
166164 explicit Verifier(raw_ostream &OS = dbgs())
167165 : VerifierSupport(OS), Context(nullptr), DL(nullptr),
195193 // FIXME: It's really gross that we have to cast away constness here.
196194 DT.recalculate(const_cast(F));
197195
198 Finder.reset();
199196 Broken = false;
200197 // FIXME: We strip const here because the inst visitor strips const.
201198 visit(const_cast(F));
202199 InstsInThisBlock.clear();
203200 PersonalityFn = nullptr;
204201
205 if (VerifyDebugInfo)
206 // Verify Debug Info.
207 verifyDebugInfo();
208
209202 return !Broken;
210203 }
211204
212205 bool verify(const Module &M) {
213206 this->M = &M;
214207 Context = &M.getContext();
215 Finder.reset();
216208 Broken = false;
217209
218210 // Scan through, checking all of the external function's linkage now...
239231
240232 visitModuleFlags(M);
241233 visitModuleIdents(M);
242
243 if (VerifyDebugInfo) {
244 Finder.reset();
245 Finder.processModule(M);
246 // Verify Debug Info.
247 verifyDebugInfo();
248 }
249234
250235 return !Broken;
251236 }
331316
332317 void VerifyBitcastType(const Value *V, Type *DestTy, Type *SrcTy);
333318 void VerifyConstantExprBitcastType(const ConstantExpr *CE);
334
319 };
320 class DebugInfoVerifier : public VerifierSupport {
321 public:
322 explicit DebugInfoVerifier(raw_ostream &OS = dbgs()) : VerifierSupport(OS) {}
323
324 bool verify(const Module &M) {
325 this->M = &M;
326 verifyDebugInfo();
327 return !Broken;
328 }
329
330 private:
335331 void verifyDebugInfo();
332 void processInstructions(DebugInfoFinder &Finder);
333 void processCallInst(DebugInfoFinder &Finder, const CallInst &CI);
336334 };
337335 } // End anonymous namespace
338336
21082106 MDNode *MD = I.getMetadata(LLVMContext::MD_range);
21092107 Assert1(!MD || isa(I), "Ranges are only for loads!", &I);
21102108
2111 if (VerifyDebugInfo) {
2112 MD = I.getMetadata(LLVMContext::MD_dbg);
2113 Finder.processLocation(*M, DILocation(MD));
2114 }
2115
21162109 InstsInThisBlock.insert(&I);
21172110 }
21182111
23122305 MDNode *MD = cast(CI.getArgOperand(0));
23132306 Assert1(MD->getNumOperands() == 1,
23142307 "invalid llvm.dbg.declare intrinsic call 2", &CI);
2315 if (VerifyDebugInfo)
2316 Finder.processDeclare(*M, cast(&CI));
23172308 } break;
2318 case Intrinsic::dbg_value: { //llvm.dbg.value
2319 if (VerifyDebugInfo) {
2320 Assert1(CI.getArgOperand(0) && isa(CI.getArgOperand(0)),
2321 "invalid llvm.dbg.value intrinsic call 1", &CI);
2322 Finder.processValue(*M, cast(&CI));
2323 }
2324 break;
2325 }
23262309 case Intrinsic::memcpy:
23272310 case Intrinsic::memmove:
23282311 case Intrinsic::memset:
23842367 }
23852368 }
23862369
2387 void Verifier::verifyDebugInfo() {
2370 void DebugInfoVerifier::verifyDebugInfo() {
2371 if (!VerifyDebugInfo)
2372 return;
2373
2374 DebugInfoFinder Finder;
2375 Finder.processModule(*M);
2376 processInstructions(Finder);
2377
23882378 // Verify Debug Info.
2389 if (VerifyDebugInfo) {
2390 for (DICompileUnit CU : Finder.compile_units()) {
2391 Assert1(CU.Verify(), "DICompileUnit does not Verify!", CU);
2392 }
2393 for (DISubprogram S : Finder.subprograms()) {
2394 Assert1(S.Verify(), "DISubprogram does not Verify!", S);
2395 }
2396 for (DIGlobalVariable GV : Finder.global_variables()) {
2397 Assert1(GV.Verify(), "DIGlobalVariable does not Verify!", GV);
2398 }
2399 for (DIType T : Finder.types()) {
2400 Assert1(T.Verify(), "DIType does not Verify!", T);
2401 }
2402 for (DIScope S : Finder.scopes()) {
2403 Assert1(S.Verify(), "DIScope does not Verify!", S);
2404 }
2405 }
2379 for (DICompileUnit CU : Finder.compile_units())
2380 Assert1(CU.Verify(), "DICompileUnit does not Verify!", CU);
2381 for (DISubprogram S : Finder.subprograms())
2382 Assert1(S.Verify(), "DISubprogram does not Verify!", S);
2383 for (DIGlobalVariable GV : Finder.global_variables())
2384 Assert1(GV.Verify(), "DIGlobalVariable does not Verify!", GV);
2385 for (DIType T : Finder.types())
2386 Assert1(T.Verify(), "DIType does not Verify!", T);
2387 for (DIScope S : Finder.scopes())
2388 Assert1(S.Verify(), "DIScope does not Verify!", S);
2389 }
2390
2391 void DebugInfoVerifier::processInstructions(DebugInfoFinder &Finder) {
2392 for (const Function &F : *M)
2393 for (auto I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
2394 if (MDNode *MD = I->getMetadata(LLVMContext::MD_dbg))
2395 Finder.processLocation(*M, DILocation(MD));
2396 if (const CallInst *CI = dyn_cast(&*I))
2397 processCallInst(Finder, *CI);
2398 }
2399 }
2400
2401 void DebugInfoVerifier::processCallInst(DebugInfoFinder &Finder,
2402 const CallInst &CI) {
2403 if (Function *F = CI.getCalledFunction())
2404 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
2405 switch (ID) {
2406 case Intrinsic::dbg_declare:
2407 Finder.processDeclare(*M, cast(&CI));
2408 break;
2409 case Intrinsic::dbg_value:
2410 Finder.processValue(*M, cast(&CI));
2411 break;
2412 default:
2413 break;
2414 }
24062415 }
24072416
24082417 //===----------------------------------------------------------------------===//
24322441
24332442 // Note that this function's return value is inverted from what you would
24342443 // expect of a function called "verify".
2435 return !V.verify(M) || Broken;
2444 DebugInfoVerifier DIV(OS ? *OS : NullStr);
2445 return !V.verify(M) || !DIV.verify(M) || Broken;
24362446 }
24372447
24382448 namespace {
24682478 AU.setPreservesAll();
24692479 }
24702480 };
2481 struct DebugInfoVerifierLegacyPass : public ModulePass {
2482 static char ID;
2483
2484 DebugInfoVerifier V;
2485 bool FatalErrors;
2486
2487 DebugInfoVerifierLegacyPass() : ModulePass(ID), FatalErrors(true) {
2488 initializeDebugInfoVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
2489 }
2490 explicit DebugInfoVerifierLegacyPass(bool FatalErrors)
2491 : ModulePass(ID), V(dbgs()), FatalErrors(FatalErrors) {
2492 initializeDebugInfoVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
2493 }
2494
2495 bool runOnModule(Module &M) override {
2496 if (!V.verify(M) && FatalErrors)
2497 report_fatal_error("Broken debug info found, compilation aborted!");
2498
2499 return false;
2500 }
2501
2502 void getAnalysisUsage(AnalysisUsage &AU) const override {
2503 AU.setPreservesAll();
2504 }
2505 };
24712506 }
24722507
24732508 char VerifierLegacyPass::ID = 0;
24742509 INITIALIZE_PASS(VerifierLegacyPass, "verify", "Module Verifier", false, false)
24752510
2511 char DebugInfoVerifierLegacyPass::ID = 0;
2512 INITIALIZE_PASS(DebugInfoVerifierLegacyPass, "verify-di", "Debug Info Verifier",
2513 false, false)
2514
24762515 FunctionPass *llvm::createVerifierPass(bool FatalErrors) {
24772516 return new VerifierLegacyPass(FatalErrors);
2517 }
2518
2519 ModulePass *llvm::createDebugInfoVerifierPass(bool FatalErrors) {
2520 return new DebugInfoVerifierLegacyPass(FatalErrors);
24782521 }
24792522
24802523 PreservedAnalyses VerifierPass::run(Module *M) {
398398 // Start off with a verification pass.
399399 PassManager passes;
400400 passes.add(createVerifierPass());
401 passes.add(createDebugInfoVerifierPass());
401402
402403 // mark which symbols can not be internalized
403404 Mangler Mangler(TargetMach->getDataLayout());
470471
471472 // Start off with a verification pass.
472473 passes.add(createVerifierPass());
474 passes.add(createDebugInfoVerifierPass());
473475
474476 // Add an appropriate DataLayout instance for this module...
475477 mergedModule->setDataLayout(TargetMach->getDataLayout());
491493
492494 // Make sure everything is still good.
493495 passes.add(createVerifierPass());
496 passes.add(createDebugInfoVerifierPass());
494497
495498 PassManager codeGenPasses;
496499
180180
181181 void LLVMAddVerifierPass(LLVMPassManagerRef PM) {
182182 unwrap(PM)->add(createVerifierPass());
183 // FIXME: should this also add createDebugInfoVerifierPass()?
183184 }
184185
185186 void LLVMAddCorrelatedValuePropagationPass(LLVMPassManagerRef PM) {
409409 // Verify that this is still valid.
410410 PassManager Passes;
411411 Passes.add(createVerifierPass());
412 Passes.add(createDebugInfoVerifierPass());
412413 Passes.run(*M);
413414
414415 // Try running on the hacked up program...
712712
713713 PassManager Passes;
714714 Passes.add(createVerifierPass());
715 Passes.add(createDebugInfoVerifierPass());
715716 Passes.add(createPrintModulePass(Out->os()));
716717 Passes.run(*M.get());
717718 Out->keep();
190190 PM.add(P);
191191
192192 // If we are verifying all of the intermediate steps, add the verifier...
193 if (VerifyEach) PM.add(createVerifierPass());
193 if (VerifyEach) {
194 PM.add(createVerifierPass());
195 PM.add(createDebugInfoVerifierPass());
196 }
194197 }
195198
196199 /// AddOptimizationPasses - This routine adds optimization passes
200203 /// OptLevel - Optimization Level
201204 static void AddOptimizationPasses(PassManagerBase &MPM,FunctionPassManager &FPM,
202205 unsigned OptLevel, unsigned SizeLevel) {
203 FPM.add(createVerifierPass()); // Verify that input is correct
206 FPM.add(createVerifierPass()); // Verify that input is correct
207 MPM.add(createDebugInfoVerifierPass()); // Verify that debug info is correct
204208
205209 PassManagerBuilder Builder;
206210 Builder.OptLevel = OptLevel;
239243 if (StripDebug)
240244 addPass(PM, createStripSymbolsPass(true));
241245
246 // Verify debug info only after it's (possibly) stripped.
247 PM.add(createDebugInfoVerifierPass());
248
242249 if (DisableOptimizations) return;
243250
244251 // -std-compile-opts adds the same module passes as -O3.
255262 // If the -strip-debug command line option was specified, do it.
256263 if (StripDebug)
257264 addPass(PM, createStripSymbolsPass(true));
265
266 // Verify debug info only after it's (possibly) stripped.
267 PM.add(createDebugInfoVerifierPass());
258268
259269 if (DisableOptimizations) return;
260270
599609 }
600610
601611 // Check that the module is well formed on completion of optimization
602 if (!NoVerify && !VerifyEach)
612 if (!NoVerify && !VerifyEach) {
603613 Passes.add(createVerifierPass());
614 Passes.add(createDebugInfoVerifierPass());
615 }
604616
605617 // Write bitcode or assembly to the output as the last step...
606618 if (!NoOutput && !AnalyzeOnly) {
5858 std::unique_ptr m(ModuleOrErr.get());
5959 PassManager passes;
6060 passes.add(createVerifierPass());
61 passes.add(createDebugInfoVerifierPass());
6162 passes.run(*m);
6263 }
6364