Skip to content

Commit e78236c

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 e78236c

File tree

6 files changed

+180
-0
lines changed

6 files changed

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

llvm/lib/Transforms/Vectorize/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_llvm_component_library(LLVMVectorize
33
LoopIdiomVectorize.cpp
44
LoopVectorizationLegality.cpp
55
LoopVectorize.cpp
6+
SandboxVectorizer/Pass.cpp
67
SandboxVectorizer/SandboxVectorizer.cpp
78
SLPVectorizer.cpp
89
Vectorize.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/Transforms/Vectorize/SandboxVectorizer/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/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ add_subdirectory(Remarks)
4545
add_subdirectory(Passes)
4646
add_subdirectory(ProfileData)
4747
add_subdirectory(SandboxIR)
48+
add_subdirectory(SandboxVectorizer)
4849
add_subdirectory(Support)
4950
add_subdirectory(TableGen)
5051
add_subdirectory(Target)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
set(LLVM_LINK_COMPONENTS
2+
AsmParser
3+
SandboxIR
4+
Core
5+
)
6+
7+
add_llvm_unittest(SandboxVectorizerTests
8+
PassTest.cpp
9+
)
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/Transforms/Vectorize/SandboxVectorizer/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)