-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[CodeGen][NewPM] Extract MachineFunctionProperties modification part to an RAII class #94854
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
Conversation
@llvm/pr-subscribers-llvm-ir Author: None (paperchalice) ChangesModify MachineFunctionProperties in PassModel makes Full diff: https://github.com/llvm/llvm-project/pull/94854.diff 4 Files Affected:
diff --git a/llvm/include/llvm/CodeGen/MachinePassManager.h b/llvm/include/llvm/CodeGen/MachinePassManager.h
index 7d15664fbe754..0cf872e2aec8a 100644
--- a/llvm/include/llvm/CodeGen/MachinePassManager.h
+++ b/llvm/include/llvm/CodeGen/MachinePassManager.h
@@ -36,35 +36,20 @@ class MachineFunction;
extern template class AnalysisManager<MachineFunction>;
using MachineFunctionAnalysisManager = AnalysisManager<MachineFunction>;
-namespace detail {
-
-template <typename PassT>
-struct MachinePassModel
- : PassModel<MachineFunction, PassT, MachineFunctionAnalysisManager> {
- explicit MachinePassModel(PassT &&Pass)
- : PassModel<MachineFunction, PassT, MachineFunctionAnalysisManager>(
- std::move(Pass)) {}
-
- friend void swap(MachinePassModel &LHS, MachinePassModel &RHS) {
- using std::swap;
- swap(LHS.Pass, RHS.Pass);
- }
-
- MachinePassModel &operator=(MachinePassModel RHS) {
- swap(*this, RHS);
- return *this;
- }
-
- MachinePassModel &operator=(const MachinePassModel &) = delete;
- PreservedAnalyses run(MachineFunction &IR,
- MachineFunctionAnalysisManager &AM) override {
+/// An RAII based helper class to modify MachineFunctionProperties when running
+/// pass. Define a MFPropsModifier in PassT::run to set
+/// MachineFunctionProperties properly.
+template <typename PassT> class MFPropsModifier {
+public:
+ MFPropsModifier(PassT &P_, MachineFunction &MF_) : P(P_), MF(MF_) {
+ auto &MFProps = MF.getProperties();
#ifndef NDEBUG
- if constexpr (is_detected<has_get_required_properties_t, PassT>::value) {
- auto &MFProps = IR.getProperties();
- auto RequiredProperties = this->Pass.getRequiredProperties();
+ if constexpr (has_get_required_properties_v<PassT>) {
+ auto &MFProps = MF.getProperties();
+ auto RequiredProperties = P.getRequiredProperties();
if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
errs() << "MachineFunctionProperties required by " << PassT::name()
- << " pass are not met by function " << IR.getName() << ".\n"
+ << " pass are not met by function " << MF.getName() << ".\n"
<< "Required properties: ";
RequiredProperties.print(errs());
errs() << "\nCurrent properties: ";
@@ -73,31 +58,36 @@ struct MachinePassModel
report_fatal_error("MachineFunctionProperties check failed");
}
}
-#endif
-
- auto PA = this->Pass.run(IR, AM);
+#endif // NDEBUG
+ if constexpr (has_get_cleared_properties_v<PassT>)
+ MFProps.reset(P.getClearedProperties());
+ }
- if constexpr (is_detected<has_get_set_properties_t, PassT>::value)
- IR.getProperties().set(this->Pass.getSetProperties());
- if constexpr (is_detected<has_get_cleared_properties_t, PassT>::value)
- IR.getProperties().reset(this->Pass.getClearedProperties());
- return PA;
+ ~MFPropsModifier() {
+ if constexpr (has_get_set_properties_v<PassT>) {
+ auto &MFProps = MF.getProperties();
+ MFProps.set(P.getSetProperties());
+ }
}
private:
+ PassT &P;
+ MachineFunction &MF;
+
template <typename T>
- using has_get_required_properties_t =
- decltype(std::declval<T &>().getRequiredProperties());
+ static constexpr bool has_get_required_properties_v =
+ is_detected<decltype(std::declval<T &>().getRequiredProperties()),
+ T>::value;
template <typename T>
- using has_get_set_properties_t =
- decltype(std::declval<T &>().getSetProperties());
+ static constexpr bool has_get_set_properties_v =
+ is_detected<decltype(std::declval<T &>().getSetProperties()), T>::value;
template <typename T>
- using has_get_cleared_properties_t =
- decltype(std::declval<T &>().getClearedProperties());
+ static constexpr bool has_get_cleared_properties_v =
+ is_detected<decltype(std::declval<T &>().getClearedProperties()),
+ T>::value;
};
-} // namespace detail
using MachineFunctionAnalysisManagerModuleProxy =
InnerAnalysisManagerProxy<MachineFunctionAnalysisManager, Module>;
@@ -219,21 +209,6 @@ createFunctionToMachineFunctionPassAdaptor(MachineFunctionPassT &&Pass) {
new PassModelT(std::forward<MachineFunctionPassT>(Pass))));
}
-template <>
-template <typename PassT>
-void PassManager<MachineFunction>::addPass(PassT &&Pass) {
- 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_same_v<PassT, PassManager<MachineFunction>>) {
- for (auto &P : Pass.Passes)
- Passes.push_back(std::move(P));
- } else {
- Passes.push_back(std::unique_ptr<MachinePassModelT>(
- new MachinePassModelT(std::forward<PassT>(Pass))));
- }
-}
-
template <>
PreservedAnalyses
PassManager<MachineFunction>::run(MachineFunction &,
diff --git a/llvm/include/llvm/IR/PassManager.h b/llvm/include/llvm/IR/PassManager.h
index d701481202f8d..da25b8b495f89 100644
--- a/llvm/include/llvm/IR/PassManager.h
+++ b/llvm/include/llvm/IR/PassManager.h
@@ -245,24 +245,28 @@ class PassManager : public PassInfoMixin<
return PA;
}
- // FIXME: Revert to enable_if style when gcc >= 11.1
- template <typename PassT> LLVM_ATTRIBUTE_MINSIZE void addPass(PassT &&Pass) {
+ template <typename PassT>
+ LLVM_ATTRIBUTE_MINSIZE
+ std::enable_if_t<!std::is_same<PassT, PassManager>::value>
+ addPass(PassT &&Pass) {
using PassModelT =
detail::PassModel<IRUnitT, PassT, AnalysisManagerT, ExtraArgTs...>;
- if constexpr (!std::is_same_v<PassT, PassManager>) {
- // Do not use make_unique or emplace_back, they cause too many template
- // instantiations, causing terrible compile times.
- Passes.push_back(std::unique_ptr<PassConceptT>(
- new PassModelT(std::forward<PassT>(Pass))));
- } else {
- /// When adding a pass manager pass that has the same type as this pass
- /// manager, simply move the passes over. This is because we don't have
- /// use cases rely on executing nested pass managers. Doing this could
- /// reduce implementation complexity and avoid potential invalidation
- /// issues that may happen with nested pass managers of the same type.
- for (auto &P : Pass.Passes)
- Passes.push_back(std::move(P));
- }
+ // Do not use make_unique or emplace_back, they cause too many template
+ // instantiations, causing terrible compile times.
+ Passes.push_back(std::unique_ptr<PassConceptT>(
+ new PassModelT(std::forward<PassT>(Pass))));
+ }
+
+ /// When adding a pass manager pass that has the same type as this pass
+ /// manager, simply move the passes over. This is because we don't have
+ /// use cases rely on executing nested pass managers. Doing this could
+ /// reduce implementation complexity and avoid potential invalidation
+ /// issues that may happen with nested pass managers of the same type.
+ template <typename PassT>
+ LLVM_ATTRIBUTE_MINSIZE
+ std::enable_if_t<std::is_same<PassT, PassManager>::value> {
+ for (auto &P : Pass.Passes)
+ Passes.push_back(std::move(P));
}
/// Returns if the pass manager contains any passes.
diff --git a/llvm/lib/CodeGen/RegAllocFast.cpp b/llvm/lib/CodeGen/RegAllocFast.cpp
index 09ce8c42a3850..f2854090bdd91 100644
--- a/llvm/lib/CodeGen/RegAllocFast.cpp
+++ b/llvm/lib/CodeGen/RegAllocFast.cpp
@@ -1790,6 +1790,7 @@ bool RegAllocFastImpl::runOnMachineFunction(MachineFunction &MF) {
PreservedAnalyses RegAllocFastPass::run(MachineFunction &MF,
MachineFunctionAnalysisManager &) {
+ MFPropsModifier _(*this, MF);
RegAllocFastImpl Impl(Opts.Filter, Opts.ClearVRegs);
bool Changed = Impl.runOnMachineFunction(MF);
if (!Changed)
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 2c56b04a1d9c8..7c7367db1c549 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -385,7 +385,8 @@ class TriggerVerifierErrorPass
class RequireAllMachineFunctionPropertiesPass
: public PassInfoMixin<RequireAllMachineFunctionPropertiesPass> {
public:
- PreservedAnalyses run(MachineFunction &, MachineFunctionAnalysisManager &) {
+ PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &) {
+ MFPropsModifier _(*this, MF);
return PreservedAnalyses::none();
}
|
…to an RAII class Modify MachineFunctionProperties in PassModel makes `PassT P; P.run(...);` not work properly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry for the slow review, I've been pretty swamped recently
lg with comment addressed, although I'll probably send out some followup changes at some point to simplify things even more
@@ -245,24 +245,27 @@ class PassManager : public PassInfoMixin< | |||
return PA; | |||
} | |||
|
|||
// FIXME: Revert to enable_if style when gcc >= 11.1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://www.llvm.org/docs/GettingStarted.html#getting-a-modern-host-c-toolchain says we still support some gcc <11 (this should be a separate PR anyway)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was introduced in b361b53 to distinguish machine function passes and ir passes. MachinePassModel
is removed now, so we can revert to enable_if
form.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah sorry, I was mixing up enable_if_t and enable_if
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/145/builds/206 Here is the relevant piece of the build log for the reference:
|
…96384) Buildbot `clang-ppc64le-rhel` failed with: ```sh error: 'MFPropsModifier' may not intend to support class template argument deduction [-Werror,-Wctad-maybe-unsupported] note: add a deduction guide to suppress this warning ``` after #94854. This PR adds deduction guide explicitly to suppress warning.
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/168/builds/287 Here is the relevant piece of the build log for the reference:
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/66/builds/437 Here is the relevant piece of the build log for the reference:
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/109/builds/255 Here is the relevant piece of the build log for the reference:
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/51/builds/363 Here is the relevant piece of the build log for the reference:
|
…to an RAII class (llvm#94854) Modify MachineFunctionProperties in PassModel makes `PassT P; P.run(...);` not work properly. This is a necessary compromise.
…lvm#96384) Buildbot `clang-ppc64le-rhel` failed with: ```sh error: 'MFPropsModifier' may not intend to support class template argument deduction [-Werror,-Wctad-maybe-unsupported] note: add a deduction guide to suppress this warning ``` after llvm#94854. This PR adds deduction guide explicitly to suppress warning.
Modify MachineFunctionProperties in PassModel makes
PassT P; P.run(...);
not work properly. This is a necessary compromise.