Skip to content

Commit 0fcaf11

Browse files
committed
rustc_codegen_ssa: append blocks to functions w/o creating a builder.
1 parent 402e9ef commit 0fcaf11

File tree

7 files changed

+54
-38
lines changed

7 files changed

+54
-38
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

+25-21
Original file line numberDiff line numberDiff line change
@@ -118,24 +118,16 @@ macro_rules! builder_methods_for_value_instructions {
118118
}
119119

120120
impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
121-
fn new_block<'b>(cx: &'a CodegenCx<'ll, 'tcx>, llfn: &'ll Value, name: &'b str) -> Self {
122-
let mut bx = Builder::with_cx(cx);
123-
let llbb = unsafe {
124-
let name = SmallCStr::new(name);
125-
llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, name.as_ptr())
126-
};
127-
bx.position_at_end(llbb);
121+
fn build(cx: &'a CodegenCx<'ll, 'tcx>, llbb: &'ll BasicBlock) -> Self {
122+
let bx = Builder::with_cx(cx);
123+
unsafe {
124+
llvm::LLVMPositionBuilderAtEnd(bx.llbuilder, llbb);
125+
}
128126
bx
129127
}
130128

131-
fn with_cx(cx: &'a CodegenCx<'ll, 'tcx>) -> Self {
132-
// Create a fresh builder from the crate context.
133-
let llbuilder = unsafe { llvm::LLVMCreateBuilderInContext(cx.llcx) };
134-
Builder { llbuilder, cx }
135-
}
136-
137-
fn build_sibling_block(&self, name: &str) -> Self {
138-
Builder::new_block(self.cx, self.llfn(), name)
129+
fn cx(&self) -> &CodegenCx<'ll, 'tcx> {
130+
self.cx
139131
}
140132

141133
fn llbb(&self) -> &'ll BasicBlock {
@@ -144,12 +136,22 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
144136

145137
fn set_span(&mut self, _span: Span) {}
146138

147-
fn position_at_end(&mut self, llbb: &'ll BasicBlock) {
139+
fn append_block(cx: &'a CodegenCx<'ll, 'tcx>, llfn: &'ll Value, name: &str) -> &'ll BasicBlock {
148140
unsafe {
149-
llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb);
141+
let name = SmallCStr::new(name);
142+
llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, name.as_ptr())
150143
}
151144
}
152145

146+
fn append_sibling_block(&mut self, name: &str) -> &'ll BasicBlock {
147+
Self::append_block(self.cx, self.llfn(), name)
148+
}
149+
150+
fn build_sibling_block(&mut self, name: &str) -> Self {
151+
let llbb = self.append_sibling_block(name);
152+
Self::build(self.cx, llbb)
153+
}
154+
153155
fn ret_void(&mut self) {
154156
unsafe {
155157
llvm::LLVMBuildRetVoid(self.llbuilder);
@@ -1144,10 +1146,6 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
11441146
unsafe { llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, UNNAMED) }
11451147
}
11461148

1147-
fn cx(&self) -> &CodegenCx<'ll, 'tcx> {
1148-
self.cx
1149-
}
1150-
11511149
fn do_not_inline(&mut self, llret: &'ll Value) {
11521150
llvm::Attribute::NoInline.apply_callsite(llvm::AttributePlace::Function, llret);
11531151
}
@@ -1161,6 +1159,12 @@ impl StaticBuilderMethods for Builder<'a, 'll, 'tcx> {
11611159
}
11621160

11631161
impl Builder<'a, 'll, 'tcx> {
1162+
fn with_cx(cx: &'a CodegenCx<'ll, 'tcx>) -> Self {
1163+
// Create a fresh builder from the crate context.
1164+
let llbuilder = unsafe { llvm::LLVMCreateBuilderInContext(cx.llcx) };
1165+
Builder { llbuilder, cx }
1166+
}
1167+
11641168
pub fn llfn(&self) -> &'ll Value {
11651169
unsafe { llvm::LLVMGetBasicBlockParent(self.llbb()) }
11661170
}

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ fn declare_unused_fn(cx: &CodegenCx<'ll, 'tcx>, def_id: &DefId) -> Instance<'tcx
223223

224224
fn codegen_unused_fn_and_counter(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) {
225225
let llfn = cx.get_fn(instance);
226-
let mut bx = Builder::new_block(cx, llfn, "unused_function");
226+
let llbb = Builder::append_block(cx, llfn, "unused_function");
227+
let mut bx = Builder::build(cx, llbb);
227228
let fn_name = bx.get_pgo_func_name_var(instance);
228229
let hash = bx.const_u64(0);
229230
let num_counters = bx.const_u32(1);

compiler/rustc_codegen_llvm/src/intrinsic.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,8 @@ fn gen_fn<'ll, 'tcx>(
678678
cx.apply_target_cpu_attr(llfn);
679679
// FIXME(eddyb) find a nicer way to do this.
680680
unsafe { llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::InternalLinkage) };
681-
let bx = Builder::new_block(cx, llfn, "entry-block");
681+
let llbb = Builder::append_block(cx, llfn, "entry-block");
682+
let bx = Builder::build(cx, llbb);
682683
codegen(bx);
683684
llfn
684685
}

compiler/rustc_codegen_ssa/src/base.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,8 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
409409
cx.set_frame_pointer_elimination(llfn);
410410
cx.apply_target_cpu_attr(llfn);
411411

412-
let mut bx = Bx::new_block(&cx, llfn, "top");
412+
let llbb = Bx::append_block(&cx, llfn, "top");
413+
let mut bx = Bx::build(&cx, llbb);
413414

414415
bx.insert_reference_to_gdb_debug_scripts_section_global();
415416

compiler/rustc_codegen_ssa/src/mir/block.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
386386

387387
// Create the failure block and the conditional branch to it.
388388
let lltarget = helper.llblock(self, target);
389-
let panic_block = self.new_block("panic");
389+
let panic_block = bx.build_sibling_block("panic");
390390
if expected {
391391
bx.cond_br(cond, lltarget, panic_block.llbb());
392392
} else {
@@ -1289,8 +1289,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
12891289
})
12901290
}
12911291

1292-
pub fn new_block(&self, name: &str) -> Bx {
1293-
Bx::new_block(self.cx, self.llfn, name)
1292+
// FIXME(eddyb) replace with `build_sibling_block`/`append_sibling_block`
1293+
// (which requires having a `Bx` already, and not all callers do).
1294+
fn new_block(&self, name: &str) -> Bx {
1295+
let llbb = Bx::append_block(self.cx, self.llfn, name);
1296+
Bx::build(self.cx, llbb)
12941297
}
12951298

12961299
/// Get the backend `BasicBlock` for a MIR `BasicBlock`, either already
@@ -1300,17 +1303,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13001303
pub fn llbb(&mut self, bb: mir::BasicBlock) -> Bx::BasicBlock {
13011304
self.cached_llbbs[bb].unwrap_or_else(|| {
13021305
// FIXME(eddyb) only name the block if `fewer_names` is `false`.
1303-
// FIXME(eddyb) create the block directly, without a builder.
1304-
let llbb = self.new_block(&format!("{:?}", bb)).llbb();
1306+
let llbb = Bx::append_block(self.cx, self.llfn, &format!("{:?}", bb));
13051307
self.cached_llbbs[bb] = Some(llbb);
13061308
llbb
13071309
})
13081310
}
13091311

13101312
pub fn build_block(&mut self, bb: mir::BasicBlock) -> Bx {
1311-
let mut bx = Bx::with_cx(self.cx);
1312-
bx.position_at_end(self.llbb(bb));
1313-
bx
1313+
let llbb = self.llbb(bb);
1314+
Bx::build(self.cx, llbb)
13141315
}
13151316

13161317
fn make_return_dest(

compiler/rustc_codegen_ssa/src/mir/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
144144

145145
let debug_context = cx.create_function_debug_context(instance, &fn_abi, llfn, &mir);
146146

147-
let mut bx = Bx::new_block(cx, llfn, "start");
147+
let start_llbb = Bx::append_block(cx, llfn, "start");
148+
let mut bx = Bx::build(cx, start_llbb);
148149

149150
if mir.basic_blocks().iter().any(|bb| bb.is_cleanup) {
150151
bx.set_personality_fn(cx.eh_personality());
@@ -159,7 +160,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
159160
.indices()
160161
.map(|bb| {
161162
if bb == mir::START_BLOCK && !reentrant_start_block {
162-
Some(bx.llbb())
163+
Some(start_llbb)
163164
} else {
164165
None
165166
}

compiler/rustc_codegen_ssa/src/traits/builder.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,21 @@ pub trait BuilderMethods<'a, 'tcx>:
4040
+ HasParamEnv<'tcx>
4141
+ HasTargetSpec
4242
{
43-
fn new_block<'b>(cx: &'a Self::CodegenCx, llfn: Self::Function, name: &'b str) -> Self;
44-
fn with_cx(cx: &'a Self::CodegenCx) -> Self;
45-
fn build_sibling_block(&self, name: &str) -> Self;
43+
fn build(cx: &'a Self::CodegenCx, llbb: Self::BasicBlock) -> Self;
44+
4645
fn cx(&self) -> &Self::CodegenCx;
4746
fn llbb(&self) -> Self::BasicBlock;
47+
4848
fn set_span(&mut self, span: Span);
4949

50-
fn position_at_end(&mut self, llbb: Self::BasicBlock);
50+
// FIXME(eddyb) replace uses of this with `append_sibling_block`.
51+
fn append_block(cx: &'a Self::CodegenCx, llfn: Self::Function, name: &str) -> Self::BasicBlock;
52+
53+
fn append_sibling_block(&mut self, name: &str) -> Self::BasicBlock;
54+
55+
// FIXME(eddyb) replace with callers using `append_sibling_block`.
56+
fn build_sibling_block(&mut self, name: &str) -> Self;
57+
5158
fn ret_void(&mut self);
5259
fn ret(&mut self, v: Self::Value);
5360
fn br(&mut self, dest: Self::BasicBlock);

0 commit comments

Comments
 (0)