Skip to content

Commit

Permalink
Remove rustc_llvm dependency from librustc
Browse files Browse the repository at this point in the history
Consequently, session creation can no longer initialize LLVM.
The few places that use the compiler without going through
rustc_driver/CompilerCalls thus need to be careful to manually
initialize LLVM (via rustc_trans!) immediately after session
creation.

This means librustc is not rebuilt when LLVM changes.
  • Loading branch information
Robin Kruppe committed May 15, 2017
1 parent 1a24a59 commit 8e4f315
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 57 deletions.
1 change: 0 additions & 1 deletion src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/librustc/Cargo.toml
Expand Up @@ -19,7 +19,6 @@ rustc_bitflags = { path = "../librustc_bitflags" }
rustc_const_math = { path = "../librustc_const_math" }
rustc_data_structures = { path = "../librustc_data_structures" }
rustc_errors = { path = "../librustc_errors" }
rustc_llvm = { path = "../librustc_llvm" }
serialize = { path = "../libserialize" }
syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" }
1 change: 0 additions & 1 deletion src/librustc/lib.rs
Expand Up @@ -55,7 +55,6 @@ extern crate getopts;
extern crate graphviz;
extern crate libc;
extern crate owning_ref;
extern crate rustc_llvm as llvm;
extern crate rustc_back;
extern crate rustc_data_structures;
extern crate serialize;
Expand Down
54 changes: 0 additions & 54 deletions src/librustc/session/mod.rs
Expand Up @@ -37,19 +37,16 @@ use syntax_pos::{Span, MultiSpan, FileMap};
use rustc_back::{LinkerFlavor, PanicStrategy};
use rustc_back::target::Target;
use rustc_data_structures::flock;
use llvm;

use std::path::{Path, PathBuf};
use std::cell::{self, Cell, RefCell};
use std::collections::HashMap;
use std::env;
use std::ffi::CString;
use std::io::Write;
use std::rc::Rc;
use std::fmt;
use std::time::Duration;
use std::sync::Arc;
use libc::c_int;

mod code_stats;
pub mod config;
Expand Down Expand Up @@ -713,8 +710,6 @@ pub fn build_session_(sopts: config::Options,
out_of_fuel: Cell::new(false),
};

init_llvm(&sess);

sess
}

Expand Down Expand Up @@ -743,55 +738,6 @@ pub enum IncrCompSession {
}
}

fn init_llvm(sess: &Session) {
unsafe {
// Before we touch LLVM, make sure that multithreading is enabled.
use std::sync::Once;
static INIT: Once = Once::new();
static mut POISONED: bool = false;
INIT.call_once(|| {
if llvm::LLVMStartMultithreaded() != 1 {
// use an extra bool to make sure that all future usage of LLVM
// cannot proceed despite the Once not running more than once.
POISONED = true;
}

configure_llvm(sess);
});

if POISONED {
bug!("couldn't enable multi-threaded LLVM");
}
}
}

unsafe fn configure_llvm(sess: &Session) {
let mut llvm_c_strs = Vec::new();
let mut llvm_args = Vec::new();

{
let mut add = |arg: &str| {
let s = CString::new(arg).unwrap();
llvm_args.push(s.as_ptr());
llvm_c_strs.push(s);
};
add("rustc"); // fake program name
if sess.time_llvm_passes() { add("-time-passes"); }
if sess.print_llvm_passes() { add("-debug-pass=Structure"); }

for arg in &sess.opts.cg.llvm_args {
add(&(*arg));
}
}

llvm::LLVMInitializePasses();

llvm::initialize_available_targets();

llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int,
llvm_args.as_ptr());
}

pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
let emitter: Box<Emitter> = match output {
config::ErrorOutputType::HumanReadable(color_config) => {
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_driver/lib.rs
Expand Up @@ -211,6 +211,7 @@ pub fn run_compiler<'a>(args: &[String],
let mut sess = session::build_session_with_codemap(
sopts, &dep_graph, input_file_path, descriptions, cstore.clone(), codemap, emitter_dest,
);
rustc_trans::init(&sess);
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));

let mut cfg = config::build_configuration(&sess, cfg);
Expand Down Expand Up @@ -415,6 +416,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
None,
descriptions.clone(),
cstore.clone());
rustc_trans::init(&sess);
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
let mut cfg = config::build_configuration(&sess, cfg.clone());
target_features::add_configuration(&mut cfg, &sess);
Expand Down
1 change: 1 addition & 0 deletions src/librustc_driver/test.rs
Expand Up @@ -112,6 +112,7 @@ fn test_env<F>(source_string: &str,
diagnostic_handler,
Rc::new(CodeMap::new(FilePathMapping::empty())),
cstore.clone());
rustc_trans::init(&sess);
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
let input = config::Input::Str {
name: driver::anon_src(),
Expand Down
52 changes: 52 additions & 0 deletions src/librustc_trans/lib.rs
Expand Up @@ -178,3 +178,55 @@ pub struct CrateTranslation {
}

__build_diagnostic_array! { librustc_trans, DIAGNOSTICS }

use rustc::session::Session;
pub fn init(sess: &Session) {
unsafe {
// Before we touch LLVM, make sure that multithreading is enabled.
use std::sync::Once;
static INIT: Once = Once::new();
static mut POISONED: bool = false;
INIT.call_once(|| {
if llvm::LLVMStartMultithreaded() != 1 {
// use an extra bool to make sure that all future usage of LLVM
// cannot proceed despite the Once not running more than once.
POISONED = true;
}

configure_llvm(sess);
});

if POISONED {
bug!("couldn't enable multi-threaded LLVM");
}
}
}

use std::ffi::CString;
use libc::c_int;
unsafe fn configure_llvm(sess: &Session) {
let mut llvm_c_strs = Vec::new();
let mut llvm_args = Vec::new();

{
let mut add = |arg: &str| {
let s = CString::new(arg).unwrap();
llvm_args.push(s.as_ptr());
llvm_c_strs.push(s);
};
add("rustc"); // fake program name
if sess.time_llvm_passes() { add("-time-passes"); }
if sess.print_llvm_passes() { add("-debug-pass=Structure"); }

for arg in &sess.opts.cg.llvm_args {
add(&(*arg));
}
}

llvm::LLVMInitializePasses();

llvm::initialize_available_targets();

llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int,
llvm_args.as_ptr());
}
1 change: 1 addition & 0 deletions src/librustdoc/core.rs
Expand Up @@ -143,6 +143,7 @@ pub fn run_core(search_paths: SearchPaths,
let mut sess = session::build_session_(
sessopts, &dep_graph, cpath, diagnostic_handler, codemap, cstore.clone()
);
rustc_trans::init(&sess);
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));

let mut cfg = config::build_configuration(&sess, config::parse_cfgspecs(cfgs));
Expand Down
2 changes: 2 additions & 0 deletions src/librustdoc/test.rs
Expand Up @@ -86,6 +86,7 @@ pub fn run(input: &str,
let mut sess = session::build_session_(
sessopts, &dep_graph, Some(input_path.clone()), handler, codemap.clone(), cstore.clone(),
);
rustc_trans::init(&sess);
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
sess.parse_sess.config =
config::build_configuration(&sess, config::parse_cfgspecs(cfgs.clone()));
Expand Down Expand Up @@ -234,6 +235,7 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
let mut sess = session::build_session_(
sessopts, &dep_graph, None, diagnostic_handler, codemap, cstore.clone(),
);
rustc_trans::init(&sess);
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));

let outdir = Mutex::new(TempDir::new("rustdoctest").ok().expect("rustdoc needs a tempdir"));
Expand Down
1 change: 1 addition & 0 deletions src/test/run-make/issue-19371/foo.rs
Expand Up @@ -61,6 +61,7 @@ fn basic_sess(sysroot: PathBuf) -> (Session, Rc<CStore>) {
let dep_graph = DepGraph::new(opts.build_dep_graph());
let cstore = Rc::new(CStore::new(&dep_graph, Box::new(rustc_trans::LlvmMetadataLoader)));
let sess = build_session(opts, &dep_graph, None, descriptions, cstore.clone());
rustc_trans::init(&sess);
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
(sess, cstore)
}
Expand Down

0 comments on commit 8e4f315

Please sign in to comment.