Skip to content

Commit

Permalink
Auto merge of #53642 - alexcrichton:fix-target-cpu-native, r=arielb1
Browse files Browse the repository at this point in the history
Fix warnings about the `native` target-cpu

This fixes a regression from #53031 where specifying `-C target-cpu=native` is
printing a lot of warnings from LLVM about `native` being an unknown CPU. It
turns out that `native` is indeed an unknown CPU and we have to perform a
mapping to an actual CPU name, but this mapping is only performed in one
location rather than all locations we inform LLVM about the target CPU.

This commit centralizes the mapping of `native` to LLVM's value of the native
CPU, ensuring that all locations we inform LLVM about the `target-cpu` it's
never `native`.

Closes #53322
  • Loading branch information
bors committed Aug 27, 2018
2 parents 3a2c603 + 715d2cc commit 91f45eb
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 17 deletions.
7 changes: 0 additions & 7 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,13 +649,6 @@ impl Session {
}
}

pub fn target_cpu(&self) -> &str {
match self.opts.cg.target_cpu {
Some(ref s) => &**s,
None => &*self.target.target.options.cpu
}
}

pub fn must_not_eliminate_frame_pointers(&self) -> bool {
if let Some(x) = self.opts.cg.force_frame_pointers {
x
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_codegen_llvm/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_codegen_llvm/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 |
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_codegen_llvm/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(",");
Expand Down
1 change: 1 addition & 0 deletions src/librustc_codegen_llvm/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions src/librustc_codegen_llvm/llvm_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
}
}
13 changes: 7 additions & 6 deletions src/rustllvm/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ extern "C" void LLVMRustPrintTargetFeatures(LLVMTargetMachineRef) {
}
#endif

extern "C" const char* LLVMRustGetHostCPUName(size_t *len) {
StringRef Name = sys::getHostCPUName();
*len = Name.size();
return Name.data();
}

extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
const char *TripleStr, const char *CPU, const char *Feature,
LLVMRustCodeModel RustCM, LLVMRustRelocMode RustReloc,
Expand All @@ -381,11 +387,6 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
return nullptr;
}

StringRef RealCPU = CPU;
if (RealCPU == "native") {
RealCPU = sys::getHostCPUName();
}

TargetOptions Options;

Options.FloatABIType = FloatABI::Default;
Expand Down Expand Up @@ -417,7 +418,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
if (RustCM != LLVMRustCodeModel::None)
CM = fromRust(RustCM);
TargetMachine *TM = TheTarget->createTargetMachine(
Trip.getTriple(), RealCPU, Feature, Options, RM, CM, OptLevel);
Trip.getTriple(), CPU, Feature, Options, RM, CM, OptLevel);
return wrap(TM);
}

Expand Down
8 changes: 7 additions & 1 deletion src/test/run-make-fulldeps/target-cpu-native/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
-include ../tools.mk

all:
$(RUSTC) foo.rs -C target-cpu=native
$(RUSTC) foo.rs -C target-cpu=native 2>&1 | tee $(TMPDIR)/out.log
$(call RUN,foo)
# Make sure no warnings about "unknown CPU `native`" were emitted
if [ "$$(stat --printf="%s" $(TMPDIR)/out.log)" = "0" ]; then \
echo no warnings generated; \
else \
exit 1; \
fi

0 comments on commit 91f45eb

Please sign in to comment.