Description
Proposal
Summary and problem statement
Change the semantics of #[inline]
attribute.
-
Currently in Rust
#[inline]
is following LLVM-style inline policies
**#[inline(always)]
->alwaysinline
**#[inline]
->inlinehint
** N/A -> N/A
**#[inline(never)]
-neverinline
-
However this is unnecessarily complex and hard to use.
-
It also causes compilation performance issues. (e.g. thousands copies of
Option::map
inrustc_middle
according tocargo-llvm-lines
, to ask llvm to examine the inlining possibilities one-by-one)
Motivation, use-cases, and solution sketches
-
Inlining should ideally happen before monomorphization.
-
Inlining should ideally be made as deterministic as possible (respecting user's intention).
-
I propose replacing the semantics of these attributes to:
**#[inline(always)]
-> do inline, not just a hint (always invoke deterministic inlining before monomorphization), raise a warning or error when failing to do so.
*** Feedback: It was suggested that this become#[inline(required)]
, and the check be a lint.
**#[inline]
-> do inline, not just a hint (always invoke deterministic inlining before monomorphization), fallback to not inlining when failing to do so with good reason TBD: <Language should identify and list the possible reasons.> (and maybe tag asalwaysinline
to ask llvm to try again).
** N/A -> keeping current behavior here: heuristically determine whether to inline or not, left to compiler internals (maybe invoke deterministic inlining before monomorphization,inlinehint
).
**#[inline(never)]
-> do not inline, not just a hint (do not invoke deterministic inlining before monomorphization,neverinline
to stop llvm from doing so too) -
This will not be a breaking change. There're performance impacts, but hopefully a positive one.
Prioritization
-
This is related to the "Targeted ergonomic wins and extensions" because it improves building experience. It is also relatively not a large change.
-
It was mentioned on zulip that the current implementation MIR-inliner is not fully ready. (Need to recheck the feasibility of using MIR inliner to provide the expected "deterministic inlining before monomorphization" behavior and timeframe approximation.)
Links and related work
inline
keyword is currently mainly used to workaround ODR in C++ language. It causes confusion to beginners too.
Initial people involved
TBD
What happens now?
This issue is part of the experimental MCP process described in RFC 2936. Once this issue is filed, a Zulip topic will be opened for discussion, and the lang-team will review open MCPs in its weekly triage meetings. You should receive feedback within a week or two.
This issue is not meant to be used for technical discussion. There is a Zulip stream for that. Use this issue to leave procedural comments, such as volunteering to review, indicating that you second the proposal (or third, etc), or raising a concern that you would like to be addressed.
EDIT:
- edited all mentioning of "MIR-inlining" to "deterministic inlining before monomorphization", since the former is an implementation detail.
- clarified that for the 'N/A' cause, the semantic is not changed.
- clarified that it's no longer a hint.