@@ -31,14 +31,24 @@ pub(crate) fn apply_to_callsite(callsite: &Value, idx: AttributePlace, attrs: &[
3131
3232/// Get LLVM attribute for the provided inline heuristic.
3333#[ inline]
34- fn inline_attr < ' ll > ( cx : & CodegenCx < ' ll , ' _ > , inline : InlineAttr ) -> Option < & ' ll Attribute > {
34+ fn inline_attr < ' ll > (
35+ cx : & CodegenCx < ' ll , ' _ > ,
36+ inline : InlineAttr ,
37+ should_always_inline : bool ,
38+ ) -> Option < & ' ll Attribute > {
3539 if !cx. tcx . sess . opts . unstable_opts . inline_llvm {
3640 // disable LLVM inlining
3741 return Some ( AttributeKind :: NoInline . create_attr ( cx. llcx ) ) ;
3842 }
3943 match inline {
4044 InlineAttr :: Hint => Some ( AttributeKind :: InlineHint . create_attr ( cx. llcx ) ) ,
41- InlineAttr :: Always => Some ( AttributeKind :: AlwaysInline . create_attr ( cx. llcx ) ) ,
45+ InlineAttr :: Always => {
46+ if should_always_inline {
47+ Some ( AttributeKind :: AlwaysInline . create_attr ( cx. llcx ) )
48+ } else {
49+ Some ( llvm:: CreateAttrStringValue ( cx. llcx , "function-inline-cost" , "0" ) )
50+ }
51+ }
4252 InlineAttr :: Never => {
4353 if cx. sess ( ) . target . arch != "amdgpu" {
4454 Some ( AttributeKind :: NoInline . create_attr ( cx. llcx ) )
@@ -327,7 +337,7 @@ fn create_alloc_family_attr(llcx: &llvm::Context) -> &llvm::Attribute {
327337 llvm:: CreateAttrStringValue ( llcx, "alloc-family" , "__rust_alloc" )
328338}
329339
330- fn should_always_inline ( body : & rustc_middle:: mir:: Body < ' _ > ) -> bool {
340+ fn very_small_body ( body : & rustc_middle:: mir:: Body < ' _ > ) -> bool {
331341 use rustc_middle:: mir:: * ;
332342 match body. basic_blocks . len ( ) {
333343 0 => return true ,
@@ -378,16 +388,14 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
378388 codegen_fn_attrs. inline
379389 } ;
380390
381- if cx. tcx . is_mir_available ( instance. def_id ( ) ) {
391+ let very_small_body = if cx. tcx . is_mir_available ( instance. def_id ( ) ) {
382392 let body = cx. tcx . instance_mir ( instance. def ) ;
383- if should_always_inline ( body) {
384- to_add. push ( AttributeKind :: AlwaysInline . create_attr ( cx. llcx ) ) ;
385- } else {
386- to_add. extend ( inline_attr ( cx, inline) ) ;
387- }
393+ very_small_body ( body)
388394 } else {
389- to_add. extend ( inline_attr ( cx, inline) ) ;
390- }
395+ false
396+ } ;
397+ let should_always_inline = very_small_body || cx. tcx . sess . opts . optimize != OptLevel :: No ;
398+ to_add. extend ( inline_attr ( cx, inline, should_always_inline) ) ;
391399
392400 // The `uwtable` attribute according to LLVM is:
393401 //
0 commit comments