Skip to content

Commit 2b31456

Browse files
committed
Add pass to ensure predecessors cache is generated after optimization
1 parent f534d9f commit 2b31456

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

src/librustc/mir/mod.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -207,27 +207,14 @@ impl<'tcx> Body<'tcx> {
207207
}
208208

209209
pub fn basic_block_terminator_opt_mut(&mut self, bb: BasicBlock) -> &mut Option<Terminator<'tcx>> {
210+
// FIXME we should look into improving the cache invalidation
210211
self.predecessors_cache = None;
211212
&mut self.basic_blocks[bb].terminator
212213
}
213214

214215
pub fn basic_block_terminator_mut(&mut self, bb: BasicBlock) -> &mut Terminator<'tcx> {
216+
// FIXME we should look into improving the cache invalidation
215217
self.predecessors_cache = None;
216-
/*
217-
let data = &mut self.basic_blocks[bb];
218-
if let Some(cache) = self.predecessors_cache.as_mut() {
219-
for successor in data.terminator().successors() {
220-
let successor_vec = &mut cache[*successor];
221-
for i in (0..successor_vec.len()).rev() {
222-
if successor_vec[i] == bb {
223-
successor_vec.swap_remove(i);
224-
break;
225-
}
226-
}
227-
}
228-
}
229-
*/
230-
231218
self.basic_blocks[bb].terminator_mut()
232219
}
233220

@@ -245,6 +232,7 @@ impl<'tcx> Body<'tcx> {
245232
}
246233

247234
#[inline]
235+
/// This will recompute the predecessors cache if it is not available
248236
pub fn predecessors(&mut self) -> &IndexVec<BasicBlock, Vec<BasicBlock>> {
249237
if self.predecessors_cache.is_none() {
250238
self.predecessors_cache = Some(self.calculate_predecessors())
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use rustc::mir::Body;
2+
use rustc::ty::TyCtxt;
3+
use crate::transform::{MirPass, MirSource};
4+
5+
pub struct EnsurePredecessorsCache;
6+
7+
impl<'tcx> MirPass<'tcx> for EnsurePredecessorsCache {
8+
fn run_pass(&self, _: TyCtxt<'tcx>, _: MirSource<'tcx>, body: &mut Body<'tcx>) {
9+
// predecessors is lazily calculated. We want to ensure that the cache is properly filled
10+
// before the next stages of compilation, since thise following stages will only be allowed
11+
// to read the cache and not generate it. If the cache is already up to date, this line is
12+
// a nop.
13+
let _predecessors = body.predecessors();
14+
}
15+
}

src/librustc_mir/transform/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod check_unsafety;
2020
pub mod simplify_branches;
2121
pub mod simplify_try;
2222
pub mod simplify;
23+
pub mod ensure_predecessors_cache;
2324
pub mod erase_regions;
2425
pub mod no_landing_pads;
2526
pub mod rustc_peek;
@@ -313,6 +314,7 @@ fn run_optimization_passes<'tcx>(
313314
&simplify::SimplifyLocals,
314315

315316
&add_call_guards::CriticalCallEdges,
317+
&ensure_predecessors_cache::EnsurePredecessorsCache,
316318
&dump_mir::Marker("PreCodegen"),
317319
]);
318320
}

0 commit comments

Comments
 (0)