Skip to content

[NewPM] Remove MachinePassInfoMixin #88243

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

Merged
merged 1 commit into from
Apr 11, 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
2 changes: 1 addition & 1 deletion llvm/include/llvm/CodeGen/DeadMachineInstructionElim.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace llvm {

class DeadMachineInstructionElimPass
: public MachinePassInfoMixin<DeadMachineInstructionElimPass> {
: public PassInfoMixin<DeadMachineInstructionElimPass> {
public:
PreservedAnalyses run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM);
Expand Down
3 changes: 1 addition & 2 deletions llvm/include/llvm/CodeGen/FreeMachineFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

namespace llvm {

class FreeMachineFunctionPass
: public MachinePassInfoMixin<FreeMachineFunctionPass> {
class FreeMachineFunctionPass : public PassInfoMixin<FreeMachineFunctionPass> {
public:
PreservedAnalyses run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM);
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/CodeGen/MIRPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ class MachineFunction;
class Module;
template <typename T> class SmallVectorImpl;

class PrintMIRPreparePass : public MachinePassInfoMixin<PrintMIRPreparePass> {
class PrintMIRPreparePass : public PassInfoMixin<PrintMIRPreparePass> {
raw_ostream &OS;

public:
PrintMIRPreparePass(raw_ostream &OS = errs()) : OS(OS) {}
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MFAM);
};

class PrintMIRPass : public MachinePassInfoMixin<PrintMIRPass> {
class PrintMIRPass : public PassInfoMixin<PrintMIRPass> {
raw_ostream &OS;

public:
Expand Down
108 changes: 40 additions & 68 deletions llvm/include/llvm/CodeGen/MachinePassManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,65 +36,6 @@ class MachineFunction;
extern template class AnalysisManager<MachineFunction>;
using MachineFunctionAnalysisManager = AnalysisManager<MachineFunction>;

/// A CRTP mix-in that provides informational APIs needed for machine passes.
///
/// This provides some boilerplate for types that are machine passes. It
/// automatically mixes in \c PassInfoMixin.
template <typename DerivedT>
struct MachinePassInfoMixin : public PassInfoMixin<DerivedT> {
protected:
class PropertyChanger {
MachineFunction &MF;

template <typename T>
using has_get_required_properties_t =
decltype(std::declval<T &>().getRequiredProperties());

template <typename T>
using has_get_set_properties_t =
decltype(std::declval<T &>().getSetProperties());

template <typename T>
using has_get_cleared_properties_t =
decltype(std::declval<T &>().getClearedProperties());

public:
PropertyChanger(MachineFunction &MF) : MF(MF) {
#ifndef NDEBUG
if constexpr (is_detected<has_get_required_properties_t,
DerivedT>::value) {
auto &MFProps = MF.getProperties();
auto RequiredProperties = DerivedT::getRequiredProperties();
if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
errs() << "MachineFunctionProperties required by " << DerivedT::name()
<< " pass are not met by function " << MF.getName() << ".\n"
<< "Required properties: ";
RequiredProperties.print(errs());
errs() << "\nCurrent properties: ";
MFProps.print(errs());
errs() << '\n';
report_fatal_error("MachineFunctionProperties check failed");
}
}
#endif
}

~PropertyChanger() {
if constexpr (is_detected<has_get_set_properties_t, DerivedT>::value)
MF.getProperties().set(DerivedT::getSetProperties());
if constexpr (is_detected<has_get_cleared_properties_t, DerivedT>::value)
MF.getProperties().reset(DerivedT::getClearedProperties());
}
};

public:
PreservedAnalyses runImpl(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
PropertyChanger PC(MF);
return static_cast<DerivedT *>(this)->run(MF, MFAM);
}
};

namespace detail {

template <typename PassT>
Expand All @@ -117,8 +58,44 @@ struct MachinePassModel
MachinePassModel &operator=(const MachinePassModel &) = delete;
PreservedAnalyses run(MachineFunction &IR,
MachineFunctionAnalysisManager &AM) override {
return this->Pass.runImpl(IR, AM);
#ifndef NDEBUG
if constexpr (is_detected<has_get_required_properties_t, PassT>::value) {
auto &MFProps = IR.getProperties();
auto RequiredProperties = PassT::getRequiredProperties();
if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
errs() << "MachineFunctionProperties required by " << PassT::name()
<< " pass are not met by function " << IR.getName() << ".\n"
<< "Required properties: ";
RequiredProperties.print(errs());
errs() << "\nCurrent properties: ";
MFProps.print(errs());
errs() << '\n';
report_fatal_error("MachineFunctionProperties check failed");
}
}
#endif

auto PA = this->Pass.run(IR, AM);

if constexpr (is_detected<has_get_set_properties_t, PassT>::value)
IR.getProperties().set(PassT::getSetProperties());
if constexpr (is_detected<has_get_cleared_properties_t, PassT>::value)
IR.getProperties().reset(PassT::getClearedProperties());
return PA;
}

private:
template <typename T>
using has_get_required_properties_t =
decltype(std::declval<T &>().getRequiredProperties());

template <typename T>
using has_get_set_properties_t =
decltype(std::declval<T &>().getSetProperties());

template <typename T>
using has_get_cleared_properties_t =
decltype(std::declval<T &>().getClearedProperties());
};
} // namespace detail

Expand Down Expand Up @@ -246,20 +223,15 @@ createModuleToMachineFunctionPassAdaptor(MachineFunctionPassT &&Pass) {
template <>
template <typename PassT>
void PassManager<MachineFunction>::addPass(PassT &&Pass) {
using PassModelT =
detail::PassModel<MachineFunction, PassT, MachineFunctionAnalysisManager>;
using MachinePassModelT = detail::MachinePassModel<PassT>;
// Do not use make_unique or emplace_back, they cause too many template
// instantiations, causing terrible compile times.
if constexpr (std::is_base_of_v<MachinePassInfoMixin<PassT>, PassT>) {
Passes.push_back(std::unique_ptr<PassConceptT>(
new MachinePassModelT(std::forward<PassT>(Pass))));
} else if constexpr (std::is_same_v<PassT, PassManager<MachineFunction>>) {
if constexpr (std::is_same_v<PassT, PassManager<MachineFunction>>) {
for (auto &P : Pass.Passes)
Passes.push_back(std::move(P));
} else {
Passes.push_back(std::unique_ptr<PassConceptT>(
new PassModelT(std::forward<PassT>(Pass))));
Passes.push_back(std::unique_ptr<MachinePassModelT>(
new MachinePassModelT(std::forward<PassT>(Pass))));
}
}

Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/Passes/CodeGenPassBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ namespace llvm {
} \
};
#define DUMMY_MACHINE_MODULE_PASS(NAME, PASS_NAME) \
struct PASS_NAME : public MachinePassInfoMixin<PASS_NAME> { \
struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
template <typename... Ts> PASS_NAME(Ts &&...) {} \
PreservedAnalyses run(Module &, ModuleAnalysisManager &) { \
return PreservedAnalyses::all(); \
} \
};
#define DUMMY_MACHINE_FUNCTION_PASS(NAME, PASS_NAME) \
struct PASS_NAME : public MachinePassInfoMixin<PASS_NAME> { \
struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
template <typename... Ts> PASS_NAME(Ts &&...) {} \
PreservedAnalyses run(MachineFunction &, \
MachineFunctionAnalysisManager &) { \
Expand Down
3 changes: 1 addition & 2 deletions llvm/include/llvm/Passes/PassBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -909,8 +909,7 @@ struct NoOpLoopPass : PassInfoMixin<NoOpLoopPass> {
};

/// No-op machine function pass which does nothing.
struct NoOpMachineFunctionPass
: public MachinePassInfoMixin<NoOpMachineFunctionPass> {
struct NoOpMachineFunctionPass : public PassInfoMixin<NoOpMachineFunctionPass> {
PreservedAnalyses run(MachineFunction &, MachineFunctionAnalysisManager &) {
return PreservedAnalyses::all();
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ class TriggerVerifierErrorPass
// A pass requires all MachineFunctionProperties.
// DO NOT USE THIS EXCEPT FOR TESTING!
class RequireAllMachineFunctionPropertiesPass
: public MachinePassInfoMixin<RequireAllMachineFunctionPropertiesPass> {
: public PassInfoMixin<RequireAllMachineFunctionPropertiesPass> {
public:
PreservedAnalyses run(MachineFunction &, MachineFunctionAnalysisManager &) {
return PreservedAnalyses::none();
Expand Down
2 changes: 1 addition & 1 deletion llvm/unittests/MIR/PassBuilderCallbacksTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ template <typename DerivedT> class MockAnalysisHandleBase {

template <typename DerivedT> class MockPassHandleBase {
public:
class Pass : public MachinePassInfoMixin<Pass> {
class Pass : public PassInfoMixin<Pass> {
friend MockPassHandleBase;

DerivedT *Handle;
Expand Down