Skip to content

propagate TIME_DEPTH to the helper threads for -Z time-passes #38645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2016
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
20 changes: 17 additions & 3 deletions src/librustc/util/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,27 @@ pub const FN_OUTPUT_NAME: &'static str = "Output";
#[derive(Clone, Copy, Debug)]
pub struct ErrorReported;

thread_local!(static TIME_DEPTH: Cell<usize> = Cell::new(0));

/// Read the current depth of `time()` calls. This is used to
/// encourage indentation across threads.
Copy link
Member

Choose a reason for hiding this comment

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

This comment seems to be nonsensical given that TIME_DEPTH is a thread-local static. Perhaps it should be an AtomicUsize and shared across threads instead?

pub fn time_depth() -> usize {
TIME_DEPTH.with(|slot| slot.get())
}

/// Set the current depth of `time()` calls. The idea is to call
/// `set_time_depth()` with the result from `time_depth()` in the
/// parent thread.
pub fn set_time_depth(depth: usize) {
TIME_DEPTH.with(|slot| slot.set(depth));
Copy link
Member

Choose a reason for hiding this comment

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

Oh.

}

pub fn time<T, F>(do_it: bool, what: &str, f: F) -> T where
F: FnOnce() -> T,
{
thread_local!(static DEPTH: Cell<usize> = Cell::new(0));
if !do_it { return f(); }

let old = DEPTH.with(|slot| {
let old = TIME_DEPTH.with(|slot| {
let r = slot.get();
slot.set(r + 1);
r
Expand All @@ -56,7 +70,7 @@ pub fn time<T, F>(do_it: bool, what: &str, f: F) -> T where
mem_string,
what);

DEPTH.with(|slot| slot.set(old));
TIME_DEPTH.with(|slot| slot.set(old));

rv
}
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_trans/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use llvm;
use llvm::{ModuleRef, TargetMachineRef, PassManagerRef, DiagnosticInfoRef, ContextRef};
use llvm::SMDiagnosticRef;
use {CrateTranslation, ModuleLlvm, ModuleSource, ModuleTranslation};
use util::common::time;
use util::common::{time, time_depth, set_time_depth};
use util::common::path2cstr;
use util::fs::link_or_copy;
use errors::{self, Handler, Level, DiagnosticBuilder};
Expand Down Expand Up @@ -1033,7 +1033,10 @@ fn run_work_multithreaded(sess: &Session,

let incr_comp_session_dir = sess.incr_comp_session_dir_opt().map(|r| r.clone());

let depth = time_depth();
thread::Builder::new().name(format!("codegen-{}", i)).spawn(move || {
set_time_depth(depth);

let diag_handler = Handler::with_emitter(true, false, box diag_emitter);

// Must construct cgcx inside the proc because it has non-Send
Expand Down