Skip to content

Commit 9ab01ae

Browse files
committed
Auto merge of rust-lang#155491 - ohadravid:faster-storage-in-copyprop-and-gvn, r=saethlin
Fix performance regression introduced in rust-lang#142531 by excluding `Storage{Live,Dead}` from CGU size estimation Fix performance regression introduced in rust-lang#142531 ([rust-timer comment](rust-lang#142531 (comment))) by excluding `Storage{Live,Dead}` from CGU size estimation. Also, avoid unneeded work for storage removal in non-opt builds in CopyProp and GVN by allocating local sets for the storage accounting only when `tcx.sess.emit_lifetime_markers()`. r? saethlin
2 parents 84c1190 + 846974e commit 9ab01ae

3 files changed

Lines changed: 33 additions & 18 deletions

File tree

compiler/rustc_mir_transform/src/copy_prop.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,10 @@ impl<'tcx> crate::MirPass<'tcx> for CopyProp {
3838
let mut any_replacement = false;
3939
// Locals that participate in copy propagation either as a source or a destination.
4040
let mut unified = DenseBitSet::new_empty(body.local_decls.len());
41-
let mut storage_to_remove = DenseBitSet::new_empty(body.local_decls.len());
4241

4342
for (local, &head) in ssa.copy_classes().iter_enumerated() {
4443
if local != head {
4544
any_replacement = true;
46-
storage_to_remove.insert(head);
4745
unified.insert(head);
4846
unified.insert(local);
4947
}
@@ -58,7 +56,7 @@ impl<'tcx> crate::MirPass<'tcx> for CopyProp {
5856
// only if the head might be uninitialized at that point, or if the local is borrowed
5957
// (since we cannot easily determine when it's used).
6058
let storage_to_remove = if tcx.sess.emit_lifetime_markers() {
61-
storage_to_remove.clear();
59+
let mut storage_to_remove = DenseBitSet::new_empty(body.local_decls.len());
6260

6361
// If the local is borrowed, we cannot easily determine if it is used, so we have to remove the storage statements.
6462
let borrowed_locals = ssa.borrowed_locals();
@@ -83,15 +81,16 @@ impl<'tcx> crate::MirPass<'tcx> for CopyProp {
8381
storage_checker.visit_basic_block_data(bb, data);
8482
}
8583

86-
storage_checker.storage_to_remove
84+
Some(storage_checker.storage_to_remove)
8785
} else {
88-
// Remove the storage statements of all the head locals.
89-
storage_to_remove
86+
None
9087
};
9188

89+
// If None, remove the storage statements of all the unified locals.
90+
let storage_to_remove = storage_to_remove.as_ref().unwrap_or(&unified);
9291
debug!(?storage_to_remove);
9392

94-
Replacer { tcx, copy_classes: ssa.copy_classes(), unified, storage_to_remove }
93+
Replacer { tcx, copy_classes: ssa.copy_classes(), unified: &unified, storage_to_remove }
9594
.visit_body_preserves_cfg(body);
9695

9796
crate::simplify::remove_unused_definitions(body);
@@ -106,8 +105,8 @@ impl<'tcx> crate::MirPass<'tcx> for CopyProp {
106105
/// all occurrences of the key get replaced by the value.
107106
struct Replacer<'a, 'tcx> {
108107
tcx: TyCtxt<'tcx>,
109-
unified: DenseBitSet<Local>,
110-
storage_to_remove: DenseBitSet<Local>,
108+
unified: &'a DenseBitSet<Local>,
109+
storage_to_remove: &'a DenseBitSet<Local>,
111110
copy_classes: &'a IndexSlice<Local, Local>,
112111
}
113112

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,16 @@ impl<'tcx> crate::MirPass<'tcx> for GVN {
173173
storage_checker.visit_basic_block_data(bb, data);
174174
}
175175

176-
storage_checker.storage_to_remove
176+
Some(storage_checker.storage_to_remove)
177177
} else {
178-
// Remove the storage statements of all the reused locals.
179-
state.reused_locals.clone()
178+
None
180179
};
181180

181+
// If None, remove the storage statements of all the reused locals.
182+
let storage_to_remove = storage_to_remove.as_ref().unwrap_or(&state.reused_locals);
182183
debug!(?storage_to_remove);
183184

184-
StorageRemover { tcx, reused_locals: state.reused_locals, storage_to_remove }
185+
StorageRemover { tcx, reused_locals: &state.reused_locals, storage_to_remove }
185186
.visit_body_preserves_cfg(body);
186187
}
187188

@@ -2058,13 +2059,13 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
20582059
}
20592060
}
20602061

2061-
struct StorageRemover<'tcx> {
2062+
struct StorageRemover<'a, 'tcx> {
20622063
tcx: TyCtxt<'tcx>,
2063-
reused_locals: DenseBitSet<Local>,
2064-
storage_to_remove: DenseBitSet<Local>,
2064+
reused_locals: &'a DenseBitSet<Local>,
2065+
storage_to_remove: &'a DenseBitSet<Local>,
20652066
}
20662067

2067-
impl<'tcx> MutVisitor<'tcx> for StorageRemover<'tcx> {
2068+
impl<'a, 'tcx> MutVisitor<'tcx> for StorageRemover<'a, 'tcx> {
20682069
fn tcx(&self) -> TyCtxt<'tcx> {
20692070
self.tcx
20702071
}

compiler/rustc_monomorphize/src/partitioning.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ use rustc_hir::definitions::DefPathDataName;
109109
use rustc_middle::bug;
110110
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
111111
use rustc_middle::middle::exported_symbols::{SymbolExportInfo, SymbolExportLevel};
112+
use rustc_middle::mir::StatementKind;
112113
use rustc_middle::mono::{
113114
CodegenUnit, CodegenUnitNameBuilder, InstantiationMode, MonoItem, MonoItemData,
114115
MonoItemPartitions, Visibility,
@@ -1334,7 +1335,21 @@ pub(crate) fn provide(providers: &mut Providers) {
13341335
| InstanceKind::DropGlue(..)
13351336
| InstanceKind::AsyncDropGlueCtorShim(..) => {
13361337
let mir = tcx.instance_mir(instance.def);
1337-
mir.basic_blocks.iter().map(|bb| bb.statements.len() + 1).sum()
1338+
mir.basic_blocks
1339+
.iter()
1340+
.map(|bb| {
1341+
bb.statements
1342+
.iter()
1343+
.filter_map(|stmt| match stmt.kind {
1344+
StatementKind::StorageLive(_) | StatementKind::StorageDead(_) => {
1345+
None
1346+
}
1347+
_ => Some(stmt),
1348+
})
1349+
.count()
1350+
+ 1
1351+
})
1352+
.sum()
13381353
}
13391354
// Other compiler-generated shims size estimate: 1
13401355
_ => 1,

0 commit comments

Comments
 (0)