Skip to content

Commit

Permalink
Make Session.crate_types thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Apr 10, 2018
1 parent 046af1c commit cbf8ad4
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/librustc/session/mod.rs
Expand Up @@ -92,7 +92,7 @@ pub struct Session {
pub one_time_diagnostics: RefCell<FxHashSet<(DiagnosticMessageId, Option<Span>, String)>>,
pub plugin_llvm_passes: OneThread<RefCell<Vec<String>>>,
pub plugin_attributes: OneThread<RefCell<Vec<(String, AttributeType)>>>,
pub crate_types: RefCell<Vec<config::CrateType>>,
pub crate_types: Once<Vec<config::CrateType>>,
pub dependency_formats: RefCell<dependency_format::Dependencies>,
/// The crate_disambiguator is constructed out of all the `-C metadata`
/// arguments passed to the compiler. Its value together with the crate-name
Expand Down Expand Up @@ -1096,7 +1096,7 @@ pub fn build_session_(
one_time_diagnostics: RefCell::new(FxHashSet()),
plugin_llvm_passes: OneThread::new(RefCell::new(Vec::new())),
plugin_attributes: OneThread::new(RefCell::new(Vec::new())),
crate_types: RefCell::new(Vec::new()),
crate_types: Once::new(),
dependency_formats: RefCell::new(FxHashMap()),
crate_disambiguator: Once::new(),
features: Once::new(),
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_driver/driver.rs
Expand Up @@ -652,7 +652,8 @@ pub fn phase_2_configure_and_expand_inner<'a, F>(sess: &'a Session,
// these need to be set "early" so that expansion sees `quote` if enabled.
sess.init_features(features);

*sess.crate_types.borrow_mut() = collect_crate_types(sess, &krate.attrs);
let crate_types = collect_crate_types(sess, &krate.attrs);
sess.crate_types.set(crate_types);

let disambiguator = compute_crate_disambiguator(sess);
sess.crate_disambiguator.set(disambiguator);
Expand Down
13 changes: 8 additions & 5 deletions src/librustc_trans/back/write.rs
Expand Up @@ -154,13 +154,16 @@ fn get_llvm_opt_size(optimize: config::OptLevel) -> llvm::CodeGenOptSize {
}
}

pub fn create_target_machine(sess: &Session) -> TargetMachineRef {
target_machine_factory(sess)().unwrap_or_else(|err| {
pub fn create_target_machine(sess: &Session, find_features: bool) -> TargetMachineRef {
target_machine_factory(sess, find_features)().unwrap_or_else(|err| {
llvm_err(sess.diagnostic(), err).raise()
})
}

pub fn target_machine_factory(sess: &Session)
// If find_features is true this won't access `sess.crate_types` by assuming
// that `is_pie_binary` is false. When we discover LLVM target features
// `sess.crate_types` is uninitialized so we cannot access it.
pub fn target_machine_factory(sess: &Session, find_features: bool)
-> Arc<Fn() -> Result<TargetMachineRef, String> + Send + Sync>
{
let reloc_model = get_reloc_model(sess);
Expand Down Expand Up @@ -201,7 +204,7 @@ pub fn target_machine_factory(sess: &Session)
};
let cpu = CString::new(cpu.as_bytes()).unwrap();
let features = CString::new(target_feature(sess).as_bytes()).unwrap();
let is_pie_binary = is_pie_binary(sess);
let is_pie_binary = !find_features && is_pie_binary(sess);
let trap_unreachable = sess.target.target.options.trap_unreachable;

Arc::new(move || {
Expand Down Expand Up @@ -1510,7 +1513,7 @@ fn start_executing_work(tcx: TyCtxt,
regular_module_config: modules_config,
metadata_module_config: metadata_config,
allocator_module_config: allocator_config,
tm_factory: target_machine_factory(tcx.sess),
tm_factory: target_machine_factory(tcx.sess, false),
total_cgus,
msvc_imps_needed: msvc_imps_needed(tcx),
target_pointer_width: tcx.sess.target.target.target_pointer_width.clone(),
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_trans/base.rs
Expand Up @@ -737,7 +737,7 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
source: ModuleSource::Translated(ModuleLlvm {
llcx: metadata_llcx,
llmod: metadata_llmod,
tm: create_target_machine(tcx.sess),
tm: create_target_machine(tcx.sess, false),
}),
kind: ModuleKind::Metadata,
};
Expand Down Expand Up @@ -803,7 +803,7 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let modules = ModuleLlvm {
llmod,
llcx,
tm: create_target_machine(tcx.sess),
tm: create_target_machine(tcx.sess, false),
};
time(tcx.sess, "write allocator module", || {
allocator::trans(tcx, &modules, kind)
Expand Down Expand Up @@ -1260,7 +1260,7 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let llvm_module = ModuleLlvm {
llcx: cx.llcx,
llmod: cx.llmod,
tm: create_target_machine(cx.sess()),
tm: create_target_machine(cx.sess(), false),
};

ModuleTranslation {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/context.rs
Expand Up @@ -162,7 +162,7 @@ pub unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (Cont

// Ensure the data-layout values hardcoded remain the defaults.
if sess.target.target.options.is_builtin {
let tm = ::back::write::create_target_machine(sess);
let tm = ::back::write::create_target_machine(sess, false);
llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm);
llvm::LLVMRustDisposeTargetMachine(tm);

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trans/llvm_util.rs
Expand Up @@ -140,7 +140,7 @@ pub fn to_llvm_feature<'a>(sess: &Session, s: &'a str) -> &'a str {
}

pub fn target_features(sess: &Session) -> Vec<Symbol> {
let target_machine = create_target_machine(sess);
let target_machine = create_target_machine(sess, true);
target_feature_whitelist(sess)
.iter()
.filter(|feature| {
Expand Down Expand Up @@ -178,7 +178,7 @@ pub fn print_passes() {

pub(crate) fn print(req: PrintRequest, sess: &Session) {
require_inited();
let tm = create_target_machine(sess);
let tm = create_target_machine(sess, true);
unsafe {
match req {
PrintRequest::TargetCPUs => llvm::LLVMRustPrintTargetCPUs(tm),
Expand Down

0 comments on commit cbf8ad4

Please sign in to comment.