@@ -6,21 +6,65 @@ use rustc_attr_parsing::InlineAttr;
6
6
use rustc_attr_parsing:: InstructionSetAttr ;
7
7
#[ cfg( feature = "master" ) ]
8
8
use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
9
+ use rustc_middle:: mir:: TerminatorKind ;
9
10
use rustc_middle:: ty;
10
11
11
12
use crate :: context:: CodegenCx ;
12
13
use crate :: gcc_util:: to_gcc_features;
13
-
14
- /// Get GCC attribute for the provided inline heuristic.
14
+ /// Checks if the function `instance` is recursively inline.
15
+ /// Returns `false` if a functions is guranteed to be non-recursive, and `true` if it *might* be recursive.
16
+ fn resursively_inline < ' gcc , ' tcx > (
17
+ cx : & CodegenCx < ' gcc , ' tcx > ,
18
+ instance : ty:: Instance < ' tcx > ,
19
+ ) -> bool {
20
+ // No body, so we can't check if this is recursively inline, so we assume it is.
21
+ if !cx. tcx . is_mir_available ( instance. def_id ( ) ) {
22
+ return true ;
23
+ }
24
+ // `expect_local` ought to never fail: we should be checking a function within this codegen unit.
25
+ let body = cx. tcx . optimized_mir ( instance. def_id ( ) ) ;
26
+ for block in body. basic_blocks . iter ( ) {
27
+ let Some ( terminator) = & block. terminator else { continue } ;
28
+ // I assmume that the resursive-inline issue applies only to functions, and not to drops.
29
+ // In principle, a recursive, `#[inline(always)]` drop could(?) exist, but I don't think it does.
30
+ let TerminatorKind :: Call { func, .. } = & terminator. kind else { continue } ;
31
+ let Some ( ( def, _args) ) = func. const_fn_def ( ) else { continue } ;
32
+ // Check if the called function is recursively inline.
33
+ if matches ! (
34
+ cx. tcx. codegen_fn_attrs( def) . inline,
35
+ InlineAttr :: Always | InlineAttr :: Force { .. }
36
+ ) {
37
+ return true ;
38
+ }
39
+ }
40
+ false
41
+ }
42
+ /// Get GCC attribute for the provided inline heuristic, attached to `instance`.
15
43
#[ cfg( feature = "master" ) ]
16
44
#[ inline]
17
45
fn inline_attr < ' gcc , ' tcx > (
18
46
cx : & CodegenCx < ' gcc , ' tcx > ,
19
47
inline : InlineAttr ,
48
+ instance : ty:: Instance < ' tcx > ,
20
49
) -> Option < FnAttribute < ' gcc > > {
21
50
match inline {
51
+ InlineAttr :: Always => {
52
+ // We can't simply always return `always_inline` unconditionally.
53
+ // It is *NOT A HINT* and does not work for recurisve functions.
54
+ //
55
+ // So, it can only be applied *if*:
56
+ // The current function does not call any functions makred `#[inline(always)]`.
57
+ //
58
+ // That prevents issues steming from recursive `#[inline(always)]` at a *relatively* small cost.
59
+ // We *only* need to check all the terminators of a function marked with this attribute.
60
+ if resursively_inline ( cx, instance) {
61
+ Some ( FnAttribute :: Inline )
62
+ } else {
63
+ Some ( FnAttribute :: AlwaysInline )
64
+ }
65
+ }
22
66
InlineAttr :: Hint => Some ( FnAttribute :: Inline ) ,
23
- InlineAttr :: Always | InlineAttr :: Force { .. } => Some ( FnAttribute :: AlwaysInline ) ,
67
+ InlineAttr :: Force { .. } => Some ( FnAttribute :: AlwaysInline ) ,
24
68
InlineAttr :: Never => {
25
69
if cx. sess ( ) . target . arch != "amdgpu" {
26
70
Some ( FnAttribute :: NoInline )
@@ -52,7 +96,7 @@ pub fn from_fn_attrs<'gcc, 'tcx>(
52
96
} else {
53
97
codegen_fn_attrs. inline
54
98
} ;
55
- if let Some ( attr) = inline_attr ( cx, inline) {
99
+ if let Some ( attr) = inline_attr ( cx, inline, instance ) {
56
100
if let FnAttribute :: AlwaysInline = attr {
57
101
func. add_attribute ( FnAttribute :: Inline ) ;
58
102
}
0 commit comments