Skip to content

Commit 358e41c

Browse files
committed
Introduce the init_llvm function
Extract the code that performs the initialization of the LLVM backend and invoke it before computing the available features. The initialization is required to happen before the features are added to the configuration, because they are computed by LLVM, therefore is is now performed when creating the `Session` object.
1 parent 92e24b9 commit 358e41c

File tree

3 files changed

+58
-51
lines changed

3 files changed

+58
-51
lines changed

src/librustc/session/mod.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ use syntax::{ast, codemap};
3030
use syntax::feature_gate::AttributeType;
3131

3232
use rustc_back::target::Target;
33+
use llvm;
3334

3435
use std::path::{Path, PathBuf};
3536
use std::cell::{Cell, RefCell};
3637
use std::collections::{HashMap, HashSet};
3738
use std::env;
39+
use std::ffi::CString;
3840
use std::rc::Rc;
3941
use std::fmt;
42+
use libc::c_int;
4043

4144
pub mod config;
4245
pub mod filesearch;
@@ -491,9 +494,63 @@ pub fn build_session_(sopts: config::Options,
491494
imported_macro_spans: RefCell::new(HashMap::new()),
492495
};
493496

497+
init_llvm(&sess);
498+
494499
sess
495500
}
496501

502+
fn init_llvm(sess: &Session) {
503+
unsafe {
504+
// Before we touch LLVM, make sure that multithreading is enabled.
505+
use std::sync::Once;
506+
static INIT: Once = Once::new();
507+
static mut POISONED: bool = false;
508+
INIT.call_once(|| {
509+
if llvm::LLVMStartMultithreaded() != 1 {
510+
// use an extra bool to make sure that all future usage of LLVM
511+
// cannot proceed despite the Once not running more than once.
512+
POISONED = true;
513+
}
514+
515+
configure_llvm(sess);
516+
});
517+
518+
if POISONED {
519+
bug!("couldn't enable multi-threaded LLVM");
520+
}
521+
}
522+
}
523+
524+
unsafe fn configure_llvm(sess: &Session) {
525+
let mut llvm_c_strs = Vec::new();
526+
let mut llvm_args = Vec::new();
527+
528+
{
529+
let mut add = |arg: &str| {
530+
let s = CString::new(arg).unwrap();
531+
llvm_args.push(s.as_ptr());
532+
llvm_c_strs.push(s);
533+
};
534+
add("rustc"); // fake program name
535+
if sess.time_llvm_passes() { add("-time-passes"); }
536+
if sess.print_llvm_passes() { add("-debug-pass=Structure"); }
537+
538+
// FIXME #21627 disable faulty FastISel on AArch64 (even for -O0)
539+
if sess.target.target.arch == "aarch64" { add("-fast-isel=0"); }
540+
541+
for arg in &sess.opts.cg.llvm_args {
542+
add(&(*arg));
543+
}
544+
}
545+
546+
llvm::LLVMInitializePasses();
547+
548+
llvm::initialize_available_targets();
549+
550+
llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int,
551+
llvm_args.as_ptr());
552+
}
553+
497554
pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
498555
let mut emitter: Box<Emitter> = match output {
499556
config::ErrorOutputType::HumanReadable(color_config) => {

src/librustc_trans/back/write.rs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use std::str;
3131
use std::sync::{Arc, Mutex};
3232
use std::sync::mpsc::channel;
3333
use std::thread;
34-
use libc::{c_uint, c_int, c_void};
34+
use libc::{c_uint, c_void};
3535

3636
pub fn llvm_err(handler: &errors::Handler, msg: String) -> ! {
3737
match llvm::last_error() {
@@ -984,36 +984,6 @@ pub fn run_assembler(sess: &Session, outputs: &OutputFilenames) {
984984
}
985985
}
986986

987-
pub unsafe fn configure_llvm(sess: &Session) {
988-
let mut llvm_c_strs = Vec::new();
989-
let mut llvm_args = Vec::new();
990-
991-
{
992-
let mut add = |arg: &str| {
993-
let s = CString::new(arg).unwrap();
994-
llvm_args.push(s.as_ptr());
995-
llvm_c_strs.push(s);
996-
};
997-
add("rustc"); // fake program name
998-
if sess.time_llvm_passes() { add("-time-passes"); }
999-
if sess.print_llvm_passes() { add("-debug-pass=Structure"); }
1000-
1001-
// FIXME #21627 disable faulty FastISel on AArch64 (even for -O0)
1002-
if sess.target.target.arch == "aarch64" { add("-fast-isel=0"); }
1003-
1004-
for arg in &sess.opts.cg.llvm_args {
1005-
add(&(*arg));
1006-
}
1007-
}
1008-
1009-
llvm::LLVMInitializePasses();
1010-
1011-
llvm::initialize_available_targets();
1012-
1013-
llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int,
1014-
llvm_args.as_ptr());
1015-
}
1016-
1017987
pub unsafe fn with_llvm_pmb(llmod: ModuleRef,
1018988
config: &ModuleConfig,
1019989
f: &mut FnMut(llvm::PassManagerBuilderRef)) {

src/librustc_trans/base.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,26 +2709,6 @@ pub fn trans_crate<'tcx>(tcx: &TyCtxt<'tcx>,
27092709
tcx.sess.opts.debug_assertions
27102710
};
27112711

2712-
// Before we touch LLVM, make sure that multithreading is enabled.
2713-
unsafe {
2714-
use std::sync::Once;
2715-
static INIT: Once = Once::new();
2716-
static mut POISONED: bool = false;
2717-
INIT.call_once(|| {
2718-
if llvm::LLVMStartMultithreaded() != 1 {
2719-
// use an extra bool to make sure that all future usage of LLVM
2720-
// cannot proceed despite the Once not running more than once.
2721-
POISONED = true;
2722-
}
2723-
2724-
::back::write::configure_llvm(&tcx.sess);
2725-
});
2726-
2727-
if POISONED {
2728-
bug!("couldn't enable multi-threaded LLVM");
2729-
}
2730-
}
2731-
27322712
let link_meta = link::build_link_meta(&tcx, name);
27332713

27342714
let codegen_units = tcx.sess.opts.cg.codegen_units;

0 commit comments

Comments
 (0)