Skip to content

Fixed a recursive inling bug, added a test for it #666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

FractalFir
Copy link
Contributor

Fixes #599.

Instead of always translating #[inline(always)](which is a hint) to FnAttribute::AlwaysInline(which makes inlining mandatory), we now do so only for functions which don't call any functions with the #[inline(always)] or the rustc force inline attribute.

In cases where #[inline(always)] is applied to a function which might be recursive, it is demoted to #[inline] instead.
This breaks the cycle, and prevents libjitgcc errors.

The regression test checks for direct and indirect recursion.

Copy link
Contributor

@antoyo antoyo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

Here are some nitpicks.

@FractalFir FractalFir force-pushed the master branch 2 times, most recently from 6259a7d to f217230 Compare May 9, 2025 19:42
Copy link
Contributor

@antoyo antoyo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nitpicks about formatting and spelling.

Comment on lines +43 to +44
}
/// Get GCC attribute for the provided inline heuristic, attached to `instance`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
/// Get GCC attribute for the provided inline heuristic, attached to `instance`.
}
/// Get GCC attribute for the provided inline heuristic, attached to `instance`.

Comment on lines +12 to +13
use mini_core::*;
#[inline(always)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use mini_core::*;
#[inline(always)]
use mini_core::*;
#[inline(always)]

Comment on lines +22 to +23
}
#[inline(always)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
#[inline(always)]
}
#[inline(always)]

Comment on lines +32 to +33
}
#[inline(always)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
#[inline(always)]
}
#[inline(always)]

Comment on lines +42 to +43
}
#[no_mangle]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
#[no_mangle]
}
#[no_mangle]

use rustc_middle::ty;

use crate::context::CodegenCx;
use crate::gcc_util::to_gcc_features;

/// Get GCC attribute for the provided inline heuristic.
/// Checks if the function `instance` is recursively inline.
/// Returns `false` if a functions is guranteed to be non-recursive, and `true` if it *might* be recursive.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Returns `false` if a functions is guranteed to be non-recursive, and `true` if it *might* be recursive.
/// Returns `false` if a functions is guaranteed to be non-recursive, and `true` if it *might* be recursive.

let body = cx.tcx.optimized_mir(instance.def_id());
for block in body.basic_blocks.iter() {
let Some(ref terminator) = block.terminator else { continue };
// I assmume that the resursive-inline issue applies only to functions, and not to drops.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// I assmume that the resursive-inline issue applies only to functions, and not to drops.
// I assume that the recursive-inline issue applies only to functions, and not to drops.

) -> Option<FnAttribute<'gcc>> {
match inline {
InlineAttr::Always => {
// We can't simply always return `always_inline` unconditionally.
// It is *NOT A HINT* and does not work for recurisve functions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// It is *NOT A HINT* and does not work for recurisve functions.
// It is *NOT A HINT* and does not work for recursive functions.

// It is *NOT A HINT* and does not work for recurisve functions.
//
// So, it can only be applied *if*:
// The current function does not call any functions makred `#[inline(always)]`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// The current function does not call any functions makred `#[inline(always)]`.
// The current function does not call any functions marked `#[inline(always)]`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix #[inline(always)] for recursive functions
2 participants