Skip to content

Commit

Permalink
Auto merge of #80749 - as-com:target-cpu-actually-native, r=nagisa
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Jan 9, 2021
2 parents 26438b4 + 80ca198 commit c87ef0a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_llvm/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 32 additions & 1 deletion compiler/rustc_codegen_llvm/src/llvm_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)),
Expand Down

0 comments on commit c87ef0a

Please sign in to comment.