[llvm] [SandboxVec] Implement Pass class (PR #107617)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 6 11:08:52 PDT 2024
https://github.com/vporpo updated https://github.com/llvm/llvm-project/pull/107617
>From 4623e12dc2c8614f581f5ec56d0796e2f670a475 Mon Sep 17 00:00:00 2001
From: Vasileios Porpodas <vporpodas at google.com>
Date: Thu, 5 Sep 2024 17:01:39 -0700
Subject: [PATCH 1/2] [SandboxVec] Implement Pass class
This patch implements the Pass base class and the FunctionPass sub-class that
operate on Sandbox IR.
---
llvm/include/llvm/SandboxIR/Pass.h | 82 +++++++++++++++++++++++++
llvm/lib/SandboxIR/CMakeLists.txt | 1 +
llvm/lib/SandboxIR/Pass.cpp | 19 ++++++
llvm/unittests/SandboxIR/CMakeLists.txt | 1 +
llvm/unittests/SandboxIR/PassTest.cpp | 67 ++++++++++++++++++++
5 files changed, 170 insertions(+)
create mode 100644 llvm/include/llvm/SandboxIR/Pass.h
create mode 100644 llvm/lib/SandboxIR/Pass.cpp
create mode 100644 llvm/unittests/SandboxIR/PassTest.cpp
diff --git a/llvm/include/llvm/SandboxIR/Pass.h b/llvm/include/llvm/SandboxIR/Pass.h
new file mode 100644
index 00000000000000..e005f1dc942635
--- /dev/null
+++ b/llvm/include/llvm/SandboxIR/Pass.h
@@ -0,0 +1,82 @@
+//===- Pass.h ---------------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASS_H
+#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASS_H
+
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/raw_ostream.h"
+
+namespace llvm::sandboxir {
+
+class Function;
+
+/// The base class of a Sandbox IR Pass.
+class Pass {
+public:
+ enum class ClassID : unsigned {
+ FunctionPass,
+ };
+ static const char *getSubclassIDStr(ClassID ID) {
+ switch (ID) {
+ case ClassID::FunctionPass:
+ return "FunctionPass";
+ }
+ llvm_unreachable("Unimplemented ID");
+ }
+
+protected:
+ /// The pass name.
+ const std::string Name;
+ /// The command-line flag used to specify that this pass should run.
+ const std::string Flag;
+ /// Used for isa/cast/dyn_cast.
+ ClassID SubclassID;
+
+public:
+ Pass(StringRef Name, StringRef Flag, ClassID SubclassID)
+ : Name(Name), Flag(Flag), SubclassID(SubclassID) {}
+ virtual ~Pass() {}
+ /// \Returns the name of the pass.
+ StringRef getName() const { return Name; }
+ /// \Returns the command-line flag used to enable the pass.
+ StringRef getFlag() const { return Flag; }
+ ClassID getSubclassID() const { return SubclassID; }
+#ifndef NDEBUG
+ friend raw_ostream &operator<<(raw_ostream &OS, const Pass &Pass) {
+ Pass.dump(OS);
+ return OS;
+ }
+ void dump(raw_ostream &OS) const { OS << Name << " " << Flag; }
+ LLVM_DUMP_METHOD void dump() const;
+#endif
+};
+
+/// A pass that runs on a sandbox::Function.
+class FunctionPass : public Pass {
+protected:
+ FunctionPass(StringRef Name, StringRef Flag, ClassID PassID)
+ : Pass(Name, Flag, PassID) {}
+
+public:
+ FunctionPass(StringRef Name, StringRef Flag)
+ : Pass(Name, Flag, ClassID::FunctionPass) {}
+ /// For isa/dyn_cast etc.
+ static bool classof(const Pass *From) {
+ switch (From->getSubclassID()) {
+ case ClassID::FunctionPass:
+ return true;
+ }
+ }
+ /// \Returns true if it modifies \p F.
+ virtual bool runOnFunction(Function &F) = 0;
+};
+
+} // namespace llvm::sandboxir
+
+#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASS_H
diff --git a/llvm/lib/SandboxIR/CMakeLists.txt b/llvm/lib/SandboxIR/CMakeLists.txt
index d94f0642ccc4a1..2f047944e0335e 100644
--- a/llvm/lib/SandboxIR/CMakeLists.txt
+++ b/llvm/lib/SandboxIR/CMakeLists.txt
@@ -1,4 +1,5 @@
add_llvm_component_library(LLVMSandboxIR
+ Pass.cpp
SandboxIR.cpp
Tracker.cpp
Type.cpp
diff --git a/llvm/lib/SandboxIR/Pass.cpp b/llvm/lib/SandboxIR/Pass.cpp
new file mode 100644
index 00000000000000..1453a6d9d101b4
--- /dev/null
+++ b/llvm/lib/SandboxIR/Pass.cpp
@@ -0,0 +1,19 @@
+//===- Pass.cpp - Passes that operate on Sandbox IR -----------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/SandboxIR/Pass.h"
+#include "llvm/Support/Debug.h"
+
+using namespace llvm::sandboxir;
+
+#ifndef NDEBUG
+void Pass::dump() const {
+ dump(dbgs());
+ dbgs() << "\n";
+}
+#endif // NDEBUG
diff --git a/llvm/unittests/SandboxIR/CMakeLists.txt b/llvm/unittests/SandboxIR/CMakeLists.txt
index 2da936bffa02bf..a228637b062a43 100644
--- a/llvm/unittests/SandboxIR/CMakeLists.txt
+++ b/llvm/unittests/SandboxIR/CMakeLists.txt
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
)
add_llvm_unittest(SandboxIRTests
+ PassTest.cpp
SandboxIRTest.cpp
TrackerTest.cpp
TypesTest.cpp
diff --git a/llvm/unittests/SandboxIR/PassTest.cpp b/llvm/unittests/SandboxIR/PassTest.cpp
new file mode 100644
index 00000000000000..ed95d14267e822
--- /dev/null
+++ b/llvm/unittests/SandboxIR/PassTest.cpp
@@ -0,0 +1,67 @@
+//===- PassesTest.cpp -----------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/SandboxIR/Pass.h"
+#include "llvm/AsmParser/Parser.h"
+#include "llvm/IR/Module.h"
+#include "llvm/SandboxIR/SandboxIR.h"
+#include "llvm/Support/SourceMgr.h"
+#include "gtest/gtest.h"
+
+using namespace llvm::sandboxir;
+
+struct PassTest : public testing::Test {
+ llvm::LLVMContext LLVMCtx;
+ std::unique_ptr<llvm::Module> LLVMM;
+ std::unique_ptr<Context> Ctx;
+
+ Function *parseFunction(const char *IR, const char *FuncName) {
+ llvm::SMDiagnostic Err;
+ LLVMM = parseAssemblyString(IR, Err, LLVMCtx);
+ if (!LLVMM)
+ Err.print("PassTest", llvm::errs());
+ Ctx = std::make_unique<Context>(LLVMCtx);
+ return Ctx->createFunction(LLVMM->getFunction(FuncName));
+ }
+};
+
+TEST_F(PassTest, FunctionPass) {
+ auto *F = parseFunction(R"IR(
+define void @foo() {
+ ret void
+}
+)IR",
+ "foo");
+ class TestPass final : public FunctionPass {
+ unsigned &BBCnt;
+
+ public:
+ TestPass(unsigned &BBCnt)
+ : FunctionPass("TestPass", "-test-pass"), BBCnt(BBCnt) {}
+ bool runOnFunction(Function &F) final {
+ for ([[maybe_unused]] auto &BB : F)
+ ++BBCnt;
+ return false;
+ }
+ };
+ unsigned BBCnt = 0;
+ TestPass TPass(BBCnt);
+ // Check getName(),
+ EXPECT_EQ(TPass.getName(), "TestPass");
+ // Check getFlag().
+ EXPECT_EQ(TPass.getFlag(), "-test-pass");
+ // Check getSubclassID().
+ EXPECT_EQ(TPass.getSubclassID(), Pass::ClassID::FunctionPass);
+ // Check getSubclassIDStr().
+ EXPECT_EQ(Pass::getSubclassIDStr(TPass.getSubclassID()), "FunctionPass");
+ // Check classof().
+ EXPECT_TRUE(llvm::isa<FunctionPass>(TPass));
+ // Check runOnFunction();
+ TPass.runOnFunction(*F);
+ EXPECT_EQ(BBCnt, 1u);
+}
>From 1bc221a6f227e6c9be5caa221bd98817a91f8194 Mon Sep 17 00:00:00 2001
From: Vasileios Porpodas <vporpodas at google.com>
Date: Fri, 6 Sep 2024 11:08:08 -0700
Subject: [PATCH 2/2] fixup! [SandboxVec] Implement Pass class
---
llvm/include/llvm/SandboxIR/Pass.h | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/llvm/include/llvm/SandboxIR/Pass.h b/llvm/include/llvm/SandboxIR/Pass.h
index e005f1dc942635..b70cd1fcc6c5ae 100644
--- a/llvm/include/llvm/SandboxIR/Pass.h
+++ b/llvm/include/llvm/SandboxIR/Pass.h
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASS_H
-#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASS_H
+#ifndef LLVM_SANDBOXIR_PASS_H
+#define LLVM_SANDBOXIR_PASS_H
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
@@ -79,4 +79,4 @@ class FunctionPass : public Pass {
} // namespace llvm::sandboxir
-#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASS_H
+#endif // LLVM_SANDBOXIR_PASS_H
More information about the llvm-commits
mailing list