2019-02-17 18:58:58 +00:00
|
|
|
use crate::back::write::create_informational_target_machine;
|
|
|
|
use crate::llvm;
|
2019-12-22 22:42:04 +00:00
|
|
|
use libc::c_int;
|
2020-10-04 09:12:56 +00:00
|
|
|
use rustc_codegen_ssa::target_features::supported_target_features;
|
2019-12-18 13:19:03 +00:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2019-12-22 22:42:04 +00:00
|
|
|
use rustc_feature::UnstableFeatures;
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_middle::bug;
|
2020-03-11 11:49:08 +00:00
|
|
|
use rustc_session::config::PrintRequest;
|
|
|
|
use rustc_session::Session;
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::symbol::Symbol;
|
2019-10-18 21:47:54 +00:00
|
|
|
use rustc_target::spec::{MergeFunctions, PanicStrategy};
|
2018-02-11 00:27:04 +00:00
|
|
|
use std::ffi::CString;
|
2017-04-30 18:33:25 +00:00
|
|
|
|
2018-08-23 18:03:22 +00:00
|
|
|
use std::slice;
|
2019-12-22 22:42:04 +00:00
|
|
|
use std::str;
|
2017-05-14 18:33:37 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use std::sync::Once;
|
|
|
|
|
2017-10-30 17:42:21 +00:00
|
|
|
static POISONED: AtomicBool = AtomicBool::new(false);
|
|
|
|
static INIT: Once = Once::new();
|
|
|
|
|
|
|
|
pub(crate) fn init(sess: &Session) {
|
2017-04-30 18:33:25 +00:00
|
|
|
unsafe {
|
|
|
|
// Before we touch LLVM, make sure that multithreading is enabled.
|
|
|
|
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.
|
2017-05-14 18:33:37 +00:00
|
|
|
POISONED.store(true, Ordering::SeqCst);
|
2017-04-30 18:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
configure_llvm(sess);
|
|
|
|
});
|
|
|
|
|
2017-05-14 18:33:37 +00:00
|
|
|
if POISONED.load(Ordering::SeqCst) {
|
2017-04-30 18:33:25 +00:00
|
|
|
bug!("couldn't enable multi-threaded LLVM");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-30 17:42:21 +00:00
|
|
|
fn require_inited() {
|
|
|
|
INIT.call_once(|| bug!("llvm is not initialized"));
|
|
|
|
if POISONED.load(Ordering::SeqCst) {
|
|
|
|
bug!("couldn't enable multi-threaded LLVM");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 18:33:25 +00:00
|
|
|
unsafe fn configure_llvm(sess: &Session) {
|
2020-10-15 09:44:00 +00:00
|
|
|
let n_args = sess.opts.cg.llvm_args.len() + sess.target.options.llvm_args.len();
|
2018-10-08 14:55:04 +00:00
|
|
|
let mut llvm_c_strs = Vec::with_capacity(n_args + 1);
|
|
|
|
let mut llvm_args = Vec::with_capacity(n_args + 1);
|
2017-04-30 18:33:25 +00:00
|
|
|
|
2018-10-12 19:35:55 +00:00
|
|
|
llvm::LLVMRustInstallFatalErrorHandler();
|
|
|
|
|
2019-12-18 13:19:03 +00:00
|
|
|
fn llvm_arg_to_arg_name(full_arg: &str) -> &str {
|
2019-12-22 22:42:04 +00:00
|
|
|
full_arg.trim().split(|c: char| c == '=' || c.is_whitespace()).next().unwrap_or("")
|
2019-12-18 13:19:03 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 15:40:40 +00:00
|
|
|
let cg_opts = sess.opts.cg.llvm_args.iter();
|
2020-10-15 09:44:00 +00:00
|
|
|
let tg_opts = sess.target.options.llvm_args.iter();
|
2020-01-18 00:11:52 +00:00
|
|
|
let sess_args = cg_opts.chain(tg_opts);
|
2020-01-09 15:40:40 +00:00
|
|
|
|
|
|
|
let user_specified_args: FxHashSet<_> =
|
2020-02-28 13:20:33 +00:00
|
|
|
sess_args.clone().map(|s| llvm_arg_to_arg_name(s)).filter(|s| !s.is_empty()).collect();
|
2019-12-18 13:19:03 +00:00
|
|
|
|
2017-04-30 18:33:25 +00:00
|
|
|
{
|
2019-12-18 13:19:03 +00:00
|
|
|
// This adds the given argument to LLVM. Unless `force` is true
|
|
|
|
// user specified arguments are *not* overridden.
|
|
|
|
let mut add = |arg: &str, force: bool| {
|
|
|
|
if force || !user_specified_args.contains(llvm_arg_to_arg_name(arg)) {
|
|
|
|
let s = CString::new(arg).unwrap();
|
|
|
|
llvm_args.push(s.as_ptr());
|
|
|
|
llvm_c_strs.push(s);
|
|
|
|
}
|
2017-04-30 18:33:25 +00:00
|
|
|
};
|
2020-08-12 22:30:41 +00:00
|
|
|
// Set the llvm "program name" to make usage and invalid argument messages more clear.
|
|
|
|
add("rustc -Cllvm-args=\"...\" with", true);
|
2019-12-22 22:42:04 +00:00
|
|
|
if sess.time_llvm_passes() {
|
|
|
|
add("-time-passes", false);
|
|
|
|
}
|
|
|
|
if sess.print_llvm_passes() {
|
|
|
|
add("-debug-pass=Structure", false);
|
|
|
|
}
|
2020-03-24 01:01:45 +00:00
|
|
|
if !sess.opts.debugging_opts.no_generate_arange_section {
|
2019-12-18 13:19:03 +00:00
|
|
|
add("-generate-arange-section", false);
|
2019-11-18 23:05:01 +00:00
|
|
|
}
|
2020-04-14 19:10:58 +00:00
|
|
|
match sess
|
|
|
|
.opts
|
|
|
|
.debugging_opts
|
|
|
|
.merge_functions
|
2020-10-15 09:44:00 +00:00
|
|
|
.unwrap_or(sess.target.options.merge_functions)
|
2020-04-14 19:10:58 +00:00
|
|
|
{
|
|
|
|
MergeFunctions::Disabled | MergeFunctions::Trampolines => {}
|
|
|
|
MergeFunctions::Aliases => {
|
|
|
|
add("-mergefunc-use-aliases", false);
|
2018-12-31 18:58:13 +00:00
|
|
|
}
|
2018-11-29 22:05:23 +00:00
|
|
|
}
|
2017-04-30 18:33:25 +00:00
|
|
|
|
2020-10-15 09:44:00 +00:00
|
|
|
if sess.target.target_os == "emscripten" && sess.panic_strategy() == PanicStrategy::Unwind {
|
2019-12-18 13:19:03 +00:00
|
|
|
add("-enable-emscripten-cxx-exceptions", false);
|
2019-10-18 21:47:54 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 23:30:35 +00:00
|
|
|
// HACK(eddyb) LLVM inserts `llvm.assume` calls to preserve align attributes
|
|
|
|
// during inlining. Unfortunately these may block other optimizations.
|
2019-12-18 13:19:03 +00:00
|
|
|
add("-preserve-alignment-assumptions-during-inlining=false", false);
|
2018-12-20 23:30:35 +00:00
|
|
|
|
2020-01-18 00:11:52 +00:00
|
|
|
for arg in sess_args {
|
2019-12-18 13:19:03 +00:00
|
|
|
add(&(*arg), true);
|
2017-04-30 18:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 23:58:28 +00:00
|
|
|
if sess.opts.debugging_opts.llvm_time_trace && get_major_version() >= 9 {
|
|
|
|
// time-trace is not thread safe and running it in parallel will cause seg faults.
|
|
|
|
if !sess.opts.debugging_opts.no_parallel_llvm {
|
|
|
|
bug!("`-Z llvm-time-trace` requires `-Z no-parallel-llvm")
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::LLVMTimeTraceProfilerInitialize();
|
|
|
|
}
|
|
|
|
|
2017-04-30 18:33:25 +00:00
|
|
|
llvm::LLVMInitializePasses();
|
|
|
|
|
2020-10-13 08:17:05 +00:00
|
|
|
rustc_llvm::initialize_available_targets();
|
2017-04-30 18:33:25 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, llvm_args.as_ptr());
|
2017-04-30 18:33:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 23:58:28 +00:00
|
|
|
pub fn time_trace_profiler_finish(file_name: &str) {
|
|
|
|
unsafe {
|
|
|
|
if get_major_version() >= 9 {
|
|
|
|
let file_name = CString::new(file_name).unwrap();
|
|
|
|
llvm::LLVMTimeTraceProfilerFinish(file_name.as_ptr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-21 06:59:28 +00:00
|
|
|
// WARNING: the features after applying `to_llvm_feature` must be known
|
2018-02-20 13:05:25 +00:00
|
|
|
// to LLVM or the feature detection code will walk past the end of the feature
|
|
|
|
// array, leading to crashes.
|
2018-02-27 02:05:58 +00:00
|
|
|
pub fn to_llvm_feature<'a>(sess: &Session, s: &'a str) -> &'a str {
|
2020-10-15 09:44:00 +00:00
|
|
|
let arch = if sess.target.arch == "x86_64" { "x86" } else { &*sess.target.arch };
|
2018-02-27 02:05:58 +00:00
|
|
|
match (arch, s) {
|
|
|
|
("x86", "pclmulqdq") => "pclmul",
|
|
|
|
("x86", "rdrand") => "rdrnd",
|
|
|
|
("x86", "bmi1") => "bmi",
|
2018-12-14 19:40:27 +00:00
|
|
|
("x86", "cmpxchg16b") => "cx16",
|
2018-04-10 21:34:15 +00:00
|
|
|
("aarch64", "fp") => "fp-armv8",
|
2018-02-27 02:05:58 +00:00
|
|
|
("aarch64", "fp16") => "fullfp16",
|
|
|
|
(_, s) => s,
|
2018-02-10 23:27:21 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-28 09:28:15 +00:00
|
|
|
|
2017-04-30 18:33:25 +00:00
|
|
|
pub fn target_features(sess: &Session) -> Vec<Symbol> {
|
2020-04-23 18:10:01 +00:00
|
|
|
let target_machine = create_informational_target_machine(sess);
|
2020-07-07 15:12:44 +00:00
|
|
|
supported_target_features(sess)
|
2018-02-10 23:36:22 +00:00
|
|
|
.iter()
|
2018-04-05 15:02:11 +00:00
|
|
|
.filter_map(|&(feature, gate)| {
|
|
|
|
if UnstableFeatures::from_environment().is_nightly_build() || gate.is_none() {
|
|
|
|
Some(feature)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2018-02-10 23:36:22 +00:00
|
|
|
.filter(|feature| {
|
2018-02-27 02:05:58 +00:00
|
|
|
let llvm_feature = to_llvm_feature(sess, feature);
|
2018-02-11 12:57:07 +00:00
|
|
|
let cstr = CString::new(llvm_feature).unwrap();
|
|
|
|
unsafe { llvm::LLVMRustHasFeature(target_machine, cstr.as_ptr()) }
|
2018-02-10 23:36:22 +00:00
|
|
|
})
|
2019-12-22 22:42:04 +00:00
|
|
|
.map(|feature| Symbol::intern(feature))
|
|
|
|
.collect()
|
2018-01-05 21:26:26 +00:00
|
|
|
}
|
2017-04-30 18:33:25 +00:00
|
|
|
|
|
|
|
pub fn print_version() {
|
2017-10-30 17:42:21 +00:00
|
|
|
// Can be called without initializing LLVM
|
2017-04-30 18:33:25 +00:00
|
|
|
unsafe {
|
2019-12-22 22:42:04 +00:00
|
|
|
println!("LLVM version: {}.{}", llvm::LLVMRustVersionMajor(), llvm::LLVMRustVersionMinor());
|
2017-04-30 18:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-03 14:48:29 +00:00
|
|
|
pub fn get_major_version() -> u32 {
|
|
|
|
unsafe { llvm::LLVMRustVersionMajor() }
|
|
|
|
}
|
|
|
|
|
2017-04-30 18:33:25 +00:00
|
|
|
pub fn print_passes() {
|
2017-10-30 17:42:21 +00:00
|
|
|
// Can be called without initializing LLVM
|
2019-12-22 22:42:04 +00:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMRustPrintPasses();
|
|
|
|
}
|
2017-04-30 18:33:25 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 17:42:21 +00:00
|
|
|
pub(crate) fn print(req: PrintRequest, sess: &Session) {
|
|
|
|
require_inited();
|
2020-04-23 18:10:01 +00:00
|
|
|
let tm = create_informational_target_machine(sess);
|
2017-04-30 18:33:25 +00:00
|
|
|
unsafe {
|
|
|
|
match req {
|
|
|
|
PrintRequest::TargetCPUs => llvm::LLVMRustPrintTargetCPUs(tm),
|
|
|
|
PrintRequest::TargetFeatures => llvm::LLVMRustPrintTargetFeatures(tm),
|
2018-05-08 13:10:16 +00:00
|
|
|
_ => bug!("rustc_codegen_llvm can't handle print request: {:?}", req),
|
2017-04-30 18:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 18:03:22 +00:00
|
|
|
|
2020-09-17 09:39:26 +00:00
|
|
|
fn handle_native(name: &str) -> &str {
|
2018-08-23 18:03:22 +00:00
|
|
|
if name != "native" {
|
2019-12-22 22:42:04 +00:00
|
|
|
return name;
|
2018-08-23 18:03:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let mut len = 0;
|
|
|
|
let ptr = llvm::LLVMRustGetHostCPUName(&mut len);
|
|
|
|
str::from_utf8(slice::from_raw_parts(ptr as *const u8, len)).unwrap()
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 09:39:26 +00:00
|
|
|
|
|
|
|
pub fn target_cpu(sess: &Session) -> &str {
|
|
|
|
let name = match sess.opts.cg.target_cpu {
|
|
|
|
Some(ref s) => &**s,
|
2020-10-15 09:44:00 +00:00
|
|
|
None => &*sess.target.options.cpu,
|
2020-09-17 09:39:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
handle_native(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tune_cpu(sess: &Session) -> Option<&str> {
|
|
|
|
match sess.opts.debugging_opts.tune_cpu {
|
|
|
|
Some(ref s) => Some(handle_native(&**s)),
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|