refactor SimlifyCfg and friends - no globals, just enums

This commit is contained in:
miguelraz 2023-04-17 20:17:01 -06:00
parent 7908a1d654
commit fc27ae14f6
4 changed files with 56 additions and 48 deletions

View File

@ -296,7 +296,7 @@ fn mir_const(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> &Steal<
&Lint(check_const_item_mutation::CheckConstItemMutation), &Lint(check_const_item_mutation::CheckConstItemMutation),
&Lint(function_item_references::FunctionItemReferences), &Lint(function_item_references::FunctionItemReferences),
// What we need to do constant evaluation. // What we need to do constant evaluation.
&simplify::SimplifyCfg::new("initial"), &simplify::SimplifyCfg::Initial,
&rustc_peek::SanityCheck, // Just a lint &rustc_peek::SanityCheck, // Just a lint
], ],
None, None,
@ -334,11 +334,7 @@ fn mir_promoted(
pm::run_passes( pm::run_passes(
tcx, tcx,
&mut body, &mut body,
&[ &[&promote_pass, &simplify::SimplifyCfg::PromoteConsts, &coverage::InstrumentCoverage],
&promote_pass,
&simplify::SimplifyCfg::new("promote-consts"),
&coverage::InstrumentCoverage,
],
Some(MirPhase::Analysis(AnalysisPhase::Initial)), Some(MirPhase::Analysis(AnalysisPhase::Initial)),
); );
@ -467,10 +463,7 @@ fn run_analysis_to_runtime_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>
pm::run_passes( pm::run_passes(
tcx, tcx,
body, body,
&[ &[&remove_uninit_drops::RemoveUninitDrops, &simplify::SimplifyCfg::RemoveFalseEdges],
&remove_uninit_drops::RemoveUninitDrops,
&simplify::SimplifyCfg::new("remove-false-edges"),
],
None, None,
); );
check_consts::post_drop_elaboration::check_live_drops(tcx, &body); // FIXME: make this a MIR lint check_consts::post_drop_elaboration::check_live_drops(tcx, &body); // FIXME: make this a MIR lint
@ -492,7 +485,7 @@ fn run_analysis_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let passes: &[&dyn MirPass<'tcx>] = &[ let passes: &[&dyn MirPass<'tcx>] = &[
&cleanup_post_borrowck::CleanupPostBorrowck, &cleanup_post_borrowck::CleanupPostBorrowck,
&remove_noop_landing_pads::RemoveNoopLandingPads, &remove_noop_landing_pads::RemoveNoopLandingPads,
&simplify::SimplifyCfg::new("early-opt"), &simplify::SimplifyCfg::EarlyOpt,
&deref_separator::Derefer, &deref_separator::Derefer,
]; ];
@ -525,7 +518,7 @@ fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
/// Returns the sequence of passes that do the initial cleanup of runtime MIR. /// Returns the sequence of passes that do the initial cleanup of runtime MIR.
fn run_runtime_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_runtime_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let passes: &[&dyn MirPass<'tcx>] = let passes: &[&dyn MirPass<'tcx>] =
&[&lower_intrinsics::LowerIntrinsics, &simplify::SimplifyCfg::new("elaborate-drops")]; &[&lower_intrinsics::LowerIntrinsics, &simplify::SimplifyCfg::ElaborateDrops];
pm::run_passes(tcx, body, passes, Some(MirPhase::Runtime(RuntimePhase::PostCleanup))); pm::run_passes(tcx, body, passes, Some(MirPhase::Runtime(RuntimePhase::PostCleanup)));
@ -551,7 +544,7 @@ fn o1<T>(x: T) -> WithMinOptLevel<T> {
&lower_slice_len::LowerSliceLenCalls, // has to be done before inlining, otherwise actual call will be almost always inlined. Also simple, so can just do first &lower_slice_len::LowerSliceLenCalls, // has to be done before inlining, otherwise actual call will be almost always inlined. Also simple, so can just do first
&unreachable_prop::UnreachablePropagation, &unreachable_prop::UnreachablePropagation,
&uninhabited_enum_branching::UninhabitedEnumBranching, &uninhabited_enum_branching::UninhabitedEnumBranching,
&o1(simplify::SimplifyCfg::new("after-uninhabited-enum-branching")), &o1(simplify::SimplifyCfg::AfterUninhabitedEnumBranching),
&inline::Inline, &inline::Inline,
&remove_storage_markers::RemoveStorageMarkers, &remove_storage_markers::RemoveStorageMarkers,
&remove_zsts::RemoveZsts, &remove_zsts::RemoveZsts,
@ -564,23 +557,23 @@ fn o1<T>(x: T) -> WithMinOptLevel<T> {
&multiple_return_terminators::MultipleReturnTerminators, &multiple_return_terminators::MultipleReturnTerminators,
&instcombine::InstCombine, &instcombine::InstCombine,
&separate_const_switch::SeparateConstSwitch, &separate_const_switch::SeparateConstSwitch,
&simplify::SimplifyLocals::new("before-const-prop"), &simplify::SimplifyLocals::BeforeConstProp,
&copy_prop::CopyProp, &copy_prop::CopyProp,
&const_prop::ConstProp, &const_prop::ConstProp,
&dataflow_const_prop::DataflowConstProp, &dataflow_const_prop::DataflowConstProp,
// //
// Const-prop runs unconditionally, but doesn't mutate the MIR at mir-opt-level=0. // Const-prop runs unconditionally, but doesn't mutate the MIR at mir-opt-level=0.
&const_debuginfo::ConstDebugInfo, &const_debuginfo::ConstDebugInfo,
&o1(simplify_branches::SimplifyConstCondition::new("after-const-prop")), &o1(simplify_branches::SimplifyConstConditionPassName::AfterConstProp),
&early_otherwise_branch::EarlyOtherwiseBranch, &early_otherwise_branch::EarlyOtherwiseBranch,
&simplify_comparison_integral::SimplifyComparisonIntegral, &simplify_comparison_integral::SimplifyComparisonIntegral,
&dead_store_elimination::DeadStoreElimination, &dead_store_elimination::DeadStoreElimination,
&dest_prop::DestinationPropagation, &dest_prop::DestinationPropagation,
&o1(simplify_branches::SimplifyConstCondition::new("final")), &o1(simplify_branches::SimplifyConstConditionPassName::Final),
&o1(remove_noop_landing_pads::RemoveNoopLandingPads), &o1(remove_noop_landing_pads::RemoveNoopLandingPads),
&o1(simplify::SimplifyCfg::new("final")), &o1(simplify::SimplifyCfg::Final),
&nrvo::RenameReturnPlace, &nrvo::RenameReturnPlace,
&simplify::SimplifyLocals::new("final"), &simplify::SimplifyLocals::Final,
&multiple_return_terminators::MultipleReturnTerminators, &multiple_return_terminators::MultipleReturnTerminators,
&deduplicate_blocks::DeduplicateBlocks, &deduplicate_blocks::DeduplicateBlocks,
&large_enums::EnumSizeOpt { discrepancy: 128 }, &large_enums::EnumSizeOpt { discrepancy: 128 },

View File

@ -95,7 +95,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> Body<'
&add_moves_for_packed_drops::AddMovesForPackedDrops, &add_moves_for_packed_drops::AddMovesForPackedDrops,
&deref_separator::Derefer, &deref_separator::Derefer,
&remove_noop_landing_pads::RemoveNoopLandingPads, &remove_noop_landing_pads::RemoveNoopLandingPads,
&simplify::SimplifyCfg::new("make_shim"), &simplify::SimplifyCfg::MakeShim,
&add_call_guards::CriticalCallEdges, &add_call_guards::CriticalCallEdges,
&abort_unwinding_calls::AbortUnwindingCalls, &abort_unwinding_calls::AbortUnwindingCalls,
], ],

View File

@ -36,13 +36,31 @@
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use smallvec::SmallVec; use smallvec::SmallVec;
pub struct SimplifyCfg { pub enum SimplifyCfg {
label: String, Initial,
PromoteConsts,
RemoveFalseEdges,
EarlyOpt,
ElaborateDrops,
Final,
MakeShim,
AfterUninhabitedEnumBranching,
} }
impl SimplifyCfg { impl SimplifyCfg {
pub fn new(label: &str) -> Self { pub fn name(&self) -> &'static str {
SimplifyCfg { label: format!("SimplifyCfg-{}", label) } match self {
SimplifyCfg::Initial => "SimplifyCfg-initial",
SimplifyCfg::PromoteConsts => "SimplifyCfg-promote-consts",
SimplifyCfg::RemoveFalseEdges => "SimplifyCfg-remove-false-edges",
SimplifyCfg::EarlyOpt => "SimplifyCfg-early-opt",
SimplifyCfg::ElaborateDrops => "SimplifyCfg-elaborate-drops",
SimplifyCfg::Final => "SimplifyCfg-final",
SimplifyCfg::MakeShim => "SimplifyCfg-make_shim",
SimplifyCfg::AfterUninhabitedEnumBranching => {
"SimplifyCfg-after-uninhabited-enum-branching"
}
}
} }
} }
@ -57,11 +75,11 @@ pub fn simplify_cfg<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
impl<'tcx> MirPass<'tcx> for SimplifyCfg { impl<'tcx> MirPass<'tcx> for SimplifyCfg {
fn name(&self) -> &str { fn name(&self) -> &str {
&self.label &self.name()
} }
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
debug!("SimplifyCfg({:?}) - simplifying {:?}", self.label, body.source); debug!("SimplifyCfg({:?}) - simplifying {:?}", self.name(), body.source);
simplify_cfg(tcx, body); simplify_cfg(tcx, body);
} }
} }
@ -423,19 +441,17 @@ fn save_unreachable_coverage(
)); ));
} }
pub struct SimplifyLocals { pub enum SimplifyLocals {
label: String, BeforeConstProp,
} Final,
impl SimplifyLocals {
pub fn new(label: &str) -> SimplifyLocals {
SimplifyLocals { label: format!("SimplifyLocals-{}", label) }
}
} }
impl<'tcx> MirPass<'tcx> for SimplifyLocals { impl<'tcx> MirPass<'tcx> for SimplifyLocals {
fn name(&self) -> &str { fn name(&self) -> &'static str {
&self.label match &self {
SimplifyLocals::BeforeConstProp => "SimplifyLocals-before-const-prop",
SimplifyLocals::Final => "SimplifyLocals-final",
}
} }
fn is_enabled(&self, sess: &rustc_session::Session) -> bool { fn is_enabled(&self, sess: &rustc_session::Session) -> bool {

View File

@ -2,20 +2,19 @@
use rustc_middle::mir::*; use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
pub enum SimplifyConstConditionPassName {
AfterConstProp,
Final,
}
/// A pass that replaces a branch with a goto when its condition is known. /// A pass that replaces a branch with a goto when its condition is known.
pub struct SimplifyConstCondition { impl<'tcx> MirPass<'tcx> for SimplifyConstConditionPassName {
label: String, fn name(&self) -> &'static str {
} match self {
SimplifyConstConditionPassName::AfterConstProp => {
impl SimplifyConstCondition { "SimplifyConstCondition-after-const-prop"
pub fn new(label: &str) -> Self { }
SimplifyConstCondition { label: format!("SimplifyConstCondition-{}", label) } SimplifyConstConditionPassName::Final => "SimplifyConstCondition-final",
} }
}
impl<'tcx> MirPass<'tcx> for SimplifyConstCondition {
fn name(&self) -> &str {
&self.label
} }
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {