Auto merge of #144086 - clubby789:alloc-zeroed, r=nikic

Pass `alloc-variant-zeroed` to LLVM

Makes use of https://github.com/llvm/llvm-project/pull/138299 (once we pull in a version of LLVM with this attribute). ~~Unfortunately also requires https://github.com/llvm/llvm-project/pull/149336 to work.~~

Closes rust-lang/rust#104847
This commit is contained in:
bors 2025-08-20 17:16:34 +00:00
commit 040a98af70
5 changed files with 36 additions and 0 deletions

View file

@ -420,6 +420,16 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
|| codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR_ZEROED)
{
to_add.push(create_alloc_family_attr(cx.llcx));
if let Some(zv) =
cx.tcx.get_attr(instance.def_id(), rustc_span::sym::rustc_allocator_zeroed_variant)
&& let Some(name) = zv.value_str()
{
to_add.push(llvm::CreateAttrStringValue(
cx.llcx,
"alloc-variant-zeroed",
&mangle_internal_symbol(cx.tcx, name.as_str()),
));
}
// apply to argument place instead of function
let alloc_align = AttributeKind::AllocAlign.create_attr(cx.llcx);
attributes::apply_to_llfn(llfn, AttributePlace::Argument(1), &[alloc_align]);

View file

@ -996,6 +996,10 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
rustc_allocator_zeroed, Normal, template!(Word), WarnFollowing,
EncodeCrossCrate::No,
),
rustc_attr!(
rustc_allocator_zeroed_variant, Normal, template!(NameValueStr: "function"), ErrorPreceding,
EncodeCrossCrate::Yes,
),
gated!(
default_lib_allocator, Normal, template!(Word), WarnFollowing,
EncodeCrossCrate::No, allocator_internals, experimental!(default_lib_allocator),

View file

@ -1837,6 +1837,7 @@ symbols! {
rustc_align,
rustc_allocator,
rustc_allocator_zeroed,
rustc_allocator_zeroed_variant,
rustc_allow_const_fn_unstable,
rustc_allow_incoherent_impl,
rustc_allowed_through_unstable_modules,

View file

@ -17,6 +17,7 @@ unsafe extern "Rust" {
#[rustc_allocator]
#[rustc_nounwind]
#[rustc_std_internal_symbol]
#[rustc_allocator_zeroed_variant = "__rust_alloc_zeroed"]
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
#[rustc_deallocator]
#[rustc_nounwind]

View file

@ -1,4 +1,6 @@
//@ revisions: normal llvm21
//@ compile-flags: -Copt-level=3 -Z merge-functions=disabled
//@ [llvm21] min-llvm-version: 21
//@ only-x86_64
#![crate_type = "lib"]
@ -176,6 +178,24 @@ pub fn vec_option_i32(n: usize) -> Vec<Option<i32>> {
vec![None; n]
}
// LLVM21-LABEL: @vec_array
#[cfg(llvm21)]
#[no_mangle]
pub fn vec_array(n: usize) -> Vec<[u32; 1_000_000]> {
// LLVM21-NOT: call {{.*}}alloc::vec::from_elem
// LLVM21-NOT: call {{.*}}reserve
// LLVM21-NOT: call {{.*}}__rust_alloc(
// LLVM21: call {{.*}}__rust_alloc_zeroed(
// LLVM21-NOT: call {{.*}}alloc::vec::from_elem
// LLVM21-NOT: call {{.*}}reserve
// LLVM21-NOT: call {{.*}}__rust_alloc(
// LLVM21: ret void
vec![[0; 1_000_000]; 3]
}
// Ensure that __rust_alloc_zeroed gets the right attributes for LLVM to optimize it away.
// CHECK: declare noalias noundef ptr @{{.*}}__rust_alloc_zeroed(i64 noundef, i64 allocalign noundef) unnamed_addr [[RUST_ALLOC_ZEROED_ATTRS:#[0-9]+]]