Skip to content

Commit 3c554f5

Browse files
committed
Auto merge of #112516 - erikdesjardins:loop, r=davidtwco
cg_llvm: use index-based loop in write_operand_repeatedly This should be easier for LLVM to analyze. Fixes #111603 This needs a perf run. [cc](#111603 (comment)) `@caojoshua`
2 parents f42f19b + bd0aae9 commit 3c554f5

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

+7-15
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,6 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
572572
) {
573573
let zero = self.const_usize(0);
574574
let count = self.const_usize(count);
575-
let start = dest.project_index(self, zero).llval;
576-
let end = dest.project_index(self, count).llval;
577575

578576
let header_bb = self.append_sibling_block("repeat_loop_header");
579577
let body_bb = self.append_sibling_block("repeat_loop_body");
@@ -582,24 +580,18 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
582580
self.br(header_bb);
583581

584582
let mut header_bx = Self::build(self.cx, header_bb);
585-
let current = header_bx.phi(self.val_ty(start), &[start], &[self.llbb()]);
583+
let i = header_bx.phi(self.val_ty(zero), &[zero], &[self.llbb()]);
586584

587-
let keep_going = header_bx.icmp(IntPredicate::IntNE, current, end);
585+
let keep_going = header_bx.icmp(IntPredicate::IntULT, i, count);
588586
header_bx.cond_br(keep_going, body_bb, next_bb);
589587

590588
let mut body_bx = Self::build(self.cx, body_bb);
591-
let align = dest.align.restrict_for_offset(dest.layout.field(self.cx(), 0).size);
592-
cg_elem
593-
.val
594-
.store(&mut body_bx, PlaceRef::new_sized_aligned(current, cg_elem.layout, align));
595-
596-
let next = body_bx.inbounds_gep(
597-
self.backend_type(cg_elem.layout),
598-
current,
599-
&[self.const_usize(1)],
600-
);
589+
let dest_elem = dest.project_index(&mut body_bx, i);
590+
cg_elem.val.store(&mut body_bx, dest_elem);
591+
592+
let next = body_bx.unchecked_uadd(i, self.const_usize(1));
601593
body_bx.br(header_bb);
602-
header_bx.add_incoming_to_phi(current, next, body_bb);
594+
header_bx.add_incoming_to_phi(i, next, body_bb);
603595

604596
*self = Self::build(self.cx, next_bb);
605597
}

tests/codegen/issues/issue-111603.rs

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55

66
use std::sync::Arc;
77

8+
// CHECK-LABEL: @new_from_array
9+
#[no_mangle]
10+
pub fn new_from_array(x: u64) -> Arc<[u64]> {
11+
// Ensure that we only generate one alloca for the array.
12+
13+
// CHECK: alloca
14+
// CHECK-SAME: [1000 x i64]
15+
// CHECK-NOT: alloca
16+
let array = [x; 1000];
17+
Arc::new(array)
18+
}
19+
820
// CHECK-LABEL: @new_uninit
921
#[no_mangle]
1022
pub fn new_uninit(x: u64) -> Arc<[u64; 1000]> {

0 commit comments

Comments
 (0)