Skip to content

Commit 65702bf

Browse files
authored
Rollup merge of rust-lang#110876 - mj10021:issue-110647-fix, r=b-naber
Added default target cpu to `--print target-cpus` output and updated docs Added default target cpu info as requested in issue rust-lang#110647 and noted the new output in the documentation
2 parents de7e29e + f239cd6 commit 65702bf

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2249,7 +2249,7 @@ extern "C" {
22492249

22502250
pub fn LLVMRustHasFeature(T: &TargetMachine, s: *const c_char) -> bool;
22512251

2252-
pub fn LLVMRustPrintTargetCPUs(T: &TargetMachine);
2252+
pub fn LLVMRustPrintTargetCPUs(T: &TargetMachine, cpu: *const c_char);
22532253
pub fn LLVMRustGetTargetFeaturesCount(T: &TargetMachine) -> size_t;
22542254
pub fn LLVMRustGetTargetFeature(
22552255
T: &TargetMachine,

compiler/rustc_codegen_llvm/src/llvm_util.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,14 @@ pub(crate) fn print(req: PrintRequest, sess: &Session) {
329329
require_inited();
330330
let tm = create_informational_target_machine(sess);
331331
match req {
332-
PrintRequest::TargetCPUs => unsafe { llvm::LLVMRustPrintTargetCPUs(tm) },
332+
PrintRequest::TargetCPUs => {
333+
// SAFETY generate a C compatible string from a byte slice to pass
334+
// the target CPU name into LLVM, the lifetime of the reference is
335+
// at least as long as the C function
336+
let cpu_cstring = CString::new(handle_native(sess.target.cpu.as_ref()))
337+
.unwrap_or_else(|e| bug!("failed to convert to cstring: {}", e));
338+
unsafe { llvm::LLVMRustPrintTargetCPUs(tm, cpu_cstring.as_ptr()) };
339+
}
333340
PrintRequest::TargetFeatures => print_target_features(sess, tm),
334341
_ => bug!("rustc_codegen_llvm can't handle print request: {:?}", req),
335342
}

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+13-4
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ static size_t getLongestEntryLength(ArrayRef<KV> Table) {
307307
return MaxLen;
308308
}
309309

310-
extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM) {
310+
extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM, const char* TargetCPU) {
311311
const TargetMachine *Target = unwrap(TM);
312312
const MCSubtargetInfo *MCInfo = Target->getMCSubtargetInfo();
313313
const Triple::ArchType HostArch = Triple(sys::getDefaultTargetTriple()).getArch();
@@ -323,9 +323,18 @@ extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM) {
323323
printf(" %-*s - Select the CPU of the current host (currently %.*s).\n",
324324
MaxCPULen, "native", (int)HostCPU.size(), HostCPU.data());
325325
}
326-
for (auto &CPU : CPUTable)
327-
printf(" %-*s\n", MaxCPULen, CPU.Key);
328-
printf("\n");
326+
for (auto &CPU : CPUTable) {
327+
// Compare cpu against current target to label the default
328+
if (strcmp(CPU.Key, TargetCPU) == 0) {
329+
printf(" %-*s - This is the default target CPU"
330+
" for the current build target (currently %s).",
331+
MaxCPULen, CPU.Key, Target->getTargetTriple().str().c_str());
332+
}
333+
else {
334+
printf(" %-*s", MaxCPULen, CPU.Key);
335+
}
336+
printf("\n");
337+
}
329338
}
330339

331340
extern "C" size_t LLVMRustGetTargetFeaturesCount(LLVMTargetMachineRef TM) {

src/doc/rustc/src/codegen-options/index.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,8 @@ change in the future.
574574
This instructs `rustc` to generate code specifically for a particular processor.
575575

576576
You can run `rustc --print target-cpus` to see the valid options to pass
577-
here. Each target has a default base CPU. Special values include:
577+
and the default target CPU for the current buid target.
578+
Each target has a default base CPU. Special values include:
578579

579580
* `native` can be passed to use the processor of the host machine.
580581
* `generic` refers to an LLVM target with minimal features but modern tuning.

0 commit comments

Comments
 (0)