Skip to content

Commit 0f61654

Browse files
committed
Auto merge of #53642 - alexcrichton:fix-target-cpu-native, r=arielb1
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
2 parents 59e52b1 + 12e33bf commit 0f61654

File tree

8 files changed

+39
-17
lines changed

8 files changed

+39
-17
lines changed

src/librustc/session/mod.rs

-7
Original file line numberDiff line numberDiff line change
@@ -649,13 +649,6 @@ impl Session {
649649
}
650650
}
651651

652-
pub fn target_cpu(&self) -> &str {
653-
match self.opts.cg.target_cpu {
654-
Some(ref s) => &**s,
655-
None => &*self.target.target.options.cpu
656-
}
657-
}
658-
659652
pub fn must_not_eliminate_frame_pointers(&self) -> bool {
660653
if let Some(x) = self.opts.cg.force_frame_pointers {
661654
x

src/librustc_codegen_llvm/attributes.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ pub fn llvm_target_features(sess: &Session) -> impl Iterator<Item = &str> {
124124
}
125125

126126
pub fn apply_target_cpu_attr(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
127-
let target_cpu = CString::new(cx.tcx.sess.target_cpu().to_string()).unwrap();
127+
let cpu = llvm_util::target_cpu(cx.tcx.sess);
128+
let target_cpu = CString::new(cpu).unwrap();
128129
llvm::AddFunctionAttrStringValue(
129130
llfn,
130131
llvm::AttributePlace::Function,

src/librustc_codegen_llvm/back/linker.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use rustc::session::config::{self, CrateType, OptLevel, DebugInfo,
2626
use rustc::ty::TyCtxt;
2727
use rustc_target::spec::{LinkerFlavor, LldFlavor};
2828
use serialize::{json, Encoder};
29+
use llvm_util;
2930

3031
/// For all the linkers we support, and information they might
3132
/// need out of the shared crate context before we get rid of it.
@@ -202,7 +203,7 @@ impl<'a> GccLinker<'a> {
202203
};
203204

204205
self.linker_arg(&format!("-plugin-opt={}", opt_level));
205-
self.linker_arg(&format!("-plugin-opt=mcpu={}", self.sess.target_cpu()));
206+
self.linker_arg(&format!("-plugin-opt=mcpu={}", llvm_util::target_cpu(self.sess)));
206207

207208
match self.sess.opts.cg.lto {
208209
config::Lto::Thin |

src/librustc_codegen_llvm/back/write.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc::session::Session;
2525
use rustc::util::nodemap::FxHashMap;
2626
use time_graph::{self, TimeGraph, Timeline};
2727
use llvm::{self, DiagnosticInfo, PassManager, SMDiagnostic};
28+
use llvm_util;
2829
use {CodegenResults, ModuleSource, ModuleCodegen, CompiledModule, ModuleKind};
2930
use CrateInfo;
3031
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
@@ -173,7 +174,7 @@ pub fn target_machine_factory(sess: &Session, find_features: bool)
173174
let singlethread = sess.target.target.options.singlethread;
174175

175176
let triple = SmallCStr::new(&sess.target.target.llvm_target);
176-
let cpu = SmallCStr::new(sess.target_cpu());
177+
let cpu = SmallCStr::new(llvm_util::target_cpu(sess));
177178
let features = attributes::llvm_target_features(sess)
178179
.collect::<Vec<_>>()
179180
.join(",");

src/librustc_codegen_llvm/llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,7 @@ extern "C" {
14491449
pub fn LLVMRustPrintTargetCPUs(T: &TargetMachine);
14501450
pub fn LLVMRustPrintTargetFeatures(T: &TargetMachine);
14511451

1452+
pub fn LLVMRustGetHostCPUName(len: *mut usize) -> *const c_char;
14521453
pub fn LLVMRustCreateTargetMachine(Triple: *const c_char,
14531454
CPU: *const c_char,
14541455
Features: *const c_char,

src/librustc_codegen_llvm/llvm_util.rs

+18
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use libc::c_int;
1717
use std::ffi::CString;
1818
use syntax::feature_gate::UnstableFeatures;
1919

20+
use std::str;
21+
use std::slice;
2022
use std::sync::atomic::{AtomicBool, Ordering};
2123
use std::sync::Once;
2224

@@ -262,3 +264,19 @@ pub(crate) fn print(req: PrintRequest, sess: &Session) {
262264
}
263265
}
264266
}
267+
268+
pub fn target_cpu(sess: &Session) -> &str {
269+
let name = match sess.opts.cg.target_cpu {
270+
Some(ref s) => &**s,
271+
None => &*sess.target.target.options.cpu
272+
};
273+
if name != "native" {
274+
return name
275+
}
276+
277+
unsafe {
278+
let mut len = 0;
279+
let ptr = llvm::LLVMRustGetHostCPUName(&mut len);
280+
str::from_utf8(slice::from_raw_parts(ptr as *const u8, len)).unwrap()
281+
}
282+
}

src/rustllvm/PassWrapper.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,12 @@ extern "C" void LLVMRustPrintTargetFeatures(LLVMTargetMachineRef) {
359359
}
360360
#endif
361361

362+
extern "C" const char* LLVMRustGetHostCPUName(size_t *len) {
363+
StringRef Name = sys::getHostCPUName();
364+
*len = Name.size();
365+
return Name.data();
366+
}
367+
362368
extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
363369
const char *TripleStr, const char *CPU, const char *Feature,
364370
LLVMRustCodeModel RustCM, LLVMRustRelocMode RustReloc,
@@ -381,11 +387,6 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
381387
return nullptr;
382388
}
383389

384-
StringRef RealCPU = CPU;
385-
if (RealCPU == "native") {
386-
RealCPU = sys::getHostCPUName();
387-
}
388-
389390
TargetOptions Options;
390391

391392
Options.FloatABIType = FloatABI::Default;
@@ -417,7 +418,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
417418
if (RustCM != LLVMRustCodeModel::None)
418419
CM = fromRust(RustCM);
419420
TargetMachine *TM = TheTarget->createTargetMachine(
420-
Trip.getTriple(), RealCPU, Feature, Options, RM, CM, OptLevel);
421+
Trip.getTriple(), CPU, Feature, Options, RM, CM, OptLevel);
421422
return wrap(TM);
422423
}
423424

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
-include ../tools.mk
22

33
all:
4-
$(RUSTC) foo.rs -C target-cpu=native
4+
$(RUSTC) foo.rs -C target-cpu=native 2>&1 | tee $(TMPDIR)/out.log
55
$(call RUN,foo)
6+
# Make sure no warnings about "unknown CPU `native`" were emitted
7+
if [ "$$(wc -c $(TMPDIR)/out.log | cut -d' ' -f 1)" = "0" ]; then \
8+
echo no warnings generated; \
9+
else \
10+
exit 1; \
11+
fi

0 commit comments

Comments
 (0)