Skip to content

Commit e3f6e68

Browse files
author
Robin Kruppe
committed
Remove (direct) rustc_llvm dependency from rustc_driver
This does not actually improve build times, since it still depends on rustc_trans, but is better layering and fits the multi-backend future slightly better.
1 parent 8e4f315 commit e3f6e68

File tree

7 files changed

+139
-103
lines changed

7 files changed

+139
-103
lines changed

src/Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/librustc/session/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ top_level_options!(
328328
}
329329
);
330330

331-
#[derive(Clone, PartialEq, Eq)]
331+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
332332
pub enum PrintRequest {
333333
FileNames,
334334
Sysroot,

src/librustc_driver/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ rustc_data_structures = { path = "../librustc_data_structures" }
2222
rustc_errors = { path = "../librustc_errors" }
2323
rustc_incremental = { path = "../librustc_incremental" }
2424
rustc_lint = { path = "../librustc_lint" }
25-
rustc_llvm = { path = "../librustc_llvm" }
2625
rustc_metadata = { path = "../librustc_metadata" }
2726
rustc_mir = { path = "../librustc_mir" }
2827
rustc_passes = { path = "../librustc_passes" }

src/librustc_driver/lib.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ extern crate rustc_save_analysis;
5656
extern crate rustc_trans;
5757
extern crate rustc_typeck;
5858
extern crate serialize;
59-
extern crate rustc_llvm as llvm;
6059
#[macro_use]
6160
extern crate log;
6261
extern crate syntax;
@@ -70,7 +69,7 @@ use rustc_resolve as resolve;
7069
use rustc_save_analysis as save;
7170
use rustc_save_analysis::DumpHandler;
7271
use rustc_trans::back::link;
73-
use rustc_trans::back::write::{create_target_machine, RELOC_MODEL_ARGS, CODE_GEN_MODEL_ARGS};
72+
use rustc_trans::back::write::{RELOC_MODEL_ARGS, CODE_GEN_MODEL_ARGS};
7473
use rustc::dep_graph::DepGraph;
7574
use rustc::session::{self, config, Session, build_session, CompileResult};
7675
use rustc::session::config::{Input, PrintRequest, OutputType, ErrorOutputType};
@@ -182,7 +181,7 @@ pub fn run_compiler<'a>(args: &[String],
182181
let (sopts, cfg) = config::build_session_options_and_crate_config(&matches);
183182

184183
if sopts.debugging_opts.debug_llvm {
185-
unsafe { llvm::LLVMRustSetDebug(1); }
184+
rustc_trans::enable_llvm_debug();
186185
}
187186

188187
let descriptions = diagnostics_registry();
@@ -671,14 +670,6 @@ impl RustcDefaultCalls {
671670
println!("{}", cfg);
672671
}
673672
}
674-
PrintRequest::TargetCPUs => {
675-
let tm = create_target_machine(sess);
676-
unsafe { llvm::LLVMRustPrintTargetCPUs(tm); }
677-
}
678-
PrintRequest::TargetFeatures => {
679-
let tm = create_target_machine(sess);
680-
unsafe { llvm::LLVMRustPrintTargetFeatures(tm); }
681-
}
682673
PrintRequest::RelocationModels => {
683674
println!("Available relocation models:");
684675
for &(name, _) in RELOC_MODEL_ARGS.iter() {
@@ -693,6 +684,9 @@ impl RustcDefaultCalls {
693684
}
694685
println!("");
695686
}
687+
PrintRequest::TargetCPUs | PrintRequest::TargetFeatures => {
688+
rustc_trans::print(*req, sess);
689+
}
696690
}
697691
}
698692
return Compilation::Stop;
@@ -730,10 +724,7 @@ pub fn version(binary: &str, matches: &getopts::Matches) {
730724
println!("commit-date: {}", unw(commit_date_str()));
731725
println!("host: {}", config::host_triple());
732726
println!("release: {}", unw(release_str()));
733-
unsafe {
734-
println!("LLVM version: {}.{}",
735-
llvm::LLVMRustVersionMajor(), llvm::LLVMRustVersionMinor());
736-
}
727+
rustc_trans::print_version();
737728
}
738729
}
739730

@@ -1026,9 +1017,7 @@ pub fn handle_options(args: &[String]) -> Option<getopts::Matches> {
10261017
}
10271018

10281019
if cg_flags.contains(&"passes=list".to_string()) {
1029-
unsafe {
1030-
::llvm::LLVMRustPrintPasses();
1031-
}
1020+
rustc_trans::print_passes();
10321021
return None;
10331022
}
10341023

src/librustc_driver/target_features.rs

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,20 @@
99
// except according to those terms.
1010

1111
use syntax::ast;
12-
use llvm::LLVMRustHasFeature;
1312
use rustc::session::Session;
14-
use rustc_trans::back::write::create_target_machine;
1513
use syntax::symbol::Symbol;
16-
use libc::c_char;
17-
18-
// WARNING: the features must be known to LLVM or the feature
19-
// detection code will walk past the end of the feature array,
20-
// leading to crashes.
21-
22-
const ARM_WHITELIST: &'static [&'static str] = &["neon\0", "vfp2\0", "vfp3\0", "vfp4\0"];
23-
24-
const X86_WHITELIST: &'static [&'static str] = &["avx\0", "avx2\0", "bmi\0", "bmi2\0", "sse\0",
25-
"sse2\0", "sse3\0", "sse4.1\0", "sse4.2\0",
26-
"ssse3\0", "tbm\0", "lzcnt\0", "popcnt\0",
27-
"sse4a\0", "rdrnd\0", "rdseed\0", "fma\0"];
28-
29-
const HEXAGON_WHITELIST: &'static [&'static str] = &["hvx\0", "hvx-double\0"];
14+
use rustc_trans;
3015

3116
/// Add `target_feature = "..."` cfgs for a variety of platform
3217
/// specific features (SSE, NEON etc.).
3318
///
3419
/// This is performed by checking whether a whitelisted set of
3520
/// features is available on the target machine, by querying LLVM.
3621
pub fn add_configuration(cfg: &mut ast::CrateConfig, sess: &Session) {
37-
let target_machine = create_target_machine(sess);
38-
39-
let whitelist = match &*sess.target.target.arch {
40-
"arm" => ARM_WHITELIST,
41-
"x86" | "x86_64" => X86_WHITELIST,
42-
"hexagon" => HEXAGON_WHITELIST,
43-
_ => &[],
44-
};
45-
4622
let tf = Symbol::intern("target_feature");
47-
for feat in whitelist {
48-
assert_eq!(feat.chars().last(), Some('\0'));
49-
if unsafe { LLVMRustHasFeature(target_machine, feat.as_ptr() as *const c_char) } {
50-
cfg.insert((tf, Some(Symbol::intern(&feat[..feat.len() - 1]))));
51-
}
23+
24+
for feat in rustc_trans::target_features(sess) {
25+
cfg.insert((tf, Some(feat)));
5226
}
5327

5428
let requested_features = sess.opts.cg.target_feature.split(',');

src/librustc_trans/lib.rs

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub use base::trans_crate;
7272
pub use back::symbol_names::provide;
7373

7474
pub use metadata::LlvmMetadataLoader;
75+
pub use llvm_util::{init, target_features, print_version, print_passes, print, enable_llvm_debug};
7576

7677
pub mod back {
7778
pub use rustc::hir::svh;
@@ -122,6 +123,7 @@ mod debuginfo;
122123
mod declare;
123124
mod glue;
124125
mod intrinsic;
126+
mod llvm_util;
125127
mod machine;
126128
mod metadata;
127129
mod meth;
@@ -178,55 +180,3 @@ pub struct CrateTranslation {
178180
}
179181

180182
__build_diagnostic_array! { librustc_trans, DIAGNOSTICS }
181-
182-
use rustc::session::Session;
183-
pub fn init(sess: &Session) {
184-
unsafe {
185-
// Before we touch LLVM, make sure that multithreading is enabled.
186-
use std::sync::Once;
187-
static INIT: Once = Once::new();
188-
static mut POISONED: bool = false;
189-
INIT.call_once(|| {
190-
if llvm::LLVMStartMultithreaded() != 1 {
191-
// use an extra bool to make sure that all future usage of LLVM
192-
// cannot proceed despite the Once not running more than once.
193-
POISONED = true;
194-
}
195-
196-
configure_llvm(sess);
197-
});
198-
199-
if POISONED {
200-
bug!("couldn't enable multi-threaded LLVM");
201-
}
202-
}
203-
}
204-
205-
use std::ffi::CString;
206-
use libc::c_int;
207-
unsafe fn configure_llvm(sess: &Session) {
208-
let mut llvm_c_strs = Vec::new();
209-
let mut llvm_args = Vec::new();
210-
211-
{
212-
let mut add = |arg: &str| {
213-
let s = CString::new(arg).unwrap();
214-
llvm_args.push(s.as_ptr());
215-
llvm_c_strs.push(s);
216-
};
217-
add("rustc"); // fake program name
218-
if sess.time_llvm_passes() { add("-time-passes"); }
219-
if sess.print_llvm_passes() { add("-debug-pass=Structure"); }
220-
221-
for arg in &sess.opts.cg.llvm_args {
222-
add(&(*arg));
223-
}
224-
}
225-
226-
llvm::LLVMInitializePasses();
227-
228-
llvm::initialize_available_targets();
229-
230-
llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int,
231-
llvm_args.as_ptr());
232-
}

src/librustc_trans/llvm_util.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use syntax_pos::symbol::Symbol;
12+
use back::write::create_target_machine;
13+
use llvm;
14+
use rustc::session::Session;
15+
use rustc::session::config::PrintRequest;
16+
use libc::{c_int, c_char};
17+
use std::ffi::CString;
18+
19+
pub fn init(sess: &Session) {
20+
unsafe {
21+
// Before we touch LLVM, make sure that multithreading is enabled.
22+
use std::sync::Once;
23+
static INIT: Once = Once::new();
24+
static mut POISONED: bool = false;
25+
INIT.call_once(|| {
26+
if llvm::LLVMStartMultithreaded() != 1 {
27+
// use an extra bool to make sure that all future usage of LLVM
28+
// cannot proceed despite the Once not running more than once.
29+
POISONED = true;
30+
}
31+
32+
configure_llvm(sess);
33+
});
34+
35+
if POISONED {
36+
bug!("couldn't enable multi-threaded LLVM");
37+
}
38+
}
39+
}
40+
41+
unsafe fn configure_llvm(sess: &Session) {
42+
let mut llvm_c_strs = Vec::new();
43+
let mut llvm_args = Vec::new();
44+
45+
{
46+
let mut add = |arg: &str| {
47+
let s = CString::new(arg).unwrap();
48+
llvm_args.push(s.as_ptr());
49+
llvm_c_strs.push(s);
50+
};
51+
add("rustc"); // fake program name
52+
if sess.time_llvm_passes() { add("-time-passes"); }
53+
if sess.print_llvm_passes() { add("-debug-pass=Structure"); }
54+
55+
for arg in &sess.opts.cg.llvm_args {
56+
add(&(*arg));
57+
}
58+
}
59+
60+
llvm::LLVMInitializePasses();
61+
62+
llvm::initialize_available_targets();
63+
64+
llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int,
65+
llvm_args.as_ptr());
66+
}
67+
68+
// WARNING: the features must be known to LLVM or the feature
69+
// detection code will walk past the end of the feature array,
70+
// leading to crashes.
71+
72+
const ARM_WHITELIST: &'static [&'static str] = &["neon\0", "vfp2\0", "vfp3\0", "vfp4\0"];
73+
74+
const X86_WHITELIST: &'static [&'static str] = &["avx\0", "avx2\0", "bmi\0", "bmi2\0", "sse\0",
75+
"sse2\0", "sse3\0", "sse4.1\0", "sse4.2\0",
76+
"ssse3\0", "tbm\0", "lzcnt\0", "popcnt\0",
77+
"sse4a\0", "rdrnd\0", "rdseed\0", "fma\0"];
78+
79+
const HEXAGON_WHITELIST: &'static [&'static str] = &["hvx\0", "hvx-double\0"];
80+
81+
pub fn target_features(sess: &Session) -> Vec<Symbol> {
82+
let target_machine = create_target_machine(sess);
83+
84+
let whitelist = match &*sess.target.target.arch {
85+
"arm" => ARM_WHITELIST,
86+
"x86" | "x86_64" => X86_WHITELIST,
87+
"hexagon" => HEXAGON_WHITELIST,
88+
_ => &[],
89+
};
90+
91+
let mut features = Vec::new();
92+
for feat in whitelist {
93+
assert_eq!(feat.chars().last(), Some('\0'));
94+
if unsafe { llvm::LLVMRustHasFeature(target_machine, feat.as_ptr() as *const c_char) } {
95+
features.push(Symbol::intern(&feat[..feat.len() - 1]));
96+
}
97+
}
98+
features
99+
}
100+
101+
pub fn print_version() {
102+
unsafe {
103+
println!("LLVM version: {}.{}",
104+
llvm::LLVMRustVersionMajor(), llvm::LLVMRustVersionMinor());
105+
}
106+
}
107+
108+
pub fn print_passes() {
109+
unsafe { llvm::LLVMRustPrintPasses(); }
110+
}
111+
112+
pub fn print(req: PrintRequest, sess: &Session) {
113+
let tm = create_target_machine(sess);
114+
unsafe {
115+
match req {
116+
PrintRequest::TargetCPUs => llvm::LLVMRustPrintTargetCPUs(tm),
117+
PrintRequest::TargetFeatures => llvm::LLVMRustPrintTargetFeatures(tm),
118+
_ => bug!("rustc_trans can't handle print request: {:?}", req),
119+
}
120+
}
121+
}
122+
123+
pub fn enable_llvm_debug() {
124+
unsafe { llvm::LLVMRustSetDebug(1); }
125+
}

0 commit comments

Comments
 (0)