Remove PrintBackendInfo trait

It is only implemented for a single type. Directly passing this type is
simpler and avoids overhead from indirect calls.
This commit is contained in:
bjorn3 2024-03-30 13:19:58 +00:00
parent e9ea578147
commit 7f445329ec
4 changed files with 31 additions and 47 deletions

View File

@ -274,10 +274,11 @@ impl CodegenBackend for LlvmCodegenBackend {
|tcx, ()| llvm_util::global_llvm_features(tcx.sess, true) |tcx, ()| llvm_util::global_llvm_features(tcx.sess, true)
} }
fn print(&self, req: &PrintRequest, out: &mut dyn PrintBackendInfo, sess: &Session) { fn print(&self, req: &PrintRequest, out: &mut String, sess: &Session) {
use std::fmt::Write;
match req.kind { match req.kind {
PrintKind::RelocationModels => { PrintKind::RelocationModels => {
writeln!(out, "Available relocation models:"); writeln!(out, "Available relocation models:").unwrap();
for name in &[ for name in &[
"static", "static",
"pic", "pic",
@ -288,25 +289,25 @@ impl CodegenBackend for LlvmCodegenBackend {
"ropi-rwpi", "ropi-rwpi",
"default", "default",
] { ] {
writeln!(out, " {name}"); writeln!(out, " {name}").unwrap();
} }
writeln!(out); writeln!(out).unwrap();
} }
PrintKind::CodeModels => { PrintKind::CodeModels => {
writeln!(out, "Available code models:"); writeln!(out, "Available code models:").unwrap();
for name in &["tiny", "small", "kernel", "medium", "large"] { for name in &["tiny", "small", "kernel", "medium", "large"] {
writeln!(out, " {name}"); writeln!(out, " {name}").unwrap();
} }
writeln!(out); writeln!(out).unwrap();
} }
PrintKind::TlsModels => { PrintKind::TlsModels => {
writeln!(out, "Available TLS models:"); writeln!(out, "Available TLS models:").unwrap();
for name in for name in
&["global-dynamic", "local-dynamic", "initial-exec", "local-exec", "emulated"] &["global-dynamic", "local-dynamic", "initial-exec", "local-exec", "emulated"]
{ {
writeln!(out, " {name}"); writeln!(out, " {name}").unwrap();
} }
writeln!(out); writeln!(out).unwrap();
} }
PrintKind::StackProtectorStrategies => { PrintKind::StackProtectorStrategies => {
writeln!( writeln!(
@ -332,7 +333,8 @@ impl CodegenBackend for LlvmCodegenBackend {
none none
Do not generate stack canaries. Do not generate stack canaries.
"# "#
); )
.unwrap();
} }
_other => llvm_util::print(req, out, sess), _other => llvm_util::print(req, out, sess),
} }

View File

@ -6,7 +6,6 @@ use crate::errors::{
use crate::llvm; use crate::llvm;
use libc::c_int; use libc::c_int;
use rustc_codegen_ssa::base::wants_wasm_eh; use rustc_codegen_ssa::base::wants_wasm_eh;
use rustc_codegen_ssa::traits::PrintBackendInfo;
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::small_c_str::SmallCStr; use rustc_data_structures::small_c_str::SmallCStr;
use rustc_fs_util::path_to_c_string; use rustc_fs_util::path_to_c_string;
@ -18,6 +17,7 @@ use rustc_target::spec::{MergeFunctions, PanicStrategy};
use rustc_target::target_features::RUSTC_SPECIFIC_FEATURES; use rustc_target::target_features::RUSTC_SPECIFIC_FEATURES;
use std::ffi::{c_char, c_void, CStr, CString}; use std::ffi::{c_char, c_void, CStr, CString};
use std::fmt::Write;
use std::path::Path; use std::path::Path;
use std::ptr; use std::ptr;
use std::slice; use std::slice;
@ -372,7 +372,7 @@ fn llvm_target_features(tm: &llvm::TargetMachine) -> Vec<(&str, &str)> {
ret ret
} }
fn print_target_features(out: &mut dyn PrintBackendInfo, sess: &Session, tm: &llvm::TargetMachine) { fn print_target_features(out: &mut String, sess: &Session, tm: &llvm::TargetMachine) {
let mut llvm_target_features = llvm_target_features(tm); let mut llvm_target_features = llvm_target_features(tm);
let mut known_llvm_target_features = FxHashSet::<&'static str>::default(); let mut known_llvm_target_features = FxHashSet::<&'static str>::default();
let mut rustc_target_features = sess let mut rustc_target_features = sess
@ -412,24 +412,26 @@ fn print_target_features(out: &mut dyn PrintBackendInfo, sess: &Session, tm: &ll
.max() .max()
.unwrap_or(0); .unwrap_or(0);
writeln!(out, "Features supported by rustc for this target:"); writeln!(out, "Features supported by rustc for this target:").unwrap();
for (feature, desc) in &rustc_target_features { for (feature, desc) in &rustc_target_features {
writeln!(out, " {feature:max_feature_len$} - {desc}."); writeln!(out, " {feature:max_feature_len$} - {desc}.").unwrap();
} }
writeln!(out, "\nCode-generation features supported by LLVM for this target:"); writeln!(out, "\nCode-generation features supported by LLVM for this target:").unwrap();
for (feature, desc) in &llvm_target_features { for (feature, desc) in &llvm_target_features {
writeln!(out, " {feature:max_feature_len$} - {desc}."); writeln!(out, " {feature:max_feature_len$} - {desc}.").unwrap();
} }
if llvm_target_features.is_empty() { if llvm_target_features.is_empty() {
writeln!(out, " Target features listing is not supported by this LLVM version."); writeln!(out, " Target features listing is not supported by this LLVM version.")
.unwrap();
} }
writeln!(out, "\nUse +feature to enable a feature, or -feature to disable it."); writeln!(out, "\nUse +feature to enable a feature, or -feature to disable it.").unwrap();
writeln!(out, "For example, rustc -C target-cpu=mycpu -C target-feature=+feature1,-feature2\n"); writeln!(out, "For example, rustc -C target-cpu=mycpu -C target-feature=+feature1,-feature2\n")
writeln!(out, "Code-generation features cannot be used in cfg or #[target_feature],"); .unwrap();
writeln!(out, "and may be renamed or removed in a future version of LLVM or rustc.\n"); writeln!(out, "Code-generation features cannot be used in cfg or #[target_feature],").unwrap();
writeln!(out, "and may be renamed or removed in a future version of LLVM or rustc.\n").unwrap();
} }
pub(crate) fn print(req: &PrintRequest, mut out: &mut dyn PrintBackendInfo, sess: &Session) { pub(crate) fn print(req: &PrintRequest, mut out: &mut String, sess: &Session) {
require_inited(); require_inited();
let tm = create_informational_target_machine(sess); let tm = create_informational_target_machine(sess);
match req.kind { match req.kind {
@ -440,9 +442,9 @@ pub(crate) fn print(req: &PrintRequest, mut out: &mut dyn PrintBackendInfo, sess
let cpu_cstring = CString::new(handle_native(sess.target.cpu.as_ref())) let cpu_cstring = CString::new(handle_native(sess.target.cpu.as_ref()))
.unwrap_or_else(|e| bug!("failed to convert to cstring: {}", e)); .unwrap_or_else(|e| bug!("failed to convert to cstring: {}", e));
unsafe extern "C" fn callback(out: *mut c_void, string: *const c_char, len: usize) { unsafe extern "C" fn callback(out: *mut c_void, string: *const c_char, len: usize) {
let out = &mut *(out as *mut &mut dyn PrintBackendInfo); let out = &mut *(out as *mut &mut String);
let bytes = slice::from_raw_parts(string as *const u8, len); let bytes = slice::from_raw_parts(string as *const u8, len);
write!(out, "{}", String::from_utf8_lossy(bytes)); write!(out, "{}", String::from_utf8_lossy(bytes)).unwrap();
} }
unsafe { unsafe {
llvm::LLVMRustPrintTargetCPUs( llvm::LLVMRustPrintTargetCPUs(

View File

@ -22,8 +22,6 @@ use rustc_session::{
use rustc_span::symbol::Symbol; use rustc_span::symbol::Symbol;
use rustc_target::abi::call::FnAbi; use rustc_target::abi::call::FnAbi;
use std::fmt;
pub trait BackendTypes { pub trait BackendTypes {
type Value: CodegenObject; type Value: CodegenObject;
type Function: CodegenObject; type Function: CodegenObject;
@ -62,7 +60,7 @@ pub trait CodegenBackend {
fn locale_resource(&self) -> &'static str; fn locale_resource(&self) -> &'static str;
fn init(&self, _sess: &Session) {} fn init(&self, _sess: &Session) {}
fn print(&self, _req: &PrintRequest, _out: &mut dyn PrintBackendInfo, _sess: &Session) {} fn print(&self, _req: &PrintRequest, _out: &mut String, _sess: &Session) {}
fn target_features(&self, _sess: &Session, _allow_unstable: bool) -> Vec<Symbol> { fn target_features(&self, _sess: &Session, _allow_unstable: bool) -> Vec<Symbol> {
vec![] vec![]
} }
@ -150,19 +148,3 @@ pub trait ExtraBackendMethods:
std::thread::Builder::new().name(name).spawn(f) std::thread::Builder::new().name(name).spawn(f)
} }
} }
pub trait PrintBackendInfo {
fn infallible_write_fmt(&mut self, args: fmt::Arguments<'_>);
}
impl PrintBackendInfo for String {
fn infallible_write_fmt(&mut self, args: fmt::Arguments<'_>) {
fmt::Write::write_fmt(self, args).unwrap();
}
}
impl dyn PrintBackendInfo + '_ {
pub fn write_fmt(&mut self, args: fmt::Arguments<'_>) {
self.infallible_write_fmt(args);
}
}

View File

@ -30,9 +30,7 @@ mod write;
pub use self::abi::AbiBuilderMethods; pub use self::abi::AbiBuilderMethods;
pub use self::asm::{AsmBuilderMethods, AsmMethods, GlobalAsmOperandRef, InlineAsmOperandRef}; pub use self::asm::{AsmBuilderMethods, AsmMethods, GlobalAsmOperandRef, InlineAsmOperandRef};
pub use self::backend::{ pub use self::backend::{Backend, BackendTypes, CodegenBackend, ExtraBackendMethods};
Backend, BackendTypes, CodegenBackend, ExtraBackendMethods, PrintBackendInfo,
};
pub use self::builder::{BuilderMethods, OverflowOp}; pub use self::builder::{BuilderMethods, OverflowOp};
pub use self::consts::ConstMethods; pub use self::consts::ConstMethods;
pub use self::coverageinfo::CoverageInfoBuilderMethods; pub use self::coverageinfo::CoverageInfoBuilderMethods;