Skip to content

Commit 2ddf21b

Browse files
authored
[SandboxIR] Pass registry (#108084)
This patch implements a simple Pass Registry class, which takes ownership of the passes registered with it and provides an interface to get the pass pointer by its name.
1 parent 0fc4147 commit 2ddf21b

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

llvm/include/llvm/SandboxIR/PassManager.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef LLVM_SANDBOXIR_PASSMANAGER_H
1919
#define LLVM_SANDBOXIR_PASSMANAGER_H
2020

21+
#include "llvm/ADT/DenseMap.h"
2122
#include "llvm/ADT/STLExtras.h"
2223
#include "llvm/SandboxIR/Pass.h"
2324
#include "llvm/Support/Debug.h"
@@ -65,6 +66,34 @@ class FunctionPassManager final
6566
bool runOnFunction(Function &F) final;
6667
};
6768

69+
/// Owns the passes and provides an API to get a pass by its name.
70+
class PassRegistry {
71+
SmallVector<std::unique_ptr<Pass>, 8> Passes;
72+
DenseMap<StringRef, Pass *> NameToPassMap;
73+
74+
public:
75+
PassRegistry() = default;
76+
/// Registers \p PassPtr and takes ownership.
77+
Pass &registerPass(std::unique_ptr<Pass> &&PassPtr) {
78+
auto &PassRef = *PassPtr.get();
79+
NameToPassMap[PassRef.getName()] = &PassRef;
80+
Passes.push_back(std::move(PassPtr));
81+
return PassRef;
82+
}
83+
/// \Returns the pass with name \p Name, or null if not registered.
84+
Pass *getPassByName(StringRef Name) const {
85+
auto It = NameToPassMap.find(Name);
86+
return It != NameToPassMap.end() ? It->second : nullptr;
87+
}
88+
#ifndef NDEBUG
89+
void print(raw_ostream &OS) const {
90+
for (const auto &PassPtr : Passes)
91+
OS << PassPtr->getName() << "\n";
92+
}
93+
LLVM_DUMP_METHOD void dump() const;
94+
#endif
95+
};
96+
6897
} // namespace llvm::sandboxir
6998

7099
#endif // LLVM_SANDBOXIR_PASSMANAGER_H

llvm/lib/SandboxIR/Pass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/SandboxIR/Pass.h"
10+
#include "llvm/SandboxIR/PassManager.h"
1011
#include "llvm/Support/Debug.h"
1112

1213
using namespace llvm::sandboxir;

llvm/lib/SandboxIR/PassManager.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ bool FunctionPassManager::runOnFunction(Function &F) {
2020
// TODO: Check ChangeAll against hashes before/after.
2121
return Change;
2222
}
23+
#ifndef NDEBUG
24+
void PassRegistry::dump() const {
25+
print(dbgs());
26+
dbgs() << "\n";
27+
}
28+
#endif // NDEBUG

llvm/unittests/SandboxIR/PassTest.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,32 @@ define void @foo() {
133133
EXPECT_EQ(Buff, "test-fpm(test-pass1,test-pass2)");
134134
#endif // NDEBUG
135135
}
136+
137+
TEST_F(PassTest, PassRegistry) {
138+
class TestPass1 final : public FunctionPass {
139+
public:
140+
TestPass1() : FunctionPass("test-pass1") {}
141+
bool runOnFunction(Function &F) final { return false; }
142+
};
143+
class TestPass2 final : public FunctionPass {
144+
public:
145+
TestPass2() : FunctionPass("test-pass2") {}
146+
bool runOnFunction(Function &F) final { return false; }
147+
};
148+
149+
PassRegistry Registry;
150+
auto &TP1 = Registry.registerPass(std::make_unique<TestPass1>());
151+
auto &TP2 = Registry.registerPass(std::make_unique<TestPass2>());
152+
153+
// Check getPassByName().
154+
EXPECT_EQ(Registry.getPassByName("test-pass1"), &TP1);
155+
EXPECT_EQ(Registry.getPassByName("test-pass2"), &TP2);
156+
157+
#ifndef NDEBUG
158+
// Check print().
159+
std::string Buff;
160+
llvm::raw_string_ostream SS(Buff);
161+
Registry.print(SS);
162+
EXPECT_EQ(Buff, "test-pass1\ntest-pass2\n");
163+
#endif // NDEBUG
164+
}

0 commit comments

Comments
 (0)