@@ -13,7 +13,7 @@ use smallvec::SmallVec;
1313
1414use crate :: attributes;
1515use crate :: llvm:: AttributePlace :: Function ;
16- use crate :: llvm:: { self , Attribute , AttributeKind , AttributePlace } ;
16+ use crate :: llvm:: { self , AllocKindFlags , Attribute , AttributeKind , AttributePlace } ;
1717use crate :: llvm_util;
1818pub use rustc_attr:: { InlineAttr , InstructionSetAttr , OptimizeAttr } ;
1919
@@ -227,6 +227,10 @@ pub(crate) fn default_optimisation_attrs<'ll>(
227227 attrs
228228}
229229
230+ fn create_alloc_family_attr ( llcx : & llvm:: Context ) -> & llvm:: Attribute {
231+ llvm:: CreateAttrStringValue ( llcx, "alloc-family" , "__rust_alloc" )
232+ }
233+
230234/// Composite function which sets LLVM attributes for function depending on its AST (`#[attribute]`)
231235/// attributes.
232236pub fn from_fn_attrs < ' ll , ' tcx > (
@@ -309,11 +313,54 @@ pub fn from_fn_attrs<'ll, 'tcx>(
309313 // Need this for AArch64.
310314 to_add. push ( llvm:: CreateAttrStringValue ( cx. llcx , "branch-target-enforcement" , "false" ) ) ;
311315 }
312- if codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: ALLOCATOR ) {
316+ if codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: ALLOCATOR )
317+ || codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: ALLOCATOR_ZEROED )
318+ {
319+ if llvm_util:: get_version ( ) >= ( 15 , 0 , 0 ) {
320+ to_add. push ( create_alloc_family_attr ( cx. llcx ) ) ;
321+ // apply to argument place instead of function
322+ let alloc_align = AttributeKind :: AllocAlign . create_attr ( cx. llcx ) ;
323+ attributes:: apply_to_llfn ( llfn, AttributePlace :: Argument ( 1 ) , & [ alloc_align] ) ;
324+ to_add. push ( llvm:: CreateAllocSizeAttr ( cx. llcx , 0 ) ) ;
325+ let mut flags = AllocKindFlags :: Alloc | AllocKindFlags :: Aligned ;
326+ if codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: ALLOCATOR ) {
327+ flags |= AllocKindFlags :: Uninitialized ;
328+ } else {
329+ flags |= AllocKindFlags :: Zeroed ;
330+ }
331+ to_add. push ( llvm:: CreateAllocKindAttr ( cx. llcx , flags) ) ;
332+ }
313333 // apply to return place instead of function (unlike all other attributes applied in this function)
314334 let no_alias = AttributeKind :: NoAlias . create_attr ( cx. llcx ) ;
315335 attributes:: apply_to_llfn ( llfn, AttributePlace :: ReturnValue , & [ no_alias] ) ;
316336 }
337+ if codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: REALLOCATOR ) {
338+ if llvm_util:: get_version ( ) >= ( 15 , 0 , 0 ) {
339+ to_add. push ( create_alloc_family_attr ( cx. llcx ) ) ;
340+ to_add. push ( llvm:: CreateAllocKindAttr (
341+ cx. llcx ,
342+ AllocKindFlags :: Realloc | AllocKindFlags :: Aligned ,
343+ ) ) ;
344+ // applies to argument place instead of function place
345+ let allocated_pointer = AttributeKind :: AllocatedPointer . create_attr ( cx. llcx ) ;
346+ attributes:: apply_to_llfn ( llfn, AttributePlace :: Argument ( 0 ) , & [ allocated_pointer] ) ;
347+ // apply to argument place instead of function
348+ let alloc_align = AttributeKind :: AllocAlign . create_attr ( cx. llcx ) ;
349+ attributes:: apply_to_llfn ( llfn, AttributePlace :: Argument ( 2 ) , & [ alloc_align] ) ;
350+ to_add. push ( llvm:: CreateAllocSizeAttr ( cx. llcx , 3 ) ) ;
351+ }
352+ let no_alias = AttributeKind :: NoAlias . create_attr ( cx. llcx ) ;
353+ attributes:: apply_to_llfn ( llfn, AttributePlace :: ReturnValue , & [ no_alias] ) ;
354+ }
355+ if codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: DEALLOCATOR ) {
356+ if llvm_util:: get_version ( ) >= ( 15 , 0 , 0 ) {
357+ to_add. push ( create_alloc_family_attr ( cx. llcx ) ) ;
358+ to_add. push ( llvm:: CreateAllocKindAttr ( cx. llcx , AllocKindFlags :: Free ) ) ;
359+ // applies to argument place instead of function place
360+ let allocated_pointer = AttributeKind :: AllocatedPointer . create_attr ( cx. llcx ) ;
361+ attributes:: apply_to_llfn ( llfn, AttributePlace :: Argument ( 0 ) , & [ allocated_pointer] ) ;
362+ }
363+ }
317364 if codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: CMSE_NONSECURE_ENTRY ) {
318365 to_add. push ( llvm:: CreateAttrString ( cx. llcx , "cmse_nonsecure_entry" ) ) ;
319366 }
0 commit comments