1//===-- VerifierInternal.h - Internal verifier infrastructure --------------==//
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// Shared definitions used by the verifier implementation files.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_IR_VERIFIERINTERNAL_H
14#define LLVM_LIB_IR_VERIFIERINTERNAL_H
15
16#include "llvm/ADT/Twine.h"
17#include "llvm/IR/Attributes.h"
18#include "llvm/IR/DebugInfoMetadata.h"
19#include "llvm/IR/Instructions.h"
20#include "llvm/IR/Metadata.h"
21#include "llvm/IR/Module.h"
22#include "llvm/IR/ModuleSlotTracker.h"
23#include "llvm/IR/Value.h"
24#include "llvm/Support/Casting.h"
25#include "llvm/Support/Printable.h"
26#include "llvm/Support/raw_ostream.h"
27#include "llvm/TargetParser/Triple.h"
28
29namespace llvm {
30
31struct VerifierSupport {
32 raw_ostream *OS;
33 const Module &M;
34 ModuleSlotTracker MST;
35 const Triple &TT;
36 const DataLayout &DL;
37 LLVMContext &Context;
38
39 /// Track the brokenness of the module while recursively visiting.
40 bool Broken = false;
41 /// Broken debug info can be "recovered" from by stripping the debug info.
42 bool BrokenDebugInfo = false;
43 /// Whether to treat broken debug info as an error.
44 bool TreatBrokenDebugInfoAsError = true;
45
46 explicit VerifierSupport(raw_ostream *OS, const Module &M)
47 : OS(OS), M(M), MST(&M), TT(M.getTargetTriple()), DL(M.getDataLayout()),
48 Context(M.getContext()) {}
49
50private:
51 void Write(const Module *M) {
52 *OS << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
53 }
54
55 void Write(const Value *V) {
56 if (V)
57 Write(V: *V);
58 }
59
60 void Write(const Value &V) {
61 if (isa<Instruction>(Val: V)) {
62 V.print(O&: *OS, MST);
63 *OS << '\n';
64 } else {
65 V.printAsOperand(O&: *OS, PrintType: true, MST);
66 *OS << '\n';
67 }
68 }
69
70 void Write(const DbgRecord *DR) {
71 if (DR) {
72 DR->print(O&: *OS, MST, IsForDebug: false);
73 *OS << '\n';
74 }
75 }
76
77 void Write(DbgVariableRecord::LocationType Type) {
78 switch (Type) {
79 case DbgVariableRecord::LocationType::Value:
80 *OS << "value";
81 break;
82 case DbgVariableRecord::LocationType::Declare:
83 *OS << "declare";
84 break;
85 case DbgVariableRecord::LocationType::DeclareValue:
86 *OS << "declare_value";
87 break;
88 case DbgVariableRecord::LocationType::Assign:
89 *OS << "assign";
90 break;
91 case DbgVariableRecord::LocationType::End:
92 *OS << "end";
93 break;
94 case DbgVariableRecord::LocationType::Any:
95 *OS << "any";
96 break;
97 };
98 }
99
100 void Write(const Metadata *MD) {
101 if (!MD)
102 return;
103 MD->print(OS&: *OS, MST, M: &M);
104 *OS << '\n';
105 }
106
107 template <class T> void Write(const MDTupleTypedArrayWrapper<T> &MD) {
108 Write(MD.get());
109 }
110
111 void Write(const NamedMDNode *NMD) {
112 if (!NMD)
113 return;
114 NMD->print(ROS&: *OS, MST);
115 *OS << '\n';
116 }
117
118 void Write(Type *T) {
119 if (!T)
120 return;
121 *OS << ' ' << *T;
122 }
123
124 void Write(const Comdat *C) {
125 if (!C)
126 return;
127 *OS << *C;
128 }
129
130 void Write(const APInt *AI) {
131 if (!AI)
132 return;
133 *OS << *AI << '\n';
134 }
135
136 void Write(const unsigned i) { *OS << i << '\n'; }
137
138 // NOLINTNEXTLINE(readability-identifier-naming)
139 void Write(const Attribute *A) {
140 if (!A)
141 return;
142 *OS << A->getAsString() << '\n';
143 }
144
145 // NOLINTNEXTLINE(readability-identifier-naming)
146 void Write(const AttributeSet *AS) {
147 if (!AS)
148 return;
149 *OS << AS->getAsString() << '\n';
150 }
151
152 // NOLINTNEXTLINE(readability-identifier-naming)
153 void Write(const AttributeList *AL) {
154 if (!AL)
155 return;
156 AL->print(O&: *OS);
157 }
158
159 void Write(Printable P) { *OS << P; }
160
161 template <typename T> void Write(ArrayRef<T> Vs) {
162 for (const T &V : Vs)
163 Write(V);
164 }
165
166 template <typename T1, typename... Ts>
167 void WriteTs(const T1 &V1, const Ts &...Vs) {
168 Write(V1);
169 WriteTs(Vs...);
170 }
171
172 template <typename... Ts> void WriteTs() {}
173
174public:
175 /// A check failed, so printout out the condition and the message.
176 ///
177 /// This provides a nice place to put a breakpoint if you want to see why
178 /// something is not correct.
179 void CheckFailed(const Twine &Message) {
180 if (OS)
181 *OS << Message << '\n';
182 Broken = true;
183 }
184
185 /// A check failed (with values to print).
186 ///
187 /// This calls the Message-only version so that the above is easier to set a
188 /// breakpoint on.
189 template <typename T1, typename... Ts>
190 void CheckFailed(const Twine &Message, const T1 &V1, const Ts &...Vs) {
191 CheckFailed(Message);
192 if (OS)
193 WriteTs(V1, Vs...);
194 }
195
196 /// A debug info check failed.
197 void DebugInfoCheckFailed(const Twine &Message) {
198 if (OS)
199 *OS << Message << '\n';
200 Broken |= TreatBrokenDebugInfoAsError;
201 BrokenDebugInfo = true;
202 }
203
204 /// A debug info check failed (with values to print).
205 template <typename T1, typename... Ts>
206 void DebugInfoCheckFailed(const Twine &Message, const T1 &V1,
207 const Ts &...Vs) {
208 DebugInfoCheckFailed(Message);
209 if (OS)
210 WriteTs(V1, Vs...);
211 }
212};
213
214//==============================================================================
215// AMDGPU-specific verification functions
216
217void verifyAMDGPUModuleFlag(VerifierSupport &VS, const MDString *ID,
218 Module::ModFlagBehavior MFB, const MDNode *Op);
219
220void verifyAMDGPUFunctionMetadata(VerifierSupport &VS, const Function &F);
221
222void verifyAMDGPUAlloca(VerifierSupport &VS, const AllocaInst &AI);
223
224void verifyAMDGPUIntrinsicCall(VerifierSupport &VS, Intrinsic::ID ID,
225 CallBase &Call);
226
227bool isAMDGPUCallBrIntrinsic(Intrinsic::ID ID);
228
229//==============================================================================
230
231} // namespace llvm
232
233#endif // LLVM_LIB_IR_VERIFIERINTERNAL_H
234