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](https://github.com/rust-lang/rust/issues/111603#issuecomment-1567531178) `@caojoshua`
This commit is contained in:
bors 2023-06-27 15:01:56 +00:00
commit 3c554f5cb4
2 changed files with 18 additions and 14 deletions

View File

@ -572,8 +572,6 @@ fn write_operand_repeatedly(
) { ) {
let zero = self.const_usize(0); let zero = self.const_usize(0);
let count = self.const_usize(count); let count = self.const_usize(count);
let start = dest.project_index(self, zero).llval;
let end = dest.project_index(self, count).llval;
let header_bb = self.append_sibling_block("repeat_loop_header"); let header_bb = self.append_sibling_block("repeat_loop_header");
let body_bb = self.append_sibling_block("repeat_loop_body"); let body_bb = self.append_sibling_block("repeat_loop_body");
@ -582,24 +580,18 @@ fn write_operand_repeatedly(
self.br(header_bb); self.br(header_bb);
let mut header_bx = Self::build(self.cx, header_bb); let mut header_bx = Self::build(self.cx, header_bb);
let current = header_bx.phi(self.val_ty(start), &[start], &[self.llbb()]); let i = header_bx.phi(self.val_ty(zero), &[zero], &[self.llbb()]);
let keep_going = header_bx.icmp(IntPredicate::IntNE, current, end); let keep_going = header_bx.icmp(IntPredicate::IntULT, i, count);
header_bx.cond_br(keep_going, body_bb, next_bb); header_bx.cond_br(keep_going, body_bb, next_bb);
let mut body_bx = Self::build(self.cx, body_bb); let mut body_bx = Self::build(self.cx, body_bb);
let align = dest.align.restrict_for_offset(dest.layout.field(self.cx(), 0).size); let dest_elem = dest.project_index(&mut body_bx, i);
cg_elem cg_elem.val.store(&mut body_bx, dest_elem);
.val
.store(&mut body_bx, PlaceRef::new_sized_aligned(current, cg_elem.layout, align));
let next = body_bx.inbounds_gep( let next = body_bx.unchecked_uadd(i, self.const_usize(1));
self.backend_type(cg_elem.layout),
current,
&[self.const_usize(1)],
);
body_bx.br(header_bb); body_bx.br(header_bb);
header_bx.add_incoming_to_phi(current, next, body_bb); header_bx.add_incoming_to_phi(i, next, body_bb);
*self = Self::build(self.cx, next_bb); *self = Self::build(self.cx, next_bb);
} }

View File

@ -5,6 +5,18 @@
use std::sync::Arc; use std::sync::Arc;
// CHECK-LABEL: @new_from_array
#[no_mangle]
pub fn new_from_array(x: u64) -> Arc<[u64]> {
// Ensure that we only generate one alloca for the array.
// CHECK: alloca
// CHECK-SAME: [1000 x i64]
// CHECK-NOT: alloca
let array = [x; 1000];
Arc::new(array)
}
// CHECK-LABEL: @new_uninit // CHECK-LABEL: @new_uninit
#[no_mangle] #[no_mangle]
pub fn new_uninit(x: u64) -> Arc<[u64; 1000]> { pub fn new_uninit(x: u64) -> Arc<[u64; 1000]> {