Skip to content

Commit fa341e6

Browse files
committed
Auto merge of #130998 - the8472:bail-before-memcpy, r=<try>
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
2 parents a1fd235 + 9a03f37 commit fa341e6

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

library/alloc/src/vec/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,6 +2519,14 @@ impl<T, A: Allocator> Vec<T, A> {
25192519
#[inline]
25202520
unsafe fn append_elements(&mut self, other: *const [T]) {
25212521
let count = unsafe { (*other).len() };
2522+
if count == 0 {
2523+
// The early return is not necessary for correctness, but in cases
2524+
// where LLVM sees all the way to the allocation site of `other`
2525+
// this can avoid a phi-node merging the two different pointers
2526+
// when zero-length allocations are special-cased.
2527+
// That in turn can enable more optimizations around the memcpy below.
2528+
return;
2529+
}
25222530
self.reserve(count);
25232531
let len = self.len();
25242532
unsafe { ptr::copy_nonoverlapping(other as *const T, self.as_mut_ptr().add(len), count) };

0 commit comments

Comments
 (0)