2019-02-17 12:58:58 -06:00
|
|
|
use crate::back::write::create_informational_target_machine;
|
2021-06-05 06:35:19 -05:00
|
|
|
use crate::{llvm, llvm_util};
|
2019-12-22 16:42:04 -06:00
|
|
|
use libc::c_int;
|
2020-10-04 04:12:56 -05:00
|
|
|
use rustc_codegen_ssa::target_features::supported_target_features;
|
2019-12-18 07:19:03 -06:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2021-06-22 21:26:14 -05:00
|
|
|
use rustc_metadata::dynamic_lib::DynamicLibrary;
|
2020-03-29 10:19:48 -05:00
|
|
|
use rustc_middle::bug;
|
2020-03-11 06:49:08 -05:00
|
|
|
use rustc_session::config::PrintRequest;
|
|
|
|
use rustc_session::Session;
|
2019-12-31 11:15:40 -06:00
|
|
|
use rustc_span::symbol::Symbol;
|
2019-10-18 16:47:54 -05:00
|
|
|
use rustc_target::spec::{MergeFunctions, PanicStrategy};
|
2021-01-07 22:25:19 -06:00
|
|
|
use std::ffi::{CStr, CString};
|
2021-06-22 21:26:14 -05:00
|
|
|
use tracing::debug;
|
2017-04-30 13:33:25 -05:00
|
|
|
|
2021-06-26 12:30:09 -05:00
|
|
|
use std::mem;
|
2021-06-22 21:26:14 -05:00
|
|
|
use std::path::Path;
|
2021-04-08 00:00:47 -05:00
|
|
|
use std::ptr;
|
2018-08-23 13:03:22 -05:00
|
|
|
use std::slice;
|
2019-12-22 16:42:04 -06:00
|
|
|
use std::str;
|
2017-05-14 13:33:37 -05:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use std::sync::Once;
|
|
|
|
|
2017-10-30 12:42:21 -05:00
|
|
|
static POISONED: AtomicBool = AtomicBool::new(false);
|
|
|
|
static INIT: Once = Once::new();
|
|
|
|
|
|
|
|
pub(crate) fn init(sess: &Session) {
|
2017-04-30 13:33:25 -05: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 13:33:37 -05:00
|
|
|
POISONED.store(true, Ordering::SeqCst);
|
2017-04-30 13:33:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
configure_llvm(sess);
|
|
|
|
});
|
|
|
|
|
2017-05-14 13:33:37 -05:00
|
|
|
if POISONED.load(Ordering::SeqCst) {
|
2017-04-30 13:33:25 -05:00
|
|
|
bug!("couldn't enable multi-threaded LLVM");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-30 12:42:21 -05: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 13:33:25 -05:00
|
|
|
unsafe fn configure_llvm(sess: &Session) {
|
2020-11-08 05:27:51 -06:00
|
|
|
let n_args = sess.opts.cg.llvm_args.len() + sess.target.llvm_args.len();
|
2018-10-08 09:55:04 -05: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 13:33:25 -05:00
|
|
|
|
2018-10-12 14:35:55 -05:00
|
|
|
llvm::LLVMRustInstallFatalErrorHandler();
|
|
|
|
|
2019-12-18 07:19:03 -06:00
|
|
|
fn llvm_arg_to_arg_name(full_arg: &str) -> &str {
|
2019-12-22 16:42:04 -06:00
|
|
|
full_arg.trim().split(|c: char| c == '=' || c.is_whitespace()).next().unwrap_or("")
|
2019-12-18 07:19:03 -06:00
|
|
|
}
|
|
|
|
|
2020-01-09 09:40:40 -06:00
|
|
|
let cg_opts = sess.opts.cg.llvm_args.iter();
|
2020-11-08 05:27:51 -06:00
|
|
|
let tg_opts = sess.target.llvm_args.iter();
|
2020-01-17 18:11:52 -06:00
|
|
|
let sess_args = cg_opts.chain(tg_opts);
|
2020-01-09 09:40:40 -06:00
|
|
|
|
|
|
|
let user_specified_args: FxHashSet<_> =
|
2020-02-28 07:20:33 -06:00
|
|
|
sess_args.clone().map(|s| llvm_arg_to_arg_name(s)).filter(|s| !s.is_empty()).collect();
|
2019-12-18 07:19:03 -06:00
|
|
|
|
2017-04-30 13:33:25 -05:00
|
|
|
{
|
2019-12-18 07:19:03 -06: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 13:33:25 -05:00
|
|
|
};
|
2020-08-12 17:30:41 -05:00
|
|
|
// Set the llvm "program name" to make usage and invalid argument messages more clear.
|
|
|
|
add("rustc -Cllvm-args=\"...\" with", true);
|
2019-12-22 16:42:04 -06:00
|
|
|
if sess.time_llvm_passes() {
|
|
|
|
add("-time-passes", false);
|
|
|
|
}
|
|
|
|
if sess.print_llvm_passes() {
|
|
|
|
add("-debug-pass=Structure", false);
|
|
|
|
}
|
2020-03-23 20:01:45 -05:00
|
|
|
if !sess.opts.debugging_opts.no_generate_arange_section {
|
2019-12-18 07:19:03 -06:00
|
|
|
add("-generate-arange-section", false);
|
2019-11-18 17:05:01 -06:00
|
|
|
}
|
2021-06-05 06:35:19 -05:00
|
|
|
|
2021-08-28 01:45:24 -05:00
|
|
|
// Disable the machine outliner by default in LLVM versions 11 and LLVM
|
|
|
|
// version 12, where it leads to miscompilation.
|
2021-06-05 06:35:19 -05:00
|
|
|
//
|
2021-08-28 01:45:24 -05:00
|
|
|
// Ref:
|
|
|
|
// - https://github.com/rust-lang/rust/issues/85351
|
|
|
|
// - https://reviews.llvm.org/D103167
|
|
|
|
let llvm_version = llvm_util::get_version();
|
|
|
|
if llvm_version >= (11, 0, 0) && llvm_version < (13, 0, 0) {
|
2021-06-05 06:35:19 -05:00
|
|
|
add("-enable-machine-outliner=never", false);
|
|
|
|
}
|
|
|
|
|
2020-11-08 05:27:51 -06:00
|
|
|
match sess.opts.debugging_opts.merge_functions.unwrap_or(sess.target.merge_functions) {
|
2020-04-14 14:10:58 -05:00
|
|
|
MergeFunctions::Disabled | MergeFunctions::Trampolines => {}
|
|
|
|
MergeFunctions::Aliases => {
|
|
|
|
add("-mergefunc-use-aliases", false);
|
2018-12-31 12:58:13 -06:00
|
|
|
}
|
2018-11-29 16:05:23 -06:00
|
|
|
}
|
2017-04-30 13:33:25 -05:00
|
|
|
|
2020-11-08 05:57:55 -06:00
|
|
|
if sess.target.os == "emscripten" && sess.panic_strategy() == PanicStrategy::Unwind {
|
2019-12-18 07:19:03 -06:00
|
|
|
add("-enable-emscripten-cxx-exceptions", false);
|
2019-10-18 16:47:54 -05:00
|
|
|
}
|
|
|
|
|
2018-12-20 17:30:35 -06:00
|
|
|
// HACK(eddyb) LLVM inserts `llvm.assume` calls to preserve align attributes
|
|
|
|
// during inlining. Unfortunately these may block other optimizations.
|
2019-12-18 07:19:03 -06:00
|
|
|
add("-preserve-alignment-assumptions-during-inlining=false", false);
|
2018-12-20 17:30:35 -06:00
|
|
|
|
Import small cold functions
The Rust code is often written under an assumption that for generic
methods inline attribute is mostly unnecessary, since for optimized
builds using ThinLTO, a method will be generated in at least one CGU and
available for import.
For example, deref implementations for Box, Vec, MutexGuard, and
MutexGuard are not currently marked as inline, neither is identity
implementation of From trait.
In PGO builds, when functions are determined to be cold, the default
multiplier of zero will stop the import, even for completely trivial
functions.
Increase slightly the default multiplier from 0 to 0.1 to import them
regardless.
2021-03-10 18:00:00 -06:00
|
|
|
// Use non-zero `import-instr-limit` multiplier for cold callsites.
|
|
|
|
add("-import-cold-multiplier=0.1", false);
|
2018-12-20 17:30:35 -06:00
|
|
|
|
2020-01-17 18:11:52 -06:00
|
|
|
for arg in sess_args {
|
2019-12-18 07:19:03 -06:00
|
|
|
add(&(*arg), true);
|
2017-04-30 13:33:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-07 16:25:45 -06:00
|
|
|
if sess.opts.debugging_opts.llvm_time_trace {
|
2020-01-31 17:58:28 -06:00
|
|
|
// 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 13:33:25 -05:00
|
|
|
llvm::LLVMInitializePasses();
|
|
|
|
|
2021-06-13 11:23:01 -05:00
|
|
|
for plugin in &sess.opts.debugging_opts.llvm_plugins {
|
2021-06-22 21:26:14 -05:00
|
|
|
let path = Path::new(plugin);
|
2021-06-20 18:38:25 -05:00
|
|
|
let res = DynamicLibrary::open(path);
|
|
|
|
match res {
|
2021-06-26 12:30:09 -05:00
|
|
|
Ok(_) => debug!("LLVM plugin loaded succesfully {} ({})", path.display(), plugin),
|
2021-06-20 18:38:25 -05:00
|
|
|
Err(e) => bug!("couldn't load plugin: {}", e),
|
2021-06-13 11:23:01 -05:00
|
|
|
}
|
2021-06-26 12:30:09 -05:00
|
|
|
mem::forget(res);
|
2021-06-13 11:23:01 -05:00
|
|
|
}
|
|
|
|
|
2020-10-13 03:17:05 -05:00
|
|
|
rustc_llvm::initialize_available_targets();
|
2017-04-30 13:33:25 -05:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, llvm_args.as_ptr());
|
2017-04-30 13:33:25 -05:00
|
|
|
}
|
|
|
|
|
2020-01-31 17:58:28 -06:00
|
|
|
pub fn time_trace_profiler_finish(file_name: &str) {
|
|
|
|
unsafe {
|
2020-11-07 16:25:45 -06:00
|
|
|
let file_name = CString::new(file_name).unwrap();
|
|
|
|
llvm::LLVMTimeTraceProfilerFinish(file_name.as_ptr());
|
2020-01-31 17:58:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-21 00:59:28 -06:00
|
|
|
// WARNING: the features after applying `to_llvm_feature` must be known
|
2018-02-20 07:05:25 -06:00
|
|
|
// to LLVM or the feature detection code will walk past the end of the feature
|
|
|
|
// array, leading to crashes.
|
2020-10-25 10:53:25 -05:00
|
|
|
// To find a list of LLVM's names, check llvm-project/llvm/include/llvm/Support/*TargetParser.def
|
|
|
|
// where the * matches the architecture's name
|
2020-10-26 02:15:23 -05:00
|
|
|
// Beware to not use the llvm github project for this, but check the git submodule
|
|
|
|
// found in src/llvm-project
|
|
|
|
// Though note that Rust can also be build with an external precompiled version of LLVM
|
|
|
|
// which might lead to failures if the oldest tested / supported LLVM version
|
|
|
|
// doesn't yet support the relevant intrinsics
|
2021-09-15 09:18:10 -05:00
|
|
|
pub fn to_llvm_feature<'a>(sess: &Session, s: &'a str) -> Vec<&'a str> {
|
2020-10-15 04:44:00 -05:00
|
|
|
let arch = if sess.target.arch == "x86_64" { "x86" } else { &*sess.target.arch };
|
2018-02-26 20:05:58 -06:00
|
|
|
match (arch, s) {
|
2021-09-15 09:18:10 -05:00
|
|
|
("x86", "sse4.2") => {
|
|
|
|
if get_version() >= (14, 0, 0) {
|
|
|
|
vec!["sse4.2", "crc32"]
|
|
|
|
} else {
|
|
|
|
vec!["sse4.2"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
("x86", "pclmulqdq") => vec!["pclmul"],
|
|
|
|
("x86", "rdrand") => vec!["rdrnd"],
|
|
|
|
("x86", "bmi1") => vec!["bmi"],
|
|
|
|
("x86", "cmpxchg16b") => vec!["cx16"],
|
|
|
|
("x86", "avx512vaes") => vec!["vaes"],
|
|
|
|
("x86", "avx512gfni") => vec!["gfni"],
|
|
|
|
("x86", "avx512vpclmulqdq") => vec!["vpclmulqdq"],
|
|
|
|
("aarch64", "fp") => vec!["fp-armv8"],
|
|
|
|
("aarch64", "fp16") => vec!["fullfp16"],
|
|
|
|
("aarch64", "fhm") => vec!["fp16fml"],
|
|
|
|
("aarch64", "rcpc2") => vec!["rcpc-immo"],
|
|
|
|
("aarch64", "dpb") => vec!["ccpp"],
|
|
|
|
("aarch64", "dpb2") => vec!["ccdp"],
|
|
|
|
("aarch64", "frintts") => vec!["fptoint"],
|
|
|
|
("aarch64", "fcma") => vec!["complxnum"],
|
|
|
|
(_, s) => vec![s],
|
2018-02-10 17:27:21 -06:00
|
|
|
}
|
|
|
|
}
|
2017-11-28 03:28:15 -06:00
|
|
|
|
2017-04-30 13:33:25 -05:00
|
|
|
pub fn target_features(sess: &Session) -> Vec<Symbol> {
|
2020-04-23 13:10:01 -05:00
|
|
|
let target_machine = create_informational_target_machine(sess);
|
2020-07-07 10:12:44 -05:00
|
|
|
supported_target_features(sess)
|
2018-02-10 17:36:22 -06:00
|
|
|
.iter()
|
2020-10-10 13:27:52 -05:00
|
|
|
.filter_map(
|
|
|
|
|&(feature, gate)| {
|
|
|
|
if sess.is_nightly_build() || gate.is_none() { Some(feature) } else { None }
|
|
|
|
},
|
|
|
|
)
|
2018-02-10 17:36:22 -06:00
|
|
|
.filter(|feature| {
|
2021-09-15 09:18:10 -05:00
|
|
|
for llvm_feature in to_llvm_feature(sess, feature) {
|
|
|
|
let cstr = CString::new(llvm_feature).unwrap();
|
|
|
|
if unsafe { llvm::LLVMRustHasFeature(target_machine, cstr.as_ptr()) } {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
2018-02-10 17:36:22 -06:00
|
|
|
})
|
2019-12-22 16:42:04 -06:00
|
|
|
.map(|feature| Symbol::intern(feature))
|
|
|
|
.collect()
|
2018-01-05 15:26:26 -06:00
|
|
|
}
|
2017-04-30 13:33:25 -05:00
|
|
|
|
|
|
|
pub fn print_version() {
|
2020-10-12 21:33:27 -05:00
|
|
|
let (major, minor, patch) = get_version();
|
|
|
|
println!("LLVM version: {}.{}.{}", major, minor, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_version() -> (u32, u32, u32) {
|
2017-10-30 12:42:21 -05:00
|
|
|
// Can be called without initializing LLVM
|
2017-04-30 13:33:25 -05:00
|
|
|
unsafe {
|
2020-10-12 21:33:27 -05:00
|
|
|
(llvm::LLVMRustVersionMajor(), llvm::LLVMRustVersionMinor(), llvm::LLVMRustVersionPatch())
|
2017-04-30 13:33:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_passes() {
|
2017-10-30 12:42:21 -05:00
|
|
|
// Can be called without initializing LLVM
|
2019-12-22 16:42:04 -06:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMRustPrintPasses();
|
|
|
|
}
|
2017-04-30 13:33:25 -05:00
|
|
|
}
|
|
|
|
|
2021-04-08 00:00:47 -05:00
|
|
|
fn llvm_target_features(tm: &llvm::TargetMachine) -> Vec<(&str, &str)> {
|
|
|
|
let len = unsafe { llvm::LLVMRustGetTargetFeaturesCount(tm) };
|
|
|
|
let mut ret = Vec::with_capacity(len);
|
|
|
|
for i in 0..len {
|
|
|
|
unsafe {
|
|
|
|
let mut feature = ptr::null();
|
|
|
|
let mut desc = ptr::null();
|
|
|
|
llvm::LLVMRustGetTargetFeature(tm, i, &mut feature, &mut desc);
|
|
|
|
if feature.is_null() || desc.is_null() {
|
|
|
|
bug!("LLVM returned a `null` target feature string");
|
|
|
|
}
|
|
|
|
let feature = CStr::from_ptr(feature).to_str().unwrap_or_else(|e| {
|
|
|
|
bug!("LLVM returned a non-utf8 feature string: {}", e);
|
|
|
|
});
|
|
|
|
let desc = CStr::from_ptr(desc).to_str().unwrap_or_else(|e| {
|
|
|
|
bug!("LLVM returned a non-utf8 feature string: {}", e);
|
|
|
|
});
|
|
|
|
ret.push((feature, desc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_target_features(sess: &Session, tm: &llvm::TargetMachine) {
|
|
|
|
let mut target_features = llvm_target_features(tm);
|
|
|
|
let mut rustc_target_features = supported_target_features(sess)
|
|
|
|
.iter()
|
|
|
|
.filter_map(|(feature, _gate)| {
|
2021-09-15 09:18:10 -05:00
|
|
|
for llvm_feature in to_llvm_feature(sess, *feature) {
|
|
|
|
// LLVM asserts that these are sorted. LLVM and Rust both use byte comparison for these strings.
|
|
|
|
match target_features.binary_search_by_key(&llvm_feature, |(f, _d)| (*f)).ok().map(
|
|
|
|
|index| {
|
|
|
|
let (_f, desc) = target_features.remove(index);
|
|
|
|
(*feature, desc)
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
Some(v) => return Some(v),
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
2021-04-08 00:00:47 -05:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
rustc_target_features.extend_from_slice(&[(
|
|
|
|
"crt-static",
|
|
|
|
"Enables C Run-time Libraries to be statically linked",
|
|
|
|
)]);
|
|
|
|
let max_feature_len = target_features
|
|
|
|
.iter()
|
|
|
|
.chain(rustc_target_features.iter())
|
|
|
|
.map(|(feature, _desc)| feature.len())
|
|
|
|
.max()
|
|
|
|
.unwrap_or(0);
|
|
|
|
|
|
|
|
println!("Features supported by rustc for this target:");
|
|
|
|
for (feature, desc) in &rustc_target_features {
|
|
|
|
println!(" {1:0$} - {2}.", max_feature_len, feature, desc);
|
|
|
|
}
|
|
|
|
println!("\nCode-generation features supported by LLVM for this target:");
|
|
|
|
for (feature, desc) in &target_features {
|
|
|
|
println!(" {1:0$} - {2}.", max_feature_len, feature, desc);
|
|
|
|
}
|
2021-09-30 12:38:50 -05:00
|
|
|
if target_features.is_empty() {
|
2021-04-08 00:00:47 -05:00
|
|
|
println!(" Target features listing is not supported by this LLVM version.");
|
|
|
|
}
|
|
|
|
println!("\nUse +feature to enable a feature, or -feature to disable it.");
|
|
|
|
println!("For example, rustc -C target-cpu=mycpu -C target-feature=+feature1,-feature2\n");
|
|
|
|
println!("Code-generation features cannot be used in cfg or #[target_feature],");
|
|
|
|
println!("and may be renamed or removed in a future version of LLVM or rustc.\n");
|
|
|
|
}
|
|
|
|
|
2017-10-30 12:42:21 -05:00
|
|
|
pub(crate) fn print(req: PrintRequest, sess: &Session) {
|
|
|
|
require_inited();
|
2020-04-23 13:10:01 -05:00
|
|
|
let tm = create_informational_target_machine(sess);
|
2021-04-08 00:00:47 -05:00
|
|
|
match req {
|
|
|
|
PrintRequest::TargetCPUs => unsafe { llvm::LLVMRustPrintTargetCPUs(tm) },
|
|
|
|
PrintRequest::TargetFeatures => print_target_features(sess, tm),
|
|
|
|
_ => bug!("rustc_codegen_llvm can't handle print request: {:?}", req),
|
2017-04-30 13:33:25 -05:00
|
|
|
}
|
|
|
|
}
|
2018-08-23 13:03:22 -05:00
|
|
|
|
2020-09-17 04:39:26 -05:00
|
|
|
fn handle_native(name: &str) -> &str {
|
2018-08-23 13:03:22 -05:00
|
|
|
if name != "native" {
|
2019-12-22 16:42:04 -06:00
|
|
|
return name;
|
2018-08-23 13:03:22 -05: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 04:39:26 -05:00
|
|
|
|
|
|
|
pub fn target_cpu(sess: &Session) -> &str {
|
2021-01-16 13:13:06 -06:00
|
|
|
let name = sess.opts.cg.target_cpu.as_ref().unwrap_or(&sess.target.cpu);
|
2020-09-17 04:39:26 -05:00
|
|
|
handle_native(name)
|
|
|
|
}
|
|
|
|
|
2021-03-13 07:29:39 -06:00
|
|
|
/// The list of LLVM features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,
|
|
|
|
/// `--target` and similar).
|
|
|
|
// FIXME(nagisa): Cache the output of this somehow? Maybe make this a query? We're calling this
|
|
|
|
// for every function that has `#[target_feature]` on it. The global features won't change between
|
|
|
|
// the functions; only crates, maybe…
|
|
|
|
pub fn llvm_global_features(sess: &Session) -> Vec<String> {
|
|
|
|
// FIXME(nagisa): this should definitely be available more centrally and to other codegen backends.
|
|
|
|
/// These features control behaviour of rustc rather than llvm.
|
|
|
|
const RUSTC_SPECIFIC_FEATURES: &[&str] = &["crt-static"];
|
|
|
|
|
|
|
|
// Features that come earlier are overriden by conflicting features later in the string.
|
|
|
|
// Typically we'll want more explicit settings to override the implicit ones, so:
|
|
|
|
//
|
|
|
|
// * Features from -Ctarget-cpu=*; are overriden by [^1]
|
|
|
|
// * Features implied by --target; are overriden by
|
|
|
|
// * Features from -Ctarget-feature; are overriden by
|
|
|
|
// * function specific features.
|
|
|
|
//
|
|
|
|
// [^1]: target-cpu=native is handled here, other target-cpu values are handled implicitly
|
|
|
|
// through LLVM TargetMachine implementation.
|
|
|
|
//
|
|
|
|
// FIXME(nagisa): it isn't clear what's the best interaction between features implied by
|
|
|
|
// `-Ctarget-cpu` and `--target` are. On one hand, you'd expect CLI arguments to always
|
|
|
|
// override anything that's implicit, so e.g. when there's no `--target` flag, features implied
|
|
|
|
// the host target are overriden by `-Ctarget-cpu=*`. On the other hand, what about when both
|
|
|
|
// `--target` and `-Ctarget-cpu=*` are specified? Both then imply some target features and both
|
|
|
|
// flags are specified by the user on the CLI. It isn't as clear-cut which order of precedence
|
|
|
|
// should be taken in cases like these.
|
|
|
|
let mut features = vec![];
|
|
|
|
|
|
|
|
// -Ctarget-cpu=native
|
2021-01-06 02:23:54 -06:00
|
|
|
match sess.opts.cg.target_cpu {
|
2021-03-13 07:29:39 -06:00
|
|
|
Some(ref s) if s == "native" => {
|
2021-01-08 10:50:21 -06:00
|
|
|
let features_string = unsafe {
|
|
|
|
let ptr = llvm::LLVMGetHostCPUFeatures();
|
|
|
|
let features_string = if !ptr.is_null() {
|
|
|
|
CStr::from_ptr(ptr)
|
|
|
|
.to_str()
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
bug!("LLVM returned a non-utf8 features string: {}", e);
|
|
|
|
})
|
|
|
|
.to_owned()
|
|
|
|
} else {
|
|
|
|
bug!("could not allocate host CPU features, LLVM returned a `null` string");
|
|
|
|
};
|
|
|
|
|
|
|
|
llvm::LLVMDisposeMessage(ptr);
|
|
|
|
|
|
|
|
features_string
|
|
|
|
};
|
2021-07-17 16:35:57 -05:00
|
|
|
features.extend(features_string.split(',').map(String::from));
|
2021-01-06 02:23:54 -06:00
|
|
|
}
|
2021-03-13 07:29:39 -06:00
|
|
|
Some(_) | None => {}
|
|
|
|
};
|
|
|
|
|
2021-05-06 10:47:55 -05:00
|
|
|
let filter = |s: &str| {
|
|
|
|
if s.is_empty() {
|
2021-09-15 09:18:10 -05:00
|
|
|
return vec![];
|
2021-05-06 10:47:55 -05:00
|
|
|
}
|
2021-07-17 16:35:57 -05:00
|
|
|
let feature = if s.starts_with('+') || s.starts_with('-') {
|
2021-05-06 10:47:55 -05:00
|
|
|
&s[1..]
|
|
|
|
} else {
|
2021-09-15 09:18:10 -05:00
|
|
|
return vec![s.to_string()];
|
2021-05-06 10:47:55 -05:00
|
|
|
};
|
|
|
|
// Rustc-specific feature requests like `+crt-static` or `-crt-static`
|
|
|
|
// are not passed down to LLVM.
|
|
|
|
if RUSTC_SPECIFIC_FEATURES.contains(&feature) {
|
2021-09-15 09:18:10 -05:00
|
|
|
return vec![];
|
2021-05-06 10:47:55 -05:00
|
|
|
}
|
|
|
|
// ... otherwise though we run through `to_llvm_feature` feature when
|
|
|
|
// passing requests down to LLVM. This means that all in-language
|
|
|
|
// features also work on the command line instead of having two
|
|
|
|
// different names when the LLVM name and the Rust name differ.
|
2021-09-15 09:18:10 -05:00
|
|
|
to_llvm_feature(sess, feature).iter().map(|f| format!("{}{}", &s[..1], f)).collect()
|
2021-05-06 10:47:55 -05:00
|
|
|
};
|
|
|
|
|
2021-03-13 07:29:39 -06:00
|
|
|
// Features implied by an implicit or explicit `--target`.
|
2021-09-15 09:18:10 -05:00
|
|
|
features.extend(sess.target.features.split(',').flat_map(&filter));
|
2021-03-13 07:29:39 -06:00
|
|
|
|
|
|
|
// -Ctarget-features
|
2021-09-15 09:18:10 -05:00
|
|
|
features.extend(sess.opts.cg.target_feature.split(',').flat_map(&filter));
|
2021-03-13 07:29:39 -06:00
|
|
|
|
2021-04-05 12:17:23 -05:00
|
|
|
// FIXME: Move outline-atomics to target definition when earliest supported LLVM is 12.
|
|
|
|
if get_version() >= (12, 0, 0) && sess.target.llvm_target.contains("aarch64-unknown-linux") {
|
|
|
|
features.push("+outline-atomics".to_string());
|
|
|
|
}
|
|
|
|
|
2021-03-13 07:29:39 -06:00
|
|
|
features
|
2021-01-06 02:23:54 -06:00
|
|
|
}
|
|
|
|
|
2020-09-17 04:39:26 -05:00
|
|
|
pub fn tune_cpu(sess: &Session) -> Option<&str> {
|
2020-12-30 11:22:41 -06:00
|
|
|
let name = sess.opts.debugging_opts.tune_cpu.as_ref()?;
|
|
|
|
Some(handle_native(name))
|
2020-09-17 04:39:26 -05:00
|
|
|
}
|