Skip to content

Commit cc5ecf7

Browse files
committed
Auto merge of rust-lang#122567 - erikdesjardins:noname, r=<try>
Avoid naming LLVM basic blocks when `fewer_names` is true This may be a small perf win. r? `@ghost`
2 parents c563f2e + 4831dce commit cc5ecf7

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

compiler/rustc_codegen_gcc/src/builder.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::borrow::Cow;
22
use std::cell::Cell;
33
use std::convert::TryFrom;
4+
use std::fmt::Display;
45
use std::ops::Deref;
56

67
use gccjit::{
@@ -526,14 +527,14 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
526527
self.block
527528
}
528529

529-
fn append_block(cx: &'a CodegenCx<'gcc, 'tcx>, func: RValue<'gcc>, name: &str) -> Block<'gcc> {
530+
fn append_block(cx: &'a CodegenCx<'gcc, 'tcx>, func: RValue<'gcc>, name: impl Display) -> Block<'gcc> {
530531
let func = cx.rvalue_as_function(func);
531-
func.new_block(name)
532+
func.new_block(name.to_string())
532533
}
533534

534-
fn append_sibling_block(&mut self, name: &str) -> Block<'gcc> {
535+
fn append_sibling_block(&mut self, name: impl Display) -> Block<'gcc> {
535536
let func = self.current_func();
536-
func.new_block(name)
537+
func.new_block(name.to_string())
537538
}
538539

539540
fn switch_to_block(&mut self, block: Self::BasicBlock) {

compiler/rustc_codegen_llvm/src/builder.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use rustc_target::abi::{self, call::FnAbi, Align, Size, WrappingRange};
2626
use rustc_target::spec::{HasTargetSpec, SanitizerSet, Target};
2727
use smallvec::SmallVec;
2828
use std::borrow::Cow;
29+
use std::fmt::Display;
2930
use std::iter;
3031
use std::ops::Deref;
3132
use std::ptr;
@@ -153,14 +154,24 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
153154

154155
fn set_span(&mut self, _span: Span) {}
155156

156-
fn append_block(cx: &'a CodegenCx<'ll, 'tcx>, llfn: &'ll Value, name: &str) -> &'ll BasicBlock {
157+
fn append_block(
158+
cx: &'a CodegenCx<'ll, 'tcx>,
159+
llfn: &'ll Value,
160+
name: impl Display,
161+
) -> &'ll BasicBlock {
157162
unsafe {
158-
let name = SmallCStr::new(name);
159-
llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, name.as_ptr())
163+
let c_str_name;
164+
let name_ptr = if cx.tcx.sess.fewer_names() {
165+
const { c"".as_ptr().cast() }
166+
} else {
167+
c_str_name = SmallCStr::new(&name.to_string());
168+
c_str_name.as_ptr()
169+
};
170+
llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, name_ptr)
160171
}
161172
}
162173

163-
fn append_sibling_block(&mut self, name: &str) -> &'ll BasicBlock {
174+
fn append_sibling_block(&mut self, name: impl Display) -> &'ll BasicBlock {
164175
Self::append_block(self.cx, self.llfn(), name)
165176
}
166177

compiler/rustc_codegen_llvm/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![feature(exact_size_is_empty)]
1212
#![feature(extern_types)]
1313
#![feature(hash_raw_entry)]
14+
#![feature(inline_const)]
1415
#![feature(iter_intersperse)]
1516
#![feature(let_chains)]
1617
#![feature(impl_trait_in_assoc_type)]

compiler/rustc_codegen_ssa/src/mir/block.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
8383
// Cross-funclet jump - need a trampoline
8484
debug_assert!(base::wants_new_eh_instructions(fx.cx.tcx().sess));
8585
debug!("llbb_with_cleanup: creating cleanup trampoline for {:?}", target);
86-
let name = &format!("{:?}_cleanup_trampoline_{:?}", self.bb, target);
87-
let trampoline_llbb = Bx::append_block(fx.cx, fx.llfn, name);
86+
let trampoline_llbb = Bx::append_block(
87+
fx.cx,
88+
fx.llfn,
89+
format_args!("{:?}_cleanup_trampoline_{:?}", self.bb, target),
90+
);
8891
let mut trampoline_bx = Bx::build(fx.cx, trampoline_llbb);
8992
trampoline_bx.cleanup_ret(self.funclet(fx).unwrap(), Some(lltarget));
9093
trampoline_llbb
@@ -1565,7 +1568,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
15651568
fn landing_pad_for_uncached(&mut self, bb: mir::BasicBlock) -> Bx::BasicBlock {
15661569
let llbb = self.llbb(bb);
15671570
if base::wants_new_eh_instructions(self.cx.sess()) {
1568-
let cleanup_bb = Bx::append_block(self.cx, self.llfn, &format!("funclet_{bb:?}"));
1571+
let cleanup_bb = Bx::append_block(self.cx, self.llfn, format_args!("funclet_{bb:?}"));
15691572
let mut cleanup_bx = Bx::build(self.cx, cleanup_bb);
15701573
let funclet = cleanup_bx.cleanup_pad(None, &[]);
15711574
cleanup_bx.br(llbb);
@@ -1688,8 +1691,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
16881691
pub fn try_llbb(&mut self, bb: mir::BasicBlock) -> Option<Bx::BasicBlock> {
16891692
match self.cached_llbbs[bb] {
16901693
CachedLlbb::None => {
1691-
// FIXME(eddyb) only name the block if `fewer_names` is `false`.
1692-
let llbb = Bx::append_block(self.cx, self.llfn, &format!("{bb:?}"));
1694+
let llbb = Bx::append_block(self.cx, self.llfn, format_args!("{bb:?}"));
16931695
self.cached_llbbs[bb] = CachedLlbb::Some(llbb);
16941696
Some(llbb)
16951697
}

compiler/rustc_codegen_ssa/src/traits/builder.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use rustc_target::abi::call::FnAbi;
2222
use rustc_target::abi::{Abi, Align, Scalar, Size, WrappingRange};
2323
use rustc_target::spec::HasTargetSpec;
2424

25+
use std::fmt::Display;
26+
2527
#[derive(Copy, Clone)]
2628
pub enum OverflowOp {
2729
Add,
@@ -49,9 +51,13 @@ pub trait BuilderMethods<'a, 'tcx>:
4951
fn set_span(&mut self, span: Span);
5052

5153
// FIXME(eddyb) replace uses of this with `append_sibling_block`.
52-
fn append_block(cx: &'a Self::CodegenCx, llfn: Self::Function, name: &str) -> Self::BasicBlock;
54+
fn append_block(
55+
cx: &'a Self::CodegenCx,
56+
llfn: Self::Function,
57+
name: impl Display,
58+
) -> Self::BasicBlock;
5359

54-
fn append_sibling_block(&mut self, name: &str) -> Self::BasicBlock;
60+
fn append_sibling_block(&mut self, name: impl Display) -> Self::BasicBlock;
5561

5662
fn switch_to_block(&mut self, llbb: Self::BasicBlock);
5763

0 commit comments

Comments
 (0)