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