From ee2982529bc2a9deff540f886c16297a0195c140 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Fri, 6 Jan 2023 21:50:08 -0500 Subject: [PATCH 1/2] Enable DeduplicateBlocks --- .../src/deduplicate_blocks.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_mir_transform/src/deduplicate_blocks.rs b/compiler/rustc_mir_transform/src/deduplicate_blocks.rs index 909116a77f54f..4a50c1d43a66e 100644 --- a/compiler/rustc_mir_transform/src/deduplicate_blocks.rs +++ b/compiler/rustc_mir_transform/src/deduplicate_blocks.rs @@ -16,12 +16,17 @@ pub struct DeduplicateBlocks; impl<'tcx> MirPass<'tcx> for DeduplicateBlocks { fn is_enabled(&self, sess: &rustc_session::Session) -> bool { - sess.mir_opt_level() >= 4 + sess.mir_opt_level() >= 1 } fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { + // Basic blocks can get really big, so to avoid checking for duplicates in basic blocks + // that are unlikely to have duplicates, we stop early. The early bail number has been + // found experimentally by eprintln while compiling the crates in the rustc-perf suite. + let limit = if tcx.sess.mir_opt_level() < 3 { 3 } else { 10 }; + debug!("Running DeduplicateBlocks on `{:?}`", body.source); - let duplicates = find_duplicates(body); + let duplicates = find_duplicates(body, limit); let has_opts_to_apply = !duplicates.is_empty(); if has_opts_to_apply { @@ -54,7 +59,7 @@ impl<'tcx> MutVisitor<'tcx> for OptApplier<'tcx> { } } -fn find_duplicates(body: &Body<'_>) -> FxHashMap { +fn find_duplicates(body: &Body<'_>, limit: usize) -> FxHashMap { let mut duplicates = FxHashMap::default(); let bbs_to_go_through = @@ -72,10 +77,7 @@ fn find_duplicates(body: &Body<'_>) -> FxHashMap { // with replacement bb3. // When the duplicates are removed, we will end up with only bb3. for (bb, bbd) in body.basic_blocks.iter_enumerated().rev().filter(|(_, bbd)| !bbd.is_cleanup) { - // Basic blocks can get really big, so to avoid checking for duplicates in basic blocks - // that are unlikely to have duplicates, we stop early. The early bail number has been - // found experimentally by eprintln while compiling the crates in the rustc-perf suite. - if bbd.statements.len() > 10 { + if bbd.statements.len() > limit { continue; } From 3b77094c95a41ce41838ee30840058b1d2e8e386 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Fri, 6 Jan 2023 21:50:08 -0500 Subject: [PATCH 2/2] Remove limit --- .../rustc_mir_transform/src/deduplicate_blocks.rs | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_mir_transform/src/deduplicate_blocks.rs b/compiler/rustc_mir_transform/src/deduplicate_blocks.rs index 4a50c1d43a66e..3b1c72c47488c 100644 --- a/compiler/rustc_mir_transform/src/deduplicate_blocks.rs +++ b/compiler/rustc_mir_transform/src/deduplicate_blocks.rs @@ -20,13 +20,8 @@ impl<'tcx> MirPass<'tcx> for DeduplicateBlocks { } fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { - // Basic blocks can get really big, so to avoid checking for duplicates in basic blocks - // that are unlikely to have duplicates, we stop early. The early bail number has been - // found experimentally by eprintln while compiling the crates in the rustc-perf suite. - let limit = if tcx.sess.mir_opt_level() < 3 { 3 } else { 10 }; - debug!("Running DeduplicateBlocks on `{:?}`", body.source); - let duplicates = find_duplicates(body, limit); + let duplicates = find_duplicates(body); let has_opts_to_apply = !duplicates.is_empty(); if has_opts_to_apply { @@ -59,7 +54,7 @@ impl<'tcx> MutVisitor<'tcx> for OptApplier<'tcx> { } } -fn find_duplicates(body: &Body<'_>, limit: usize) -> FxHashMap { +fn find_duplicates(body: &Body<'_>) -> FxHashMap { let mut duplicates = FxHashMap::default(); let bbs_to_go_through = @@ -77,10 +72,6 @@ fn find_duplicates(body: &Body<'_>, limit: usize) -> FxHashMap limit { - continue; - } - let to_hash = BasicBlockHashable { basic_block_data: bbd }; let entry = same_hashes.entry(to_hash); match entry {