@@ -31,14 +31,24 @@ pub(crate) fn apply_to_callsite(callsite: &Value, idx: AttributePlace, attrs: &[
31
31
32
32
/// Get LLVM attribute for the provided inline heuristic.
33
33
#[ 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 > {
35
39
if !cx. tcx . sess . opts . unstable_opts . inline_llvm {
36
40
// disable LLVM inlining
37
41
return Some ( AttributeKind :: NoInline . create_attr ( cx. llcx ) ) ;
38
42
}
39
43
match inline {
40
44
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
+ }
42
52
InlineAttr :: Never => {
43
53
if cx. sess ( ) . target . arch != "amdgpu" {
44
54
Some ( AttributeKind :: NoInline . create_attr ( cx. llcx ) )
@@ -327,7 +337,7 @@ fn create_alloc_family_attr(llcx: &llvm::Context) -> &llvm::Attribute {
327
337
llvm:: CreateAttrStringValue ( llcx, "alloc-family" , "__rust_alloc" )
328
338
}
329
339
330
- fn should_always_inline ( body : & rustc_middle:: mir:: Body < ' _ > ) -> bool {
340
+ fn very_small_body ( body : & rustc_middle:: mir:: Body < ' _ > ) -> bool {
331
341
use rustc_middle:: mir:: * ;
332
342
match body. basic_blocks . len ( ) {
333
343
0 => return true ,
@@ -378,16 +388,14 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
378
388
codegen_fn_attrs. inline
379
389
} ;
380
390
381
- if cx. tcx . is_mir_available ( instance. def_id ( ) ) {
391
+ let very_small_body = if cx. tcx . is_mir_available ( instance. def_id ( ) ) {
382
392
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)
388
394
} 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) ) ;
391
399
392
400
// The `uwtable` attribute according to LLVM is:
393
401
//
0 commit comments