@@ -14,8 +14,8 @@ use crate::context::CodegenCx;
1414use  crate :: errors:: { MissingFeatures ,  SanitizerMemtagRequiresMte ,  TargetFeatureDisableOrEnable } ; 
1515use  crate :: llvm:: AttributePlace :: Function ; 
1616use  crate :: llvm:: { self ,  AllocKindFlags ,  Attribute ,  AttributeKind ,  AttributePlace ,  MemoryEffects } ; 
17+ use  crate :: llvm_util; 
1718use  crate :: value:: Value ; 
18- use  crate :: { attributes,  llvm_util} ; 
1919
2020pub  fn  apply_to_llfn ( llfn :  & Value ,  idx :  AttributePlace ,  attrs :  & [ & Attribute ] )  { 
2121    if  !attrs. is_empty ( )  { 
@@ -324,13 +324,18 @@ fn create_alloc_family_attr(llcx: &llvm::Context) -> &llvm::Attribute {
324324    llvm:: CreateAttrStringValue ( llcx,  "alloc-family" ,  "__rust_alloc" ) 
325325} 
326326
327- /// Helper for `FnAbi::apply_attrs_llfn `: 
327+ /// Helper for `FnAbi::apply_attrs_* `: 
328328/// Composite function which sets LLVM attributes for function depending on its AST (`#[attribute]`) 
329329/// attributes. 
330+ /// 
331+ /// `apply_attrs` is called to apply the attributes, so this can be used both for declarations and calls. 
332+ /// However, some things are not represented as attributes and and only be set on declarations, so 
333+ /// `declare_llfn` should be `Some` if this is a declaration. 
330334pub  fn  llfn_attrs_from_instance < ' ll ,  ' tcx > ( 
331335    cx :  & CodegenCx < ' ll ,  ' tcx > , 
332-     llfn :  & ' ll  Value , 
333336    instance :  ty:: Instance < ' tcx > , 
337+     declare_llfn :  Option < & ' ll  Value > , 
338+     apply_attrs :  impl  Fn ( AttributePlace ,  & [ & Attribute ] ) , 
334339)  { 
335340    let  codegen_fn_attrs = cx. tcx . codegen_fn_attrs ( instance. def_id ( ) ) ; 
336341
@@ -440,7 +445,7 @@ pub fn llfn_attrs_from_instance<'ll, 'tcx>(
440445        to_add. push ( create_alloc_family_attr ( cx. llcx ) ) ; 
441446        // apply to argument place instead of function 
442447        let  alloc_align = AttributeKind :: AllocAlign . create_attr ( cx. llcx ) ; 
443-         attributes :: apply_to_llfn ( llfn ,   AttributePlace :: Argument ( 1 ) ,  & [ alloc_align] ) ; 
448+         apply_attrs ( AttributePlace :: Argument ( 1 ) ,  & [ alloc_align] ) ; 
444449        to_add. push ( llvm:: CreateAllocSizeAttr ( cx. llcx ,  0 ) ) ; 
445450        let  mut  flags = AllocKindFlags :: Alloc  | AllocKindFlags :: Aligned ; 
446451        if  codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: ALLOCATOR )  { 
@@ -451,7 +456,7 @@ pub fn llfn_attrs_from_instance<'ll, 'tcx>(
451456        to_add. push ( llvm:: CreateAllocKindAttr ( cx. llcx ,  flags) ) ; 
452457        // apply to return place instead of function (unlike all other attributes applied in this function) 
453458        let  no_alias = AttributeKind :: NoAlias . create_attr ( cx. llcx ) ; 
454-         attributes :: apply_to_llfn ( llfn ,   AttributePlace :: ReturnValue ,  & [ no_alias] ) ; 
459+         apply_attrs ( AttributePlace :: ReturnValue ,  & [ no_alias] ) ; 
455460    } 
456461    if  codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: REALLOCATOR )  { 
457462        to_add. push ( create_alloc_family_attr ( cx. llcx ) ) ; 
@@ -461,26 +466,28 @@ pub fn llfn_attrs_from_instance<'ll, 'tcx>(
461466        ) ) ; 
462467        // applies to argument place instead of function place 
463468        let  allocated_pointer = AttributeKind :: AllocatedPointer . create_attr ( cx. llcx ) ; 
464-         attributes :: apply_to_llfn ( llfn ,   AttributePlace :: Argument ( 0 ) ,  & [ allocated_pointer] ) ; 
469+         apply_attrs ( AttributePlace :: Argument ( 0 ) ,  & [ allocated_pointer] ) ; 
465470        // apply to argument place instead of function 
466471        let  alloc_align = AttributeKind :: AllocAlign . create_attr ( cx. llcx ) ; 
467-         attributes :: apply_to_llfn ( llfn ,   AttributePlace :: Argument ( 2 ) ,  & [ alloc_align] ) ; 
472+         apply_attrs ( AttributePlace :: Argument ( 2 ) ,  & [ alloc_align] ) ; 
468473        to_add. push ( llvm:: CreateAllocSizeAttr ( cx. llcx ,  3 ) ) ; 
469474        let  no_alias = AttributeKind :: NoAlias . create_attr ( cx. llcx ) ; 
470-         attributes :: apply_to_llfn ( llfn ,   AttributePlace :: ReturnValue ,  & [ no_alias] ) ; 
475+         apply_attrs ( AttributePlace :: ReturnValue ,  & [ no_alias] ) ; 
471476    } 
472477    if  codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: DEALLOCATOR )  { 
473478        to_add. push ( create_alloc_family_attr ( cx. llcx ) ) ; 
474479        to_add. push ( llvm:: CreateAllocKindAttr ( cx. llcx ,  AllocKindFlags :: Free ) ) ; 
475480        // applies to argument place instead of function place 
476481        let  allocated_pointer = AttributeKind :: AllocatedPointer . create_attr ( cx. llcx ) ; 
477-         attributes :: apply_to_llfn ( llfn ,   AttributePlace :: Argument ( 0 ) ,  & [ allocated_pointer] ) ; 
482+         apply_attrs ( AttributePlace :: Argument ( 0 ) ,  & [ allocated_pointer] ) ; 
478483    } 
479484    if  codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: CMSE_NONSECURE_ENTRY )  { 
480485        to_add. push ( llvm:: CreateAttrString ( cx. llcx ,  "cmse_nonsecure_entry" ) ) ; 
481486    } 
482487    if  let  Some ( align)  = codegen_fn_attrs. alignment  { 
483-         llvm:: set_alignment ( llfn,  align) ; 
488+         if  let  Some ( llfn)  = declare_llfn { 
489+             llvm:: set_alignment ( llfn,  align) ; 
490+         } 
484491    } 
485492    if  let  Some ( backchain)  = backchain_attr ( cx)  { 
486493        to_add. push ( backchain) ; 
@@ -551,7 +558,7 @@ pub fn llfn_attrs_from_instance<'ll, 'tcx>(
551558        to_add. push ( llvm:: CreateAttrStringValue ( cx. llcx ,  "target-features" ,  & target_features) ) ; 
552559    } 
553560
554-     attributes :: apply_to_llfn ( llfn ,   Function ,  & to_add) ; 
561+     apply_attrs ( Function ,  & to_add) ; 
555562} 
556563
557564fn  wasm_import_module ( tcx :  TyCtxt < ' _ > ,  id :  DefId )  -> Option < & String >  { 
0 commit comments