Skip to content
This repository was archived by the owner on Jan 7, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3062,12 +3062,11 @@ version = "0.0.0"
name = "rustc_yk_sections"
version = "0.0.0"
dependencies = [
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc 0.0.0",
"rustc_codegen_utils 0.0.0",
"rustc_data_structures 0.0.0",
"rustc_yk_link 0.0.0",
"ykpack 0.1.0 (git+https://github.com/softdevteam/ykpack)",
"ykpack 0.1.0 (git+https://github.com/softdevteam/yk)",
]

[[package]]
Expand Down Expand Up @@ -4046,7 +4045,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "ykpack"
version = "0.1.0"
source = "git+https://github.com/softdevteam/ykpack#9c5542d9d556ce745f0b21983a009a1297f371e4"
source = "git+https://github.com/softdevteam/yk#0c7016c47bff4b149a2bc1d304cf637f6d64719e"
dependencies = [
"fallible-iterator 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"rmp-serde 0.14.0 (git+https://github.com/3Hren/msgpack-rust?rev=40b3d480b20961e6eeceb416b32bcd0a3383846a)",
Expand Down Expand Up @@ -4390,4 +4389,4 @@ dependencies = [
"checksum xattr 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "244c3741f4240ef46274860397c7c74e50eb23624996930e484c16679633a54c"
"checksum xz2 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "df8bf41d3030c3577c9458fd6640a05afbf43b150d0b531b16bd77d3f794f27a"
"checksum yaml-rust 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e66366e18dc58b46801afbf2ca7661a9f59cc8c5962c29892b6039b4f86fa992"
"checksum ykpack 0.1.0 (git+https://github.com/softdevteam/ykpack)" = "<none>"
"checksum ykpack 0.1.0 (git+https://github.com/softdevteam/yk)" = "<none>"
4 changes: 2 additions & 2 deletions src/librustc_yk_sections/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
authors = ["Edd Barrett <vext01@gmail.com>"]
name = "rustc_yk_sections"
version = "0.0.0"
edition = "2018"

[lib]
name = "rustc_yk_sections"
Expand All @@ -12,7 +13,6 @@ test = false
[dependencies]
rustc = { path = "../librustc" }
rustc_yk_link = { path = "../librustc_yk_link" }
ykpack = { git = "https://github.com/softdevteam/ykpack" }
ykpack = { git = "https://github.com/softdevteam/yk" }
rustc_codegen_utils = { path = "../librustc_codegen_utils" }
rustc_data_structures = { path = "../librustc_data_structures" }
log = "0.4.5"
83 changes: 7 additions & 76 deletions src/librustc_yk_sections/emit_tir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! This module converts MIR into Yorick TIR (Tracing IR). TIR is more suitable for the run-time
//! tracer: TIR (unlike MIR) is in SSA form (but it does preserve MIR's block structure).
//! This module converts MIR into Yorick TIR (Tracing IR).
//! Note that we preserve the MIR block structure when lowering to TIR.
//!
//! Serialisation itself is performed by an external library: ykpack.

Expand All @@ -30,8 +30,7 @@ use std::error::Error;
use std::cell::{Cell, RefCell};
use std::mem::size_of;
use rustc_data_structures::bit_set::BitSet;
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
use rustc_data_structures::graph::dominators::DominatorFrontiers;
use rustc_data_structures::indexed_vec::IndexVec;
use ykpack;
use ykpack::LocalIndex as TirLocal;
use rustc_data_structures::fx::FxHashSet;
Expand All @@ -48,7 +47,7 @@ pub enum TirMode {
TextDump(PathBuf),
}

/// A conversion context holds the state needed to perform the conversion to (pre-SSA) TIR.
/// A conversion context holds the state needed to perform the TIR lowering.
struct ConvCx<'a, 'tcx, 'gcx> {
/// The compiler's god struct. Needed for queries etc.
tcx: &'a TyCtxt<'a, 'tcx, 'gcx>,
Expand Down Expand Up @@ -106,11 +105,6 @@ impl<'a, 'tcx, 'gcx> ConvCx<'a, 'tcx, 'gcx> {
})
}

/// Finalise the conversion context, returning the definition sites and block defines mappings.
fn done(self) -> (Vec<BitSet<BasicBlock>>, IndexVec<BasicBlock, FxHashSet<TirLocal>>) {
(self.def_sites.into_inner(), self.block_defines.into_inner())
}

/// Add `bb` as a definition site of the TIR variable `var`.
fn push_def_site(&self, bb: BasicBlock, var: TirLocal) {
let mut sites = self.def_sites.borrow_mut();
Expand Down Expand Up @@ -165,12 +159,12 @@ fn do_generate_tir<'a, 'tcx, 'gcx>(
// same between builds for the reproducible build tests to pass.
let mut tir_path = exe_path.clone();
tir_path.set_extension(TMP_EXT);
let mut file = File::create(&tir_path)?;
let file = File::create(&tir_path)?;
(tir_path, Some(file), None)
},
TirMode::TextDump(dump_path) => {
// In text dump mode we just write lines to a file and we don't need an encoder.
let mut file = File::create(&dump_path)?;
let file = File::create(&dump_path)?;
(dump_path.clone(), None, Some(file))
},
};
Expand All @@ -187,19 +181,9 @@ fn do_generate_tir<'a, 'tcx, 'gcx>(

for def_id in sorted_def_ids {
if tcx.is_mir_available(*def_id) {
info!("generating TIR for {:?}", def_id);

let mir = tcx.optimized_mir(*def_id);
let ccx = ConvCx::new(tcx, mir);

let mut pack = (&ccx, def_id, tcx.optimized_mir(*def_id)).to_pack();
{
let ykpack::Pack::Mir(ykpack::Mir{ref mut blocks, ..}) = pack;
let (def_sites, block_defines) = ccx.done();
insert_phis(blocks, mir, def_sites, block_defines);
}

// FIXME - rename variables with fresh SSA names.
let pack = (&ccx, def_id, tcx.optimized_mir(*def_id)).to_pack();

if let Some(ref mut e) = enc {
e.serialise(pack)?;
Expand All @@ -218,51 +202,6 @@ fn do_generate_tir<'a, 'tcx, 'gcx>(
Ok(tir_path)
}

/// Insert PHI nodes into the initial pre-SSA TIR pack.
///
/// Algorithm reference:
/// Bottom of p406 of 'Modern Compiler Implementation in Java (2nd ed.)' by Andrew Appel.
fn insert_phis(blocks: &mut Vec<ykpack::BasicBlock>, mir: &Mir,
mut def_sites: Vec<BitSet<BasicBlock>>,
a_orig: IndexVec<BasicBlock, FxHashSet<TirLocal>>) {
let doms = mir.dominators();
let df = DominatorFrontiers::new(mir, &doms);
let num_tir_vars = def_sites.len();
let num_tir_blks = a_orig.len();

let mut a_phi: Vec<BitSet<TirLocal>> = Vec::with_capacity(num_tir_blks);
a_phi.resize(num_tir_blks, BitSet::new_empty(num_tir_vars));

// We don't need the elements of `def_sites` again past this point, so we can take them out
// of `def_sites` with a draining iterator and mutate in-place.
for (a, mut w) in def_sites.drain(..).enumerate() {
while !w.is_empty() {
let n = bitset_pop(&mut w);
for y in df.frontier(n).iter() {
let y_usize = y.index();
// `def_sites` is guaranteed to only contain indices expressible by `u32`.
let a_u32 = a as u32;
if !a_phi[y_usize].contains(a_u32) {
a_phi[y_usize].insert(a_u32);
if !a_orig[y].contains(&a_u32) {
// The assertion in `tir_var()` has already checked the cast is safe.
insert_phi(&mut blocks[y_usize], a as u32, mir.predecessors_for(y).len());
w.insert(y);
}
}
}
}
}
}

fn insert_phi(block: &mut ykpack::BasicBlock, var: TirLocal, arity: usize) {
let lhs = ykpack::Place::Local(var);
let rhs_vars = (0..arity).map(|_| lhs.clone()).collect();
let rhs = ykpack::Rvalue::Phi(rhs_vars);
block.stmts.insert(0, ykpack::Statement::Assign(lhs, rhs));
}


/// The trait for converting MIR data structures into a bytecode packs.
trait ToPack<T> {
fn to_pack(&mut self) -> T;
Expand Down Expand Up @@ -415,11 +354,3 @@ impl<'tcx> ToPack<ykpack::Rvalue> for (&ConvCx<'_, 'tcx, '_>, &Rvalue<'tcx>) {
}
}
}

/// At the time of writing, you can't pop from a `BitSet`.
fn bitset_pop<T>(s: &mut BitSet<T>) -> T where T: Eq + Idx + Clone {
let e = s.iter().next().unwrap().clone();
let removed = s.remove(e);
debug_assert!(removed);
e
}
1 change: 0 additions & 1 deletion src/librustc_yk_sections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ extern crate rustc_yk_link;
extern crate rustc_codegen_utils;
extern crate ykpack;
extern crate rustc_data_structures;
#[macro_use] extern crate log;

pub mod emit_tir;
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ fn main() {
// Assign(Local(0), Unimplemented)
// term: Goto { target_bb: 4 }
// bb4:
// Assign(Local(0), Phi([Local(0), Local(0)]))
// Unimplemented
// ...
// [End TIR for main]
2 changes: 1 addition & 1 deletion src/tools/tidy/src/extdeps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const WHITELISTED_SOURCES: &[&str] = &[
// The following are needed for Yorick whilst we use an unreleased revision not on crates.io.
"\"git+https://github.com/3Hren/msgpack-rust?\
rev=40b3d480b20961e6eeceb416b32bcd0a3383846a#40b3d480b20961e6eeceb416b32bcd0a3383846a\"",
"\"git+https://github.com/softdevteam/ykpack#e0fd6162b9522fd04b6bbb77a232986c4ea5c257\"",
"\"git+https://github.com/softdevteam/yk#0c7016c47bff4b149a2bc1d304cf637f6d64719e\"",
];

/// Checks for external package sources.
Expand Down