Skip to content

Commit 5cf5610

Browse files
committed
Pass alloc-variant-zeroed to LLVM
1 parent 1c9952f commit 5cf5610

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,16 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
436436
|| codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR_ZEROED)
437437
{
438438
to_add.push(create_alloc_family_attr(cx.llcx));
439+
if let Some(zv) =
440+
cx.tcx.get_attr(instance.def_id(), rustc_span::sym::rustc_allocator_zeroed_variant)
441+
&& let Some(name) = zv.value_str()
442+
{
443+
to_add.push(llvm::CreateAttrStringValue(
444+
cx.llcx,
445+
"alloc-variant-zeroed",
446+
name.as_str(),
447+
));
448+
}
439449
// apply to argument place instead of function
440450
let alloc_align = AttributeKind::AllocAlign.create_attr(cx.llcx);
441451
attributes::apply_to_llfn(llfn, AttributePlace::Argument(1), &[alloc_align]);

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,10 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
993993
rustc_allocator_zeroed, Normal, template!(Word), WarnFollowing,
994994
EncodeCrossCrate::No,
995995
),
996+
rustc_attr!(
997+
rustc_allocator_zeroed_variant, Normal, template!(NameValueStr: "function"), ErrorPreceding,
998+
EncodeCrossCrate::Yes,
999+
),
9961000
gated!(
9971001
default_lib_allocator, Normal, template!(Word), WarnFollowing,
9981002
EncodeCrossCrate::No, allocator_internals, experimental!(default_lib_allocator),

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,7 @@ symbols! {
18161816
rustc_align,
18171817
rustc_allocator,
18181818
rustc_allocator_zeroed,
1819+
rustc_allocator_zeroed_variant,
18191820
rustc_allow_const_fn_unstable,
18201821
rustc_allow_incoherent_impl,
18211822
rustc_allowed_through_unstable_modules,

library/alloc/src/alloc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ unsafe extern "Rust" {
1717
#[rustc_allocator]
1818
#[rustc_nounwind]
1919
#[rustc_std_internal_symbol]
20+
#[rustc_allocator_zeroed_variant = "__rust_alloc_zeroed"]
2021
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
2122
#[rustc_deallocator]
2223
#[rustc_nounwind]

tests/codegen-llvm/vec-calloc.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
//@ revisions: normal llvm21
12
//@ compile-flags: -Copt-level=3 -Z merge-functions=disabled
2-
//@ only-x86_64
3+
//@ [llvm21] min-llvm-version: 21
34

45
#![crate_type = "lib"]
56

@@ -176,6 +177,24 @@ pub fn vec_option_i32(n: usize) -> Vec<Option<i32>> {
176177
vec![None; n]
177178
}
178179

180+
// LLVM21-LABEL: @vec_array
181+
#[cfg(llvm21)]
182+
#[no_mangle]
183+
pub fn vec_array(n: usize) -> Vec<[u32; 1_000_000]> {
184+
// LLVM21-NOT: call {{.*}}alloc::vec::from_elem
185+
// LLVM21-NOT: call {{.*}}reserve
186+
// LLVM21-NOT: call {{.*}}__rust_alloc(
187+
188+
// LLVM21: call {{.*}}__rust_alloc_zeroed(
189+
190+
// LLVM21-NOT: call {{.*}}alloc::vec::from_elem
191+
// LLVM21-NOT: call {{.*}}reserve
192+
// LLVM21-NOT: call {{.*}}__rust_alloc(
193+
194+
// LLVM21: ret void
195+
vec![[0; 1_000_000]; 3]
196+
}
197+
179198
// Ensure that __rust_alloc_zeroed gets the right attributes for LLVM to optimize it away.
180199
// CHECK: declare noalias noundef ptr @{{.*}}__rust_alloc_zeroed(i64 noundef, i64 allocalign noundef) unnamed_addr [[RUST_ALLOC_ZEROED_ATTRS:#[0-9]+]]
181200

0 commit comments

Comments
 (0)