Skip to content

Commit 2863ecf

Browse files
committed
Compute block costs lazily.
1 parent cb1aa85 commit 2863ecf

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

compiler/rustc_mir_transform/src/jump_threading.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
//! Applying the optimisation can create a lot of new MIR, so we bound the instruction
3131
//! cost by `MAX_COST`.
3232
33+
use std::cell::OnceCell;
34+
3335
use rustc_arena::DroplessArena;
3436
use rustc_const_eval::const_eval::DummyMachine;
3537
use rustc_const_eval::interpret::{ImmTy, Immediate, InterpCx, OpTy, Projectable};
@@ -82,15 +84,7 @@ impl<'tcx> crate::MirPass<'tcx> for JumpThreading {
8284
loop_headers: loop_headers(body),
8385
entry_states: IndexVec::from_elem(ConditionSet::BOTTOM, &body.basic_blocks),
8486
opportunities: Vec::new(),
85-
costs: body
86-
.basic_blocks
87-
.iter_enumerated()
88-
.map(|(bb, bbdata)| {
89-
let mut cost = CostChecker::new(tcx, typing_env, None, body);
90-
cost.visit_basic_block_data(bb, bbdata);
91-
cost.cost()
92-
})
93-
.collect(),
87+
costs: IndexVec::from_elem(OnceCell::new(), &body.basic_blocks),
9488
};
9589

9690
for (bb, bbdata) in traversal::postorder(body) {
@@ -166,7 +160,7 @@ struct TOFinder<'a, 'tcx> {
166160
arena: &'a DroplessArena,
167161
opportunities: Vec<ThreadingOpportunity>,
168162
/// Pre-computed cost of duplicating each block.
169-
costs: IndexVec<BasicBlock, usize>,
163+
costs: IndexVec<BasicBlock, OnceCell<usize>>,
170164
}
171165

172166
/// Singly-linked list to represent chains of blocks. This is cheap to copy, and is converted to
@@ -334,13 +328,22 @@ impl<'a, 'tcx> TOFinder<'a, 'tcx> {
334328
cond.chain = BBChain::cons(self.arena, bb, cond.chain);
335329
// Remove conditions for which the duplication cost is too high.
336330
// This is required to keep the size of the `ConditionSet` tractable.
337-
let cost = cond.cost + self.costs[head];
331+
let cost = cond.cost + self.cost(head);
338332
cond.cost = cost;
339333
cost <= MAX_COST
340334
});
341335
ConditionSet(state)
342336
}
343337

338+
fn cost(&self, bb: BasicBlock) -> usize {
339+
*self.costs[bb].get_or_init(|| {
340+
let bbdata = &self.body[bb];
341+
let mut cost = CostChecker::new(self.tcx, self.typing_env, None, self.body);
342+
cost.visit_basic_block_data(bb, bbdata);
343+
cost.cost()
344+
})
345+
}
346+
344347
/// Remove all conditions in the state that alias given place.
345348
fn flood_state(
346349
&self,

0 commit comments

Comments
 (0)