Skip to content

Commit 395bead

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 395bead

File tree

4 files changed

+54
-73
lines changed

4 files changed

+54
-73
lines changed

llvm/include/llvm/CodeGen/MachinePassManager.h

Lines changed: 31 additions & 56 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,31 +58,36 @@ 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>
89-
using has_get_required_properties_t =
90-
decltype(std::declval<T &>().getRequiredProperties());
78+
static constexpr bool has_get_required_properties_v =
79+
is_detected<decltype(std::declval<T &>().getRequiredProperties()),
80+
T>::value;
9181

9282
template <typename T>
93-
using has_get_set_properties_t =
94-
decltype(std::declval<T &>().getSetProperties());
83+
static constexpr bool has_get_set_properties_v =
84+
is_detected<decltype(std::declval<T &>().getSetProperties()), T>::value;
9585

9686
template <typename T>
97-
using has_get_cleared_properties_t =
98-
decltype(std::declval<T &>().getClearedProperties());
87+
static constexpr bool has_get_cleared_properties_v =
88+
is_detected<decltype(std::declval<T &>().getClearedProperties()),
89+
T>::value;
9990
};
100-
} // namespace detail
10191

10292
using MachineFunctionAnalysisManagerModuleProxy =
10393
InnerAnalysisManagerProxy<MachineFunctionAnalysisManager, Module>;
@@ -219,21 +209,6 @@ createFunctionToMachineFunctionPassAdaptor(MachineFunctionPassT &&Pass) {
219209
new PassModelT(std::forward<MachineFunctionPassT>(Pass))));
220210
}
221211

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-
237212
template <>
238213
PreservedAnalyses
239214
PassManager<MachineFunction>::run(MachineFunction &,

llvm/include/llvm/IR/PassManager.h

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -245,24 +245,28 @@ 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
250+
std::enable_if_t<!std::is_same<PassT, PassManager>::value>
251+
addPass(PassT &&Pass) {
250252
using PassModelT =
251253
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-
}
254+
// Do not use make_unique or emplace_back, they cause too many template
255+
// instantiations, causing terrible compile times.
256+
Passes.push_back(std::unique_ptr<PassConceptT>(
257+
new PassModelT(std::forward<PassT>(Pass))));
258+
}
259+
260+
/// When adding a pass manager pass that has the same type as this pass
261+
/// manager, simply move the passes over. This is because we don't have
262+
/// use cases rely on executing nested pass managers. Doing this could
263+
/// reduce implementation complexity and avoid potential invalidation
264+
/// issues that may happen with nested pass managers of the same type.
265+
template <typename PassT>
266+
LLVM_ATTRIBUTE_MINSIZE
267+
std::enable_if_t<std::is_same<PassT, PassManager>::value> {
268+
for (auto &P : Pass.Passes)
269+
Passes.push_back(std::move(P));
266270
}
267271

268272
/// 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)