Skip to content

Commit f12e10b

Browse files
authored
[SandboxVec] Implement Pass class (#107617)
This patch implements the Pass base class and the FunctionPass sub-class that operate on Sandbox IR.
1 parent bdf0224 commit f12e10b

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed

llvm/include/llvm/SandboxIR/Pass.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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_SANDBOXIR_PASS_H
10+
#define LLVM_SANDBOXIR_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+
protected:
22+
/// The pass name. This is also used as a command-line flag and should not
23+
/// contain whitespaces.
24+
const std::string Name;
25+
26+
public:
27+
Pass(StringRef Name) : Name(Name) {
28+
assert(!Name.contains(' ') &&
29+
"A pass name should not contain whitespaces!");
30+
assert(!Name.starts_with('-') && "A pass name should not start with '-'!");
31+
}
32+
virtual ~Pass() {}
33+
/// \Returns the name of the pass.
34+
StringRef getName() const { return Name; }
35+
#ifndef NDEBUG
36+
friend raw_ostream &operator<<(raw_ostream &OS, const Pass &Pass) {
37+
Pass.print(OS);
38+
return OS;
39+
}
40+
void print(raw_ostream &OS) const { OS << Name; }
41+
LLVM_DUMP_METHOD void dump() const;
42+
#endif
43+
};
44+
45+
/// A pass that runs on a sandbox::Function.
46+
class FunctionPass : public Pass {
47+
public:
48+
FunctionPass(StringRef Name) : Pass(Name) {}
49+
/// \Returns true if it modifies \p F.
50+
virtual bool runOnFunction(Function &F) = 0;
51+
};
52+
53+
} // namespace llvm::sandboxir
54+
55+
#endif // LLVM_SANDBOXIR_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+
print(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: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//===- PassTest.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) : FunctionPass("test-pass"), BBCnt(BBCnt) {}
45+
bool runOnFunction(Function &F) final {
46+
for ([[maybe_unused]] auto &BB : F)
47+
++BBCnt;
48+
return false;
49+
}
50+
};
51+
unsigned BBCnt = 0;
52+
TestPass TPass(BBCnt);
53+
// Check getName(),
54+
EXPECT_EQ(TPass.getName(), "test-pass");
55+
// Check classof().
56+
EXPECT_TRUE(llvm::isa<FunctionPass>(TPass));
57+
// Check runOnFunction();
58+
TPass.runOnFunction(*F);
59+
EXPECT_EQ(BBCnt, 1u);
60+
#ifndef NDEBUG
61+
{
62+
// Check print().
63+
std::string Buff;
64+
llvm::raw_string_ostream SS(Buff);
65+
TPass.print(SS);
66+
EXPECT_EQ(Buff, "test-pass");
67+
}
68+
{
69+
// Check operator<<().
70+
std::string Buff;
71+
llvm::raw_string_ostream SS(Buff);
72+
SS << TPass;
73+
EXPECT_EQ(Buff, "test-pass");
74+
}
75+
// Check pass name assertions.
76+
class TestNamePass final : public FunctionPass {
77+
public:
78+
TestNamePass(llvm::StringRef Name) : FunctionPass(Name) {}
79+
bool runOnFunction(Function &F) { return false; }
80+
};
81+
EXPECT_DEATH(TestNamePass("white space"), ".*whitespace.*");
82+
EXPECT_DEATH(TestNamePass("-dash"), ".*start with.*");
83+
#endif
84+
}

0 commit comments

Comments
 (0)