Skip to content

Commit

Permalink
Don't alloca for unused locals
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Aug 19, 2024
1 parent 5601d14 commit 8df3ccc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
8 changes: 5 additions & 3 deletions compiler/rustc_codegen_ssa/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
})
.collect();

let (reachable_blocks, reachable_locals) =
traversal::mono_reachable_as_bitset(mir, cx.tcx(), instance);

let mut fx = FunctionCx {
instance,
mir,
Expand All @@ -218,7 +221,8 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(

fx.per_local_var_debug_info = fx.compute_per_local_var_debug_info(&mut start_bx);

let memory_locals = analyze::non_ssa_locals(&fx);
let mut memory_locals = analyze::non_ssa_locals(&fx);
memory_locals.intersect(&reachable_locals);

// Allocate variable and temp allocas
let local_values = {
Expand Down Expand Up @@ -277,8 +281,6 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
// So drop the builder of `start_llbb` to avoid having two at the same time.
drop(start_bx);

let reachable_blocks = traversal::mono_reachable_as_bitset(mir, cx.tcx(), instance);

// Codegen the body of each block using reverse postorder
for (bb, _) in traversal::reverse_postorder(mir) {
if reachable_blocks.contains(bb) {
Expand Down
23 changes: 21 additions & 2 deletions compiler/rustc_middle/src/mir/traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,10 @@ pub fn mono_reachable_as_bitset<'a, 'tcx>(
body: &'a Body<'tcx>,
tcx: TyCtxt<'tcx>,
instance: Instance<'tcx>,
) -> BitSet<BasicBlock> {
) -> (BitSet<BasicBlock>, BitSet<Local>) {
let mut iter = mono_reachable(body, tcx, instance);
while let Some(_) = iter.next() {}
iter.visited
(iter.visited, iter.locals)
}

pub struct MonoReachable<'a, 'tcx> {
Expand All @@ -318,6 +318,23 @@ pub struct MonoReachable<'a, 'tcx> {
// store ours in a BitSet and thus save allocations because BitSet has a small size
// optimization.
worklist: BitSet<BasicBlock>,
locals: BitSet<Local>,
}

struct UsedLocals<'a> {
locals: &'a mut BitSet<Local>,
}

use crate::mir::visit::Visitor;
impl<'a, 'tcx> Visitor<'tcx> for UsedLocals<'a> {
fn visit_local(
&mut self,
local: Local,
_ctx: crate::mir::visit::PlaceContext,
_location: Location,
) {
self.locals.insert(local);
}
}

impl<'a, 'tcx> MonoReachable<'a, 'tcx> {
Expand All @@ -334,6 +351,7 @@ impl<'a, 'tcx> MonoReachable<'a, 'tcx> {
instance,
visited: BitSet::new_empty(body.basic_blocks.len()),
worklist,
locals: BitSet::new_empty(body.local_decls.len()),
}
}

Expand All @@ -357,6 +375,7 @@ impl<'a, 'tcx> Iterator for MonoReachable<'a, 'tcx> {
}

let data = &self.body[idx];
UsedLocals { locals: &mut self.locals }.visit_basic_block_data(idx, data);

if let Some((bits, targets)) =
Body::try_const_mono_switchint(self.tcx, self.instance, data)
Expand Down

0 comments on commit 8df3ccc

Please sign in to comment.