Skip to content

Commit b3cb5de

Browse files
committed
[CodeGen][NewPM] Extract MachineFunctionProperties modification part to an RAII class
Modify MachineFunctionProperties in PassModel makes `PassT P; P.run(...);` not work properly.
1 parent c5ff983 commit b3cb5de

File tree

4 files changed

+57
-67
lines changed

4 files changed

+57
-67
lines changed

llvm/include/llvm/CodeGen/MachinePassManager.h

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,20 @@ class MachineFunction;
3636
extern template class AnalysisManager<MachineFunction>;
3737
using MachineFunctionAnalysisManager = AnalysisManager<MachineFunction>;
3838

39-
namespace detail {
40-
41-
template <typename PassT>
42-
struct MachinePassModel
43-
: PassModel<MachineFunction, PassT, MachineFunctionAnalysisManager> {
44-
explicit MachinePassModel(PassT &&Pass)
45-
: PassModel<MachineFunction, PassT, MachineFunctionAnalysisManager>(
46-
std::move(Pass)) {}
47-
48-
friend void swap(MachinePassModel &LHS, MachinePassModel &RHS) {
49-
using std::swap;
50-
swap(LHS.Pass, RHS.Pass);
51-
}
52-
53-
MachinePassModel &operator=(MachinePassModel RHS) {
54-
swap(*this, RHS);
55-
return *this;
56-
}
57-
58-
MachinePassModel &operator=(const MachinePassModel &) = delete;
59-
PreservedAnalyses run(MachineFunction &IR,
60-
MachineFunctionAnalysisManager &AM) override {
39+
/// An RAII based helper class to modify MachineFunctionProperties when running
40+
/// pass. Define a MFPropsModifier in PassT::run to set
41+
/// MachineFunctionProperties properly.
42+
template <typename PassT> class MFPropsModifier {
43+
public:
44+
MFPropsModifier(PassT &P_, MachineFunction &MF_) : P(P_), MF(MF_) {
45+
auto &MFProps = MF.getProperties();
6146
#ifndef NDEBUG
62-
if constexpr (is_detected<has_get_required_properties_t, PassT>::value) {
63-
auto &MFProps = IR.getProperties();
64-
auto RequiredProperties = this->Pass.getRequiredProperties();
47+
if constexpr (has_get_required_properties_v<PassT>) {
48+
auto &MFProps = MF.getProperties();
49+
auto RequiredProperties = P.getRequiredProperties();
6550
if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
6651
errs() << "MachineFunctionProperties required by " << PassT::name()
67-
<< " pass are not met by function " << IR.getName() << ".\n"
52+
<< " pass are not met by function " << MF.getName() << ".\n"
6853
<< "Required properties: ";
6954
RequiredProperties.print(errs());
7055
errs() << "\nCurrent properties: ";
@@ -73,18 +58,22 @@ struct MachinePassModel
7358
report_fatal_error("MachineFunctionProperties check failed");
7459
}
7560
}
76-
#endif
77-
78-
auto PA = this->Pass.run(IR, AM);
61+
#endif // NDEBUG
62+
if constexpr (has_get_cleared_properties_v<PassT>)
63+
MFProps.reset(P.getClearedProperties());
64+
}
7965

80-
if constexpr (is_detected<has_get_set_properties_t, PassT>::value)
81-
IR.getProperties().set(this->Pass.getSetProperties());
82-
if constexpr (is_detected<has_get_cleared_properties_t, PassT>::value)
83-
IR.getProperties().reset(this->Pass.getClearedProperties());
84-
return PA;
66+
~MFPropsModifier() {
67+
if constexpr (has_get_set_properties_v<PassT>) {
68+
auto &MFProps = MF.getProperties();
69+
MFProps.set(P.getSetProperties());
70+
}
8571
}
8672

8773
private:
74+
PassT &P;
75+
MachineFunction &MF;
76+
8877
template <typename T>
8978
using has_get_required_properties_t =
9079
decltype(std::declval<T &>().getRequiredProperties());
@@ -96,8 +85,19 @@ struct MachinePassModel
9685
template <typename T>
9786
using has_get_cleared_properties_t =
9887
decltype(std::declval<T &>().getClearedProperties());
88+
89+
template <typename T>
90+
static constexpr bool has_get_required_properties_v =
91+
is_detected<has_get_required_properties_t, T>::value;
92+
93+
template <typename T>
94+
static constexpr bool has_get_set_properties_v =
95+
is_detected<has_get_set_properties_t, T>::value;
96+
97+
template <typename T>
98+
static constexpr bool has_get_cleared_properties_v =
99+
is_detected<has_get_cleared_properties_t, T>::value;
99100
};
100-
} // namespace detail
101101

102102
using MachineFunctionAnalysisManagerModuleProxy =
103103
InnerAnalysisManagerProxy<MachineFunctionAnalysisManager, Module>;
@@ -219,21 +219,6 @@ createFunctionToMachineFunctionPassAdaptor(MachineFunctionPassT &&Pass) {
219219
new PassModelT(std::forward<MachineFunctionPassT>(Pass))));
220220
}
221221

222-
template <>
223-
template <typename PassT>
224-
void PassManager<MachineFunction>::addPass(PassT &&Pass) {
225-
using MachinePassModelT = detail::MachinePassModel<PassT>;
226-
// Do not use make_unique or emplace_back, they cause too many template
227-
// instantiations, causing terrible compile times.
228-
if constexpr (std::is_same_v<PassT, PassManager<MachineFunction>>) {
229-
for (auto &P : Pass.Passes)
230-
Passes.push_back(std::move(P));
231-
} else {
232-
Passes.push_back(std::unique_ptr<MachinePassModelT>(
233-
new MachinePassModelT(std::forward<PassT>(Pass))));
234-
}
235-
}
236-
237222
template <>
238223
PreservedAnalyses
239224
PassManager<MachineFunction>::run(MachineFunction &,

llvm/include/llvm/IR/PassManager.h

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -245,24 +245,27 @@ class PassManager : public PassInfoMixin<
245245
return PA;
246246
}
247247

248-
// FIXME: Revert to enable_if style when gcc >= 11.1
249-
template <typename PassT> LLVM_ATTRIBUTE_MINSIZE void addPass(PassT &&Pass) {
248+
template <typename PassT>
249+
LLVM_ATTRIBUTE_MINSIZE std::enable_if_t<!std::is_same_v<PassT, PassManager>>
250+
addPass(PassT &&Pass) {
250251
using PassModelT =
251252
detail::PassModel<IRUnitT, PassT, AnalysisManagerT, ExtraArgTs...>;
252-
if constexpr (!std::is_same_v<PassT, PassManager>) {
253-
// Do not use make_unique or emplace_back, they cause too many template
254-
// instantiations, causing terrible compile times.
255-
Passes.push_back(std::unique_ptr<PassConceptT>(
256-
new PassModelT(std::forward<PassT>(Pass))));
257-
} else {
258-
/// When adding a pass manager pass that has the same type as this pass
259-
/// manager, simply move the passes over. This is because we don't have
260-
/// use cases rely on executing nested pass managers. Doing this could
261-
/// reduce implementation complexity and avoid potential invalidation
262-
/// issues that may happen with nested pass managers of the same type.
263-
for (auto &P : Pass.Passes)
264-
Passes.push_back(std::move(P));
265-
}
253+
// Do not use make_unique or emplace_back, they cause too many template
254+
// instantiations, causing terrible compile times.
255+
Passes.push_back(std::unique_ptr<PassConceptT>(
256+
new PassModelT(std::forward<PassT>(Pass))));
257+
}
258+
259+
/// When adding a pass manager pass that has the same type as this pass
260+
/// manager, simply move the passes over. This is because we don't have
261+
/// use cases rely on executing nested pass managers. Doing this could
262+
/// reduce implementation complexity and avoid potential invalidation
263+
/// issues that may happen with nested pass managers of the same type.
264+
template <typename PassT>
265+
LLVM_ATTRIBUTE_MINSIZE std::enable_if_t<std::is_same_v<PassT, PassManager>>
266+
addPass(PassT &&Pass) {
267+
for (auto &P : Pass.Passes)
268+
Passes.push_back(std::move(P));
266269
}
267270

268271
/// Returns if the pass manager contains any passes.

llvm/lib/CodeGen/RegAllocFast.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,7 @@ bool RegAllocFastImpl::runOnMachineFunction(MachineFunction &MF) {
17901790

17911791
PreservedAnalyses RegAllocFastPass::run(MachineFunction &MF,
17921792
MachineFunctionAnalysisManager &) {
1793+
MFPropsModifier _(*this, MF);
17931794
RegAllocFastImpl Impl(Opts.Filter, Opts.ClearVRegs);
17941795
bool Changed = Impl.runOnMachineFunction(MF);
17951796
if (!Changed)

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ class TriggerVerifierErrorPass
385385
class RequireAllMachineFunctionPropertiesPass
386386
: public PassInfoMixin<RequireAllMachineFunctionPropertiesPass> {
387387
public:
388-
PreservedAnalyses run(MachineFunction &, MachineFunctionAnalysisManager &) {
388+
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &) {
389+
MFPropsModifier _(*this, MF);
389390
return PreservedAnalyses::none();
390391
}
391392

0 commit comments

Comments
 (0)