Skip to content

Commit 7f44532

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 e9ea578 commit 7f44532

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
@@ -274,10 +274,11 @@ impl CodegenBackend for LlvmCodegenBackend {
274274
|tcx, ()| llvm_util::global_llvm_features(tcx.sess, true)
275275
}
276276

277-
fn print(&self, req: &PrintRequest, out: &mut dyn PrintBackendInfo, sess: &Session) {
277+
fn print(&self, req: &PrintRequest, out: &mut String, sess: &Session) {
278+
use std::fmt::Write;
278279
match req.kind {
279280
PrintKind::RelocationModels => {
280-
writeln!(out, "Available relocation models:");
281+
writeln!(out, "Available relocation models:").unwrap();
281282
for name in &[
282283
"static",
283284
"pic",
@@ -288,25 +289,25 @@ impl CodegenBackend for LlvmCodegenBackend {
288289
"ropi-rwpi",
289290
"default",
290291
] {
291-
writeln!(out, " {name}");
292+
writeln!(out, " {name}").unwrap();
292293
}
293-
writeln!(out);
294+
writeln!(out).unwrap();
294295
}
295296
PrintKind::CodeModels => {
296-
writeln!(out, "Available code models:");
297+
writeln!(out, "Available code models:").unwrap();
297298
for name in &["tiny", "small", "kernel", "medium", "large"] {
298-
writeln!(out, " {name}");
299+
writeln!(out, " {name}").unwrap();
299300
}
300-
writeln!(out);
301+
writeln!(out).unwrap();
301302
}
302303
PrintKind::TlsModels => {
303-
writeln!(out, "Available TLS models:");
304+
writeln!(out, "Available TLS models:").unwrap();
304305
for name in
305306
&["global-dynamic", "local-dynamic", "initial-exec", "local-exec", "emulated"]
306307
{
307-
writeln!(out, " {name}");
308+
writeln!(out, " {name}").unwrap();
308309
}
309-
writeln!(out);
310+
writeln!(out).unwrap();
310311
}
311312
PrintKind::StackProtectorStrategies => {
312313
writeln!(
@@ -332,7 +333,8 @@ impl CodegenBackend for LlvmCodegenBackend {
332333
none
333334
Do not generate stack canaries.
334335
"#
335-
);
336+
)
337+
.unwrap();
336338
}
337339
_other => llvm_util::print(req, out, sess),
338340
}

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;
@@ -372,7 +372,7 @@ fn llvm_target_features(tm: &llvm::TargetMachine) -> Vec<(&str, &str)> {
372372
ret
373373
}
374374

375-
fn print_target_features(out: &mut dyn PrintBackendInfo, sess: &Session, tm: &llvm::TargetMachine) {
375+
fn print_target_features(out: &mut String, sess: &Session, tm: &llvm::TargetMachine) {
376376
let mut llvm_target_features = llvm_target_features(tm);
377377
let mut known_llvm_target_features = FxHashSet::<&'static str>::default();
378378
let mut rustc_target_features = sess
@@ -412,24 +412,26 @@ fn print_target_features(out: &mut dyn PrintBackendInfo, sess: &Session, tm: &ll
412412
.max()
413413
.unwrap_or(0);
414414

415-
writeln!(out, "Features supported by rustc for this target:");
415+
writeln!(out, "Features supported by rustc for this target:").unwrap();
416416
for (feature, desc) in &rustc_target_features {
417-
writeln!(out, " {feature:max_feature_len$} - {desc}.");
417+
writeln!(out, " {feature:max_feature_len$} - {desc}.").unwrap();
418418
}
419-
writeln!(out, "\nCode-generation features supported by LLVM for this target:");
419+
writeln!(out, "\nCode-generation features supported by LLVM for this target:").unwrap();
420420
for (feature, desc) in &llvm_target_features {
421-
writeln!(out, " {feature:max_feature_len$} - {desc}.");
421+
writeln!(out, " {feature:max_feature_len$} - {desc}.").unwrap();
422422
}
423423
if llvm_target_features.is_empty() {
424-
writeln!(out, " Target features listing is not supported by this LLVM version.");
424+
writeln!(out, " Target features listing is not supported by this LLVM version.")
425+
.unwrap();
425426
}
426-
writeln!(out, "\nUse +feature to enable a feature, or -feature to disable it.");
427-
writeln!(out, "For example, rustc -C target-cpu=mycpu -C target-feature=+feature1,-feature2\n");
428-
writeln!(out, "Code-generation features cannot be used in cfg or #[target_feature],");
429-
writeln!(out, "and may be renamed or removed in a future version of LLVM or rustc.\n");
427+
writeln!(out, "\nUse +feature to enable a feature, or -feature to disable it.").unwrap();
428+
writeln!(out, "For example, rustc -C target-cpu=mycpu -C target-feature=+feature1,-feature2\n")
429+
.unwrap();
430+
writeln!(out, "Code-generation features cannot be used in cfg or #[target_feature],").unwrap();
431+
writeln!(out, "and may be renamed or removed in a future version of LLVM or rustc.\n").unwrap();
430432
}
431433

432-
pub(crate) fn print(req: &PrintRequest, mut out: &mut dyn PrintBackendInfo, sess: &Session) {
434+
pub(crate) fn print(req: &PrintRequest, mut out: &mut String, sess: &Session) {
433435
require_inited();
434436
let tm = create_informational_target_machine(sess);
435437
match req.kind {
@@ -440,9 +442,9 @@ pub(crate) fn print(req: &PrintRequest, mut out: &mut dyn PrintBackendInfo, sess
440442
let cpu_cstring = CString::new(handle_native(sess.target.cpu.as_ref()))
441443
.unwrap_or_else(|e| bug!("failed to convert to cstring: {}", e));
442444
unsafe extern "C" fn callback(out: *mut c_void, string: *const c_char, len: usize) {
443-
let out = &mut *(out as *mut &mut dyn PrintBackendInfo);
445+
let out = &mut *(out as *mut &mut String);
444446
let bytes = slice::from_raw_parts(string as *const u8, len);
445-
write!(out, "{}", String::from_utf8_lossy(bytes));
447+
write!(out, "{}", String::from_utf8_lossy(bytes)).unwrap();
446448
}
447449
unsafe {
448450
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)