Skip to content

Commit

Permalink
Auto merge of #32210 - Aatch:mir-traversal, r=nikomatsakis
Browse files Browse the repository at this point in the history
rBreak Critical Edges and other MIR work

This PR is built on top of #32080.

This adds the basic depth-first traversals for MIR, preorder, postorder and reverse postorder. The MIR blocks are now translated using reverse postorder. There is also a transform for breaking critical edges, which includes the edges from `invoke`d calls (`Drop` and `Call`), to account for the fact that we can't add code after an `invoke`. It also stops generating the intermediate block (since the transform essentially does it if necessary already).

The kinds of cases this deals with are difficult to produce, so the test is the one I managed to get. However, it seems to bootstrap with `-Z orbit`, which it didn't before my changes.
  • Loading branch information
bors committed Apr 3, 2016
2 parents 0894b06 + 605bc04 commit c0b8c43
Show file tree
Hide file tree
Showing 10 changed files with 539 additions and 49 deletions.
28 changes: 27 additions & 1 deletion src/librustc_data_structures/bitvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::iter::FromIterator;

/// A very simple BitVector type.
#[derive(Clone)]
pub struct BitVector {
data: Vec<u64>,
}
Expand Down Expand Up @@ -50,7 +53,9 @@ impl BitVector {
pub fn grow(&mut self, num_bits: usize) {
let num_words = u64s(num_bits);
let extra_words = self.data.len() - num_words;
self.data.extend((0..extra_words).map(|_| 0));
if extra_words > 0 {
self.data.extend((0..extra_words).map(|_| 0));
}
}

/// Iterates over indexes of set bits in a sorted order
Expand Down Expand Up @@ -93,6 +98,27 @@ impl<'a> Iterator for BitVectorIter<'a> {
}
}

impl FromIterator<bool> for BitVector {
fn from_iter<I>(iter: I) -> BitVector where I: IntoIterator<Item=bool> {
let iter = iter.into_iter();
let (len, _) = iter.size_hint();
// Make the minimum length for the bitvector 64 bits since that's
// the smallest non-zero size anyway.
let len = if len < 64 { 64 } else { len };
let mut bv = BitVector::new(len);
for (idx, val) in iter.enumerate() {
if idx > len {
bv.grow(idx);
}
if val {
bv.insert(idx);
}
}

bv
}
}

/// A "bit matrix" is basically a square matrix of booleans
/// represented as one gigantic bitvector. In other words, it is as if
/// you have N bitvectors, each of length N. Note that `elements` here is `N`/
Expand Down
20 changes: 13 additions & 7 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,10 +881,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
passes.push_pass(box mir::transform::remove_dead_blocks::RemoveDeadBlocks);
passes.push_pass(box mir::transform::type_check::TypeckMir);
passes.push_pass(box mir::transform::simplify_cfg::SimplifyCfg);
// Late passes
passes.push_pass(box mir::transform::no_landing_pads::NoLandingPads);
passes.push_pass(box mir::transform::remove_dead_blocks::RemoveDeadBlocks);
passes.push_pass(box mir::transform::erase_regions::EraseRegions);
// And run everything.
passes.run_passes(tcx, &mut mir_map);
});
Expand Down Expand Up @@ -937,16 +934,25 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,

/// Run the translation phase to LLVM, after which the AST and analysis can
pub fn phase_4_translate_to_llvm<'tcx>(tcx: &TyCtxt<'tcx>,
mir_map: MirMap<'tcx>,
analysis: ty::CrateAnalysis)
-> trans::CrateTranslation {
mut mir_map: MirMap<'tcx>,
analysis: ty::CrateAnalysis) -> trans::CrateTranslation {
let time_passes = tcx.sess.time_passes();

time(time_passes,
"resolving dependency formats",
|| dependency_format::calculate(&tcx.sess));

// Option dance to work around the lack of stack once closures.
// Run the passes that transform the MIR into a more suitable for translation
// to LLVM code.
time(time_passes, "Prepare MIR codegen passes", || {
let mut passes = ::rustc::mir::transform::Passes::new();
passes.push_pass(box mir::transform::no_landing_pads::NoLandingPads);
passes.push_pass(box mir::transform::remove_dead_blocks::RemoveDeadBlocks);
passes.push_pass(box mir::transform::erase_regions::EraseRegions);
passes.push_pass(box mir::transform::break_critical_edges::BreakCriticalEdges);
passes.run_passes(tcx, &mut mir_map);
});

time(time_passes,
"translation",
move || trans::trans_crate(tcx, &mir_map, analysis))
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ mod hair;
pub mod mir_map;
pub mod pretty;
pub mod transform;
pub mod traversal;
117 changes: 117 additions & 0 deletions src/librustc_mir/transform/break_critical_edges.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use rustc::ty::TyCtxt;
use rustc::mir::repr::*;
use rustc::mir::transform::{MirPass, Pass};
use syntax::ast::NodeId;

use rustc_data_structures::bitvec::BitVector;

use traversal;

pub struct BreakCriticalEdges;

/**
* Breaks critical edges in the MIR.
*
* Critical edges are edges that are neither the only edge leaving a
* block, nor the only edge entering one.
*
* When you want something to happen "along" an edge, you can either
* do at the end of the predecessor block, or at the start of the
* successor block. Critical edges have to be broken in order to prevent
* "edge actions" from affecting other edges.
*
* This function will break those edges by inserting new blocks along them.
*
* A special case is Drop and Call terminators with unwind/cleanup successors,
* They use `invoke` in LLVM, which terminates a block, meaning that code cannot
* be inserted after them, so even if an edge is the only edge leaving a block
* like that, we still insert blocks if the edge is one of many entering the
* target.
*
* NOTE: Simplify CFG will happily undo most of the work this pass does.
*
*/

impl<'tcx> MirPass<'tcx> for BreakCriticalEdges {
fn run_pass(&mut self, _: &TyCtxt<'tcx>, _: NodeId, mir: &mut Mir<'tcx>) {
break_critical_edges(mir);
}
}

impl Pass for BreakCriticalEdges {}

fn break_critical_edges(mir: &mut Mir) {
let mut pred_count = vec![0u32; mir.basic_blocks.len()];

// Build the precedecessor map for the MIR
for (_, data) in traversal::preorder(mir) {
if let Some(ref term) = data.terminator {
for &tgt in term.successors().iter() {
pred_count[tgt.index()] += 1;
}
}
}

let cleanup_map : BitVector = mir.basic_blocks
.iter().map(|bb| bb.is_cleanup).collect();

// We need a place to store the new blocks generated
let mut new_blocks = Vec::new();

let bbs = mir.all_basic_blocks();
let cur_len = mir.basic_blocks.len();

for &bb in &bbs {
let data = mir.basic_block_data_mut(bb);

if let Some(ref mut term) = data.terminator {
let is_invoke = term_is_invoke(term);
let term_span = term.span;
let term_scope = term.scope;
let succs = term.successors_mut();
if succs.len() > 1 || (succs.len() > 0 && is_invoke) {
for tgt in succs {
let num_preds = pred_count[tgt.index()];
if num_preds > 1 {
// It's a critical edge, break it
let goto = Terminator {
span: term_span,
scope: term_scope,
kind: TerminatorKind::Goto { target: *tgt }
};
let mut data = BasicBlockData::new(Some(goto));
data.is_cleanup = cleanup_map.contains(tgt.index());

// Get the index it will be when inserted into the MIR
let idx = cur_len + new_blocks.len();
new_blocks.push(data);
*tgt = BasicBlock::new(idx);
}
}
}
}
}

debug!("Broke {} N edges", new_blocks.len());

mir.basic_blocks.extend_from_slice(&new_blocks);
}

// Returns true if the terminator would use an invoke in LLVM.
fn term_is_invoke(term: &Terminator) -> bool {
match term.kind {
TerminatorKind::Call { cleanup: Some(_), .. } |
TerminatorKind::Drop { unwind: Some(_), .. } => true,
_ => false
}
}
1 change: 1 addition & 0 deletions src/librustc_mir/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ pub mod simplify_cfg;
pub mod erase_regions;
pub mod no_landing_pads;
pub mod type_check;
pub mod break_critical_edges;
Loading

0 comments on commit c0b8c43

Please sign in to comment.