Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions llvm/include/llvm/SandboxIR/PassManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class PassRegistry {
DenseMap<StringRef, Pass *> NameToPassMap;

public:
static constexpr const char PassDelimToken = ',';
PassRegistry() = default;
/// Registers \p PassPtr and takes ownership.
Pass &registerPass(std::unique_ptr<Pass> &&PassPtr) {
Expand All @@ -85,6 +86,9 @@ class PassRegistry {
auto It = NameToPassMap.find(Name);
return It != NameToPassMap.end() ? It->second : nullptr;
}
/// Creates a pass pipeline and returns the first pass manager.
FunctionPassManager &parseAndCreatePassPipeline(StringRef Pipeline);

#ifndef NDEBUG
void print(raw_ostream &OS) const {
for (const auto &PassPtr : Passes)
Expand Down
32 changes: 32 additions & 0 deletions llvm/lib/SandboxIR/PassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,38 @@ bool FunctionPassManager::runOnFunction(Function &F) {
// TODO: Check ChangeAll against hashes before/after.
return Change;
}

FunctionPassManager &
PassRegistry::parseAndCreatePassPipeline(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;
// Start with a FunctionPassManager.
auto &InitialPM = static_cast<FunctionPassManager &>(
registerPass(std::make_unique<FunctionPassManager>("init-fpm")));

for (auto [Idx, C] : enumerate(PipelineStr)) {
// Keep moving Idx until we find the end of the pass name.
bool FoundDelim = C == EndToken || C == 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 = getPassByName(PassName);
if (Pass == nullptr) {
errs() << "Pass '" << PassName << "' not registered!\n";
exit(1);
}
// TODO: This is safe for now, but would require proper upcasting once we
// add more Pass sub-classes.
InitialPM.addPass(static_cast<FunctionPass *>(Pass));
}
return InitialPM;
}
#ifndef NDEBUG
void PassRegistry::dump() const {
print(dbgs());
Expand Down
31 changes: 31 additions & 0 deletions llvm/unittests/SandboxIR/PassTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,34 @@ TEST_F(PassTest, PassRegistry) {
EXPECT_EQ(Buff, "test-pass1\ntest-pass2\n");
#endif // NDEBUG
}

TEST_F(PassTest, ParsePassPipeline) {
class TestPass1 final : public FunctionPass {
public:
TestPass1() : FunctionPass("test-pass1") {}
bool runOnFunction(Function &F) final { return false; }
};
class TestPass2 final : public FunctionPass {
public:
TestPass2() : FunctionPass("test-pass2") {}
bool runOnFunction(Function &F) final { return false; }
};

PassRegistry Registry;
Registry.registerPass(std::make_unique<TestPass1>());
Registry.registerPass(std::make_unique<TestPass2>());

auto &FPM =
Registry.parseAndCreatePassPipeline("test-pass1,test-pass2,test-pass1");
#ifndef NDEBUG
std::string Buff;
llvm::raw_string_ostream SS(Buff);
FPM.print(SS);
EXPECT_EQ(Buff, "init-fpm(test-pass1,test-pass2,test-pass1)");
#endif // NDEBUG

EXPECT_DEATH(Registry.parseAndCreatePassPipeline("bad-pass-name"),
".*not registered.*");
EXPECT_DEATH(Registry.parseAndCreatePassPipeline(""), ".*not registered.*");
EXPECT_DEATH(Registry.parseAndCreatePassPipeline(","), ".*not registered.*");
}