Skip to content
This repository was archived by the owner on Jan 7, 2022. It is now read-only.
Closed
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
10 changes: 10 additions & 0 deletions src/librustc_mir/transform/add_yk_swt_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ use rustc::hir;
use rustc::hir::def_id::{DefIndex, LOCAL_CRATE};
use rustc::hir::map::blocks::FnLikeNode;

// These crates are not transformed by this MIR pass.
// FIXME: Currently libstd and its deps are blacklisted. In the long run, we don't want this.
static BLACKLISTED_CRATES: &'static [&'static str] = &["std", "alloc", "panic_unwind",
"panic_abort", "core", "libc", "profiler_builtins", "unwind"];

/// A MIR transformation that, for each basic block, inserts a call to the software trace recorder.
/// The arguments to the calls (crate hash, DefId and block index) identify the position to be
/// inserted into a trace.
Expand Down Expand Up @@ -170,6 +175,11 @@ fn is_untraceable(tcx: TyCtxt<'a, 'tcx, 'tcx>, src: MirSource) -> bool {
return true;
}

// Don't transform any crate which is blacklisted. See the comment against BLACKLISTED_CRATES.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this comment (it just says what the line of code below does, and the line of code is very clear IMHO).

if BLACKLISTED_CRATES.contains(&&*tcx.crate_name(LOCAL_CRATE).as_str()) {
return true;
}

// We can't transform promoted items, because they are `const`, and our trace recorder isn't.
if let Some(_) = src.promoted {
return true;
Expand Down
55 changes: 54 additions & 1 deletion src/libstd/yk_swt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,45 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use ::cell::RefCell;
use ::fmt;

#[allow(missing_docs)]
/// A block location in the Rust MIR.
pub struct MirLoc {
pub crate_hash: u64,
pub def_idx: u32,
pub bb_idx: u32,
}

impl fmt::Debug for MirLoc {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth this manual code vs. #[derive(Debug)]?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I did a custom one was because the derived one is quite long, with the names of the fields etc.

I wanted a shorter representation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, but let's only do this sparingly, just because it's that bit more code to read and maintain.

fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "loc<{}, {}, {}>", self.crate_hash, self.def_idx, self.bb_idx)
}
}

thread_local! {
/// The software trace currently being collected (if any).
/// When `Some`, a tracing is enabled, otherwise tracing is disabled.
pub static TRACE: RefCell<Option<Vec<MirLoc>>> = RefCell::new(None);
}

/// Start software tracing.
#[cfg_attr(not(stage0), no_trace)]
pub fn start_tracing() {
TRACE.with(|rc| {
let mut trace_o = rc.borrow_mut();
match *trace_o {
Some(_) => panic!("tracing was already started for this thread!"),
None => *trace_o = Some(Vec::new()),
}
});
}

// FIXME Anything used in `rec_loc` below cannot itself be traced, or we get infinite recursion. To
// work around this, many crates are ignored by the software tracing MIR pass (see
// librustc_mir/transform/add_yk_swt_calls.rs). Consider re-implementing the trace recorder in C?

/// The software trace recorder function.
/// The `AddYkSWTCalls` MIR pass injects a call this for every MIR block. The call is done
/// indirectly via a wrapper in libcore.
Expand All @@ -15,5 +54,19 @@
#[cfg_attr(not(stage0), no_trace)]
#[cfg(not(test))]
fn rec_loc(crate_hash: u64, def_idx: u32, bb_idx: u32) {
// Not implemented.
TRACE.with(|rc| {
let mut trace_o = rc.borrow_mut();
match trace_o.as_mut() {
Some(trace) => trace.push(MirLoc{crate_hash, def_idx, bb_idx}),
None => (), // We are not currently tracing, do nothing.
}
});
}

/// Stop tracing and return the trace.
#[cfg_attr(not(stage0), no_trace)]
pub fn stop_tracing() -> Vec<MirLoc> {
TRACE.with(|rc| {
rc.borrow_mut().take().expect("tracing not started on this thread")
})
}
19 changes: 19 additions & 0 deletions src/test/run-fail/yk_swt/trace-already-started.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2019 King's College London.
// Created by the Software Development Team <http://soft-dev.org/>.
//
// 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.

// error-pattern: thread 'main' panicked at 'tracing was already started for this thread!'

#![feature(yk_swt)]

use std::yk_swt::{start_tracing, stop_tracing};

pub fn main() {
start_tracing();
start_tracing(); // Can't call a second time without a stop_tracing() first.
}
19 changes: 19 additions & 0 deletions src/test/run-fail/yk_swt/trace-not-started.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2019 King's College London.
// Created by the Software Development Team <http://soft-dev.org/>.
//
// 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.

// error-pattern: thread 'main' panicked at 'tracing not started on this thread'

#![feature(yk_swt)]

use std::yk_swt::{start_tracing, stop_tracing};

pub fn main() {
// Missing start_tracing();
let _ = stop_tracing();
}
4 changes: 4 additions & 0 deletions src/test/run-pass/signal-alternate-stack-cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
// ignore-wasm32-bare no libc
// ignore-windows

// Software tracing breaks this test as the thread local used to record the trace becomes valid at
// atexit() time.
#![no_trace]

#![feature(libc)]
extern crate libc;

Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/thinlto/thin-lto-inlines2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
// praying two functions go into separate codegen units and then assuming that
// if inlining *doesn't* happen the first byte of the functions will differ.

#![no_trace]

extern crate thin_lto_inlines_aux as bar;

pub fn foo() -> u32 {
Expand Down
28 changes: 28 additions & 0 deletions src/test/run-pass/yk_swt/collect-trace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2019 King's College London.
// Created by the Software Development Team <http://soft-dev.org/>.
//
// 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.

#![feature(yk_swt)]

use std::yk_swt::{start_tracing, stop_tracing};

pub fn main() {
start_tracing();
let _ = work();
let trace = stop_tracing();
assert!(!trace.is_empty());
}

#[inline(never)]
fn work() -> u64{
let mut res = 100;
for i in 0..10 {
res += res / 2 + i;
}
res
}