LLVM 19.0.0git
MachineCheckDebugify.cpp
Go to the documentation of this file.
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
18#include "llvm/CodeGen/Passes.h"
19#include "llvm/IR/Constants.h"
22#include "llvm/Pass.h"
23
24#define DEBUG_TYPE "mir-check-debugify"
25
26using namespace llvm;
27
28namespace {
29
30struct CheckDebugMachineModule : public ModulePass {
31 bool runOnModule(Module &M) override {
32 NamedMDNode *NMD = M.getNamedMetadata("llvm.mir.debugify");
33 if (!NMD) {
34 errs() << "WARNING: Please run mir-debugify to generate "
35 "llvm.mir.debugify metadata first.\n";
36 return false;
37 }
38
40 getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
41
42 auto getDebugifyOperand = [&](unsigned Idx) -> unsigned {
43 return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(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()) {
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(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(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(LocalVar->getName(), Var, 10);
85 assert(Var <= NumVars && "Unexpected name for DILocalVariable");
86 MissingVars.reset(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 CheckDebugMachineModule() : ModulePass(ID) {}
108
109 void getAnalysisUsage(AnalysisUsage &AU) const override {
111 AU.setPreservesAll();
112 }
113
114 static char ID; // Pass identification.
115};
116char CheckDebugMachineModule::ID = 0;
117
118} // end anonymous namespace
119
120INITIALIZE_PASS_BEGIN(CheckDebugMachineModule, DEBUG_TYPE,
121 "Machine Check Debug Module", false, false)
122INITIALIZE_PASS_END(CheckDebugMachineModule, DEBUG_TYPE,
124
126 return new CheckDebugMachineModule();
127}
#define Fail
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:371
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define Check(C,...)
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
#define DEBUG_TYPE
bool Debug
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some functions that are useful when dealing with strings.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
A debug info location.
Definition: DebugLoc.h:33
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1428
Representation of each machine instruction.
Definition: MachineInstr.h:69
This class contains meta information specific to a module.
MachineFunction * getMachineFunction(const Function &F) const
Returns the MachineFunction associated to IR function F if there is one, otherwise nullptr.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A tuple of MDNodes.
Definition: Metadata.h:1729
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:1379
unsigned getNumOperands() const
Definition: Metadata.cpp:1375
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:130
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
ModulePass * createCheckDebugMachineModulePass()
Creates MIR Check Debug pass.