Skip to content

Commit b6eb7c2

Browse files
committed
Fixed a recursive inling bug, added a test for it
1 parent cfe88fa commit b6eb7c2

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

src/attributes.rs

+49-3
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,67 @@ use rustc_attr_parsing::InlineAttr;
66
use rustc_attr_parsing::InstructionSetAttr;
77
#[cfg(feature = "master")]
88
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
9+
use rustc_middle::mir::TerminatorKind;
910
use rustc_middle::ty;
1011

1112
use crate::context::CodegenCx;
1213
use 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 guranteed 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 assmume that the resursive-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+
/// Get GCC attribute for the provided inline heuristic, attached to `instance`.
1545
#[cfg(feature = "master")]
1646
#[inline]
1747
fn inline_attr<'gcc, 'tcx>(
1848
cx: &CodegenCx<'gcc, 'tcx>,
1949
inline: InlineAttr,
50+
instance: ty::Instance<'tcx>,
2051
) -> Option<FnAttribute<'gcc>> {
2152
match inline {
53+
InlineAttr::Always => {
54+
// We can't simply always return `always_inline` unconditionally.
55+
// It is *NOT A HINT* and does not work for recurisve functions.
56+
//
57+
// So, it can only be applied *if*:
58+
// The current function does not call any functions makred `#[inline(always)]`.
59+
//
60+
// That prevents issues steming from recursive `#[inline(always)]` at a *relatively* small cost.
61+
// We *only* need to check all the terminators of a function marked with this attribute.
62+
if resursively_inline(cx, instance) {
63+
Some(FnAttribute::Inline)
64+
} else {
65+
Some(FnAttribute::AlwaysInline)
66+
}
67+
}
2268
InlineAttr::Hint => Some(FnAttribute::Inline),
23-
InlineAttr::Always | InlineAttr::Force { .. } => Some(FnAttribute::AlwaysInline),
69+
InlineAttr::Force { .. } => Some(FnAttribute::AlwaysInline),
2470
InlineAttr::Never => {
2571
if cx.sess().target.arch != "amdgpu" {
2672
Some(FnAttribute::NoInline)
@@ -52,7 +98,7 @@ pub fn from_fn_attrs<'gcc, 'tcx>(
5298
} else {
5399
codegen_fn_attrs.inline
54100
};
55-
if let Some(attr) = inline_attr(cx, inline) {
101+
if let Some(attr) = inline_attr(cx, inline, instance) {
56102
if let FnAttribute::AlwaysInline = attr {
57103
func.add_attribute(FnAttribute::Inline);
58104
}

tests/run/always_inline.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Compiler:
2+
//
3+
// Run-time:
4+
// status: 0
5+
6+
#![feature(no_core)]
7+
#![no_std]
8+
#![no_core]
9+
#![no_main]
10+
11+
extern crate mini_core;
12+
use mini_core::*;
13+
#[inline(always)]
14+
fn fib(n: u8) -> u8 {
15+
if n == 0 {
16+
return 1;
17+
};
18+
if n == 1 {
19+
return 1;
20+
};
21+
fib(n - 1) + fib(n - 2)
22+
}
23+
#[inline(always)]
24+
fn fib_b(n: u8) -> u8 {
25+
if n == 0 {
26+
return 1;
27+
};
28+
if n == 1 {
29+
return 1;
30+
};
31+
fib_a(n - 1) + fib_a(n - 2)
32+
}
33+
#[inline(always)]
34+
fn fib_a(n: u8) -> u8 {
35+
if n == 0 {
36+
return 1;
37+
};
38+
if n == 1 {
39+
return 1;
40+
};
41+
fib_b(n - 1) + fib_b(n - 2)
42+
}
43+
#[no_mangle]
44+
extern "C" fn main(argc: i32, _argv: *const *const u8) -> i32 {
45+
if fib(2) != fib_a(2) {
46+
intrinsics::abort();
47+
}
48+
0
49+
}

0 commit comments

Comments
 (0)