Skip to content

Commit 648d038

Browse files
committed
Auto merge of rust-lang#94123 - bjorn3:cg_ssa_singleton_builder, r=tmiasko
Partially move cg_ssa towards using a single builder Not all codegen backends can handle hopping between blocks well. For example Cranelift requires blocks to be terminated before switching to building a new block. Rust-gpu requires a `RefCell` to allow hopping between blocks and cg_gcc currently has a buggy implementation of hopping between blocks. This PR reduces the amount of cases where cg_ssa switches between blocks before they are finished and mostly fixes the block hopping in cg_gcc. (~~only `scalar_to_backend` doesn't handle it correctly yet in cg_gcc~~ fixed that one.) `@antoyo` please review the cg_gcc changes.
2 parents e855e2d + 18c34e4 commit 648d038

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

Diff for: src/builder.rs

+19-16
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,6 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
390390
bx
391391
}
392392

393-
fn build_sibling_block(&mut self, name: &str) -> Self {
394-
let block = self.append_sibling_block(name);
395-
Self::build(self.cx, block)
396-
}
397-
398393
fn llbb(&self) -> Block<'gcc> {
399394
self.block.expect("block")
400395
}
@@ -409,6 +404,11 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
409404
func.new_block(name)
410405
}
411406

407+
fn switch_to_block(&mut self, block: Self::BasicBlock) {
408+
*self.cx.current_block.borrow_mut() = Some(block);
409+
self.block = Some(block);
410+
}
411+
412412
fn ret_void(&mut self) {
413413
self.llbb().end_with_void_return(None)
414414
}
@@ -880,28 +880,31 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
880880
let start = dest.project_index(&mut self, zero).llval;
881881
let end = dest.project_index(&mut self, count).llval;
882882

883-
let mut header_bx = self.build_sibling_block("repeat_loop_header");
884-
let mut body_bx = self.build_sibling_block("repeat_loop_body");
885-
let next_bx = self.build_sibling_block("repeat_loop_next");
883+
let header_bb = self.append_sibling_block("repeat_loop_header");
884+
let body_bb = self.append_sibling_block("repeat_loop_body");
885+
let next_bb = self.append_sibling_block("repeat_loop_next");
886886

887887
let ptr_type = start.get_type();
888888
let current = self.llbb().get_function().new_local(None, ptr_type, "loop_var");
889889
let current_val = current.to_rvalue();
890890
self.assign(current, start);
891891

892-
self.br(header_bx.llbb());
892+
self.br(header_bb);
893893

894-
let keep_going = header_bx.icmp(IntPredicate::IntNE, current_val, end);
895-
header_bx.cond_br(keep_going, body_bx.llbb(), next_bx.llbb());
894+
self.switch_to_block(header_bb);
895+
let keep_going = self.icmp(IntPredicate::IntNE, current_val, end);
896+
self.cond_br(keep_going, body_bb, next_bb);
896897

898+
self.switch_to_block(body_bb);
897899
let align = dest.align.restrict_for_offset(dest.layout.field(self.cx(), 0).size);
898-
cg_elem.val.store(&mut body_bx, PlaceRef::new_sized_aligned(current_val, cg_elem.layout, align));
900+
cg_elem.val.store(&mut self, PlaceRef::new_sized_aligned(current_val, cg_elem.layout, align));
899901

900-
let next = body_bx.inbounds_gep(self.backend_type(cg_elem.layout), current.to_rvalue(), &[self.const_usize(1)]);
901-
body_bx.llbb().add_assignment(None, current, next);
902-
body_bx.br(header_bx.llbb());
902+
let next = self.inbounds_gep(self.backend_type(cg_elem.layout), current.to_rvalue(), &[self.const_usize(1)]);
903+
self.llbb().add_assignment(None, current, next);
904+
self.br(header_bb);
903905

904-
next_bx
906+
self.switch_to_block(next_bb);
907+
self
905908
}
906909

907910
fn range_metadata(&mut self, _load: RValue<'gcc>, _range: WrappingRange) {

0 commit comments

Comments
 (0)