Skip to content

Commit 378b528

Browse files
committed
Auto merge of #165101 - stephenduong1004:propagate-cold, r=<try>
Propagate block coldness across unwind edges and mark cold callsites
2 parents 96a18bf + 7c5e8c3 commit 378b528

4 files changed

Lines changed: 26 additions & 8 deletions

File tree

‎compiler/rustc_codegen_llvm/src/builder.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,6 +1578,11 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
15781578
let cold_inline = llvm::AttributeKind::Cold.create_attr(self.llcx);
15791579
attributes::apply_to_callsite(llret, llvm::AttributePlace::Function, &[cold_inline]);
15801580
}
1581+
1582+
fn apply_attrs_to_cold_callsite(&mut self, llret: &'ll Value) {
1583+
let cold_attr = llvm::AttributeKind::Cold.create_attr(self.llcx);
1584+
attributes::apply_to_callsite(llret, llvm::AttributePlace::Function, &[cold_attr]);
1585+
}
15811586
}
15821587

15831588
impl<'ll> StaticBuilderMethods for Builder<'_, 'll, '_> {

‎compiler/rustc_codegen_ssa/src/mir/block.rs‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,16 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
266266
return MergingSucc::False;
267267
}
268268

269+
let is_cleanup = fx.mir[self.bb].is_cleanup;
270+
let is_cold = fx.cold_blocks.contains(self.bb);
271+
let apply_callsite_attrs = |bx: &mut Bx, call_llval| {
272+
if is_cleanup {
273+
bx.apply_attrs_to_cleanup_callsite(call_llval);
274+
} else if is_cold {
275+
bx.apply_attrs_to_cold_callsite(call_llval);
276+
}
277+
};
278+
269279
if let Some(unwind_block) = unwind_block {
270280
let ret_llbb = if let Some((_, target)) = destination {
271281
self.llbb_with_cleanup(fx, target)
@@ -284,9 +294,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
284294
self.funclet(fx),
285295
instance,
286296
);
287-
if fx.mir[self.bb].is_cleanup {
288-
bx.apply_attrs_to_cleanup_callsite(invokeret);
289-
}
297+
apply_callsite_attrs(bx, invokeret);
290298

291299
if let Some((ret_dest, target)) = destination {
292300
bx.switch_to_block(fx.llbb(target));
@@ -314,9 +322,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
314322
self.funclet(fx),
315323
instance,
316324
);
317-
if fx.mir[self.bb].is_cleanup {
318-
bx.apply_attrs_to_cleanup_callsite(llret);
319-
}
325+
apply_callsite_attrs(bx, llret);
320326

321327
if let Some((ret_dest, target)) = destination {
322328
for &(tmp, size) in lifetime_ends_after_call {

‎compiler/rustc_codegen_ssa/src/mir/mod.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,9 @@ fn find_cold_blocks<'tcx>(
675675

676676
// Traverse all basic blocks from end of the function to the start.
677677
for (bb, bb_data) in traversal::postorder(mir) {
678+
if bb_data.is_cleanup {
679+
continue;
680+
}
678681
let terminator = bb_data.terminator();
679682

680683
match terminator.kind {
@@ -698,8 +701,8 @@ fn find_cold_blocks<'tcx>(
698701
_ => {}
699702
}
700703

701-
// If all successors of a BB are cold and there's at least one of them, mark this BB as cold
702-
let mut succ = terminator.successors();
704+
// If all non-cleanup successors of a BB are cold and there's at least one of them, mark this BB as cold.
705+
let mut succ = terminator.successors().filter(|&s| !mir.basic_blocks[s].is_cleanup);
703706
if let Some(first) = succ.next()
704707
&& cold_blocks.contains(first)
705708
&& succ.all(|s| cold_blocks.contains(s))

‎compiler/rustc_codegen_ssa/src/traits/builder.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,4 +690,8 @@ pub trait BuilderMethods<'a, 'tcx>:
690690
fn zext(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value;
691691

692692
fn apply_attrs_to_cleanup_callsite(&mut self, llret: Self::Value);
693+
694+
fn apply_attrs_to_cold_callsite(&mut self, llret: Self::Value) {
695+
self.apply_attrs_to_cleanup_callsite(llret);
696+
}
693697
}

0 commit comments

Comments
 (0)