Skip to content

Commit 124411e

Browse files
committed
Pass alloc-variant-zeroed to LLVM
1 parent 8c12d76 commit 124411e

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
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
@@ -772,6 +772,10 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
772772
rustc_allocator_zeroed, Normal, template!(Word), WarnFollowing,
773773
EncodeCrossCrate::No,
774774
),
775+
rustc_attr!(
776+
rustc_allocator_zeroed_variant, Normal, template!(NameValueStr: "function"), ErrorPreceding,
777+
EncodeCrossCrate::Yes,
778+
),
775779
gated!(
776780
default_lib_allocator, Normal, template!(Word), WarnFollowing,
777781
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
@@ -1810,6 +1810,7 @@ symbols! {
18101810
rustc_abi,
18111811
rustc_allocator,
18121812
rustc_allocator_zeroed,
1813+
rustc_allocator_zeroed_variant,
18131814
rustc_allow_const_fn_unstable,
18141815
rustc_allow_incoherent_impl,
18151816
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/vec-calloc.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,23 @@ pub fn vec_option_i32(n: usize) -> Vec<Option<i32>> {
176176
vec![None; n]
177177
}
178178

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

0 commit comments

Comments
 (0)