Skip to content

Commit 490a09a

Browse files
authored
[UnrollAnalyzerTest] Remove dependency to pass managers (#78473)
Remove use of LegacyPassManager in the UnrollAnalyzerTest unit test. Given that the goal isn't to test pass manager interfaces, and since the LoopUnrollAnalyzer isn't even implemented as a pass, we do not really need the complexity of using a pass manager. Instead we just make sure that we run LoopUnrollAnalyzer and other needed analyses standalone (without any pass manager). This was inspired by the LoopInfoTest unit test.
1 parent 5266c12 commit 490a09a

File tree

1 file changed

+45
-70
lines changed

1 file changed

+45
-70
lines changed

llvm/unittests/Analysis/UnrollAnalyzerTest.cpp

Lines changed: 45 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -6,61 +6,52 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "llvm/Analysis/AssumptionCache.h"
910
#include "llvm/Analysis/LoopInfo.h"
1011
#include "llvm/Analysis/LoopUnrollAnalyzer.h"
12+
#include "llvm/Analysis/ScalarEvolution.h"
13+
#include "llvm/Analysis/TargetLibraryInfo.h"
1114
#include "llvm/AsmParser/Parser.h"
1215
#include "llvm/IR/Dominators.h"
13-
#include "llvm/IR/LegacyPassManager.h"
14-
#include "llvm/InitializePasses.h"
1516
#include "llvm/Support/SourceMgr.h"
1617
#include "gtest/gtest.h"
1718

1819
using namespace llvm;
19-
namespace llvm {
20-
void initializeUnrollAnalyzerTestPass(PassRegistry &);
2120

22-
static SmallVector<DenseMap<Value *, Value *>, 16> SimplifiedValuesVector;
23-
static unsigned TripCount = 0;
21+
typedef SmallVector<DenseMap<Value *, Value *>, 16> SimplifiedValuesVectorTy;
2422

25-
namespace {
26-
struct UnrollAnalyzerTest : public FunctionPass {
27-
static char ID;
28-
bool runOnFunction(Function &F) override {
29-
LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
30-
ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
23+
/// Build loop info and scalar evolution for the function and run the analysis.
24+
static void
25+
runUnrollAnalyzer(Module &M, StringRef FuncName,
26+
SimplifiedValuesVectorTy &SimplifiedValuesVector) {
27+
auto *F = M.getFunction(FuncName);
28+
ASSERT_NE(F, nullptr) << "Could not find " << FuncName;
3129

32-
Function::iterator FI = F.begin();
33-
FI++; // First basic block is entry - skip it.
34-
BasicBlock *Header = &*FI++;
35-
Loop *L = LI->getLoopFor(Header);
36-
BasicBlock *Exiting = L->getExitingBlock();
30+
TargetLibraryInfoImpl TLII;
31+
TargetLibraryInfo TLI(TLII);
32+
AssumptionCache AC(*F);
33+
DominatorTree DT(*F);
34+
LoopInfo LI(DT);
35+
ScalarEvolution SE(*F, TLI, AC, DT, LI);
3736

38-
SimplifiedValuesVector.clear();
39-
TripCount = SE->getSmallConstantTripCount(L, Exiting);
40-
for (unsigned Iteration = 0; Iteration < TripCount; Iteration++) {
41-
DenseMap<Value *, Value *> SimplifiedValues;
42-
UnrolledInstAnalyzer Analyzer(Iteration, SimplifiedValues, *SE, L);
43-
for (auto *BB : L->getBlocks())
44-
for (Instruction &I : *BB)
45-
Analyzer.visit(I);
46-
SimplifiedValuesVector.push_back(SimplifiedValues);
47-
}
48-
return false;
49-
}
50-
void getAnalysisUsage(AnalysisUsage &AU) const override {
51-
AU.addRequired<DominatorTreeWrapperPass>();
52-
AU.addRequired<LoopInfoWrapperPass>();
53-
AU.addRequired<ScalarEvolutionWrapperPass>();
54-
AU.setPreservesAll();
55-
}
56-
UnrollAnalyzerTest() : FunctionPass(ID) {
57-
initializeUnrollAnalyzerTestPass(*PassRegistry::getPassRegistry());
37+
Function::iterator FI = F->begin();
38+
FI++; // First basic block is entry - skip it.
39+
BasicBlock *Header = &*FI++;
40+
Loop *L = LI.getLoopFor(Header);
41+
BasicBlock *Exiting = L->getExitingBlock();
42+
43+
SimplifiedValuesVector.clear();
44+
unsigned TripCount = SE.getSmallConstantTripCount(L, Exiting);
45+
for (unsigned Iteration = 0; Iteration < TripCount; Iteration++) {
46+
DenseMap<Value *, Value *> SimplifiedValues;
47+
UnrolledInstAnalyzer Analyzer(Iteration, SimplifiedValues, SE, L);
48+
for (auto *BB : L->getBlocks())
49+
for (Instruction &I : *BB)
50+
Analyzer.visit(I);
51+
SimplifiedValuesVector.push_back(SimplifiedValues);
5852
}
59-
};
6053
}
6154

62-
char UnrollAnalyzerTest::ID = 0;
63-
6455
std::unique_ptr<Module> makeLLVMModule(LLVMContext &Context,
6556
const char *ModuleStr) {
6657
SMDiagnostic Err;
@@ -85,12 +76,11 @@ TEST(UnrollAnalyzerTest, BasicSimplifications) {
8576
" %x.lcssa = phi i64 [ %x2, %loop ]\n"
8677
" ret i64 %x.lcssa\n"
8778
"}\n";
88-
UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
8979
LLVMContext Context;
9080
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
91-
legacy::PassManager Passes;
92-
Passes.add(P);
93-
Passes.run(*M);
81+
SimplifiedValuesVectorTy SimplifiedValuesVector;
82+
runUnrollAnalyzer(*M, "propagate_loop_phis", SimplifiedValuesVector);
83+
unsigned TripCount = SimplifiedValuesVector.size();
9484

9585
// Perform checks
9686
Module::iterator MI = M->begin();
@@ -148,12 +138,10 @@ TEST(UnrollAnalyzerTest, OuterLoopSimplification) {
148138
" ret void\n"
149139
"}\n";
150140

151-
UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
152141
LLVMContext Context;
153142
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
154-
legacy::PassManager Passes;
155-
Passes.add(P);
156-
Passes.run(*M);
143+
SimplifiedValuesVectorTy SimplifiedValuesVector;
144+
runUnrollAnalyzer(*M, "foo", SimplifiedValuesVector);
157145

158146
Module::iterator MI = M->begin();
159147
Function *F = &*MI++;
@@ -177,6 +165,7 @@ TEST(UnrollAnalyzerTest, OuterLoopSimplification) {
177165
auto I2 = SimplifiedValuesVector[0].find(Y2);
178166
EXPECT_TRUE(I2 == SimplifiedValuesVector[0].end());
179167
}
168+
180169
TEST(UnrollAnalyzerTest, CmpSimplifications) {
181170
const char *ModuleStr =
182171
"target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
@@ -193,12 +182,10 @@ TEST(UnrollAnalyzerTest, CmpSimplifications) {
193182
"for.end:\n"
194183
" ret void\n"
195184
"}\n";
196-
UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
197185
LLVMContext Context;
198186
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
199-
legacy::PassManager Passes;
200-
Passes.add(P);
201-
Passes.run(*M);
187+
SimplifiedValuesVectorTy SimplifiedValuesVector;
188+
runUnrollAnalyzer(*M, "branch_iv_trunc", SimplifiedValuesVector);
202189

203190
// Perform checks
204191
Module::iterator MI = M->begin();
@@ -221,6 +208,7 @@ TEST(UnrollAnalyzerTest, CmpSimplifications) {
221208
EXPECT_TRUE(I2 != SimplifiedValuesVector[5].end());
222209
EXPECT_EQ(cast<ConstantInt>((*I2).second)->getZExtValue(), 1U);
223210
}
211+
224212
TEST(UnrollAnalyzerTest, PtrCmpSimplifications) {
225213
const char *ModuleStr =
226214
"target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
@@ -240,12 +228,10 @@ TEST(UnrollAnalyzerTest, PtrCmpSimplifications) {
240228
"loop.exit:\n"
241229
" ret void\n"
242230
"}\n";
243-
UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
244231
LLVMContext Context;
245232
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
246-
legacy::PassManager Passes;
247-
Passes.add(P);
248-
Passes.run(*M);
233+
SimplifiedValuesVectorTy SimplifiedValuesVector;
234+
runUnrollAnalyzer(*M, "ptr_cmp", SimplifiedValuesVector);
249235

250236
// Perform checks
251237
Module::iterator MI = M->begin();
@@ -263,6 +249,7 @@ TEST(UnrollAnalyzerTest, PtrCmpSimplifications) {
263249
EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end());
264250
EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 0U);
265251
}
252+
266253
TEST(UnrollAnalyzerTest, CastSimplifications) {
267254
const char *ModuleStr =
268255
"target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
@@ -286,12 +273,10 @@ TEST(UnrollAnalyzerTest, CastSimplifications) {
286273
" ret void\n"
287274
"}\n";
288275

289-
UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
290276
LLVMContext Context;
291277
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
292-
legacy::PassManager Passes;
293-
Passes.add(P);
294-
Passes.run(*M);
278+
SimplifiedValuesVectorTy SimplifiedValuesVector;
279+
runUnrollAnalyzer(*M, "const_load_cast", SimplifiedValuesVector);
295280

296281
// Perform checks
297282
Module::iterator MI = M->begin();
@@ -319,13 +304,3 @@ TEST(UnrollAnalyzerTest, CastSimplifications) {
319304
EXPECT_TRUE(I3 != SimplifiedValuesVector[5].end());
320305
EXPECT_EQ(cast<ConstantInt>((*I3).second)->getZExtValue(), 3U);
321306
}
322-
323-
} // end namespace llvm
324-
325-
INITIALIZE_PASS_BEGIN(UnrollAnalyzerTest, "unrollanalyzertestpass",
326-
"unrollanalyzertestpass", false, false)
327-
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
328-
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
329-
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
330-
INITIALIZE_PASS_END(UnrollAnalyzerTest, "unrollanalyzertestpass",
331-
"unrollanalyzertestpass", false, false)

0 commit comments

Comments
 (0)