Skip to content

Commit 5bba169

Browse files
committed
test that the phi node got eliminated
1 parent 9a03f37 commit 5bba169

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
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: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,11 +2520,9 @@ impl<T, A: Allocator> Vec<T, A> {
25202520
unsafe fn append_elements(&mut self, other: *const [T]) {
25212521
let count = unsafe { (*other).len() };
25222522
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.
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
25282526
return;
25292527
}
25302528
self.reserve(count);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
10+
// CHECK-LABEL: @vec_append_with_temp_alloc
11+
#[no_mangle]
12+
pub fn vec_append_with_temp_alloc(dst: &mut Vec<u8>, src: &[u8]) {
13+
// CHECK: %[[TEMP:.+]] = tail call noalias noundef ptr @__rust_alloc
14+
15+
// First memcpy, it uses the src pointer directly
16+
// CHECK: call void @llvm.memcpy.{{.*}}%src.0
17+
let temp = src.to_vec();
18+
19+
// final memcpy to destination
20+
// CHECK: call void @llvm.memcpy.{{.*}}%dst.i{{.*}}%[[TEMP]]
21+
dst.extend(&temp);
22+
// CHECK: ret
23+
}
24+
25+
26+
// CHECK-LABEL: @string_append_with_temp_alloc
27+
#[no_mangle]
28+
pub fn string_append_with_temp_alloc(dst: &mut String, src: &str) {
29+
// CHECK: %[[TEMP:.+]] = tail call noalias noundef ptr @__rust_alloc
30+
31+
// First memcpy, it uses the src pointer directly
32+
// CHECK: call void @llvm.memcpy.{{.*}}%src.0
33+
let temp = src.to_string();
34+
35+
// final memcpy to destination
36+
// CHECK: call void @llvm.memcpy.{{.*}}%dst.i{{.*}}%[[TEMP]]
37+
dst.push_str(&temp);
38+
// CHECK: ret
39+
}

0 commit comments

Comments
 (0)