From ea17aa9141cde9f26ec09b423ca2efe8aa08cc33 Mon Sep 17 00:00:00 2001 From: James Dietz Date: Wed, 26 Apr 2023 21:11:14 -0400 Subject: [PATCH 1/4] `--print target-cpus` shows default target cpu, updated docs --- compiler/rustc_codegen_llvm/src/llvm_util.rs | 8 +++++++- compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp | 11 +++++++++-- src/doc/rustc/src/codegen-options/index.md | 3 ++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index 46692fd5e8bcf..71fe8d36132f4 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -329,7 +329,13 @@ pub(crate) fn print(req: PrintRequest, sess: &Session) { require_inited(); let tm = create_informational_target_machine(sess); match req { - PrintRequest::TargetCPUs => unsafe { llvm::LLVMRustPrintTargetCPUs(tm) }, + PrintRequest::TargetCPUs => { + println!( + "Default CPU for this target:\n {}", + handle_native(sess.target.cpu.as_ref()) + ); + unsafe { llvm::LLVMRustPrintTargetCPUs(tm, handle_native(sess.target.cpu.as_ref())) }; + } PrintRequest::TargetFeatures => print_target_features(sess, tm), _ => bug!("rustc_codegen_llvm can't handle print request: {:?}", req), } diff --git a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp index 1acdc95ca8d2d..b65780342755c 100644 --- a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp @@ -307,7 +307,7 @@ static size_t getLongestEntryLength(ArrayRef Table) { return MaxLen; } -extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM) { +extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM, &Char[]) { const TargetMachine *Target = unwrap(TM); const MCSubtargetInfo *MCInfo = Target->getMCSubtargetInfo(); const Triple::ArchType HostArch = Triple(sys::getDefaultTargetTriple()).getArch(); @@ -324,7 +324,14 @@ extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM) { MaxCPULen, "native", (int)HostCPU.size(), HostCPU.data()); } for (auto &CPU : CPUTable) - printf(" %-*s\n", MaxCPULen, CPU.Key); + + printf(" %-*s", MaxCPULen, CPU.Key); + if (CPU.Key == Target->getTargetTriple().getArch()) { + printf(" default target\n"); + } + else { + printf("\n"); + } printf("\n"); } diff --git a/src/doc/rustc/src/codegen-options/index.md b/src/doc/rustc/src/codegen-options/index.md index d7c6a884fc8fb..e2b859e705df0 100644 --- a/src/doc/rustc/src/codegen-options/index.md +++ b/src/doc/rustc/src/codegen-options/index.md @@ -574,7 +574,8 @@ change in the future. This instructs `rustc` to generate code specifically for a particular processor. You can run `rustc --print target-cpus` to see the valid options to pass -here. Each target has a default base CPU. Special values include: +and the default target CPU for the current buid target. +Each target has a default base CPU. Special values include: * `native` can be passed to use the processor of the host machine. * `generic` refers to an LLVM target with minimal features but modern tuning. From 9aa596a014d2582c1c51166953bd3fd85c71cca8 Mon Sep 17 00:00:00 2001 From: James Dietz Date: Fri, 28 Apr 2023 17:23:40 -0400 Subject: [PATCH 2/4] moved default CPU message inline --- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 2 +- compiler/rustc_codegen_llvm/src/llvm_util.rs | 8 +++----- compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp | 16 +++++++--------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index c95148013eb74..53d97f3520106 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -2249,7 +2249,7 @@ extern "C" { pub fn LLVMRustHasFeature(T: &TargetMachine, s: *const c_char) -> bool; - pub fn LLVMRustPrintTargetCPUs(T: &TargetMachine); + pub fn LLVMRustPrintTargetCPUs(T: &TargetMachine, cpu: *const c_char); pub fn LLVMRustGetTargetFeaturesCount(T: &TargetMachine) -> size_t; pub fn LLVMRustGetTargetFeature( T: &TargetMachine, diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index 71fe8d36132f4..1baef931ff9b7 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -330,11 +330,9 @@ pub(crate) fn print(req: PrintRequest, sess: &Session) { let tm = create_informational_target_machine(sess); match req { PrintRequest::TargetCPUs => { - println!( - "Default CPU for this target:\n {}", - handle_native(sess.target.cpu.as_ref()) - ); - unsafe { llvm::LLVMRustPrintTargetCPUs(tm, handle_native(sess.target.cpu.as_ref())) }; + let cpu_cstring = CString::new(handle_native(sess.target.cpu.as_ref())) + .expect("failed to convert to cstring"); + unsafe { llvm::LLVMRustPrintTargetCPUs(tm, cpu_cstring.as_ptr()) }; } PrintRequest::TargetFeatures => print_target_features(sess, tm), _ => bug!("rustc_codegen_llvm can't handle print request: {:?}", req), diff --git a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp index b65780342755c..03e76380c246c 100644 --- a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp @@ -307,7 +307,7 @@ static size_t getLongestEntryLength(ArrayRef Table) { return MaxLen; } -extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM, &Char[]) { +extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM, const char* TargetCPU) { const TargetMachine *Target = unwrap(TM); const MCSubtargetInfo *MCInfo = Target->getMCSubtargetInfo(); const Triple::ArchType HostArch = Triple(sys::getDefaultTargetTriple()).getArch(); @@ -323,16 +323,14 @@ extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM, &Char[]) { printf(" %-*s - Select the CPU of the current host (currently %.*s).\n", MaxCPULen, "native", (int)HostCPU.size(), HostCPU.data()); } - for (auto &CPU : CPUTable) - + for (auto &CPU : CPUTable) { printf(" %-*s", MaxCPULen, CPU.Key); - if (CPU.Key == Target->getTargetTriple().getArch()) { - printf(" default target\n"); - } - else { - printf("\n"); + // Compare cpu against current target to label the default + if (strcmp(CPU.Key, TargetCPU) == 0) { + printf(" - this is the default target cpu for the current target"); } - printf("\n"); + printf("\n"); + } } extern "C" size_t LLVMRustGetTargetFeaturesCount(LLVMTargetMachineRef TM) { From cb74cd524f2798a098943425c271d9eada8d901e Mon Sep 17 00:00:00 2001 From: James Dietz Date: Sun, 30 Apr 2023 09:47:29 -0400 Subject: [PATCH 3/4] change expect() to unwrap_or_else() and update msg --- compiler/rustc_codegen_llvm/src/llvm_util.rs | 2 +- compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index 1baef931ff9b7..a5d4ca30fab11 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -331,7 +331,7 @@ pub(crate) fn print(req: PrintRequest, sess: &Session) { match req { PrintRequest::TargetCPUs => { let cpu_cstring = CString::new(handle_native(sess.target.cpu.as_ref())) - .expect("failed to convert to cstring"); + .unwrap_or_else(|e| bug!("failed to convert to cstring: {}", e)); unsafe { llvm::LLVMRustPrintTargetCPUs(tm, cpu_cstring.as_ptr()) }; } PrintRequest::TargetFeatures => print_target_features(sess, tm), diff --git a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp index 03e76380c246c..e88a3cdf620d1 100644 --- a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp @@ -324,10 +324,14 @@ extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM, const char* Tar MaxCPULen, "native", (int)HostCPU.size(), HostCPU.data()); } for (auto &CPU : CPUTable) { - printf(" %-*s", MaxCPULen, CPU.Key); // Compare cpu against current target to label the default if (strcmp(CPU.Key, TargetCPU) == 0) { - printf(" - this is the default target cpu for the current target"); + printf(" %-*s - This is the default target CPU" + " for the current build target (currently %s).", + MaxCPULen, CPU.Key, Target->getTargetTriple().str().c_str()); + } + else { + printf(" %-*s", MaxCPULen, CPU.Key); } printf("\n"); } From f239cd6a35776cbc91400a9e498d8b7487fe4975 Mon Sep 17 00:00:00 2001 From: James Dietz Date: Thu, 4 May 2023 20:54:17 -0400 Subject: [PATCH 4/4] added SAFETY comment --- compiler/rustc_codegen_llvm/src/llvm_util.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index a5d4ca30fab11..2fbdab9f8ce03 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -330,6 +330,9 @@ pub(crate) fn print(req: PrintRequest, sess: &Session) { let tm = create_informational_target_machine(sess); match req { PrintRequest::TargetCPUs => { + // SAFETY generate a C compatible string from a byte slice to pass + // the target CPU name into LLVM, the lifetime of the reference is + // at least as long as the C function let cpu_cstring = CString::new(handle_native(sess.target.cpu.as_ref())) .unwrap_or_else(|e| bug!("failed to convert to cstring: {}", e)); unsafe { llvm::LLVMRustPrintTargetCPUs(tm, cpu_cstring.as_ptr()) };