[llvm] [SandboxVec] Implement Pass class (PR #107617)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 6 10:26:13 PDT 2024
https://github.com/vporpo created https://github.com/llvm/llvm-project/pull/107617
This patch implements the Pass base class and the FunctionPass sub-class that operate on Sandbox IR.
>From e78236c52a1ebae91bb81db24352bbddce7484a8 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] [SandboxVec] Implement Pass class
This patch implements the Pass base class and the FunctionPass sub-class that
operate on Sandbox IR.
---
.../Vectorize/SandboxVectorizer/Pass.h | 83 +++++++++++++++++++
llvm/lib/Transforms/Vectorize/CMakeLists.txt | 1 +
.../Vectorize/SandboxVectorizer/Pass.cpp | 19 +++++
llvm/unittests/CMakeLists.txt | 1 +
.../SandboxVectorizer/CMakeLists.txt | 9 ++
llvm/unittests/SandboxVectorizer/PassTest.cpp | 67 +++++++++++++++
6 files changed, 180 insertions(+)
create mode 100644 llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Pass.h
create mode 100644 llvm/lib/Transforms/Vectorize/SandboxVectorizer/Pass.cpp
create mode 100644 llvm/unittests/SandboxVectorizer/CMakeLists.txt
create mode 100644 llvm/unittests/SandboxVectorizer/PassTest.cpp
diff --git a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Pass.h b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Pass.h
new file mode 100644
index 00000000000000..fa45e4808c0f6f
--- /dev/null
+++ b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Pass.h
@@ -0,0 +1,83 @@
+//===- 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"
+#include <string>
+
+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(const std::string &Name, const std::string &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(const std::string &Name, const std::string &Flag, ClassID PassID)
+ : Pass(Name, Flag, PassID) {}
+
+public:
+ FunctionPass(const std::string &Name, const std::string &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/Transforms/Vectorize/CMakeLists.txt b/llvm/lib/Transforms/Vectorize/CMakeLists.txt
index 649faad48d71e5..2406856d4eaf09 100644
--- a/llvm/lib/Transforms/Vectorize/CMakeLists.txt
+++ b/llvm/lib/Transforms/Vectorize/CMakeLists.txt
@@ -3,6 +3,7 @@ add_llvm_component_library(LLVMVectorize
LoopIdiomVectorize.cpp
LoopVectorizationLegality.cpp
LoopVectorize.cpp
+ SandboxVectorizer/Pass.cpp
SandboxVectorizer/SandboxVectorizer.cpp
SLPVectorizer.cpp
Vectorize.cpp
diff --git a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Pass.cpp b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Pass.cpp
new file mode 100644
index 00000000000000..686c75af5586fd
--- /dev/null
+++ b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/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/Transforms/Vectorize/SandboxVectorizer/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/CMakeLists.txt b/llvm/unittests/CMakeLists.txt
index 911ede701982f6..e709cf161cf6ad 100644
--- a/llvm/unittests/CMakeLists.txt
+++ b/llvm/unittests/CMakeLists.txt
@@ -45,6 +45,7 @@ add_subdirectory(Remarks)
add_subdirectory(Passes)
add_subdirectory(ProfileData)
add_subdirectory(SandboxIR)
+add_subdirectory(SandboxVectorizer)
add_subdirectory(Support)
add_subdirectory(TableGen)
add_subdirectory(Target)
diff --git a/llvm/unittests/SandboxVectorizer/CMakeLists.txt b/llvm/unittests/SandboxVectorizer/CMakeLists.txt
new file mode 100644
index 00000000000000..6c32364ff412d6
--- /dev/null
+++ b/llvm/unittests/SandboxVectorizer/CMakeLists.txt
@@ -0,0 +1,9 @@
+set(LLVM_LINK_COMPONENTS
+ AsmParser
+ SandboxIR
+ Core
+ )
+
+add_llvm_unittest(SandboxVectorizerTests
+ PassTest.cpp
+ )
diff --git a/llvm/unittests/SandboxVectorizer/PassTest.cpp b/llvm/unittests/SandboxVectorizer/PassTest.cpp
new file mode 100644
index 00000000000000..7dff8830bd7bb5
--- /dev/null
+++ b/llvm/unittests/SandboxVectorizer/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/Transforms/Vectorize/SandboxVectorizer/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);
+}
More information about the llvm-commits
mailing list