Skip to content

Commit 9e7f72c

Browse files
committed
Auto merge of rust-lang#110477 - miguelraz:canoodling2-electric-boogaloo, r=compiler-errors
Don't allocate on SimplifyCfg/Locals/Const on every MIR pass Hey! 👋🏾 This is a first PR attempt to see if I could speed up some rustc internals. Thought process: ```rust pub struct SimplifyCfg { label: String, } ``` in [compiler/src/rustc_mir_transform/simplify.rs](https://github.com/rust-lang/rust/blob/7908a1d65496b88626e4b7c193c81d777005d6f3/compiler/rustc_mir_transform/src/simplify.rs#L39) fires multiple times per MIR analysis. This means that a likely string allocation is happening in each of these runs, which may add up, as they are not being lazily allocated or cached in between the different passes. ...yes, I know that adding a global static array is probably not the future-proof solution, but I wanted to lob this now as a proof of concept to see if it's worth shaving off a few cycles and then making more robust.
2 parents da48140 + fc27ae1 commit 9e7f72c

File tree

4 files changed

+56
-48
lines changed

4 files changed

+56
-48
lines changed

compiler/rustc_mir_transform/src/lib.rs

+11-18
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ fn mir_const(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> &Steal<
296296
&Lint(check_const_item_mutation::CheckConstItemMutation),
297297
&Lint(function_item_references::FunctionItemReferences),
298298
// What we need to do constant evaluation.
299-
&simplify::SimplifyCfg::new("initial"),
299+
&simplify::SimplifyCfg::Initial,
300300
&rustc_peek::SanityCheck, // Just a lint
301301
],
302302
None,
@@ -334,11 +334,7 @@ fn mir_promoted(
334334
pm::run_passes(
335335
tcx,
336336
&mut body,
337-
&[
338-
&promote_pass,
339-
&simplify::SimplifyCfg::new("promote-consts"),
340-
&coverage::InstrumentCoverage,
341-
],
337+
&[&promote_pass, &simplify::SimplifyCfg::PromoteConsts, &coverage::InstrumentCoverage],
342338
Some(MirPhase::Analysis(AnalysisPhase::Initial)),
343339
);
344340

@@ -467,10 +463,7 @@ fn run_analysis_to_runtime_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>
467463
pm::run_passes(
468464
tcx,
469465
body,
470-
&[
471-
&remove_uninit_drops::RemoveUninitDrops,
472-
&simplify::SimplifyCfg::new("remove-false-edges"),
473-
],
466+
&[&remove_uninit_drops::RemoveUninitDrops, &simplify::SimplifyCfg::RemoveFalseEdges],
474467
None,
475468
);
476469
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>) {
492485
let passes: &[&dyn MirPass<'tcx>] = &[
493486
&cleanup_post_borrowck::CleanupPostBorrowck,
494487
&remove_noop_landing_pads::RemoveNoopLandingPads,
495-
&simplify::SimplifyCfg::new("early-opt"),
488+
&simplify::SimplifyCfg::EarlyOpt,
496489
&deref_separator::Derefer,
497490
];
498491

@@ -525,7 +518,7 @@ fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
525518
/// Returns the sequence of passes that do the initial cleanup of runtime MIR.
526519
fn run_runtime_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
527520
let passes: &[&dyn MirPass<'tcx>] =
528-
&[&lower_intrinsics::LowerIntrinsics, &simplify::SimplifyCfg::new("elaborate-drops")];
521+
&[&lower_intrinsics::LowerIntrinsics, &simplify::SimplifyCfg::ElaborateDrops];
529522

530523
pm::run_passes(tcx, body, passes, Some(MirPhase::Runtime(RuntimePhase::PostCleanup)));
531524

@@ -551,7 +544,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
551544
&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
552545
&unreachable_prop::UnreachablePropagation,
553546
&uninhabited_enum_branching::UninhabitedEnumBranching,
554-
&o1(simplify::SimplifyCfg::new("after-uninhabited-enum-branching")),
547+
&o1(simplify::SimplifyCfg::AfterUninhabitedEnumBranching),
555548
&inline::Inline,
556549
&remove_storage_markers::RemoveStorageMarkers,
557550
&remove_zsts::RemoveZsts,
@@ -564,23 +557,23 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
564557
&multiple_return_terminators::MultipleReturnTerminators,
565558
&instcombine::InstCombine,
566559
&separate_const_switch::SeparateConstSwitch,
567-
&simplify::SimplifyLocals::new("before-const-prop"),
560+
&simplify::SimplifyLocals::BeforeConstProp,
568561
&copy_prop::CopyProp,
569562
&const_prop::ConstProp,
570563
&dataflow_const_prop::DataflowConstProp,
571564
//
572565
// Const-prop runs unconditionally, but doesn't mutate the MIR at mir-opt-level=0.
573566
&const_debuginfo::ConstDebugInfo,
574-
&o1(simplify_branches::SimplifyConstCondition::new("after-const-prop")),
567+
&o1(simplify_branches::SimplifyConstConditionPassName::AfterConstProp),
575568
&early_otherwise_branch::EarlyOtherwiseBranch,
576569
&simplify_comparison_integral::SimplifyComparisonIntegral,
577570
&dead_store_elimination::DeadStoreElimination,
578571
&dest_prop::DestinationPropagation,
579-
&o1(simplify_branches::SimplifyConstCondition::new("final")),
572+
&o1(simplify_branches::SimplifyConstConditionPassName::Final),
580573
&o1(remove_noop_landing_pads::RemoveNoopLandingPads),
581-
&o1(simplify::SimplifyCfg::new("final")),
574+
&o1(simplify::SimplifyCfg::Final),
582575
&nrvo::RenameReturnPlace,
583-
&simplify::SimplifyLocals::new("final"),
576+
&simplify::SimplifyLocals::Final,
584577
&multiple_return_terminators::MultipleReturnTerminators,
585578
&deduplicate_blocks::DeduplicateBlocks,
586579
&large_enums::EnumSizeOpt { discrepancy: 128 },

compiler/rustc_mir_transform/src/shim.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> Body<'
9595
&add_moves_for_packed_drops::AddMovesForPackedDrops,
9696
&deref_separator::Derefer,
9797
&remove_noop_landing_pads::RemoveNoopLandingPads,
98-
&simplify::SimplifyCfg::new("make_shim"),
98+
&simplify::SimplifyCfg::MakeShim,
9999
&add_call_guards::CriticalCallEdges,
100100
&abort_unwinding_calls::AbortUnwindingCalls,
101101
],

compiler/rustc_mir_transform/src/simplify.rs

+32-16
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,31 @@ use rustc_middle::mir::*;
3636
use rustc_middle::ty::TyCtxt;
3737
use smallvec::SmallVec;
3838

39-
pub struct SimplifyCfg {
40-
label: String,
39+
pub enum SimplifyCfg {
40+
Initial,
41+
PromoteConsts,
42+
RemoveFalseEdges,
43+
EarlyOpt,
44+
ElaborateDrops,
45+
Final,
46+
MakeShim,
47+
AfterUninhabitedEnumBranching,
4148
}
4249

4350
impl SimplifyCfg {
44-
pub fn new(label: &str) -> Self {
45-
SimplifyCfg { label: format!("SimplifyCfg-{}", label) }
51+
pub fn name(&self) -> &'static str {
52+
match self {
53+
SimplifyCfg::Initial => "SimplifyCfg-initial",
54+
SimplifyCfg::PromoteConsts => "SimplifyCfg-promote-consts",
55+
SimplifyCfg::RemoveFalseEdges => "SimplifyCfg-remove-false-edges",
56+
SimplifyCfg::EarlyOpt => "SimplifyCfg-early-opt",
57+
SimplifyCfg::ElaborateDrops => "SimplifyCfg-elaborate-drops",
58+
SimplifyCfg::Final => "SimplifyCfg-final",
59+
SimplifyCfg::MakeShim => "SimplifyCfg-make_shim",
60+
SimplifyCfg::AfterUninhabitedEnumBranching => {
61+
"SimplifyCfg-after-uninhabited-enum-branching"
62+
}
63+
}
4664
}
4765
}
4866

@@ -57,11 +75,11 @@ pub fn simplify_cfg<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
5775

5876
impl<'tcx> MirPass<'tcx> for SimplifyCfg {
5977
fn name(&self) -> &str {
60-
&self.label
78+
&self.name()
6179
}
6280

6381
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
64-
debug!("SimplifyCfg({:?}) - simplifying {:?}", self.label, body.source);
82+
debug!("SimplifyCfg({:?}) - simplifying {:?}", self.name(), body.source);
6583
simplify_cfg(tcx, body);
6684
}
6785
}
@@ -423,19 +441,17 @@ fn save_unreachable_coverage(
423441
));
424442
}
425443

426-
pub struct SimplifyLocals {
427-
label: String,
428-
}
429-
430-
impl SimplifyLocals {
431-
pub fn new(label: &str) -> SimplifyLocals {
432-
SimplifyLocals { label: format!("SimplifyLocals-{}", label) }
433-
}
444+
pub enum SimplifyLocals {
445+
BeforeConstProp,
446+
Final,
434447
}
435448

436449
impl<'tcx> MirPass<'tcx> for SimplifyLocals {
437-
fn name(&self) -> &str {
438-
&self.label
450+
fn name(&self) -> &'static str {
451+
match &self {
452+
SimplifyLocals::BeforeConstProp => "SimplifyLocals-before-const-prop",
453+
SimplifyLocals::Final => "SimplifyLocals-final",
454+
}
439455
}
440456

441457
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {

compiler/rustc_mir_transform/src/simplify_branches.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@ use crate::MirPass;
22
use rustc_middle::mir::*;
33
use rustc_middle::ty::TyCtxt;
44

5-
/// A pass that replaces a branch with a goto when its condition is known.
6-
pub struct SimplifyConstCondition {
7-
label: String,
8-
}
9-
10-
impl SimplifyConstCondition {
11-
pub fn new(label: &str) -> Self {
12-
SimplifyConstCondition { label: format!("SimplifyConstCondition-{}", label) }
13-
}
5+
pub enum SimplifyConstConditionPassName {
6+
AfterConstProp,
7+
Final,
148
}
15-
16-
impl<'tcx> MirPass<'tcx> for SimplifyConstCondition {
17-
fn name(&self) -> &str {
18-
&self.label
9+
/// A pass that replaces a branch with a goto when its condition is known.
10+
impl<'tcx> MirPass<'tcx> for SimplifyConstConditionPassName {
11+
fn name(&self) -> &'static str {
12+
match self {
13+
SimplifyConstConditionPassName::AfterConstProp => {
14+
"SimplifyConstCondition-after-const-prop"
15+
}
16+
SimplifyConstConditionPassName::Final => "SimplifyConstCondition-final",
17+
}
1918
}
2019

2120
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

0 commit comments

Comments
 (0)