diff options
Diffstat (limited to 'src/librustc_codegen_llvm')
| -rw-r--r-- | src/librustc_codegen_llvm/attributes.rs | 3 | ||||
| -rw-r--r-- | src/librustc_codegen_llvm/back/linker.rs | 3 | ||||
| -rw-r--r-- | src/librustc_codegen_llvm/back/write.rs | 3 | ||||
| -rw-r--r-- | src/librustc_codegen_llvm/llvm/ffi.rs | 1 | ||||
| -rw-r--r-- | src/librustc_codegen_llvm/llvm_util.rs | 18 |
5 files changed, 25 insertions, 3 deletions
diff --git a/src/librustc_codegen_llvm/attributes.rs b/src/librustc_codegen_llvm/attributes.rs index 2b64642b766..c8a2b47e98d 100644 --- a/src/librustc_codegen_llvm/attributes.rs +++ b/src/librustc_codegen_llvm/attributes.rs @@ -124,7 +124,8 @@ pub fn llvm_target_features(sess: &Session) -> impl Iterator<Item = &str> { } pub fn apply_target_cpu_attr(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) { - let target_cpu = CString::new(cx.tcx.sess.target_cpu().to_string()).unwrap(); + let cpu = llvm_util::target_cpu(cx.tcx.sess); + let target_cpu = CString::new(cpu).unwrap(); llvm::AddFunctionAttrStringValue( llfn, llvm::AttributePlace::Function, diff --git a/src/librustc_codegen_llvm/back/linker.rs b/src/librustc_codegen_llvm/back/linker.rs index a429e8f2d81..eee60b262a1 100644 --- a/src/librustc_codegen_llvm/back/linker.rs +++ b/src/librustc_codegen_llvm/back/linker.rs @@ -26,6 +26,7 @@ use rustc::session::config::{self, CrateType, OptLevel, DebugInfo, use rustc::ty::TyCtxt; use rustc_target::spec::{LinkerFlavor, LldFlavor}; use serialize::{json, Encoder}; +use llvm_util; /// For all the linkers we support, and information they might /// need out of the shared crate context before we get rid of it. @@ -202,7 +203,7 @@ impl<'a> GccLinker<'a> { }; self.linker_arg(&format!("-plugin-opt={}", opt_level)); - self.linker_arg(&format!("-plugin-opt=mcpu={}", self.sess.target_cpu())); + self.linker_arg(&format!("-plugin-opt=mcpu={}", llvm_util::target_cpu(self.sess))); match self.sess.opts.cg.lto { config::Lto::Thin | diff --git a/src/librustc_codegen_llvm/back/write.rs b/src/librustc_codegen_llvm/back/write.rs index 80e9965e39c..2373428d68c 100644 --- a/src/librustc_codegen_llvm/back/write.rs +++ b/src/librustc_codegen_llvm/back/write.rs @@ -25,6 +25,7 @@ use rustc::session::Session; use rustc::util::nodemap::FxHashMap; use time_graph::{self, TimeGraph, Timeline}; use llvm::{self, DiagnosticInfo, PassManager, SMDiagnostic}; +use llvm_util; use {CodegenResults, ModuleSource, ModuleCodegen, CompiledModule, ModuleKind}; use CrateInfo; use rustc::hir::def_id::{CrateNum, LOCAL_CRATE}; @@ -173,7 +174,7 @@ pub fn target_machine_factory(sess: &Session, find_features: bool) let singlethread = sess.target.target.options.singlethread; let triple = SmallCStr::new(&sess.target.target.llvm_target); - let cpu = SmallCStr::new(sess.target_cpu()); + let cpu = SmallCStr::new(llvm_util::target_cpu(sess)); let features = attributes::llvm_target_features(sess) .collect::<Vec<_>>() .join(","); diff --git a/src/librustc_codegen_llvm/llvm/ffi.rs b/src/librustc_codegen_llvm/llvm/ffi.rs index d3039a05b6d..51b0299e63f 100644 --- a/src/librustc_codegen_llvm/llvm/ffi.rs +++ b/src/librustc_codegen_llvm/llvm/ffi.rs @@ -1449,6 +1449,7 @@ extern "C" { pub fn LLVMRustPrintTargetCPUs(T: &TargetMachine); pub fn LLVMRustPrintTargetFeatures(T: &TargetMachine); + pub fn LLVMRustGetHostCPUName(len: *mut usize) -> *const c_char; pub fn LLVMRustCreateTargetMachine(Triple: *const c_char, CPU: *const c_char, Features: *const c_char, diff --git a/src/librustc_codegen_llvm/llvm_util.rs b/src/librustc_codegen_llvm/llvm_util.rs index ff26e0f35f0..9fcc33d82cf 100644 --- a/src/librustc_codegen_llvm/llvm_util.rs +++ b/src/librustc_codegen_llvm/llvm_util.rs @@ -17,6 +17,8 @@ use libc::c_int; use std::ffi::CString; use syntax::feature_gate::UnstableFeatures; +use std::str; +use std::slice; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Once; @@ -262,3 +264,19 @@ pub(crate) fn print(req: PrintRequest, sess: &Session) { } } } + +pub fn target_cpu(sess: &Session) -> &str { + let name = match sess.opts.cg.target_cpu { + Some(ref s) => &**s, + None => &*sess.target.target.options.cpu + }; + 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() + } +} |
