Skip to content

Commit d2f4a4d

Browse files
committed
Remove PrintBackendInfo trait
It is only implemented for a single type. Directly passing this type is simpler and avoids overhead from indirect calls.
1 parent 9f69cf4 commit d2f4a4d

File tree

4 files changed

+31
-47
lines changed

4 files changed

+31
-47
lines changed

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,11 @@ impl CodegenBackend for LlvmCodegenBackend {
273273
|tcx, ()| llvm_util::global_llvm_features(tcx.sess, true)
274274
}
275275

276-
fn print(&self, req: &PrintRequest, out: &mut dyn PrintBackendInfo, sess: &Session) {
276+
fn print(&self, req: &PrintRequest, out: &mut String, sess: &Session) {
277+
use std::fmt::Write;
277278
match req.kind {
278279
PrintKind::RelocationModels => {
279-
writeln!(out, "Available relocation models:");
280+
writeln!(out, "Available relocation models:").unwrap();
280281
for name in &[
281282
"static",
282283
"pic",
@@ -287,25 +288,25 @@ impl CodegenBackend for LlvmCodegenBackend {
287288
"ropi-rwpi",
288289
"default",
289290
] {
290-
writeln!(out, " {name}");
291+
writeln!(out, " {name}").unwrap();
291292
}
292-
writeln!(out);
293+
writeln!(out).unwrap();
293294
}
294295
PrintKind::CodeModels => {
295-
writeln!(out, "Available code models:");
296+
writeln!(out, "Available code models:").unwrap();
296297
for name in &["tiny", "small", "kernel", "medium", "large"] {
297-
writeln!(out, " {name}");
298+
writeln!(out, " {name}").unwrap();
298299
}
299-
writeln!(out);
300+
writeln!(out).unwrap();
300301
}
301302
PrintKind::TlsModels => {
302-
writeln!(out, "Available TLS models:");
303+
writeln!(out, "Available TLS models:").unwrap();
303304
for name in
304305
&["global-dynamic", "local-dynamic", "initial-exec", "local-exec", "emulated"]
305306
{
306-
writeln!(out, " {name}");
307+
writeln!(out, " {name}").unwrap();
307308
}
308-
writeln!(out);
309+
writeln!(out).unwrap();
309310
}
310311
PrintKind::StackProtectorStrategies => {
311312
writeln!(
@@ -331,7 +332,8 @@ impl CodegenBackend for LlvmCodegenBackend {
331332
none
332333
Do not generate stack canaries.
333334
"#
334-
);
335+
)
336+
.unwrap();
335337
}
336338
_other => llvm_util::print(req, out, sess),
337339
}

compiler/rustc_codegen_llvm/src/llvm_util.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::errors::{
66
use crate::llvm;
77
use libc::c_int;
88
use rustc_codegen_ssa::base::wants_wasm_eh;
9-
use rustc_codegen_ssa::traits::PrintBackendInfo;
109
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1110
use rustc_data_structures::small_c_str::SmallCStr;
1211
use rustc_fs_util::path_to_c_string;
@@ -18,6 +17,7 @@ use rustc_target::spec::{MergeFunctions, PanicStrategy};
1817
use rustc_target::target_features::RUSTC_SPECIFIC_FEATURES;
1918

2019
use std::ffi::{c_char, c_void, CStr, CString};
20+
use std::fmt::Write;
2121
use std::path::Path;
2222
use std::ptr;
2323
use std::slice;
@@ -371,7 +371,7 @@ fn llvm_target_features(tm: &llvm::TargetMachine) -> Vec<(&str, &str)> {
371371
ret
372372
}
373373

374-
fn print_target_features(out: &mut dyn PrintBackendInfo, sess: &Session, tm: &llvm::TargetMachine) {
374+
fn print_target_features(out: &mut String, sess: &Session, tm: &llvm::TargetMachine) {
375375
let mut llvm_target_features = llvm_target_features(tm);
376376
let mut known_llvm_target_features = FxHashSet::<&'static str>::default();
377377
let mut rustc_target_features = sess
@@ -406,24 +406,26 @@ fn print_target_features(out: &mut dyn PrintBackendInfo, sess: &Session, tm: &ll
406406
.max()
407407
.unwrap_or(0);
408408

409-
writeln!(out, "Features supported by rustc for this target:");
409+
writeln!(out, "Features supported by rustc for this target:").unwrap();
410410
for (feature, desc) in &rustc_target_features {
411-
writeln!(out, " {feature:max_feature_len$} - {desc}.");
411+
writeln!(out, " {feature:max_feature_len$} - {desc}.").unwrap();
412412
}
413-
writeln!(out, "\nCode-generation features supported by LLVM for this target:");
413+
writeln!(out, "\nCode-generation features supported by LLVM for this target:").unwrap();
414414
for (feature, desc) in &llvm_target_features {
415-
writeln!(out, " {feature:max_feature_len$} - {desc}.");
415+
writeln!(out, " {feature:max_feature_len$} - {desc}.").unwrap();
416416
}
417417
if llvm_target_features.is_empty() {
418-
writeln!(out, " Target features listing is not supported by this LLVM version.");
418+
writeln!(out, " Target features listing is not supported by this LLVM version.")
419+
.unwrap();
419420
}
420-
writeln!(out, "\nUse +feature to enable a feature, or -feature to disable it.");
421-
writeln!(out, "For example, rustc -C target-cpu=mycpu -C target-feature=+feature1,-feature2\n");
422-
writeln!(out, "Code-generation features cannot be used in cfg or #[target_feature],");
423-
writeln!(out, "and may be renamed or removed in a future version of LLVM or rustc.\n");
421+
writeln!(out, "\nUse +feature to enable a feature, or -feature to disable it.").unwrap();
422+
writeln!(out, "For example, rustc -C target-cpu=mycpu -C target-feature=+feature1,-feature2\n")
423+
.unwrap();
424+
writeln!(out, "Code-generation features cannot be used in cfg or #[target_feature],").unwrap();
425+
writeln!(out, "and may be renamed or removed in a future version of LLVM or rustc.\n").unwrap();
424426
}
425427

426-
pub(crate) fn print(req: &PrintRequest, mut out: &mut dyn PrintBackendInfo, sess: &Session) {
428+
pub(crate) fn print(req: &PrintRequest, mut out: &mut String, sess: &Session) {
427429
require_inited();
428430
let tm = create_informational_target_machine(sess);
429431
match req.kind {
@@ -434,9 +436,9 @@ pub(crate) fn print(req: &PrintRequest, mut out: &mut dyn PrintBackendInfo, sess
434436
let cpu_cstring = CString::new(handle_native(sess.target.cpu.as_ref()))
435437
.unwrap_or_else(|e| bug!("failed to convert to cstring: {}", e));
436438
unsafe extern "C" fn callback(out: *mut c_void, string: *const c_char, len: usize) {
437-
let out = &mut *(out as *mut &mut dyn PrintBackendInfo);
439+
let out = &mut *(out as *mut &mut String);
438440
let bytes = slice::from_raw_parts(string as *const u8, len);
439-
write!(out, "{}", String::from_utf8_lossy(bytes));
441+
write!(out, "{}", String::from_utf8_lossy(bytes)).unwrap();
440442
}
441443
unsafe {
442444
llvm::LLVMRustPrintTargetCPUs(

compiler/rustc_codegen_ssa/src/traits/backend.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ use rustc_session::{
2222
use rustc_span::symbol::Symbol;
2323
use rustc_target::abi::call::FnAbi;
2424

25-
use std::fmt;
26-
2725
pub trait BackendTypes {
2826
type Value: CodegenObject;
2927
type Function: CodegenObject;
@@ -62,7 +60,7 @@ pub trait CodegenBackend {
6260
fn locale_resource(&self) -> &'static str;
6361

6462
fn init(&self, _sess: &Session) {}
65-
fn print(&self, _req: &PrintRequest, _out: &mut dyn PrintBackendInfo, _sess: &Session) {}
63+
fn print(&self, _req: &PrintRequest, _out: &mut String, _sess: &Session) {}
6664
fn target_features(&self, _sess: &Session, _allow_unstable: bool) -> Vec<Symbol> {
6765
vec![]
6866
}
@@ -150,19 +148,3 @@ pub trait ExtraBackendMethods:
150148
std::thread::Builder::new().name(name).spawn(f)
151149
}
152150
}
153-
154-
pub trait PrintBackendInfo {
155-
fn infallible_write_fmt(&mut self, args: fmt::Arguments<'_>);
156-
}
157-
158-
impl PrintBackendInfo for String {
159-
fn infallible_write_fmt(&mut self, args: fmt::Arguments<'_>) {
160-
fmt::Write::write_fmt(self, args).unwrap();
161-
}
162-
}
163-
164-
impl dyn PrintBackendInfo + '_ {
165-
pub fn write_fmt(&mut self, args: fmt::Arguments<'_>) {
166-
self.infallible_write_fmt(args);
167-
}
168-
}

compiler/rustc_codegen_ssa/src/traits/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ mod write;
3030

3131
pub use self::abi::AbiBuilderMethods;
3232
pub use self::asm::{AsmBuilderMethods, AsmMethods, GlobalAsmOperandRef, InlineAsmOperandRef};
33-
pub use self::backend::{
34-
Backend, BackendTypes, CodegenBackend, ExtraBackendMethods, PrintBackendInfo,
35-
};
33+
pub use self::backend::{Backend, BackendTypes, CodegenBackend, ExtraBackendMethods};
3634
pub use self::builder::{BuilderMethods, OverflowOp};
3735
pub use self::consts::ConstMethods;
3836
pub use self::coverageinfo::CoverageInfoBuilderMethods;

0 commit comments

Comments
 (0)