Skip to content

Commit

Permalink
Auto merge of rust-lang#130998 - the8472:bail-before-memcpy, r=<try>
Browse files Browse the repository at this point in the history
avoid phi node for pointers flowing into Vec appends

related discussion: https://rust-lang.zulipchat.com/#narrow/stream/187780-t-compiler.2Fwg-llvm/topic/nocapture.20and.20allocation.20elimination

r? ghost
  • Loading branch information
bors committed Sep 28, 2024
2 parents a1fd235 + 9a03f37 commit fa341e6
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2519,6 +2519,14 @@ impl<T, A: Allocator> Vec<T, A> {
#[inline]
unsafe fn append_elements(&mut self, other: *const [T]) {
let count = unsafe { (*other).len() };
if count == 0 {
// The early return is not necessary for correctness, but in cases
// where LLVM sees all the way to the allocation site of `other`
// this can avoid a phi-node merging the two different pointers
// when zero-length allocations are special-cased.
// That in turn can enable more optimizations around the memcpy below.
return;
}
self.reserve(count);
let len = self.len();
unsafe { ptr::copy_nonoverlapping(other as *const T, self.as_mut_ptr().add(len), count) };
Expand Down

0 comments on commit fa341e6

Please sign in to comment.