Skip to content

Commit 873d468

Browse files
committed
Auto merge of #151337 - the8472:bail-before-memcpy2, r=Mark-Simulacrum
optimize `vec.extend(slice.to_vec())`, take 2 Redoing #130998 It was reverted in #151150 due to flakiness. I have traced this to layout randomization perturbing the test (the failure reproduces locally with layout randomization), which is now excluded.
2 parents 38c7129 + 2b8f4a5 commit 873d468

4 files changed

Lines changed: 55 additions & 6 deletions

File tree

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,16 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
516516
to_add.push(llvm::CreateAllocKindAttr(cx.llcx, AllocKindFlags::Free));
517517
// applies to argument place instead of function place
518518
let allocated_pointer = AttributeKind::AllocatedPointer.create_attr(cx.llcx);
519-
attributes::apply_to_llfn(llfn, AttributePlace::Argument(0), &[allocated_pointer]);
519+
let attrs: &[_] = if llvm_util::get_version() >= (21, 0, 0) {
520+
// "Does not capture provenance" means "if the function call stashes the pointer somewhere,
521+
// accessing that pointer after the function returns is UB". That is definitely the case here since
522+
// freeing will destroy the provenance.
523+
let captures_addr = AttributeKind::CapturesAddress.create_attr(cx.llcx);
524+
&[allocated_pointer, captures_addr]
525+
} else {
526+
&[allocated_pointer]
527+
};
528+
attributes::apply_to_llfn(llfn, AttributePlace::Argument(0), attrs);
520529
}
521530
if let Some(align) = codegen_fn_attrs.alignment {
522531
llvm::set_alignment(llfn, align);

library/alloc/src/slice.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,16 @@ impl<T> [T] {
444444
impl<T: TrivialClone> ConvertVec for T {
445445
#[inline]
446446
fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A> {
447-
let mut v = Vec::with_capacity_in(s.len(), alloc);
447+
let len = s.len();
448+
let mut v = Vec::with_capacity_in(len, alloc);
448449
// SAFETY:
449450
// allocated above with the capacity of `s`, and initialize to `s.len()` in
450451
// ptr::copy_to_non_overlapping below.
451-
unsafe {
452-
s.as_ptr().copy_to_nonoverlapping(v.as_mut_ptr(), s.len());
453-
v.set_len(s.len());
452+
if len > 0 {
453+
unsafe {
454+
s.as_ptr().copy_to_nonoverlapping(v.as_mut_ptr(), len);
455+
v.set_len(len);
456+
}
454457
}
455458
v
456459
}

library/alloc/src/vec/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2818,7 +2818,11 @@ impl<T, A: Allocator> Vec<T, A> {
28182818
let count = other.len();
28192819
self.reserve(count);
28202820
let len = self.len();
2821-
unsafe { ptr::copy_nonoverlapping(other as *const T, self.as_mut_ptr().add(len), count) };
2821+
if count > 0 {
2822+
unsafe {
2823+
ptr::copy_nonoverlapping(other as *const T, self.as_mut_ptr().add(len), count)
2824+
};
2825+
}
28222826
self.len += count;
28232827
}
28242828

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//@ compile-flags: -O -Zmerge-functions=disabled
2+
//@ needs-deterministic-layouts
3+
//@ min-llvm-version: 21
4+
#![crate_type = "lib"]
5+
6+
//! Check that a temporary intermediate allocations can eliminated and replaced
7+
//! with memcpy forwarding.
8+
//! This requires Vec code to be structured in a way that avoids phi nodes from the
9+
//! zero-capacity length flowing into the memcpy arguments.
10+
11+
// CHECK-LABEL: @vec_append_with_temp_alloc
12+
// CHECK-SAME: ptr{{.*}}[[DST:%[a-z]+]]{{.*}}ptr{{.*}}[[SRC:%[a-z]+]]
13+
#[no_mangle]
14+
pub fn vec_append_with_temp_alloc(dst: &mut Vec<u8>, src: &[u8]) {
15+
// CHECK-NOT: call void @llvm.memcpy
16+
// CHECK: call void @llvm.memcpy.{{.*}}[[DST]].i{{.*}}[[SRC]]
17+
// CHECK-NOT: call void @llvm.memcpy
18+
let temp = src.to_vec();
19+
dst.extend(&temp);
20+
// CHECK: ret
21+
}
22+
23+
// CHECK-LABEL: @string_append_with_temp_alloc
24+
// CHECK-SAME: ptr{{.*}}[[DST:%[a-z]+]]{{.*}}ptr{{.*}}[[SRC:%[a-z]+]]
25+
#[no_mangle]
26+
pub fn string_append_with_temp_alloc(dst: &mut String, src: &str) {
27+
// CHECK-NOT: call void @llvm.memcpy
28+
// CHECK: call void @llvm.memcpy.{{.*}}[[DST]].i{{.*}}[[SRC]]
29+
// CHECK-NOT: call void @llvm.memcpy
30+
let temp = src.to_string();
31+
dst.push_str(&temp);
32+
// CHECK: ret
33+
}

0 commit comments

Comments
 (0)