2019-02-18 03:58:58 +09:00
|
|
|
use crate::back::write::create_informational_target_machine;
|
2021-06-05 14:35:19 +03:00
|
|
|
use crate::{llvm, llvm_util};
|
2017-04-30 20:33:25 +02:00
|
|
|
use libc::c_int;
|
2021-11-08 18:03:55 -05:00
|
|
|
use libloading::Library;
|
2021-07-23 16:19:22 +03:00
|
|
|
use rustc_codegen_ssa::target_features::{
|
|
|
|
supported_target_features, tied_target_features, RUSTC_SPECIFIC_FEATURES,
|
|
|
|
};
|
2022-01-31 13:04:27 +00:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
2021-07-23 16:19:22 +03:00
|
|
|
use rustc_data_structures::small_c_str::SmallCStr;
|
2021-12-13 00:00:00 +00:00
|
|
|
use rustc_fs_util::path_to_c_string;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::bug;
|
2020-03-11 12:49:08 +01:00
|
|
|
use rustc_session::config::PrintRequest;
|
|
|
|
use rustc_session::Session;
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::symbol::Symbol;
|
2019-10-18 14:47:54 -07:00
|
|
|
use rustc_target::spec::{MergeFunctions, PanicStrategy};
|
2021-07-23 16:19:22 +03:00
|
|
|
use smallvec::{smallvec, SmallVec};
|
2021-01-07 23:25:19 -05:00
|
|
|
use std::ffi::{CStr, CString};
|
2021-06-23 04:26:14 +02:00
|
|
|
use tracing::debug;
|
2017-04-30 20:33:25 +02:00
|
|
|
|
2021-06-26 19:30:09 +02:00
|
|
|
use std::mem;
|
2021-12-13 00:00:00 +00:00
|
|
|
use std::path::Path;
|
2021-04-08 00:00:47 -05:00
|
|
|
use std::ptr;
|
2018-08-23 11:03:22 -07:00
|
|
|
use std::slice;
|
|
|
|
use std::str;
|
2017-05-14 20:33:37 +02:00
|
|
|
use std::sync::Once;
|
|
|
|
|
2017-10-30 18:42:21 +01:00
|
|
|
static INIT: Once = Once::new();
|
|
|
|
|
|
|
|
pub(crate) fn init(sess: &Session) {
|
2017-04-30 20:33:25 +02:00
|
|
|
unsafe {
|
|
|
|
// Before we touch LLVM, make sure that multithreading is enabled.
|
2021-10-12 00:00:00 +00:00
|
|
|
if llvm::LLVMIsMultithreaded() != 1 {
|
|
|
|
bug!("LLVM compiled without support for threads");
|
|
|
|
}
|
2017-04-30 20:33:25 +02:00
|
|
|
INIT.call_once(|| {
|
|
|
|
configure_llvm(sess);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-30 18:42:21 +01:00
|
|
|
fn require_inited() {
|
2021-10-12 00:00:00 +00:00
|
|
|
if !INIT.is_completed() {
|
|
|
|
bug!("LLVM is not initialized");
|
2017-10-30 18:42:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 20:33:25 +02:00
|
|
|
unsafe fn configure_llvm(sess: &Session) {
|
2020-11-08 14:27:51 +03:00
|
|
|
let n_args = sess.opts.cg.llvm_args.len() + sess.target.llvm_args.len();
|
2018-10-08 16:55:04 +02: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 20:33:25 +02:00
|
|
|
|
2018-10-12 15:35:55 -04:00
|
|
|
llvm::LLVMRustInstallFatalErrorHandler();
|
2022-02-03 07:03:44 -08:00
|
|
|
// On Windows, an LLVM assertion will open an Abort/Retry/Ignore dialog
|
|
|
|
// box for the purpose of launching a debugger. However, on CI this will
|
|
|
|
// cause it to hang until it times out, which can take several hours.
|
|
|
|
if std::env::var_os("CI").is_some() {
|
|
|
|
llvm::LLVMRustDisableSystemDialogsOnCrash();
|
|
|
|
}
|
2018-10-12 15:35:55 -04:00
|
|
|
|
2019-12-18 14:19:03 +01:00
|
|
|
fn llvm_arg_to_arg_name(full_arg: &str) -> &str {
|
|
|
|
full_arg.trim().split(|c: char| c == '=' || c.is_whitespace()).next().unwrap_or("")
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:43:05 +01:00
|
|
|
let cg_opts = sess.opts.cg.llvm_args.iter().map(AsRef::as_ref);
|
|
|
|
let tg_opts = sess.target.llvm_args.iter().map(AsRef::as_ref);
|
2020-01-17 16:11:52 -08:00
|
|
|
let sess_args = cg_opts.chain(tg_opts);
|
2020-01-09 16:40:40 +01:00
|
|
|
|
|
|
|
let user_specified_args: FxHashSet<_> =
|
2020-02-28 14:20:33 +01:00
|
|
|
sess_args.clone().map(|s| llvm_arg_to_arg_name(s)).filter(|s| !s.is_empty()).collect();
|
2019-12-18 14:19:03 +01:00
|
|
|
|
2017-04-30 20:33:25 +02:00
|
|
|
{
|
2019-12-18 14:19:03 +01: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 20:33:25 +02:00
|
|
|
};
|
2020-08-12 15:30:41 -07:00
|
|
|
// Set the llvm "program name" to make usage and invalid argument messages more clear.
|
|
|
|
add("rustc -Cllvm-args=\"...\" with", true);
|
2019-12-18 14:19:03 +01:00
|
|
|
if sess.time_llvm_passes() {
|
|
|
|
add("-time-passes", false);
|
|
|
|
}
|
|
|
|
if sess.print_llvm_passes() {
|
|
|
|
add("-debug-pass=Structure", false);
|
|
|
|
}
|
2021-11-10 10:47:00 -08:00
|
|
|
if sess.target.generate_arange_section
|
2022-07-06 07:44:47 -05:00
|
|
|
&& !sess.opts.unstable_opts.no_generate_arange_section
|
2021-11-01 14:16:25 -07:00
|
|
|
{
|
2019-12-18 14:19:03 +01:00
|
|
|
add("-generate-arange-section", false);
|
2019-11-18 15:05:01 -08:00
|
|
|
}
|
2021-06-05 14:35:19 +03:00
|
|
|
|
2022-07-06 07:44:47 -05:00
|
|
|
match sess.opts.unstable_opts.merge_functions.unwrap_or(sess.target.merge_functions) {
|
2020-04-14 12:10:58 -07:00
|
|
|
MergeFunctions::Disabled | MergeFunctions::Trampolines => {}
|
|
|
|
MergeFunctions::Aliases => {
|
|
|
|
add("-mergefunc-use-aliases", false);
|
2018-12-31 10:58:13 -08:00
|
|
|
}
|
2018-11-29 23:05:23 +01:00
|
|
|
}
|
2017-04-30 20:33:25 +02:00
|
|
|
|
2020-11-08 14:57:55 +03:00
|
|
|
if sess.target.os == "emscripten" && sess.panic_strategy() == PanicStrategy::Unwind {
|
2019-12-18 14:19:03 +01:00
|
|
|
add("-enable-emscripten-cxx-exceptions", false);
|
2019-10-18 14:47:54 -07:00
|
|
|
}
|
|
|
|
|
2018-12-21 00:30:35 +01:00
|
|
|
// HACK(eddyb) LLVM inserts `llvm.assume` calls to preserve align attributes
|
|
|
|
// during inlining. Unfortunately these may block other optimizations.
|
2019-12-18 14:19:03 +01:00
|
|
|
add("-preserve-alignment-assumptions-during-inlining=false", false);
|
2018-12-21 00:30:35 +01: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-11 00:00:00 +00:00
|
|
|
// Use non-zero `import-instr-limit` multiplier for cold callsites.
|
|
|
|
add("-import-cold-multiplier=0.1", false);
|
2018-12-21 00:30:35 +01:00
|
|
|
|
2020-01-17 16:11:52 -08:00
|
|
|
for arg in sess_args {
|
2019-12-18 14:19:03 +01:00
|
|
|
add(&(*arg), true);
|
2017-04-30 20:33:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 07:44:47 -05:00
|
|
|
if sess.opts.unstable_opts.llvm_time_trace {
|
2020-01-31 18:58:28 -05:00
|
|
|
llvm::LLVMTimeTraceProfilerInitialize();
|
|
|
|
}
|
|
|
|
|
2017-04-30 20:33:25 +02:00
|
|
|
llvm::LLVMInitializePasses();
|
|
|
|
|
2021-12-20 14:50:03 +01:00
|
|
|
// Use the legacy plugin registration if we don't use the new pass manager
|
|
|
|
if !should_use_new_llvm_pass_manager(
|
2022-07-06 07:44:47 -05:00
|
|
|
&sess.opts.unstable_opts.new_llvm_pass_manager,
|
2021-12-20 14:50:03 +01:00
|
|
|
&sess.target.arch,
|
|
|
|
) {
|
2021-11-24 11:43:40 +01:00
|
|
|
// Register LLVM plugins by loading them into the compiler process.
|
2022-07-06 07:44:47 -05:00
|
|
|
for plugin in &sess.opts.unstable_opts.llvm_plugins {
|
2021-11-24 11:43:40 +01:00
|
|
|
let lib = Library::new(plugin).unwrap_or_else(|e| bug!("couldn't load plugin: {}", e));
|
|
|
|
debug!("LLVM plugin loaded successfully {:?} ({})", lib, plugin);
|
|
|
|
|
|
|
|
// Intentionally leak the dynamic library. We can't ever unload it
|
|
|
|
// since the library can make things that will live arbitrarily long.
|
|
|
|
mem::forget(lib);
|
|
|
|
}
|
2021-06-13 18:23:01 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 10:17:05 +02:00
|
|
|
rustc_llvm::initialize_available_targets();
|
2017-04-30 20:33:25 +02:00
|
|
|
|
|
|
|
llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, llvm_args.as_ptr());
|
|
|
|
}
|
|
|
|
|
2021-12-13 00:00:00 +00:00
|
|
|
pub fn time_trace_profiler_finish(file_name: &Path) {
|
2020-01-31 18:58:28 -05:00
|
|
|
unsafe {
|
2021-12-13 00:00:00 +00:00
|
|
|
let file_name = path_to_c_string(file_name);
|
2020-11-07 23:25:45 +01:00
|
|
|
llvm::LLVMTimeTraceProfilerFinish(file_name.as_ptr());
|
2020-01-31 18:58:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 16:19:22 +03:00
|
|
|
// WARNING: the features after applying `to_llvm_features` must be known
|
2018-02-20 16:05:25 +03:00
|
|
|
// to LLVM or the feature detection code will walk past the end of the feature
|
|
|
|
// array, leading to crashes.
|
2021-07-23 16:19:22 +03:00
|
|
|
//
|
2020-10-25 16:53:25 +01: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 08:15:23 +01: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-07-23 16:19:22 +03:00
|
|
|
pub fn to_llvm_features<'a>(sess: &Session, s: &'a str) -> SmallVec<[&'a str; 2]> {
|
2020-10-15 11:44:00 +02: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) {
|
2021-09-15 10:18:10 -04:00
|
|
|
("x86", "sse4.2") => {
|
|
|
|
if get_version() >= (14, 0, 0) {
|
2021-07-23 16:19:22 +03:00
|
|
|
smallvec!["sse4.2", "crc32"]
|
2021-09-15 10:18:10 -04:00
|
|
|
} else {
|
2021-07-23 16:19:22 +03:00
|
|
|
smallvec!["sse4.2"]
|
2021-09-15 10:18:10 -04:00
|
|
|
}
|
|
|
|
}
|
2021-07-23 16:19:22 +03:00
|
|
|
("x86", "pclmulqdq") => smallvec!["pclmul"],
|
|
|
|
("x86", "rdrand") => smallvec!["rdrnd"],
|
|
|
|
("x86", "bmi1") => smallvec!["bmi"],
|
|
|
|
("x86", "cmpxchg16b") => smallvec!["cx16"],
|
|
|
|
("x86", "avx512vaes") => smallvec!["vaes"],
|
|
|
|
("x86", "avx512gfni") => smallvec!["gfni"],
|
|
|
|
("x86", "avx512vpclmulqdq") => smallvec!["vpclmulqdq"],
|
|
|
|
("aarch64", "rcpc2") => smallvec!["rcpc-immo"],
|
|
|
|
("aarch64", "dpb") => smallvec!["ccpp"],
|
|
|
|
("aarch64", "dpb2") => smallvec!["ccdp"],
|
|
|
|
("aarch64", "frintts") => smallvec!["fptoint"],
|
|
|
|
("aarch64", "fcma") => smallvec!["complxnum"],
|
|
|
|
("aarch64", "pmuv3") => smallvec!["perfmon"],
|
|
|
|
("aarch64", "paca") => smallvec!["pauth"],
|
|
|
|
("aarch64", "pacg") => smallvec!["pauth"],
|
2022-03-10 17:50:46 +00:00
|
|
|
// Rust ties fp and neon together. In LLVM neon implicitly enables fp,
|
|
|
|
// but we manually enable neon when a feature only implicitly enables fp
|
|
|
|
("aarch64", "f32mm") => smallvec!["f32mm", "neon"],
|
|
|
|
("aarch64", "f64mm") => smallvec!["f64mm", "neon"],
|
|
|
|
("aarch64", "fhm") => smallvec!["fp16fml", "neon"],
|
|
|
|
("aarch64", "fp16") => smallvec!["fullfp16", "neon"],
|
|
|
|
("aarch64", "jsconv") => smallvec!["jsconv", "neon"],
|
|
|
|
("aarch64", "sve") => smallvec!["sve", "neon"],
|
|
|
|
("aarch64", "sve2") => smallvec!["sve2", "neon"],
|
|
|
|
("aarch64", "sve2-aes") => smallvec!["sve2-aes", "neon"],
|
|
|
|
("aarch64", "sve2-sm4") => smallvec!["sve2-sm4", "neon"],
|
|
|
|
("aarch64", "sve2-sha3") => smallvec!["sve2-sha3", "neon"],
|
|
|
|
("aarch64", "sve2-bitperm") => smallvec!["sve2-bitperm", "neon"],
|
2021-07-23 16:19:22 +03:00
|
|
|
(_, s) => smallvec![s],
|
2018-02-11 02:27:21 +03:00
|
|
|
}
|
|
|
|
}
|
2017-11-28 10:28:15 +01:00
|
|
|
|
2022-01-31 13:04:27 +00:00
|
|
|
// Given a map from target_features to whether they are enabled or disabled,
|
|
|
|
// ensure only valid combinations are allowed.
|
|
|
|
pub fn check_tied_features(
|
|
|
|
sess: &Session,
|
|
|
|
features: &FxHashMap<&str, bool>,
|
|
|
|
) -> Option<&'static [&'static str]> {
|
2022-06-06 18:05:07 +08:00
|
|
|
if !features.is_empty() {
|
|
|
|
for tied in tied_target_features(sess) {
|
|
|
|
// Tied features must be set to the same value, or not set at all
|
|
|
|
let mut tied_iter = tied.iter();
|
|
|
|
let enabled = features.get(tied_iter.next().unwrap());
|
|
|
|
if tied_iter.any(|f| enabled != features.get(f)) {
|
|
|
|
return Some(tied);
|
|
|
|
}
|
2022-01-31 13:04:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-06 18:05:07 +08:00
|
|
|
return None;
|
2022-01-31 13:04:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 19:36:42 -07:00
|
|
|
// Used to generate cfg variables and apply features
|
|
|
|
// Must express features in the way Rust understands them
|
2022-07-11 14:26:58 +01:00
|
|
|
pub fn target_features(sess: &Session, allow_unstable: bool) -> Vec<Symbol> {
|
2020-04-23 21:10:01 +03:00
|
|
|
let target_machine = create_informational_target_machine(sess);
|
2022-07-11 14:26:58 +01:00
|
|
|
let mut features: Vec<Symbol> = supported_target_features(sess)
|
|
|
|
.iter()
|
|
|
|
.filter_map(|&(feature, gate)| {
|
|
|
|
if sess.is_nightly_build() || allow_unstable || gate.is_none() {
|
|
|
|
Some(feature)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter(|feature| {
|
|
|
|
// check that all features in a given smallvec are enabled
|
|
|
|
for llvm_feature in to_llvm_features(sess, feature) {
|
|
|
|
let cstr = SmallCStr::new(llvm_feature);
|
|
|
|
if !unsafe { llvm::LLVMRustHasFeature(target_machine, cstr.as_ptr()) } {
|
|
|
|
return false;
|
2021-09-15 10:18:10 -04:00
|
|
|
}
|
2022-07-11 14:26:58 +01:00
|
|
|
}
|
|
|
|
true
|
|
|
|
})
|
|
|
|
.map(|feature| Symbol::intern(feature))
|
|
|
|
.collect();
|
2022-02-15 15:22:13 +01:00
|
|
|
|
|
|
|
// LLVM 14 changed the ABI for i128 arguments to __float/__fix builtins on Win64
|
|
|
|
// (see https://reviews.llvm.org/D110413). This unstable target feature is intended for use
|
|
|
|
// by compiler-builtins, to export the builtins with the expected, LLVM-version-dependent ABI.
|
|
|
|
// The target feature can be dropped once we no longer support older LLVM versions.
|
|
|
|
if sess.is_nightly_build() && get_version() >= (14, 0, 0) {
|
|
|
|
features.push(Symbol::intern("llvm14-builtins-abi"));
|
|
|
|
}
|
|
|
|
features
|
2018-01-05 13:26:26 -08:00
|
|
|
}
|
2017-04-30 20:33:25 +02:00
|
|
|
|
|
|
|
pub fn print_version() {
|
2020-10-12 22:33:27 -04:00
|
|
|
let (major, minor, patch) = get_version();
|
|
|
|
println!("LLVM version: {}.{}.{}", major, minor, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_version() -> (u32, u32, u32) {
|
2017-10-30 18:42:21 +01:00
|
|
|
// Can be called without initializing LLVM
|
2017-04-30 20:33:25 +02:00
|
|
|
unsafe {
|
2020-10-12 22:33:27 -04:00
|
|
|
(llvm::LLVMRustVersionMajor(), llvm::LLVMRustVersionMinor(), llvm::LLVMRustVersionPatch())
|
2017-04-30 20:33:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_passes() {
|
2017-10-30 18:42:21 +01:00
|
|
|
// Can be called without initializing LLVM
|
2017-04-30 20:33:25 +02:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMRustPrintPasses();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-23 16:19:22 +03:00
|
|
|
for llvm_feature in to_llvm_features(sess, *feature) {
|
2021-09-15 10:18:10 -04:00
|
|
|
// LLVM asserts that these are sorted. LLVM and Rust both use byte comparison for these strings.
|
2021-07-23 16:19:22 +03:00
|
|
|
match target_features.binary_search_by_key(&llvm_feature, |(f, _d)| f).ok().map(
|
2021-09-15 10:18:10 -04:00
|
|
|
|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 19:38:50 +02: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 18:42:21 +01:00
|
|
|
pub(crate) fn print(req: PrintRequest, sess: &Session) {
|
|
|
|
require_inited();
|
2020-04-23 21:10:01 +03: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 20:33:25 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-23 11:03:22 -07:00
|
|
|
|
2020-09-17 17:39:26 +08:00
|
|
|
fn handle_native(name: &str) -> &str {
|
2018-08-23 11:03:22 -07:00
|
|
|
if name != "native" {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 17:39:26 +08:00
|
|
|
|
|
|
|
pub fn target_cpu(sess: &Session) -> &str {
|
2022-03-22 11:43:05 +01:00
|
|
|
match sess.opts.cg.target_cpu {
|
|
|
|
Some(ref name) => handle_native(name),
|
|
|
|
None => handle_native(sess.target.cpu.as_ref()),
|
|
|
|
}
|
2020-09-17 17:39:26 +08:00
|
|
|
}
|
|
|
|
|
2021-03-13 15:29:39 +02:00
|
|
|
/// The list of LLVM features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,
|
|
|
|
/// `--target` and similar).
|
2021-07-23 16:19:22 +03:00
|
|
|
pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec<String> {
|
2022-03-03 19:47:23 +08:00
|
|
|
// Features that come earlier are overridden by conflicting features later in the string.
|
2021-03-13 15:29:39 +02:00
|
|
|
// Typically we'll want more explicit settings to override the implicit ones, so:
|
|
|
|
//
|
2022-03-03 19:47:23 +08:00
|
|
|
// * Features from -Ctarget-cpu=*; are overridden by [^1]
|
|
|
|
// * Features implied by --target; are overridden by
|
|
|
|
// * Features from -Ctarget-feature; are overridden by
|
2021-03-13 15:29:39 +02:00
|
|
|
// * 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
|
2022-03-03 19:47:23 +08:00
|
|
|
// the host target are overridden by `-Ctarget-cpu=*`. On the other hand, what about when both
|
2021-03-13 15:29:39 +02:00
|
|
|
// `--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 03:23:54 -05:00
|
|
|
match sess.opts.cg.target_cpu {
|
2021-03-13 15:29:39 +02:00
|
|
|
Some(ref s) if s == "native" => {
|
2021-01-08 11:50:21 -05: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 23:35:57 +02:00
|
|
|
features.extend(features_string.split(',').map(String::from));
|
2021-01-06 03:23:54 -05:00
|
|
|
}
|
2021-03-13 15:29:39 +02:00
|
|
|
Some(_) | None => {}
|
|
|
|
};
|
|
|
|
|
2021-07-23 16:19:22 +03:00
|
|
|
// Features implied by an implicit or explicit `--target`.
|
|
|
|
features.extend(
|
|
|
|
sess.target
|
|
|
|
.features
|
|
|
|
.split(',')
|
|
|
|
.filter(|v| !v.is_empty() && backend_feature_name(v).is_some())
|
2022-07-27 14:26:00 +02:00
|
|
|
// Drop +atomics-32 feature introduced in LLVM 15.
|
|
|
|
.filter(|v| *v != "+atomics-32" || get_version() >= (15, 0, 0))
|
2021-07-23 16:19:22 +03:00
|
|
|
.map(String::from),
|
|
|
|
);
|
2022-01-31 13:04:27 +00:00
|
|
|
|
2021-07-23 16:19:22 +03:00
|
|
|
// -Ctarget-features
|
|
|
|
let supported_features = supported_target_features(sess);
|
2022-06-06 18:05:07 +08:00
|
|
|
let mut featsmap = FxHashMap::default();
|
2021-07-23 16:19:22 +03:00
|
|
|
let feats = sess
|
|
|
|
.opts
|
|
|
|
.cg
|
|
|
|
.target_feature
|
|
|
|
.split(',')
|
|
|
|
.filter_map(|s| {
|
|
|
|
let enable_disable = match s.chars().next() {
|
|
|
|
None => return None,
|
|
|
|
Some(c @ '+' | c @ '-') => c,
|
|
|
|
Some(_) => {
|
|
|
|
if diagnostics {
|
|
|
|
let mut diag = sess.struct_warn(&format!(
|
|
|
|
"unknown feature specified for `-Ctarget-feature`: `{}`",
|
|
|
|
s
|
|
|
|
));
|
|
|
|
diag.note("features must begin with a `+` to enable or `-` to disable it");
|
|
|
|
diag.emit();
|
|
|
|
}
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let feature = backend_feature_name(s)?;
|
|
|
|
// Warn against use of LLVM specific feature names on the CLI.
|
|
|
|
if diagnostics && !supported_features.iter().any(|&(v, _)| v == feature) {
|
|
|
|
let rust_feature = supported_features.iter().find_map(|&(rust_feature, _)| {
|
|
|
|
let llvm_features = to_llvm_features(sess, rust_feature);
|
|
|
|
if llvm_features.contains(&feature) && !llvm_features.contains(&rust_feature) {
|
|
|
|
Some(rust_feature)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let mut diag = sess.struct_warn(&format!(
|
|
|
|
"unknown feature specified for `-Ctarget-feature`: `{}`",
|
|
|
|
feature
|
|
|
|
));
|
|
|
|
diag.note("it is still passed through to the codegen backend");
|
|
|
|
if let Some(rust_feature) = rust_feature {
|
|
|
|
diag.help(&format!("you might have meant: `{}`", rust_feature));
|
|
|
|
} else {
|
|
|
|
diag.note("consider filing a feature request");
|
|
|
|
}
|
|
|
|
diag.emit();
|
|
|
|
}
|
2022-06-06 18:05:07 +08:00
|
|
|
|
|
|
|
if diagnostics {
|
|
|
|
// FIXME(nagisa): figure out how to not allocate a full hashset here.
|
|
|
|
featsmap.insert(feature, enable_disable == '+');
|
|
|
|
}
|
|
|
|
|
|
|
|
// rustc-specific features do not get passed down to LLVM…
|
|
|
|
if RUSTC_SPECIFIC_FEATURES.contains(&feature) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
// ... otherwise though we run through `to_llvm_features` 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.
|
|
|
|
Some(
|
|
|
|
to_llvm_features(sess, feature)
|
|
|
|
.into_iter()
|
|
|
|
.map(move |f| format!("{}{}", enable_disable, f)),
|
|
|
|
)
|
2021-07-23 16:19:22 +03:00
|
|
|
})
|
2022-06-06 18:05:07 +08:00
|
|
|
.flatten();
|
|
|
|
features.extend(feats);
|
|
|
|
|
|
|
|
if diagnostics && let Some(f) = check_tied_features(sess, &featsmap) {
|
|
|
|
sess.err(&format!(
|
|
|
|
"target features {} must all be enabled or disabled together",
|
|
|
|
f.join(", ")
|
|
|
|
));
|
2022-01-31 13:04:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 16:19:22 +03:00
|
|
|
features
|
|
|
|
}
|
2021-03-13 15:29:39 +02:00
|
|
|
|
2021-07-23 16:19:22 +03:00
|
|
|
/// Returns a feature name for the given `+feature` or `-feature` string.
|
|
|
|
///
|
|
|
|
/// Only allows features that are backend specific (i.e. not [`RUSTC_SPECIFIC_FEATURES`].)
|
|
|
|
fn backend_feature_name(s: &str) -> Option<&str> {
|
|
|
|
// features must start with a `+` or `-`.
|
|
|
|
let feature = s.strip_prefix(&['+', '-'][..]).unwrap_or_else(|| {
|
|
|
|
bug!("target feature `{}` must begin with a `+` or `-`", s);
|
|
|
|
});
|
|
|
|
// Rustc-specific feature requests like `+crt-static` or `-crt-static`
|
|
|
|
// are not passed down to LLVM.
|
|
|
|
if RUSTC_SPECIFIC_FEATURES.contains(&feature) {
|
|
|
|
return None;
|
2022-01-31 13:04:27 +00:00
|
|
|
}
|
2021-07-23 16:19:22 +03:00
|
|
|
Some(feature)
|
2021-01-06 03:23:54 -05:00
|
|
|
}
|
|
|
|
|
2020-09-17 17:39:26 +08:00
|
|
|
pub fn tune_cpu(sess: &Session) -> Option<&str> {
|
2022-07-06 07:44:47 -05:00
|
|
|
let name = sess.opts.unstable_opts.tune_cpu.as_ref()?;
|
2020-12-30 18:22:41 +01:00
|
|
|
Some(handle_native(name))
|
2020-09-17 17:39:26 +08:00
|
|
|
}
|
2021-12-20 14:49:04 +01:00
|
|
|
|
|
|
|
pub(crate) fn should_use_new_llvm_pass_manager(user_opt: &Option<bool>, target_arch: &str) -> bool {
|
|
|
|
// The new pass manager is enabled by default for LLVM >= 13.
|
|
|
|
// This matches Clang, which also enables it since Clang 13.
|
|
|
|
|
2022-04-19 15:02:43 +02:00
|
|
|
// Since LLVM 15, the legacy pass manager is no longer supported.
|
|
|
|
if llvm_util::get_version() >= (15, 0, 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-09 10:00:07 +01:00
|
|
|
// There are some perf issues with the new pass manager when targeting
|
|
|
|
// s390x with LLVM 13, so enable the new pass manager only with LLVM 14.
|
|
|
|
// See https://github.com/rust-lang/rust/issues/89609.
|
|
|
|
let min_version = if target_arch == "s390x" { 14 } else { 13 };
|
|
|
|
user_opt.unwrap_or_else(|| llvm_util::get_version() >= (min_version, 0, 0))
|
2021-12-20 14:49:04 +01:00
|
|
|
}
|