1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
//===- llvm/unittest/CodeGen/PassManager.cpp - PassManager tests ----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/CGSCCPassManager.h"
#include "llvm/Analysis/LoopAnalysisManager.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/MachinePassManager.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/TargetParser/Host.h"
#include "llvm/TargetParser/Triple.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
class TestFunctionAnalysis : public AnalysisInfoMixin<TestFunctionAnalysis> {
public:
struct Result {
Result(int Count) : InstructionCount(Count) {}
int InstructionCount;
};
/// Run the analysis pass over the function and return a result.
Result run(Function &F, FunctionAnalysisManager &AM) {
int Count = 0;
for (Function::iterator BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI)
for (BasicBlock::iterator II = BBI->begin(), IE = BBI->end(); II != IE;
++II)
++Count;
return Result(Count);
}
private:
friend AnalysisInfoMixin<TestFunctionAnalysis>;
static AnalysisKey Key;
};
AnalysisKey TestFunctionAnalysis::Key;
class TestMachineFunctionAnalysis
: public AnalysisInfoMixin<TestMachineFunctionAnalysis> {
public:
struct Result {
Result(int Count) : InstructionCount(Count) {}
int InstructionCount;
};
/// Run the analysis pass over the machine function and return a result.
Result run(MachineFunction &MF, MachineFunctionAnalysisManager::Base &AM) {
auto &MFAM = static_cast<MachineFunctionAnalysisManager &>(AM);
// Query function analysis result.
TestFunctionAnalysis::Result &FAR =
MFAM.getResult<TestFunctionAnalysis>(MF.getFunction());
// + 5
return FAR.InstructionCount;
}
private:
friend AnalysisInfoMixin<TestMachineFunctionAnalysis>;
static AnalysisKey Key;
};
AnalysisKey TestMachineFunctionAnalysis::Key;
const std::string DoInitErrMsg = "doInitialization failed";
const std::string DoFinalErrMsg = "doFinalization failed";
struct TestMachineFunctionPass : public PassInfoMixin<TestMachineFunctionPass> {
TestMachineFunctionPass(int &Count, std::vector<int> &BeforeInitialization,
std::vector<int> &BeforeFinalization,
std::vector<int> &MachineFunctionPassCount)
: Count(Count), BeforeInitialization(BeforeInitialization),
BeforeFinalization(BeforeFinalization),
MachineFunctionPassCount(MachineFunctionPassCount) {}
Error doInitialization(Module &M, MachineFunctionAnalysisManager &MFAM) {
// Force doInitialization fail by starting with big `Count`.
if (Count > 10000)
return make_error<StringError>(DoInitErrMsg, inconvertibleErrorCode());
// + 1
++Count;
BeforeInitialization.push_back(Count);
return Error::success();
}
Error doFinalization(Module &M, MachineFunctionAnalysisManager &MFAM) {
// Force doFinalization fail by starting with big `Count`.
if (Count > 1000)
return make_error<StringError>(DoFinalErrMsg, inconvertibleErrorCode());
// + 1
++Count;
BeforeFinalization.push_back(Count);
return Error::success();
}
PreservedAnalyses run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
// Query function analysis result.
TestFunctionAnalysis::Result &FAR =
MFAM.getResult<TestFunctionAnalysis>(MF.getFunction());
// 3 + 1 + 1 = 5
Count += FAR.InstructionCount;
// Query module analysis result.
MachineModuleInfo &MMI =
MFAM.getResult<MachineModuleAnalysis>(*MF.getFunction().getParent())
.getMMI();
// 1 + 1 + 1 = 3
Count += (MMI.getModule() == MF.getFunction().getParent());
// Query machine function analysis result.
TestMachineFunctionAnalysis::Result &MFAR =
MFAM.getResult<TestMachineFunctionAnalysis>(MF);
// 3 + 1 + 1 = 5
Count += MFAR.InstructionCount;
MachineFunctionPassCount.push_back(Count);
return PreservedAnalyses::none();
}
int &Count;
std::vector<int> &BeforeInitialization;
std::vector<int> &BeforeFinalization;
std::vector<int> &MachineFunctionPassCount;
};
struct TestMachineModulePass : public PassInfoMixin<TestMachineModulePass> {
TestMachineModulePass(int &Count, std::vector<int> &MachineModulePassCount)
: Count(Count), MachineModulePassCount(MachineModulePassCount) {}
Error run(Module &M, MachineFunctionAnalysisManager &MFAM) {
MachineModuleInfo &MMI = MFAM.getResult<MachineModuleAnalysis>(M).getMMI();
// + 1
Count += (MMI.getModule() == &M);
MachineModulePassCount.push_back(Count);
return Error::success();
}
PreservedAnalyses run(MachineFunction &MF,
MachineFunctionAnalysisManager &AM) {
llvm_unreachable(
"This should never be reached because this is machine module pass");
}
int &Count;
std::vector<int> &MachineModulePassCount;
};
std::unique_ptr<Module> parseIR(LLVMContext &Context, const char *IR) {
SMDiagnostic Err;
return parseAssemblyString(IR, Err, Context);
}
class PassManagerTest : public ::testing::Test {
protected:
LLVMContext Context;
std::unique_ptr<Module> M;
std::unique_ptr<TargetMachine> TM;
public:
PassManagerTest()
: M(parseIR(Context, "define void @f() {\n"
"entry:\n"
" call void @g()\n"
" call void @h()\n"
" ret void\n"
"}\n"
"define void @g() {\n"
" ret void\n"
"}\n"
"define void @h() {\n"
" ret void\n"
"}\n")) {
// MachineModuleAnalysis needs a TargetMachine instance.
llvm::InitializeAllTargets();
std::string TripleName = Triple::normalize(sys::getDefaultTargetTriple());
std::string Error;
const Target *TheTarget =
TargetRegistry::lookupTarget(TripleName, Error);
if (!TheTarget)
return;
TargetOptions Options;
TM.reset(TheTarget->createTargetMachine(TripleName, "", "", Options,
std::nullopt));
}
};
TEST_F(PassManagerTest, Basic) {
if (!TM)
GTEST_SKIP();
LLVMTargetMachine *LLVMTM = static_cast<LLVMTargetMachine *>(TM.get());
M->setDataLayout(TM->createDataLayout());
MachineModuleInfo MMI(LLVMTM);
LoopAnalysisManager LAM;
FunctionAnalysisManager FAM;
CGSCCAnalysisManager CGAM;
ModuleAnalysisManager MAM;
PassBuilder PB(TM.get());
PB.registerModuleAnalyses(MAM);
PB.registerFunctionAnalyses(FAM);
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
FAM.registerPass([&] { return TestFunctionAnalysis(); });
FAM.registerPass([&] { return PassInstrumentationAnalysis(); });
MAM.registerPass([&] { return MachineModuleAnalysis(MMI); });
MAM.registerPass([&] { return PassInstrumentationAnalysis(); });
MachineFunctionAnalysisManager MFAM;
{
// Test move assignment.
MachineFunctionAnalysisManager NestedMFAM(FAM, MAM);
NestedMFAM.registerPass([&] { return PassInstrumentationAnalysis(); });
NestedMFAM.registerPass([&] { return TestMachineFunctionAnalysis(); });
MFAM = std::move(NestedMFAM);
}
int Count = 0;
std::vector<int> BeforeInitialization[2];
std::vector<int> BeforeFinalization[2];
std::vector<int> TestMachineFunctionCount[2];
std::vector<int> TestMachineModuleCount[2];
MachineFunctionPassManager MFPM;
{
// Test move assignment.
MachineFunctionPassManager NestedMFPM;
NestedMFPM.addPass(TestMachineModulePass(Count, TestMachineModuleCount[0]));
NestedMFPM.addPass(TestMachineFunctionPass(Count, BeforeInitialization[0],
BeforeFinalization[0],
TestMachineFunctionCount[0]));
NestedMFPM.addPass(TestMachineModulePass(Count, TestMachineModuleCount[1]));
NestedMFPM.addPass(TestMachineFunctionPass(Count, BeforeInitialization[1],
BeforeFinalization[1],
TestMachineFunctionCount[1]));
MFPM = std::move(NestedMFPM);
}
ASSERT_FALSE(errorToBool(MFPM.run(*M, MFAM)));
// Check first machine module pass
EXPECT_EQ(1u, TestMachineModuleCount[0].size());
EXPECT_EQ(3, TestMachineModuleCount[0][0]);
// Check first machine function pass
EXPECT_EQ(1u, BeforeInitialization[0].size());
EXPECT_EQ(1, BeforeInitialization[0][0]);
EXPECT_EQ(3u, TestMachineFunctionCount[0].size());
EXPECT_EQ(10, TestMachineFunctionCount[0][0]);
EXPECT_EQ(13, TestMachineFunctionCount[0][1]);
EXPECT_EQ(16, TestMachineFunctionCount[0][2]);
EXPECT_EQ(1u, BeforeFinalization[0].size());
EXPECT_EQ(31, BeforeFinalization[0][0]);
// Check second machine module pass
EXPECT_EQ(1u, TestMachineModuleCount[1].size());
EXPECT_EQ(17, TestMachineModuleCount[1][0]);
// Check second machine function pass
EXPECT_EQ(1u, BeforeInitialization[1].size());
EXPECT_EQ(2, BeforeInitialization[1][0]);
EXPECT_EQ(3u, TestMachineFunctionCount[1].size());
EXPECT_EQ(24, TestMachineFunctionCount[1][0]);
EXPECT_EQ(27, TestMachineFunctionCount[1][1]);
EXPECT_EQ(30, TestMachineFunctionCount[1][2]);
EXPECT_EQ(1u, BeforeFinalization[1].size());
EXPECT_EQ(32, BeforeFinalization[1][0]);
EXPECT_EQ(32, Count);
// doInitialization returns error
Count = 10000;
MFPM.addPass(TestMachineFunctionPass(Count, BeforeInitialization[1],
BeforeFinalization[1],
TestMachineFunctionCount[1]));
std::string Message;
llvm::handleAllErrors(MFPM.run(*M, MFAM), [&](llvm::StringError &Error) {
Message = Error.getMessage();
});
EXPECT_EQ(Message, DoInitErrMsg);
// doFinalization returns error
Count = 1000;
MFPM.addPass(TestMachineFunctionPass(Count, BeforeInitialization[1],
BeforeFinalization[1],
TestMachineFunctionCount[1]));
llvm::handleAllErrors(MFPM.run(*M, MFAM), [&](llvm::StringError &Error) {
Message = Error.getMessage();
});
EXPECT_EQ(Message, DoFinalErrMsg);
}
} // namespace
|