Skip to content

Commit 8263975

Browse files
committedJul 10, 2018
[VPlan] Add VPlanTestBase.h with helper class to build VPlan for tests.
Reviewers: dcaballe, hsaito, rengolin Reviewed By: dcaballe Differential Revision: https://reviews.llvm.org/D49032 llvm-svn: 336653
1 parent 474be00 commit 8263975

File tree

2 files changed

+76
-30
lines changed

2 files changed

+76
-30
lines changed
 

‎llvm/unittests/Transforms/Vectorize/VPlanHCFGTest.cpp

+8-30
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,16 @@
88
//===----------------------------------------------------------------------===//
99

1010
#include "../lib/Transforms/Vectorize/VPlan.h"
11-
#include "../lib/Transforms/Vectorize/VPlanHCFGBuilder.h"
1211
#include "../lib/Transforms/Vectorize/VPlanHCFGTransforms.h"
13-
#include "llvm/AsmParser/Parser.h"
14-
#include "llvm/IR/Dominators.h"
12+
#include "VPlanTestBase.h"
1513
#include "gtest/gtest.h"
1614

1715
namespace llvm {
1816
namespace {
1917

20-
class VPlanHCFGTest : public testing::Test {
21-
protected:
22-
std::unique_ptr<DominatorTree> DT;
23-
std::unique_ptr<LoopInfo> LI;
24-
25-
VPlanHCFGTest() {}
26-
27-
VPlanPtr doBuildPlan(BasicBlock *LoopHeader) {
28-
DT.reset(new DominatorTree(*LoopHeader->getParent()));
29-
LI.reset(new LoopInfo(*DT));
30-
31-
auto Plan = llvm::make_unique<VPlan>();
32-
VPlanHCFGBuilder HCFGBuilder(LI->getLoopFor(LoopHeader), LI.get());
33-
HCFGBuilder.buildHierarchicalCFG(*Plan.get());
34-
return Plan;
35-
}
36-
};
18+
class VPlanHCFGTest : public VPlanTestBase {};
3719

3820
TEST_F(VPlanHCFGTest, testBuildHCFGInnerLoop) {
39-
LLVMContext Ctx;
4021
const char *ModuleString =
4122
"define void @f(i32* %A, i64 %N) {\n"
4223
"entry:\n"
@@ -54,12 +35,11 @@ TEST_F(VPlanHCFGTest, testBuildHCFGInnerLoop) {
5435
" ret void\n"
5536
"}\n";
5637

57-
SMDiagnostic Err;
58-
std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, Ctx);
38+
Module &M = parseModule(ModuleString);
5939

60-
Function *F = M->getFunction("f");
40+
Function *F = M.getFunction("f");
6141
BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor();
62-
auto Plan = doBuildPlan(LoopHeader);
42+
auto Plan = buildHCFG(LoopHeader);
6343

6444
VPBasicBlock *Entry = Plan->getEntry()->getEntryBasicBlock();
6545
EXPECT_NE(nullptr, Entry->getSingleSuccessor());
@@ -115,7 +95,6 @@ TEST_F(VPlanHCFGTest, testBuildHCFGInnerLoop) {
11595
}
11696

11797
TEST_F(VPlanHCFGTest, testVPInstructionToVPRecipesInner) {
118-
LLVMContext Ctx;
11998
const char *ModuleString =
12099
"define void @f(i32* %A, i64 %N) {\n"
121100
"entry:\n"
@@ -133,12 +112,11 @@ TEST_F(VPlanHCFGTest, testVPInstructionToVPRecipesInner) {
133112
" ret void\n"
134113
"}\n";
135114

136-
SMDiagnostic Err;
137-
std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, Ctx);
115+
Module &M = parseModule(ModuleString);
138116

139-
Function *F = M->getFunction("f");
117+
Function *F = M.getFunction("f");
140118
BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor();
141-
auto Plan = doBuildPlan(LoopHeader);
119+
auto Plan = buildHCFG(LoopHeader);
142120

143121
LoopVectorizationLegality::InductionList Inductions;
144122
SmallPtrSet<Instruction *, 1> DeadInstructions;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//===- llvm/unittest/Transforms/Vectorize/VPlanTestBase.h -----------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
/// \file
10+
/// This file defines a VPlanTestBase class, which provides helpers to parse
11+
/// a LLVM IR string and create VPlans given a loop entry block.
12+
//===----------------------------------------------------------------------===//
13+
#ifndef LLVM_UNITTESTS_TRANSFORMS_VECTORIZE_VPLANTESTBASE_H
14+
#define LLVM_UNITTESTS_TRANSFORMS_VECTORIZE_VPLANTESTBASE_H
15+
16+
#include "../lib/Transforms/Vectorize/VPlan.h"
17+
#include "../lib/Transforms/Vectorize/VPlanHCFGBuilder.h"
18+
#include "llvm/Analysis/LoopInfo.h"
19+
#include "llvm/AsmParser/Parser.h"
20+
#include "llvm/IR/Dominators.h"
21+
#include "llvm/Support/SourceMgr.h"
22+
#include "gtest/gtest.h"
23+
24+
namespace llvm {
25+
26+
/// Helper class to create a module from an assembly string and VPlans for a
27+
/// given loop entry block.
28+
class VPlanTestBase : public testing::Test {
29+
protected:
30+
std::unique_ptr<DominatorTree> DT;
31+
std::unique_ptr<LoopInfo> LI;
32+
std::unique_ptr<Module> M;
33+
std::unique_ptr<LLVMContext> Ctx;
34+
35+
VPlanTestBase() : Ctx(new LLVMContext) {}
36+
~VPlanTestBase() {
37+
LI.release();
38+
DT.release();
39+
M.release();
40+
// We need to release objects depending on Ctx first.
41+
Ctx.release();
42+
}
43+
44+
Module &parseModule(const char *ModuleString) {
45+
SMDiagnostic Err;
46+
M = parseAssemblyString(ModuleString, Err, *Ctx);
47+
EXPECT_TRUE(M);
48+
return *M;
49+
}
50+
51+
void doAnalysis(Function &F) {
52+
DT.reset(new DominatorTree(F));
53+
LI.reset(new LoopInfo(*DT));
54+
}
55+
56+
VPlanPtr buildHCFG(BasicBlock *LoopHeader) {
57+
doAnalysis(*LoopHeader->getParent());
58+
59+
auto Plan = llvm::make_unique<VPlan>();
60+
VPlanHCFGBuilder HCFGBuilder(LI->getLoopFor(LoopHeader), LI.get());
61+
HCFGBuilder.buildHierarchicalCFG(*Plan.get());
62+
return Plan;
63+
}
64+
};
65+
66+
} // namespace llvm
67+
68+
#endif // LLVM_UNITTESTS_TRANSFORMS_VECTORIZE_VPLANTESTBASE_H

0 commit comments

Comments
 (0)
Please sign in to comment.