Skip to content

Commit d2d3dfa

Browse files
committed
another method
1 parent 3555fea commit d2d3dfa

File tree

5 files changed

+53
-146
lines changed

5 files changed

+53
-146
lines changed

llvm/include/llvm/CodeGen/MachinePassManager.h

Lines changed: 46 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -44,122 +44,50 @@ using MachineFunctionAnalysisManager = AnalysisManager<MachineFunction>;
4444
/// automatically mixes in \c PassInfoMixin.
4545
template <typename DerivedT>
4646
struct MachinePassInfoMixin : public PassInfoMixin<DerivedT> {
47-
// TODO: Add MachineFunctionProperties support.
48-
};
49-
50-
namespace detail {
51-
struct MachinePassConcept
52-
: PassConcept<MachineFunction, MachineFunctionAnalysisManager> {
53-
virtual MachineFunctionProperties getRequiredProperties() const = 0;
54-
virtual MachineFunctionProperties getSetProperties() const = 0;
55-
virtual MachineFunctionProperties getClearedProperties() const = 0;
56-
};
57-
58-
template <typename PassT> struct MachinePassModel : MachinePassConcept {
59-
explicit MachinePassModel(PassT &&Pass) : Pass(std::move(Pass)) {}
60-
// We have to explicitly define all the special member functions because MSVC
61-
// refuses to generate them.
62-
MachinePassModel(const MachinePassModel &Arg) : Pass(Arg.Pass) {}
63-
MachinePassModel(MachinePassModel &&Arg) : Pass(std::move(Arg.Pass)) {}
47+
class PropertyChanger {
48+
MachineFunction &MF;
6449

65-
friend void swap(MachinePassModel &LHS, MachinePassModel &RHS) {
66-
using std::swap;
67-
swap(LHS.Pass, RHS.Pass);
68-
}
50+
template <typename T>
51+
using has_get_required_properties_t =
52+
decltype(std::declval<T &>().getRequiredProperties());
6953

70-
MachinePassModel &operator=(MachinePassModel RHS) {
71-
swap(*this, RHS);
72-
return *this;
73-
}
54+
template <typename T>
55+
using has_get_set_properties_t =
56+
decltype(std::declval<T &>().getSetProperties());
7457

75-
MachinePassModel &operator=(const MachinePassModel &) = delete;
76-
PreservedAnalyses run(MachineFunction &IR,
77-
MachineFunctionAnalysisManager &AM) override {
78-
return Pass.run(IR, AM);
79-
}
58+
template <typename T>
59+
using has_get_cleared_properties_t =
60+
decltype(std::declval<T &>().getClearedProperties());
8061

81-
void printPipeline(
82-
raw_ostream &OS,
83-
function_ref<StringRef(StringRef)> MapClassName2PassName) override {
84-
Pass.printPipeline(OS, MapClassName2PassName);
85-
}
86-
87-
StringRef name() const override { return PassT::name(); }
88-
89-
template <typename T>
90-
using has_required_t = decltype(std::declval<T &>().isRequired());
91-
template <typename T>
92-
static std::enable_if_t<is_detected<has_required_t, T>::value, bool>
93-
passIsRequiredImpl() {
94-
return T::isRequired();
95-
}
96-
template <typename T>
97-
static std::enable_if_t<!is_detected<has_required_t, T>::value, bool>
98-
passIsRequiredImpl() {
99-
return false;
100-
}
101-
bool isRequired() const override { return passIsRequiredImpl<PassT>(); }
102-
103-
template <typename T>
104-
using has_get_required_properties_t =
105-
decltype(std::declval<T &>().getRequiredProperties());
106-
template <typename T>
107-
static std::enable_if_t<is_detected<has_get_required_properties_t, T>::value,
108-
MachineFunctionProperties>
109-
getRequiredPropertiesImpl() {
110-
return PassT::getRequiredProperties();
111-
}
112-
template <typename T>
113-
static std::enable_if_t<!is_detected<has_get_required_properties_t, T>::value,
114-
MachineFunctionProperties>
115-
getRequiredPropertiesImpl() {
116-
return MachineFunctionProperties();
117-
}
118-
MachineFunctionProperties getRequiredProperties() const override {
119-
return getRequiredPropertiesImpl<PassT>();
120-
}
121-
122-
template <typename T>
123-
using has_get_set_properties_t =
124-
decltype(std::declval<T &>().getSetProperties());
125-
template <typename T>
126-
static std::enable_if_t<is_detected<has_get_set_properties_t, T>::value,
127-
MachineFunctionProperties>
128-
getSetPropertiesImpl() {
129-
return PassT::getSetProperties();
130-
}
131-
template <typename T>
132-
static std::enable_if_t<!is_detected<has_get_set_properties_t, T>::value,
133-
MachineFunctionProperties>
134-
getSetPropertiesImpl() {
135-
return MachineFunctionProperties();
136-
}
137-
MachineFunctionProperties getSetProperties() const override {
138-
return getSetPropertiesImpl<PassT>();
139-
}
140-
141-
template <typename T>
142-
using has_get_cleared_properties_t =
143-
decltype(std::declval<T &>().getClearedProperties());
144-
template <typename T>
145-
static std::enable_if_t<is_detected<has_get_cleared_properties_t, T>::value,
146-
MachineFunctionProperties>
147-
getClearedPropertiesImpl() {
148-
return PassT::getClearedProperties();
149-
}
150-
template <typename T>
151-
static std::enable_if_t<!is_detected<has_get_cleared_properties_t, T>::value,
152-
MachineFunctionProperties>
153-
getClearedPropertiesImpl() {
154-
return MachineFunctionProperties();
155-
}
156-
MachineFunctionProperties getClearedProperties() const override {
157-
return getClearedPropertiesImpl<PassT>();
158-
}
62+
public:
63+
PropertyChanger(MachineFunction &MF) : MF(MF) {
64+
#ifndef NDEBUG
65+
if constexpr (is_detected<has_get_required_properties_t,
66+
DerivedT>::value) {
67+
auto &MFProps = MF.getProperties();
68+
auto RequiredProperties = DerivedT::getRequiredProperties();
69+
if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
70+
errs() << "MachineFunctionProperties required by " << DerivedT::name()
71+
<< " pass are not met by function " << MF.getName() << ".\n"
72+
<< "Required properties: ";
73+
RequiredProperties.print(errs());
74+
errs() << "\nCurrent properties: ";
75+
MFProps.print(errs());
76+
errs() << '\n';
77+
report_fatal_error("MachineFunctionProperties check failed");
78+
}
79+
#endif
80+
}
81+
}
15982

160-
PassT Pass;
83+
~PropertyChanger() {
84+
if constexpr (is_detected<has_get_set_properties_t, DerivedT>::value)
85+
MF.getProperties().set(DerivedT::getSetProperties());
86+
if constexpr (is_detected<has_get_cleared_properties_t, DerivedT>::value)
87+
MF.getProperties().reset(DerivedT::getClearedProperties());
88+
}
89+
};
16190
};
162-
} // namespace detail
16391

16492
using MachineFunctionAnalysisManagerModuleProxy =
16593
InnerAnalysisManagerProxy<MachineFunctionAnalysisManager, Module>;
@@ -251,11 +179,12 @@ class FunctionAnalysisManagerMachineFunctionProxy
251179

252180
class ModuleToMachineFunctionPassAdaptor
253181
: public PassInfoMixin<ModuleToMachineFunctionPassAdaptor> {
254-
using MachinePassConcept = detail::MachinePassConcept;
255-
256182
public:
183+
using PassConceptT =
184+
detail::PassConcept<MachineFunction, MachineFunctionAnalysisManager>;
185+
257186
explicit ModuleToMachineFunctionPassAdaptor(
258-
std::unique_ptr<MachinePassConcept> Pass)
187+
std::unique_ptr<PassConceptT> Pass)
259188
: Pass(std::move(Pass)) {}
260189

261190
/// Runs the function pass across every function in the module.
@@ -266,17 +195,18 @@ class ModuleToMachineFunctionPassAdaptor
266195
static bool isRequired() { return true; }
267196

268197
private:
269-
std::unique_ptr<MachinePassConcept> Pass;
198+
std::unique_ptr<PassConceptT> Pass;
270199
};
271200

272201
template <typename MachineFunctionPassT>
273202
ModuleToMachineFunctionPassAdaptor
274203
createModuleToMachineFunctionPassAdaptor(MachineFunctionPassT &&Pass) {
275-
using PassModelT = detail::MachinePassModel<MachineFunctionPassT>;
204+
using PassModelT = detail::PassModel<MachineFunction, MachineFunctionPassT,
205+
MachineFunctionAnalysisManager>;
276206
// Do not use make_unique, it causes too many template instantiations,
277207
// causing terrible compile times.
278208
return ModuleToMachineFunctionPassAdaptor(
279-
std::unique_ptr<detail::MachinePassConcept>(
209+
std::unique_ptr<ModuleToMachineFunctionPassAdaptor::PassConceptT>(
280210
new PassModelT(std::forward<MachineFunctionPassT>(Pass))));
281211
}
282212

llvm/include/llvm/IR/PassManager.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ extern llvm::cl::opt<bool> UseNewDbgInfoFormat;
6464

6565
namespace llvm {
6666

67-
class MachineFunction;
68-
6967
// RemoveDIs: Provide facilities for converting debug-info from one form to
7068
// another, which are no-ops for everything but modules.
7169
template <class IRUnitT> inline bool shouldConvertDbgInfo(IRUnitT &IR) {
@@ -273,10 +271,8 @@ class PassManager : public PassInfoMixin<
273271
LLVM_ATTRIBUTE_MINSIZE
274272
std::enable_if_t<!std::is_same<PassT, PassManager>::value>
275273
addPass(PassT &&Pass) {
276-
using PassModelT = std::conditional_t<
277-
std::is_same_v<IRUnitT, MachineFunction>,
278-
detail::MachinePassModel<PassT>,
279-
detail::PassModel<IRUnitT, PassT, AnalysisManagerT, ExtraArgTs...>>;
274+
using PassModelT =
275+
detail::PassModel<IRUnitT, PassT, AnalysisManagerT, ExtraArgTs...>;
280276
// Do not use make_unique or emplace_back, they cause too many template
281277
// instantiations, causing terrible compile times.
282278
Passes.push_back(std::unique_ptr<PassConceptT>(
@@ -302,9 +298,8 @@ class PassManager : public PassInfoMixin<
302298
static bool isRequired() { return true; }
303299

304300
protected:
305-
using PassConceptT = std::conditional_t<
306-
std::is_same_v<IRUnitT, MachineFunction>, detail::MachinePassConcept,
307-
detail::PassConcept<IRUnitT, AnalysisManagerT, ExtraArgTs...>>;
301+
using PassConceptT =
302+
detail::PassConcept<IRUnitT, AnalysisManagerT, ExtraArgTs...>;
308303

309304
std::vector<std::unique_ptr<PassConceptT>> Passes;
310305
};

llvm/include/llvm/IR/PassManagerInternal.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,6 @@ struct AnalysisPassModel
328328
PassT Pass;
329329
};
330330

331-
struct MachinePassConcept;
332-
template <typename PassT> struct MachinePassModel;
333-
334331
} // end namespace detail
335332

336333
} // end namespace llvm

llvm/lib/CodeGen/MachinePassManager.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,27 +121,11 @@ PassManager<MachineFunction>::run(MachineFunction &MF,
121121
for (auto &Pass : Passes) {
122122
if (!PI.runBeforePass<MachineFunction>(*Pass, MF))
123123
continue;
124-
auto &MFProps = MF.getProperties();
125-
#ifndef NDEBUG
126-
auto RequiredProperties = Pass->getRequiredProperties();
127-
if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
128-
errs() << "MachineFunctionProperties required by " << Pass->name()
129-
<< " pass are not met by function " << F.getName() << ".\n"
130-
<< "Required properties: ";
131-
RequiredProperties.print(errs());
132-
errs() << "\nCurrent properties: ";
133-
MFProps.print(errs());
134-
errs() << '\n';
135-
report_fatal_error("MachineFunctionProperties check failed");
136-
}
137-
#endif
138124

139125
PreservedAnalyses PassPA = Pass->run(MF, MFAM);
140126
if (MMI.getMachineFunction(F)) {
141127
MFAM.invalidate(MF, PassPA);
142128
PI.runAfterPass(*Pass, MF, PassPA);
143-
MFProps.set(Pass->getSetProperties());
144-
MFProps.reset(Pass->getClearedProperties());
145129
} else {
146130
MFAM.clear(MF, F.getName());
147131
PI.runAfterPassInvalidated<MachineFunction>(*Pass, PassPA);

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,10 @@ class TriggerVerifierErrorPass
368368
// A pass requires all MachineFunctionProperties.
369369
// DO NOT USE THIS EXCEPT FOR TESTING!
370370
class RequireAllMachineFunctionPropertiesPass
371-
: public PassInfoMixin<RequireAllMachineFunctionPropertiesPass> {
371+
: public MachinePassInfoMixin<RequireAllMachineFunctionPropertiesPass> {
372372
public:
373-
PreservedAnalyses run(MachineFunction &, MachineFunctionAnalysisManager &) {
373+
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &) {
374+
PropertyChanger PC(MF);
374375
return PreservedAnalyses::none();
375376
}
376377

0 commit comments

Comments
 (0)