@@ -13,7 +13,7 @@ use smallvec::SmallVec;
13
13
14
14
use crate :: attributes;
15
15
use crate :: llvm:: AttributePlace :: Function ;
16
- use crate :: llvm:: { self , Attribute , AttributeKind , AttributePlace } ;
16
+ use crate :: llvm:: { self , AllocKindFlags , Attribute , AttributeKind , AttributePlace } ;
17
17
use crate :: llvm_util;
18
18
pub use rustc_attr:: { InlineAttr , InstructionSetAttr , OptimizeAttr } ;
19
19
@@ -227,6 +227,10 @@ pub(crate) fn default_optimisation_attrs<'ll>(
227
227
attrs
228
228
}
229
229
230
+ fn create_alloc_family_attr ( llcx : & llvm:: Context ) -> & llvm:: Attribute {
231
+ llvm:: CreateAttrStringValue ( llcx, "alloc-family" , "__rust_alloc" )
232
+ }
233
+
230
234
/// Composite function which sets LLVM attributes for function depending on its AST (`#[attribute]`)
231
235
/// attributes.
232
236
pub fn from_fn_attrs < ' ll , ' tcx > (
@@ -309,11 +313,54 @@ pub fn from_fn_attrs<'ll, 'tcx>(
309
313
// Need this for AArch64.
310
314
to_add. push ( llvm:: CreateAttrStringValue ( cx. llcx , "branch-target-enforcement" , "false" ) ) ;
311
315
}
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
+ }
313
333
// apply to return place instead of function (unlike all other attributes applied in this function)
314
334
let no_alias = AttributeKind :: NoAlias . create_attr ( cx. llcx ) ;
315
335
attributes:: apply_to_llfn ( llfn, AttributePlace :: ReturnValue , & [ no_alias] ) ;
316
336
}
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
+ }
317
364
if codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: CMSE_NONSECURE_ENTRY ) {
318
365
to_add. push ( llvm:: CreateAttrString ( cx. llcx , "cmse_nonsecure_entry" ) ) ;
319
366
}
0 commit comments