Skip to content

Commit

Permalink
Auto merge of rust-lang#129283 - saethlin:unreachable-allocas, r=<try>
Browse files Browse the repository at this point in the history
Don't alloca for unused locals

This fixes the second problem in rust-lang#129282

r? `@ghost`
  • Loading branch information
bors committed Aug 20, 2024
2 parents 636d7ff + f98c04d commit 2d91c0b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_cranelift/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
.generic_activity("codegen prelude")
.run(|| crate::abi::codegen_fn_prelude(fx, start_block));

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

for (bb, bb_data) in fx.mir.basic_blocks.iter_enumerated() {
let block = fx.get_block(bb);
Expand Down
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 2d91c0b

Please sign in to comment.