| 1 | //===- MachineCheckDebugify.cpp - Check debug info ------------------------===// |
| 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 This checks debug info after mir-debugify (+ pass-to-test). Currently |
| 10 | /// it simply checks the integrity of line info in DILocation and |
| 11 | /// DILocalVariable which mir-debugifiy generated before. |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/MachineCheckDebugify.h" |
| 15 | #include "llvm/ADT/StringExtras.h" |
| 16 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 17 | #include "llvm/CodeGen/MachineFunction.h" |
| 18 | #include "llvm/CodeGen/MachineFunctionAnalysis.h" |
| 19 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 20 | #include "llvm/CodeGen/Passes.h" |
| 21 | #include "llvm/IR/Constants.h" |
| 22 | #include "llvm/IR/DebugInfoMetadata.h" |
| 23 | #include "llvm/IR/Module.h" |
| 24 | #include "llvm/InitializePasses.h" |
| 25 | #include "llvm/Pass.h" |
| 26 | |
| 27 | #define DEBUG_TYPE "mir-check-debugify" |
| 28 | |
| 29 | using namespace llvm; |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | bool checkDebugMachineModuleImpl( |
| 34 | Module &M, llvm::function_ref<MachineFunction *(Function &)> GetMF) { |
| 35 | NamedMDNode *NMD = M.getNamedMetadata(Name: "llvm.mir.debugify" ); |
| 36 | if (!NMD) { |
| 37 | errs() << "WARNING: Please run mir-debugify to generate " |
| 38 | "llvm.mir.debugify metadata first.\n" ; |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | auto getDebugifyOperand = [&](unsigned Idx) -> unsigned { |
| 43 | return mdconst::extract<ConstantInt>(MD: NMD->getOperand(i: Idx)->getOperand(I: 0)) |
| 44 | ->getZExtValue(); |
| 45 | }; |
| 46 | assert(NMD->getNumOperands() == 2 && |
| 47 | "llvm.mir.debugify should have exactly 2 operands!" ); |
| 48 | unsigned NumLines = getDebugifyOperand(0); |
| 49 | unsigned NumVars = getDebugifyOperand(1); |
| 50 | BitVector MissingLines{NumLines, true}; |
| 51 | BitVector MissingVars{NumVars, true}; |
| 52 | |
| 53 | for (Function &F : M.functions()) { |
| 54 | MachineFunction *MF = GetMF(F); |
| 55 | if (!MF) |
| 56 | continue; |
| 57 | for (MachineBasicBlock &MBB : *MF) { |
| 58 | // Find missing lines. |
| 59 | // TODO: Avoid meta instructions other than dbg_val. |
| 60 | for (MachineInstr &MI : MBB) { |
| 61 | if (MI.isDebugValue()) |
| 62 | continue; |
| 63 | const DebugLoc DL = MI.getDebugLoc(); |
| 64 | if (DL && DL.getLine() != 0) { |
| 65 | MissingLines.reset(Idx: DL.getLine() - 1); |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | if (!DL) { |
| 70 | errs() << "WARNING: Instruction with empty DebugLoc in function " ; |
| 71 | errs() << F.getName() << " --" ; |
| 72 | MI.print(OS&: errs()); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Find missing variables. |
| 77 | // TODO: Handle DBG_INSTR_REF which is under an experimental option now. |
| 78 | for (MachineInstr &MI : MBB) { |
| 79 | if (!MI.isDebugValue()) |
| 80 | continue; |
| 81 | const DILocalVariable *LocalVar = MI.getDebugVariable(); |
| 82 | unsigned Var = ~0U; |
| 83 | |
| 84 | (void)to_integer(S: LocalVar->getName(), Num&: Var, Base: 10); |
| 85 | assert(Var <= NumVars && "Unexpected name for DILocalVariable" ); |
| 86 | MissingVars.reset(Idx: Var - 1); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | bool Fail = false; |
| 92 | for (unsigned Idx : MissingLines.set_bits()) { |
| 93 | errs() << "WARNING: Missing line " << Idx + 1 << "\n" ; |
| 94 | Fail = true; |
| 95 | } |
| 96 | |
| 97 | for (unsigned Idx : MissingVars.set_bits()) { |
| 98 | errs() << "WARNING: Missing variable " << Idx + 1 << "\n" ; |
| 99 | Fail = true; |
| 100 | } |
| 101 | errs() << "Machine IR debug info check: " ; |
| 102 | errs() << (Fail ? "FAIL" : "PASS" ) << "\n" ; |
| 103 | |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | struct CheckDebugMachineModuleLegacy : public ModulePass { |
| 108 | bool runOnModule(Module &M) override { |
| 109 | MachineModuleInfo &MMI = |
| 110 | getAnalysis<MachineModuleInfoWrapperPass>().getMMI(); |
| 111 | return checkDebugMachineModuleImpl( |
| 112 | M, GetMF: [&MMI](Function &F) -> MachineFunction * { |
| 113 | return MMI.getMachineFunction(F); |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | CheckDebugMachineModuleLegacy() : ModulePass(ID) {} |
| 118 | |
| 119 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 120 | AU.addRequired<MachineModuleInfoWrapperPass>(); |
| 121 | AU.setPreservesAll(); |
| 122 | } |
| 123 | |
| 124 | static char ID; // Pass identification. |
| 125 | }; |
| 126 | char CheckDebugMachineModuleLegacy::ID = 0; |
| 127 | |
| 128 | } // end anonymous namespace |
| 129 | |
| 130 | INITIALIZE_PASS_BEGIN(CheckDebugMachineModuleLegacy, DEBUG_TYPE, |
| 131 | "Machine Check Debug Module" , false, false) |
| 132 | INITIALIZE_PASS_END(CheckDebugMachineModuleLegacy, DEBUG_TYPE, |
| 133 | "Machine Check Debug Module" , false, false) |
| 134 | |
| 135 | ModulePass *llvm::createCheckDebugMachineModuleLegacyPass() { |
| 136 | return new CheckDebugMachineModuleLegacy(); |
| 137 | } |
| 138 | |
| 139 | PreservedAnalyses CheckDebugMachineModulePass::run(Module &M, |
| 140 | ModuleAnalysisManager &AM) { |
| 141 | FunctionAnalysisManager &FAM = |
| 142 | AM.getResult<FunctionAnalysisManagerModuleProxy>(IR&: M).getManager(); |
| 143 | checkDebugMachineModuleImpl(M, GetMF: [&FAM](Function &F) -> MachineFunction * { |
| 144 | MachineFunctionAnalysis::Result *MFA = |
| 145 | FAM.getCachedResult<MachineFunctionAnalysis>(IR&: F); |
| 146 | return MFA ? &MFA->getMF() : nullptr; |
| 147 | }); |
| 148 | return PreservedAnalyses::all(); |
| 149 | } |
| 150 | |