Skip to content

Commit 4623e12

Browse files
committed
[SandboxVec] Implement Pass class
This patch implements the Pass base class and the FunctionPass sub-class that operate on Sandbox IR.
1 parent c1c4251 commit 4623e12

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

llvm/include/llvm/SandboxIR/Pass.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//===- Pass.h ---------------------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASS_H
10+
#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASS_H
11+
12+
#include "llvm/Support/ErrorHandling.h"
13+
#include "llvm/Support/raw_ostream.h"
14+
15+
namespace llvm::sandboxir {
16+
17+
class Function;
18+
19+
/// The base class of a Sandbox IR Pass.
20+
class Pass {
21+
public:
22+
enum class ClassID : unsigned {
23+
FunctionPass,
24+
};
25+
static const char *getSubclassIDStr(ClassID ID) {
26+
switch (ID) {
27+
case ClassID::FunctionPass:
28+
return "FunctionPass";
29+
}
30+
llvm_unreachable("Unimplemented ID");
31+
}
32+
33+
protected:
34+
/// The pass name.
35+
const std::string Name;
36+
/// The command-line flag used to specify that this pass should run.
37+
const std::string Flag;
38+
/// Used for isa/cast/dyn_cast.
39+
ClassID SubclassID;
40+
41+
public:
42+
Pass(StringRef Name, StringRef Flag, ClassID SubclassID)
43+
: Name(Name), Flag(Flag), SubclassID(SubclassID) {}
44+
virtual ~Pass() {}
45+
/// \Returns the name of the pass.
46+
StringRef getName() const { return Name; }
47+
/// \Returns the command-line flag used to enable the pass.
48+
StringRef getFlag() const { return Flag; }
49+
ClassID getSubclassID() const { return SubclassID; }
50+
#ifndef NDEBUG
51+
friend raw_ostream &operator<<(raw_ostream &OS, const Pass &Pass) {
52+
Pass.dump(OS);
53+
return OS;
54+
}
55+
void dump(raw_ostream &OS) const { OS << Name << " " << Flag; }
56+
LLVM_DUMP_METHOD void dump() const;
57+
#endif
58+
};
59+
60+
/// A pass that runs on a sandbox::Function.
61+
class FunctionPass : public Pass {
62+
protected:
63+
FunctionPass(StringRef Name, StringRef Flag, ClassID PassID)
64+
: Pass(Name, Flag, PassID) {}
65+
66+
public:
67+
FunctionPass(StringRef Name, StringRef Flag)
68+
: Pass(Name, Flag, ClassID::FunctionPass) {}
69+
/// For isa/dyn_cast etc.
70+
static bool classof(const Pass *From) {
71+
switch (From->getSubclassID()) {
72+
case ClassID::FunctionPass:
73+
return true;
74+
}
75+
}
76+
/// \Returns true if it modifies \p F.
77+
virtual bool runOnFunction(Function &F) = 0;
78+
};
79+
80+
} // namespace llvm::sandboxir
81+
82+
#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASS_H

llvm/lib/SandboxIR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
add_llvm_component_library(LLVMSandboxIR
2+
Pass.cpp
23
SandboxIR.cpp
34
Tracker.cpp
45
Type.cpp

llvm/lib/SandboxIR/Pass.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===- Pass.cpp - Passes that operate on Sandbox IR -----------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/SandboxIR/Pass.h"
10+
#include "llvm/Support/Debug.h"
11+
12+
using namespace llvm::sandboxir;
13+
14+
#ifndef NDEBUG
15+
void Pass::dump() const {
16+
dump(dbgs());
17+
dbgs() << "\n";
18+
}
19+
#endif // NDEBUG

llvm/unittests/SandboxIR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
55
)
66

77
add_llvm_unittest(SandboxIRTests
8+
PassTest.cpp
89
SandboxIRTest.cpp
910
TrackerTest.cpp
1011
TypesTest.cpp

llvm/unittests/SandboxIR/PassTest.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===- PassesTest.cpp -----------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/SandboxIR/Pass.h"
10+
#include "llvm/AsmParser/Parser.h"
11+
#include "llvm/IR/Module.h"
12+
#include "llvm/SandboxIR/SandboxIR.h"
13+
#include "llvm/Support/SourceMgr.h"
14+
#include "gtest/gtest.h"
15+
16+
using namespace llvm::sandboxir;
17+
18+
struct PassTest : public testing::Test {
19+
llvm::LLVMContext LLVMCtx;
20+
std::unique_ptr<llvm::Module> LLVMM;
21+
std::unique_ptr<Context> Ctx;
22+
23+
Function *parseFunction(const char *IR, const char *FuncName) {
24+
llvm::SMDiagnostic Err;
25+
LLVMM = parseAssemblyString(IR, Err, LLVMCtx);
26+
if (!LLVMM)
27+
Err.print("PassTest", llvm::errs());
28+
Ctx = std::make_unique<Context>(LLVMCtx);
29+
return Ctx->createFunction(LLVMM->getFunction(FuncName));
30+
}
31+
};
32+
33+
TEST_F(PassTest, FunctionPass) {
34+
auto *F = parseFunction(R"IR(
35+
define void @foo() {
36+
ret void
37+
}
38+
)IR",
39+
"foo");
40+
class TestPass final : public FunctionPass {
41+
unsigned &BBCnt;
42+
43+
public:
44+
TestPass(unsigned &BBCnt)
45+
: FunctionPass("TestPass", "-test-pass"), BBCnt(BBCnt) {}
46+
bool runOnFunction(Function &F) final {
47+
for ([[maybe_unused]] auto &BB : F)
48+
++BBCnt;
49+
return false;
50+
}
51+
};
52+
unsigned BBCnt = 0;
53+
TestPass TPass(BBCnt);
54+
// Check getName(),
55+
EXPECT_EQ(TPass.getName(), "TestPass");
56+
// Check getFlag().
57+
EXPECT_EQ(TPass.getFlag(), "-test-pass");
58+
// Check getSubclassID().
59+
EXPECT_EQ(TPass.getSubclassID(), Pass::ClassID::FunctionPass);
60+
// Check getSubclassIDStr().
61+
EXPECT_EQ(Pass::getSubclassIDStr(TPass.getSubclassID()), "FunctionPass");
62+
// Check classof().
63+
EXPECT_TRUE(llvm::isa<FunctionPass>(TPass));
64+
// Check runOnFunction();
65+
TPass.runOnFunction(*F);
66+
EXPECT_EQ(BBCnt, 1u);
67+
}

0 commit comments

Comments
 (0)