-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[SandboxVectorizer] Use sbvec-passes flag to create a pipeline of Region passes after BottomUpVec. #111223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SandboxVectorizer] Use sbvec-passes flag to create a pipeline of Region passes after BottomUpVec. #111223
Changes from 2 commits
1c1145a
674c91e
ee3e3d3
4b4c063
1772712
fe5f7d8
196b167
314bf8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_NULLPASS_H | ||
| #define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_NULLPASS_H | ||
|
|
||
| #include "llvm/SandboxIR/Pass.h" | ||
|
|
||
| namespace llvm::sandboxir { | ||
|
|
||
| class Region; | ||
|
|
||
| /// A Region pass that does nothing, for use as a placeholder in tests. | ||
| class NullPass final : public RegionPass { | ||
| public: | ||
| NullPass() : RegionPass("null") {} | ||
| bool runOnRegion(Region &R) final { return false; } | ||
| }; | ||
|
|
||
| } // namespace llvm::sandboxir | ||
|
|
||
| #endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_NULLPASS_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,11 @@ | |
| #ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_SANDBOXVECTORIZER_H | ||
| #define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_SANDBOXVECTORIZER_H | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include "llvm/IR/PassManager.h" | ||
| #include "llvm/SandboxIR/PassManager.h" | ||
| #include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.h" | ||
|
|
||
| namespace llvm { | ||
|
|
||
|
|
@@ -17,10 +21,22 @@ class TargetTransformInfo; | |
| class SandboxVectorizerPass : public PassInfoMixin<SandboxVectorizerPass> { | ||
| TargetTransformInfo *TTI = nullptr; | ||
|
|
||
| public: | ||
| PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); | ||
| // Used to build a RegionPass pipeline to be run on Regions created by the | ||
| // bottom-up vectorization pass. | ||
| sandboxir::PassRegistry PR; | ||
|
|
||
| // The main vectorizer pass. | ||
| std::unique_ptr<sandboxir::BottomUpVec> BottomUpVecPass; | ||
|
|
||
| // The PM containing the pipeline of region passes. It's owned by the pass | ||
| // registry. | ||
| sandboxir::RegionPassManager *RPM; | ||
|
||
|
|
||
| bool runImpl(Function &F); | ||
|
|
||
| public: | ||
| SandboxVectorizerPass(); | ||
| PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); | ||
| }; | ||
|
|
||
| } // namespace llvm | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| #include "llvm/SandboxIR/PassManager.h" | ||
| #include "llvm/Support/CommandLine.h" | ||
| #include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.h" | ||
| #include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/NullPass.h" | ||
|
|
||
| using namespace llvm; | ||
|
|
||
|
|
@@ -30,6 +31,57 @@ cl::opt<std::string> UserDefinedPassPipeline( | |
| cl::desc("Comma-separated list of vectorizer passes. If not set " | ||
| "we run the predefined pipeline.")); | ||
|
|
||
| static void registerAllRegionPasses(sandboxir::PassRegistry &PR) { | ||
| PR.registerPass(std::make_unique<sandboxir::NullPass>()); | ||
| } | ||
|
|
||
| static sandboxir::RegionPassManager & | ||
| parseAndCreatePassPipeline(sandboxir::PassRegistry &PR, StringRef Pipeline) { | ||
| static constexpr const char EndToken = '\0'; | ||
| // Add EndToken to the end to ease parsing. | ||
| std::string PipelineStr = std::string(Pipeline) + EndToken; | ||
| int FlagBeginIdx = 0; | ||
| auto &RPM = static_cast<sandboxir::RegionPassManager &>( | ||
| PR.registerPass(std::make_unique<sandboxir::RegionPassManager>("rpm"))); | ||
|
|
||
| for (auto [Idx, C] : enumerate(PipelineStr)) { | ||
| // Keep moving Idx until we find the end of the pass name. | ||
| bool FoundDelim = C == EndToken || C == PR.PassDelimToken; | ||
| if (!FoundDelim) | ||
| continue; | ||
| unsigned Sz = Idx - FlagBeginIdx; | ||
| std::string PassName(&PipelineStr[FlagBeginIdx], Sz); | ||
| FlagBeginIdx = Idx + 1; | ||
|
|
||
| // Get the pass that corresponds to PassName and add it to the pass manager. | ||
| auto *Pass = PR.getPassByName(PassName); | ||
| if (Pass == nullptr) { | ||
| errs() << "Pass '" << PassName << "' not registered!\n"; | ||
| exit(1); | ||
| } | ||
| // TODO: Add a type check here. The downcast is correct as long as | ||
| // registerAllRegionPasses only registers regions passes. | ||
| RPM.addPass(static_cast<sandboxir::RegionPass *>(Pass)); | ||
| } | ||
| return RPM; | ||
| } | ||
|
|
||
| SandboxVectorizerPass::SandboxVectorizerPass() { | ||
| registerAllRegionPasses(PR); | ||
|
|
||
| // Create a pipeline to be run on each Region created by BottomUpVec. | ||
| if (UserDefinedPassPipeline == DefaultPipelineMagicStr) { | ||
|
||
| // Create the default pass pipeline. | ||
| RPM = &static_cast<sandboxir::RegionPassManager &>(PR.registerPass( | ||
| std::make_unique<sandboxir::FunctionPassManager>("rpm"))); | ||
| // TODO: Add passes to the default pipeline. | ||
| } else { | ||
| // Create the user-defined pipeline. | ||
| RPM = &parseAndCreatePassPipeline(PR, UserDefinedPassPipeline); | ||
| } | ||
| BottomUpVecPass = std::make_unique<sandboxir::BottomUpVec>(RPM); | ||
| } | ||
|
|
||
| PreservedAnalyses SandboxVectorizerPass::run(Function &F, | ||
| FunctionAnalysisManager &AM) { | ||
| TTI = &AM.getResult<TargetIRAnalysis>(F); | ||
|
|
@@ -56,31 +108,13 @@ bool SandboxVectorizerPass::runImpl(Function &LLVMF) { | |
| return false; | ||
| } | ||
|
|
||
| sandboxir::Context Ctx(LLVMF.getContext()); | ||
| // Create SandboxIR for `LLVMF`. | ||
| sandboxir::Function &F = *Ctx.createFunction(&LLVMF); | ||
| // Create the passes and register them with the PassRegistry. | ||
| sandboxir::PassRegistry PR; | ||
| auto &BottomUpVecPass = static_cast<sandboxir::FunctionPass &>( | ||
| PR.registerPass(std::make_unique<sandboxir::BottomUpVec>())); | ||
|
|
||
| sandboxir::FunctionPassManager *PM = nullptr; | ||
| if (UserDefinedPassPipeline == DefaultPipelineMagicStr) { | ||
| // Create the default pass pipeline. | ||
| PM = &static_cast<sandboxir::FunctionPassManager &>(PR.registerPass( | ||
| std::make_unique<sandboxir::FunctionPassManager>("pm"))); | ||
| PM->addPass(&BottomUpVecPass); | ||
| } else { | ||
| // Create the user-defined pipeline. | ||
| PM = &PR.parseAndCreatePassPipeline(UserDefinedPassPipeline); | ||
| } | ||
|
|
||
| if (PrintPassPipeline) { | ||
|
||
| PM->printPipeline(outs()); | ||
| RPM->printPipeline(outs()); | ||
| return false; | ||
| } | ||
|
|
||
| // Run the pass pipeline. | ||
| bool Change = PM->runOnFunction(F); | ||
| return Change; | ||
| // Create SandboxIR for LLVMF and run BottomUpVec on it. | ||
| sandboxir::Context Ctx(LLVMF.getContext()); | ||
| sandboxir::Function &F = *Ctx.createFunction(&LLVMF); | ||
| return BottomUpVecPass->runOnFunction(F); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| ; RUN: opt -passes=sandbox-vectorizer -sbvec-print-pass-pipeline -sbvec-passes=bottom-up-vec,bottom-up-vec %s -disable-output | FileCheck %s | ||
| ; RUN: opt -passes=sandbox-vectorizer -sbvec-print-pass-pipeline -sbvec-passes=null,null %s -disable-output | FileCheck %s | ||
|
|
||
| ; !!!WARNING!!! This won't get updated by update_test_checks.py ! | ||
|
|
||
| ; This checks the user defined pass pipeline. | ||
| define void @pipeline() { | ||
| ; CHECK: pm | ||
| ; CHECK: bottom-up-vec | ||
| ; CHECK: bottom-up-vec | ||
| ; CHECK: rpm | ||
| ; CHECK: null | ||
| ; CHECK: null | ||
| ; CHECK-EMPTY: | ||
| ret void | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this a member variable? It's created and used only within
runOnFunction()at the moment, so making it a class member implies that it used across multiple member functions ofSandboxVectorizerPass. Are you planning to move it to a separate class thatSandboxVectorizerPassinherits from?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we move away from
cl::opt("sbvec-passes")and to proper pass parameters, we would want the LLVM pass to hold the region pass manager in the class, rather than keep it local torunImpl, because we'd specify the region pass pipeline via the constructorbut with
cl::optI don't think it really mattersThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually, I thought that
BottomUpVecPasswould own the region pass manager so it could invoke it when it creates a regionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not the only reason possible to make something a member. Making it local to
runImplmeans constructing theBottomUpVecpass every time, which seems like a waste to me.Also, conceptually I see the
SandboxVectorizerpass as a wrapper aroundBottomUpVecthat handles the creation of sandbox IR. I don't see anything wrong with the wrapper owning the thing it's wrapping.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, I forgot that we were building the pass pipeline over and over again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed
BottomUpVecalso having a reference to the region pass manager