From 2a466466c768dc56382550a3130674b2eebe227c Mon Sep 17 00:00:00 2001 From: marc0246 <40955683+marc0246@users.noreply.github.com> Date: Tue, 16 May 2023 08:53:05 +0200 Subject: [PATCH] Fix duplicate `arcinner_layout_for_value_layout` calls --- library/alloc/src/sync.rs | 25 +++++++++++++++++++------ tests/codegen/issues/issue-111603.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 tests/codegen/issues/issue-111603.rs diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index 7347980abbc..bfdb7a92bef 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -502,6 +502,7 @@ pub fn new_cyclic(data_fn: F) -> Arc /// assert_eq!(*five, 5) /// ``` #[cfg(not(no_global_oom_handling))] + #[inline] #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] pub fn new_uninit() -> Arc> { @@ -535,6 +536,7 @@ pub fn new_uninit() -> Arc> { /// /// [zeroed]: mem::MaybeUninit::zeroed #[cfg(not(no_global_oom_handling))] + #[inline] #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] pub fn new_zeroed() -> Arc> { @@ -844,6 +846,7 @@ impl Arc<[T]> { /// assert_eq!(*values, [1, 2, 3]) /// ``` #[cfg(not(no_global_oom_handling))] + #[inline] #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit]> { @@ -871,6 +874,7 @@ pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit]> { /// /// [zeroed]: mem::MaybeUninit::zeroed #[cfg(not(no_global_oom_handling))] + #[inline] #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] pub fn new_zeroed_slice(len: usize) -> Arc<[mem::MaybeUninit]> { @@ -1300,10 +1304,10 @@ unsafe fn allocate_for_layout( mem_to_arcinner: impl FnOnce(*mut u8) -> *mut ArcInner, ) -> *mut ArcInner { let layout = arcinner_layout_for_value_layout(value_layout); - unsafe { - Arc::try_allocate_for_layout(value_layout, allocate, mem_to_arcinner) - .unwrap_or_else(|_| handle_alloc_error(layout)) - } + + let ptr = allocate(layout).unwrap_or_else(|_| handle_alloc_error(layout)); + + unsafe { Self::initialize_arcinner(ptr, layout, mem_to_arcinner) } } /// Allocates an `ArcInner` with sufficient space for @@ -1321,7 +1325,16 @@ unsafe fn try_allocate_for_layout( let ptr = allocate(layout)?; - // Initialize the ArcInner + let inner = unsafe { Self::initialize_arcinner(ptr, layout, mem_to_arcinner) }; + + Ok(inner) + } + + unsafe fn initialize_arcinner( + ptr: NonNull<[u8]>, + layout: Layout, + mem_to_arcinner: impl FnOnce(*mut u8) -> *mut ArcInner, + ) -> *mut ArcInner { let inner = mem_to_arcinner(ptr.as_non_null_ptr().as_ptr()); debug_assert_eq!(unsafe { Layout::for_value(&*inner) }, layout); @@ -1330,7 +1343,7 @@ unsafe fn try_allocate_for_layout( ptr::write(&mut (*inner).weak, atomic::AtomicUsize::new(1)); } - Ok(inner) + inner } /// Allocates an `ArcInner` with sufficient space for an unsized inner value. diff --git a/tests/codegen/issues/issue-111603.rs b/tests/codegen/issues/issue-111603.rs new file mode 100644 index 00000000000..90b3c314d2f --- /dev/null +++ b/tests/codegen/issues/issue-111603.rs @@ -0,0 +1,28 @@ +// compile-flags: -O + +#![crate_type = "lib"] +#![feature(get_mut_unchecked, new_uninit)] + +use std::sync::Arc; + +// CHECK-LABEL: @new_uninit +#[no_mangle] +pub fn new_uninit(x: u64) -> Arc<[u64; 1000]> { + // CHECK: call alloc::sync::arcinner_layout_for_value_layout + // CHECK-NOT: call alloc::sync::arcinner_layout_for_value_layout + let mut arc = Arc::new_uninit(); + unsafe { Arc::get_mut_unchecked(&mut arc) }.write([x; 1000]); + unsafe { arc.assume_init() } +} + +// CHECK-LABEL: @new_uninit_slice +#[no_mangle] +pub fn new_uninit_slice(x: u64) -> Arc<[u64]> { + // CHECK: call alloc::sync::arcinner_layout_for_value_layout + // CHECK-NOT: call alloc::sync::arcinner_layout_for_value_layout + let mut arc = Arc::new_uninit_slice(1000); + for elem in unsafe { Arc::get_mut_unchecked(&mut arc) } { + elem.write(x); + } + unsafe { arc.assume_init() } +}