Skip to content

Commit fff4338

Browse files
committed
Add a simple test
1 parent 2c061d8 commit fff4338

File tree

4 files changed

+99
-19
lines changed

4 files changed

+99
-19
lines changed

llvm/include/llvm/CodeGen/MachinePassManager.h

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -138,26 +138,44 @@ template <typename PassT> struct MachinePassModel : MachinePassConcept {
138138
return getSetPropertiesImpl<PassT>();
139139
}
140140

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-
}
141+
template <typename T>
142+
using has_get_cleared_properties_t =
143+
decltype(std::declval<T &>().getClearedProperties());
144+
145+
public:
146+
PropertyChanger(MachineFunction &MF) : MF(MF) {
147+
#ifndef NDEBUG
148+
if constexpr (is_detected<has_get_required_properties_t,
149+
DerivedT>::value) {
150+
auto &MFProps = MF.getProperties();
151+
auto RequiredProperties = DerivedT::getRequiredProperties();
152+
if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
153+
errs() << "MachineFunctionProperties required by " << DerivedT::name()
154+
<< " pass are not met by function " << MF.getName() << ".\n"
155+
<< "Required properties: ";
156+
RequiredProperties.print(errs());
157+
errs() << "\nCurrent properties: ";
158+
MFProps.print(errs());
159+
errs() << '\n';
160+
report_fatal_error("MachineFunctionProperties check failed");
161+
}
162+
#endif
163+
}
164+
}
165+
166+
~PropertyChanger() {
167+
if constexpr (is_detected<has_get_set_properties_t, DerivedT>::value)
168+
MF.getProperties().set(DerivedT::getSetProperties());
169+
if constexpr (is_detected<has_get_cleared_properties_t, DerivedT>::value)
170+
MF.getProperties().reset(DerivedT::getClearedProperties());
171+
}
172+
};
159173

160-
PassT Pass;
174+
PreservedAnalyses run(MachineFunction &MF,
175+
MachineFunctionAnalysisManager &MFAM) {
176+
PropertyChanger PC(MF);
177+
return static_cast<DerivedT *>(this)->run(MF, MFAM);
178+
}
161179
};
162180
} // namespace detail
163181

@@ -280,6 +298,27 @@ createModuleToMachineFunctionPassAdaptor(MachineFunctionPassT &&Pass) {
280298
new PassModelT(std::forward<MachineFunctionPassT>(Pass))));
281299
}
282300

301+
template <>
302+
template <typename PassT>
303+
std::enable_if_t<!std::is_same<PassT, PassManager<MachineFunction>>::value>
304+
PassManager<MachineFunction>::addPass(PassT &&Pass) {
305+
using PassModelT =
306+
detail::PassModel<MachineFunction, PassT, MachineFunctionAnalysisManager>;
307+
using MachineFunctionPassModelT =
308+
detail::PassModel<MachineFunction, MachinePassInfoMixin<PassT>,
309+
MachineFunctionAnalysisManager>;
310+
// Do not use make_unique or emplace_back, they cause too many template
311+
// instantiations, causing terrible compile times.
312+
if constexpr (std::is_base_of_v<MachinePassInfoMixin<PassT>, PassT>) {
313+
Passes.push_back(
314+
std::unique_ptr<PassConceptT>(new MachineFunctionPassModelT(
315+
std::forward<MachinePassInfoMixin<PassT>>(Pass))));
316+
} else {
317+
Passes.push_back(std::unique_ptr<PassConceptT>(
318+
new PassModelT(std::forward<PassT>(Pass))));
319+
}
320+
}
321+
283322
template <>
284323
PreservedAnalyses
285324
PassManager<MachineFunction>::run(MachineFunction &,

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ MACHINE_FUNCTION_PASS("dead-mi-elimination", DeadMachineInstructionElimPass())
127127
// MACHINE_FUNCTION_PASS("free-machine-function", FreeMachineFunctionPass())
128128
MACHINE_FUNCTION_PASS("no-op-machine-function", NoOpMachineFunctionPass())
129129
MACHINE_FUNCTION_PASS("print", PrintMIRPass())
130+
MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
131+
RequireAllMachineFunctionPropertiesPass())
130132
#undef MACHINE_FUNCTION_PASS
131133

132134
// After a pass is converted to new pass manager, its entry should be moved from

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,33 @@ class TriggerVerifierErrorPass
365365
static StringRef name() { return "TriggerVerifierErrorPass"; }
366366
};
367367

368+
// A pass requires all MachineFunctionProperties.
369+
// DO NOT USE THIS EXCEPT FOR TESTING!
370+
class RequireAllMachineFunctionPropertiesPass
371+
: public PassInfoMixin<RequireAllMachineFunctionPropertiesPass> {
372+
public:
373+
PreservedAnalyses run(MachineFunction &, MachineFunctionAnalysisManager &) {
374+
return PreservedAnalyses::none();
375+
}
376+
377+
static MachineFunctionProperties getRequiredProperties() {
378+
MachineFunctionProperties MFProps;
379+
MFProps.set(MachineFunctionProperties::Property::FailedISel);
380+
MFProps.set(MachineFunctionProperties::Property::FailsVerification);
381+
MFProps.set(MachineFunctionProperties::Property::IsSSA);
382+
MFProps.set(MachineFunctionProperties::Property::Legalized);
383+
MFProps.set(MachineFunctionProperties::Property::NoPHIs);
384+
MFProps.set(MachineFunctionProperties::Property::NoVRegs);
385+
MFProps.set(MachineFunctionProperties::Property::RegBankSelected);
386+
MFProps.set(MachineFunctionProperties::Property::Selected);
387+
MFProps.set(MachineFunctionProperties::Property::TiedOpsRewritten);
388+
MFProps.set(MachineFunctionProperties::Property::TracksDebugUserValues);
389+
MFProps.set(MachineFunctionProperties::Property::TracksLiveness);
390+
return MFProps;
391+
}
392+
static StringRef name() { return "RequireAllMachineFunctionPropertiesPass"; }
393+
};
394+
368395
} // namespace
369396

370397
PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# REQUIRES: asserts
2+
# RUN: not --crash llc -mtriple=x86_64-pc-linux-gnu -passes=require-all-machine-function-properties -filetype=null %s 2>&1 | FileCheck %s
3+
4+
# CHECK: MachineFunctionProperties required by RequireAllMachineFunctionPropertiesPass pass are not met by function f.
5+
6+
---
7+
name: f
8+
selected: false
9+
body: |
10+
bb.0:
11+
RET 0
12+
...

0 commit comments

Comments
 (0)