@@ -6,21 +6,68 @@ use rustc_attr_parsing::InlineAttr;
66use rustc_attr_parsing:: InstructionSetAttr ;
77#[ cfg( feature = "master" ) ]
88use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
9+ use rustc_middle:: mir:: TerminatorKind ;
910use rustc_middle:: ty;
1011
1112use crate :: context:: CodegenCx ;
1213use crate :: gcc_util:: to_gcc_features;
1314
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`.
1546#[ cfg( feature = "master" ) ]
1647#[ inline]
1748fn inline_attr < ' gcc , ' tcx > (
1849 cx : & CodegenCx < ' gcc , ' tcx > ,
1950 inline : InlineAttr ,
51+ instance : ty:: Instance < ' tcx > ,
2052) -> Option < FnAttribute < ' gcc > > {
2153 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+ }
2269 InlineAttr :: Hint => Some ( FnAttribute :: Inline ) ,
23- InlineAttr :: Always | InlineAttr :: Force { .. } => Some ( FnAttribute :: AlwaysInline ) ,
70+ InlineAttr :: Force { .. } => Some ( FnAttribute :: AlwaysInline ) ,
2471 InlineAttr :: Never => {
2572 if cx. sess ( ) . target . arch != "amdgpu" {
2673 Some ( FnAttribute :: NoInline )
@@ -52,7 +99,7 @@ pub fn from_fn_attrs<'gcc, 'tcx>(
5299 } else {
53100 codegen_fn_attrs. inline
54101 } ;
55- if let Some ( attr) = inline_attr ( cx, inline) {
102+ if let Some ( attr) = inline_attr ( cx, inline, instance ) {
56103 if let FnAttribute :: AlwaysInline = attr {
57104 func. add_attribute ( FnAttribute :: Inline ) ;
58105 }
0 commit comments