Skip to content

Commit 10b12d8

Browse files
committed
Move pass pipeline and region creation logic to the main SandboxVectorizer class.
1 parent 61453a0 commit 10b12d8

File tree

5 files changed

+72
-56
lines changed

5 files changed

+72
-56
lines changed

llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ class BottomUpVec final : public FunctionPass {
2929
void tryVectorize(ArrayRef<Value *> Seeds);
3030

3131
// The PM containing the pipeline of region passes.
32-
RegionPassManager RPM;
32+
// TODO: Remove maybe_unused once we build regions to run passes on.
33+
[[maybe_unused]] RegionPassManager *RPM;
3334

3435
public:
35-
BottomUpVec();
36+
BottomUpVec(RegionPassManager *RPM);
3637
bool runOnFunction(Function &F) final;
3738
};
3839

llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/PrintInstructionCountPass.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_PRINTINSTRUCTIONCOUNTPASS_H
33

44
#include "llvm/SandboxIR/Pass.h"
5+
#include "llvm/SandboxIR/Region.h"
56

67
namespace llvm::sandboxir {
78

8-
class Region;
9-
109
/// A Region pass that prints the instruction count for the region to stdout.
1110
/// Used to test -sbvec-passes while we don't have any actual optimization
1211
/// passes.

llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class TargetTransformInfo;
2020
class SandboxVectorizerPass : public PassInfoMixin<SandboxVectorizerPass> {
2121
TargetTransformInfo *TTI = nullptr;
2222

23+
// A pipeline of region passes. Typically, this will be run by BottomUpVec on
24+
// each region it creates, but it can also be run on regions created from
25+
// IR metadata in tests.
26+
sandboxir::RegionPassManager RPM;
27+
2328
// The main vectorizer pass.
2429
sandboxir::BottomUpVec BottomUpVecPass;
2530

llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,11 @@
1212
#include "llvm/SandboxIR/Instruction.h"
1313
#include "llvm/SandboxIR/Region.h"
1414
#include "llvm/Support/CommandLine.h"
15-
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/NullPass.h"
16-
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/PrintInstructionCountPass.h"
1715

1816
namespace llvm::sandboxir {
1917

20-
static cl::opt<bool>
21-
PrintPassPipeline("sbvec-print-pass-pipeline", cl::init(false), cl::Hidden,
22-
cl::desc("Prints the pass pipeline and returns."));
23-
24-
static cl::opt<bool> UseRegionsFromMetadata(
25-
"sbvec-use-regions-from-metadata", cl::init(false), cl::Hidden,
26-
cl::desc("Skips bottom-up vectorization, builds regions from metadata "
27-
"already present in the IR and runs the region pass pipeline."));
28-
29-
/// A magic string for the default pass pipeline.
30-
static const char *DefaultPipelineMagicStr = "*";
31-
32-
static cl::opt<std::string> UserDefinedPassPipeline(
33-
"sbvec-passes", cl::init(DefaultPipelineMagicStr), cl::Hidden,
34-
cl::desc("Comma-separated list of vectorizer passes. If not set "
35-
"we run the predefined pipeline."));
36-
37-
static std::unique_ptr<RegionPass> createRegionPass(StringRef Name) {
38-
#define REGION_PASS(NAME, CREATE_PASS) \
39-
if (Name == NAME) \
40-
return std::make_unique<decltype(CREATE_PASS)>(CREATE_PASS);
41-
#include "PassRegistry.def"
42-
return nullptr;
43-
}
44-
45-
BottomUpVec::BottomUpVec() : FunctionPass("bottom-up-vec"), RPM("rpm") {
46-
// Create a pipeline to be run on each Region created by BottomUpVec.
47-
if (UserDefinedPassPipeline == DefaultPipelineMagicStr) {
48-
// TODO: Add default passes to RPM.
49-
} else {
50-
// Create the user-defined pipeline.
51-
RPM.setPassPipeline(UserDefinedPassPipeline, createRegionPass);
52-
}
53-
}
18+
BottomUpVec::BottomUpVec(RegionPassManager *RPM)
19+
: FunctionPass("bottom-up-vec"), RPM(RPM) {}
5420

5521
// TODO: This is a temporary function that returns some seeds.
5622
// Replace this with SeedCollector's function when it lands.
@@ -89,19 +55,6 @@ void BottomUpVec::vectorizeRec(ArrayRef<Value *> Bndl) {
8955
void BottomUpVec::tryVectorize(ArrayRef<Value *> Bndl) { vectorizeRec(Bndl); }
9056

9157
bool BottomUpVec::runOnFunction(Function &F) {
92-
if (PrintPassPipeline) {
93-
RPM.printPipeline(outs());
94-
return false;
95-
}
96-
if (UseRegionsFromMetadata) {
97-
SmallVector<std::unique_ptr<Region>> Regions =
98-
Region::createRegionsFromMD(F);
99-
for (auto &R : Regions) {
100-
RPM.runOnRegion(*R);
101-
}
102-
return false;
103-
}
104-
10558
Change = false;
10659
// TODO: Start from innermost BBs first
10760
for (auto &BB : F) {

llvm/lib/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.cpp

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,55 @@
1010
#include "llvm/Analysis/TargetTransformInfo.h"
1111
#include "llvm/SandboxIR/Constant.h"
1212
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.h"
13+
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/NullPass.h"
14+
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/PrintInstructionCountPass.h"
1315

14-
using namespace llvm;
16+
using namespace llvm::sandboxir;
17+
18+
namespace llvm {
1519

1620
#define SV_NAME "sandbox-vectorizer"
1721
#define DEBUG_TYPE SV_NAME
1822

19-
SandboxVectorizerPass::SandboxVectorizerPass() = default;
23+
static cl::opt<bool>
24+
PrintPassPipeline("sbvec-print-pass-pipeline", cl::init(false), cl::Hidden,
25+
cl::desc("Prints the pass pipeline and returns."));
26+
27+
/// A magic string for the default pass pipeline.
28+
static const char *DefaultPipelineMagicStr = "*";
29+
30+
static cl::opt<std::string> UserDefinedPassPipeline(
31+
"sbvec-passes", cl::init(DefaultPipelineMagicStr), cl::Hidden,
32+
cl::desc("Comma-separated list of vectorizer passes. If not set "
33+
"we run the predefined pipeline."));
34+
35+
static cl::opt<bool> UseRegionsFromMetadata(
36+
"sbvec-use-regions-from-metadata", cl::init(false), cl::Hidden,
37+
cl::desc("Skips bottom-up vectorization, builds regions from metadata "
38+
"already present in the IR and runs the region pass pipeline."));
39+
40+
static std::unique_ptr<sandboxir::RegionPass> createRegionPass(StringRef Name) {
41+
#define REGION_PASS(NAME, CREATE_PASS) \
42+
if (Name == NAME) \
43+
return std::make_unique<decltype(CREATE_PASS)>(CREATE_PASS);
44+
#include "Passes/PassRegistry.def"
45+
return nullptr;
46+
}
47+
48+
sandboxir::RegionPassManager createRegionPassManager() {
49+
sandboxir::RegionPassManager RPM("rpm");
50+
// Create a pipeline to be run on each Region created by BottomUpVec.
51+
if (UserDefinedPassPipeline == DefaultPipelineMagicStr) {
52+
// TODO: Add default passes to RPM.
53+
} else {
54+
// Create the user-defined pipeline.
55+
RPM.setPassPipeline(UserDefinedPassPipeline, createRegionPass);
56+
}
57+
return RPM;
58+
}
59+
60+
SandboxVectorizerPass::SandboxVectorizerPass()
61+
: RPM(createRegionPassManager()), BottomUpVecPass(&RPM) {}
2062

2163
SandboxVectorizerPass::SandboxVectorizerPass(SandboxVectorizerPass &&) =
2264
default;
@@ -37,6 +79,11 @@ PreservedAnalyses SandboxVectorizerPass::run(Function &F,
3779
}
3880

3981
bool SandboxVectorizerPass::runImpl(Function &LLVMF) {
82+
if (PrintPassPipeline) {
83+
RPM.printPipeline(outs());
84+
return false;
85+
}
86+
4087
// If the target claims to have no vector registers early return.
4188
if (!TTI->getNumberOfRegisters(TTI->getRegisterClassForType(true))) {
4289
LLVM_DEBUG(dbgs() << "SBVec: Target has no vector registers, return.\n");
@@ -52,5 +99,16 @@ bool SandboxVectorizerPass::runImpl(Function &LLVMF) {
5299
// Create SandboxIR for LLVMF and run BottomUpVec on it.
53100
sandboxir::Context Ctx(LLVMF.getContext());
54101
sandboxir::Function &F = *Ctx.createFunction(&LLVMF);
55-
return BottomUpVecPass.runOnFunction(F);
102+
if (UseRegionsFromMetadata) {
103+
SmallVector<std::unique_ptr<sandboxir::Region>> Regions =
104+
sandboxir::Region::createRegionsFromMD(F);
105+
for (auto &R : Regions) {
106+
RPM.runOnRegion(*R);
107+
}
108+
return false;
109+
} else {
110+
return BottomUpVecPass.runOnFunction(F);
111+
}
56112
}
113+
114+
} // namespace llvm

0 commit comments

Comments
 (0)