LLVM 22.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"
21#include "llvm/IR/Module.h"
23#include "llvm/Pass.h"
24
25#define DEBUG_TYPE "mir-check-debugify"
26
27using namespace llvm;
28
29namespace {
30
31struct CheckDebugMachineModule : public ModulePass {
32 bool runOnModule(Module &M) override {
33 NamedMDNode *NMD = M.getNamedMetadata("llvm.mir.debugify");
34 if (!NMD) {
35 errs() << "WARNING: Please run mir-debugify to generate "
36 "llvm.mir.debugify metadata first.\n";
37 return false;
38 }
39
41 getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
42
43 auto getDebugifyOperand = [&](unsigned Idx) -> unsigned {
45 ->getZExtValue();
46 };
47 assert(NMD->getNumOperands() == 2 &&
48 "llvm.mir.debugify should have exactly 2 operands!");
49 unsigned NumLines = getDebugifyOperand(0);
50 unsigned NumVars = getDebugifyOperand(1);
51 BitVector MissingLines{NumLines, true};
52 BitVector MissingVars{NumVars, true};
53
54 for (Function &F : M.functions()) {
56 if (!MF)
57 continue;
58 for (MachineBasicBlock &MBB : *MF) {
59 // Find missing lines.
60 // TODO: Avoid meta instructions other than dbg_val.
61 for (MachineInstr &MI : MBB) {
62 if (MI.isDebugValue())
63 continue;
64 const DebugLoc DL = MI.getDebugLoc();
65 if (DL && DL.getLine() != 0) {
66 MissingLines.reset(DL.getLine() - 1);
67 continue;
68 }
69
70 if (!DL) {
71 errs() << "WARNING: Instruction with empty DebugLoc in function ";
72 errs() << F.getName() << " --";
73 MI.print(errs());
74 }
75 }
76
77 // Find missing variables.
78 // TODO: Handle DBG_INSTR_REF which is under an experimental option now.
79 for (MachineInstr &MI : MBB) {
80 if (!MI.isDebugValue())
81 continue;
82 const DILocalVariable *LocalVar = MI.getDebugVariable();
83 unsigned Var = ~0U;
84
85 (void)to_integer(LocalVar->getName(), Var, 10);
86 assert(Var <= NumVars && "Unexpected name for DILocalVariable");
87 MissingVars.reset(Var - 1);
88 }
89 }
90 }
91
92 bool Fail = false;
93 for (unsigned Idx : MissingLines.set_bits()) {
94 errs() << "WARNING: Missing line " << Idx + 1 << "\n";
95 Fail = true;
96 }
97
98 for (unsigned Idx : MissingVars.set_bits()) {
99 errs() << "WARNING: Missing variable " << Idx + 1 << "\n";
100 Fail = true;
101 }
102 errs() << "Machine IR debug info check: ";
103 errs() << (Fail ? "FAIL" : "PASS") << "\n";
104
105 return false;
106 }
107
108 CheckDebugMachineModule() : ModulePass(ID) {}
109
110 void getAnalysisUsage(AnalysisUsage &AU) const override {
112 AU.setPreservesAll();
113 }
114
115 static char ID; // Pass identification.
116};
117char CheckDebugMachineModule::ID = 0;
118
119} // end anonymous namespace
120
121INITIALIZE_PASS_BEGIN(CheckDebugMachineModule, DEBUG_TYPE,
122 "Machine Check Debug Module", false, false)
123INITIALIZE_PASS_END(CheckDebugMachineModule, DEBUG_TYPE,
124 "Machine Check Debug Module", false, false)
125
127 return new CheckDebugMachineModule();
128}
#define Fail
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define DEBUG_TYPE
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
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.
BitVector & reset()
Definition BitVector.h:392
iterator_range< const_set_bits_iterator > set_bits() const
Definition BitVector.h:140
A debug info location.
Definition DebugLoc.h:124
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1445
Representation of each machine instruction.
This class contains meta information specific to a module.
LLVM_ABI 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:255
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A tuple of MDNodes.
Definition Metadata.h:1753
LLVM_ABI MDNode * getOperand(unsigned i) const
LLVM_ABI unsigned getNumOperands() const
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:666
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ABI ModulePass * createCheckDebugMachineModulePass()
Creates MIR Check Debug pass.
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,...