Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SandboxIR] PassManager #107932

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions llvm/include/llvm/SandboxIR/Pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class Pass {
Pass.print(OS);
return OS;
}
void print(raw_ostream &OS) const { OS << Name; }
LLVM_DUMP_METHOD void dump() const;
virtual void print(raw_ostream &OS) const { OS << Name; }
LLVM_DUMP_METHOD virtual void dump() const;
#endif
};

Expand Down
70 changes: 70 additions & 0 deletions llvm/include/llvm/SandboxIR/PassManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//===- PassManager.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
//
//===----------------------------------------------------------------------===//
//
// Registers and executes the Sandbox IR passes.
//
// The pass manager contains an ordered sequence of passes that it runs in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems error prone for the pass manager to not own the pass (e.g. stack references). e.g. in the new pass manager if you want to run a pass multiple times, you just create multiple instances of it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pass registry would own the passes and would also provide the name-to-pass lookup so that you can build the pipeline from a pipeline string like "pass1,pass2".

If the pass manager owns the passes, then how is the mapping from names to passes handled?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see includes of PassRegistry.def, especially in PassBuilder.cpp. it's basically a string comparison chain

I think your way is fine, as long as your explanation of the registry owning passes is documented

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, it shouldn't be too hard to follow a similar design. Let me try it out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I will stick with this design for now. Using a PassRegistry.def complicates the design quite a bit and I am not sure it is worth it at this point. We can revisit it later if needed.

// order. The passes are owned by the PassRegistry, not by the PassManager.
//
// Note that in this design a pass manager is also a pass. So a pass manager
// runs when it is it's turn to run in its parent pass-manager pass pipeline.
//

#ifndef LLVM_SANDBOXIR_PASSMANAGER_H
#define LLVM_SANDBOXIR_PASSMANAGER_H

#include "llvm/ADT/STLExtras.h"
#include "llvm/SandboxIR/Pass.h"
#include "llvm/Support/Debug.h"

namespace llvm::sandboxir {

class Value;

/// Base class.
template <typename ParentPass, typename ContainedPass>
class PassManager : public ParentPass {
protected:
/// The list of passes that this pass manager will run.
SmallVector<ContainedPass *> Passes;

PassManager(StringRef Name) : ParentPass(Name) {}
PassManager(const PassManager &) = delete;
virtual ~PassManager() = default;
PassManager &operator=(const PassManager &) = delete;

public:
/// Adds \p Pass to the pass pipeline.
void addPass(ContainedPass *Pass) {
// TODO: Check that Pass's class type works with this PassManager type.
Passes.push_back(Pass);
}
#ifndef NDEBUG
void print(raw_ostream &OS) const override {
OS << this->getName();
OS << "(";
interleave(Passes, OS, [&OS](auto *Pass) { OS << Pass->getName(); }, ",");
OS << ")";
}
LLVM_DUMP_METHOD void dump() const override {
print(dbgs());
dbgs() << "\n";
}
#endif
};

class FunctionPassManager final
: public PassManager<FunctionPass, FunctionPass> {
public:
FunctionPassManager(StringRef Name) : PassManager(Name) {}
bool runOnFunction(Function &F) final;
};

} // namespace llvm::sandboxir

#endif // LLVM_SANDBOXIR_PASSMANAGER_H
1 change: 1 addition & 0 deletions llvm/lib/SandboxIR/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_llvm_component_library(LLVMSandboxIR
Pass.cpp
PassManager.cpp
SandboxIR.cpp
Tracker.cpp
Type.cpp
Expand Down
22 changes: 22 additions & 0 deletions llvm/lib/SandboxIR/PassManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===- PassManager.cpp - Runs a pipeline of Sandbox IR passes -------------===//
//
// 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/PassManager.h"
#include "llvm/SandboxIR/SandboxIR.h"

using namespace llvm::sandboxir;

bool FunctionPassManager::runOnFunction(Function &F) {
bool Change = false;
for (FunctionPass *Pass : Passes) {
Change |= Pass->runOnFunction(F);
// TODO: run the verifier.
}
// TODO: Check ChangeAll against hashes before/after.
return Change;
}
51 changes: 51 additions & 0 deletions llvm/unittests/SandboxIR/PassTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "llvm/SandboxIR/Pass.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Module.h"
#include "llvm/SandboxIR/PassManager.h"
#include "llvm/SandboxIR/SandboxIR.h"
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"
Expand Down Expand Up @@ -82,3 +83,53 @@ define void @foo() {
EXPECT_DEATH(TestNamePass("-dash"), ".*start with.*");
#endif
}

TEST_F(PassTest, FunctionPassManager) {
auto *F = parseFunction(R"IR(
define void @foo() {
ret void
}
)IR",
"foo");
class TestPass1 final : public FunctionPass {
unsigned &BBCnt;

public:
TestPass1(unsigned &BBCnt) : FunctionPass("test-pass1"), BBCnt(BBCnt) {}
bool runOnFunction(Function &F) final {
for ([[maybe_unused]] auto &BB : F)
++BBCnt;
return false;
}
};
class TestPass2 final : public FunctionPass {
unsigned &BBCnt;

public:
TestPass2(unsigned &BBCnt) : FunctionPass("test-pass2"), BBCnt(BBCnt) {}
bool runOnFunction(Function &F) final {
for ([[maybe_unused]] auto &BB : F)
++BBCnt;
return false;
}
};
unsigned BBCnt1 = 0;
unsigned BBCnt2 = 0;
TestPass1 TPass1(BBCnt1);
TestPass2 TPass2(BBCnt2);

FunctionPassManager FPM("test-fpm");
FPM.addPass(&TPass1);
FPM.addPass(&TPass2);
// Check runOnFunction().
FPM.runOnFunction(*F);
EXPECT_EQ(BBCnt1, 1u);
EXPECT_EQ(BBCnt2, 1u);
#ifndef NDEBUG
// Check dump().
std::string Buff;
llvm::raw_string_ostream SS(Buff);
FPM.print(SS);
EXPECT_EQ(Buff, "test-fpm(test-pass1,test-pass2)");
#endif // NDEBUG
}
Loading