Auto merge of #80749 - as-com:target-cpu-actually-native, r=nagisa

Make target-cpu=native detect individual features

This PR makes target-cpu=native check for and enable/disable individual features instead of detecting and targeting a CPU by name. This brings the flag's behavior more in line with clang and gcc and ensures that the host actually supports each feature that we are compiling for.

This should resolve issues with miscompilations on e.g. "Haswell" Pentiums and Celerons that lack support for AVX, and also enable support for `aes` on Broadwell processors that support it. It should also resolve issues with failing to detect feature support in newer CPUs that aren't yet known by LLVM (see: #80633).

Fixes #54688
Fixes #48464
Fixes #38218
This commit is contained in:
bors 2021-01-09 04:36:24 +00:00
commit c87ef0a2fc
3 changed files with 38 additions and 2 deletions

View File

@ -164,7 +164,8 @@ pub fn target_machine_factory(
let code_model = to_llvm_code_model(sess.code_model());
let features = attributes::llvm_target_features(sess).collect::<Vec<_>>();
let mut features = llvm_util::handle_native_features(sess);
features.extend(attributes::llvm_target_features(sess).map(|s| s.to_owned()));
let mut singlethread = sess.target.singlethread;
// On the wasm target once the `atomics` feature is enabled that means that

View File

@ -1708,6 +1708,10 @@ extern "C" {
PM: &PassManager<'_>,
);
pub fn LLVMGetHostCPUFeatures() -> *mut c_char;
pub fn LLVMDisposeMessage(message: *mut c_char);
// Stuff that's in llvm-wrapper/ because it's not upstream yet.
/// Opens an object file.

View File

@ -8,7 +8,7 @@ use rustc_session::config::PrintRequest;
use rustc_session::Session;
use rustc_span::symbol::Symbol;
use rustc_target::spec::{MergeFunctions, PanicStrategy};
use std::ffi::CString;
use std::ffi::{CStr, CString};
use std::slice;
use std::str;
@ -221,6 +221,37 @@ pub fn target_cpu(sess: &Session) -> &str {
handle_native(name)
}
pub fn handle_native_features(sess: &Session) -> Vec<String> {
match sess.opts.cg.target_cpu {
Some(ref s) => {
if s != "native" {
return vec![];
}
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
};
features_string.split(",").map(|s| s.to_owned()).collect()
}
None => vec![],
}
}
pub fn tune_cpu(sess: &Session) -> Option<&str> {
match sess.opts.debugging_opts.tune_cpu {
Some(ref s) => Some(handle_native(&**s)),