Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit fd59d69

Browse files
committed
Auto merge of rust-lang#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 fb4aebd + c44e0f4 commit fd59d69

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

library/alloc/src/slice.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ pub(crate) mod hack {
160160
impl<T: Copy> ConvertVec for T {
161161
#[inline]
162162
fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A> {
163+
if s.is_empty() {
164+
// The early return is not necessary for correctness, but it helps
165+
// LLVM by avoiding phi nodes flowing into memcpy.
166+
// See codegen/lib-optimizations/append-elements.rs
167+
return Vec::new_in(alloc);
168+
}
163169
let mut v = Vec::with_capacity_in(s.len(), alloc);
164170
// SAFETY:
165171
// allocated above with the capacity of `s`, and initialize to `s.len()` in

library/alloc/src/vec/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,6 +2519,12 @@ 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 it helps
2524+
// LLVM by avoiding phi nodes flowing into memcpy.
2525+
// See codegen/lib-optimizations/append-elements.rs
2526+
return;
2527+
}
25222528
self.reserve(count);
25232529
let len = self.len();
25242530
unsafe { ptr::copy_nonoverlapping(other as *const T, self.as_mut_ptr().add(len), count) };
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//@ compile-flags: -O
2+
#![crate_type = "lib"]
3+
4+
//! Check that pointer flowing into vec.append comes straight from an allocation
5+
//! and not through a phi node that merges the allocation and zero-length cases.
6+
//! With this and https://github.com/llvm/llvm-project/pull/110280 the intermediate
7+
//! allocation should be optimized away in the future.
8+
9+
// CHECK-LABEL: @vec_append_with_temp_alloc
10+
#[no_mangle]
11+
pub fn vec_append_with_temp_alloc(dst: &mut Vec<u8>, src: &[u8]) {
12+
// CHECK: %[[TEMP:.+]] = tail call noalias noundef ptr @__rust_alloc
13+
14+
// First memcpy, it uses the src pointer directly
15+
// CHECK: call void @llvm.memcpy.{{.*}}%src.0
16+
let temp = src.to_vec();
17+
18+
// final memcpy to destination
19+
// CHECK: call void @llvm.memcpy.{{.*}}%dst.i{{.*}}%[[TEMP]]
20+
dst.extend(&temp);
21+
// CHECK: ret
22+
}
23+
24+
// CHECK-LABEL: @string_append_with_temp_alloc
25+
#[no_mangle]
26+
pub fn string_append_with_temp_alloc(dst: &mut String, src: &str) {
27+
// CHECK: %[[TEMP:.+]] = tail call noalias noundef ptr @__rust_alloc
28+
29+
// First memcpy, it uses the src pointer directly
30+
// CHECK: call void @llvm.memcpy.{{.*}}%src.0
31+
let temp = src.to_string();
32+
33+
// final memcpy to destination
34+
// CHECK: call void @llvm.memcpy.{{.*}}%dst.i{{.*}}%[[TEMP]]
35+
dst.push_str(&temp);
36+
// CHECK: ret
37+
}

0 commit comments

Comments
 (0)