-
Notifications
You must be signed in to change notification settings - Fork 13.4k
mir_transform: implement #[rustc_force_inline]
#134082
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f86169a
mir_transform: implement forced inlining
davidtwco 02d423c
codegen_attrs: force inlining takes precedence
davidtwco 4507939
inline: force inlining shims
davidtwco e4bae91
inline: re-introduce some callee body checks
davidtwco 90066c0
inline: remove unnecessary promoted check
davidtwco 5f316f5
validator: move force inline check
davidtwco dbec6be
inline: move should inline check
davidtwco 3169a44
don't collect `#[rustc_force_inline]` in eager mode
davidtwco ce602ac
clarify `target_feature` + forced inlining
davidtwco cc9a9ec
mir_build: check annotated functions w/out callers
davidtwco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use rustc_attr_parsing::InlineAttr; | ||
use rustc_hir::def_id::DefId; | ||
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; | ||
use rustc_middle::mir::{Body, TerminatorKind}; | ||
use rustc_middle::ty; | ||
use rustc_middle::ty::TyCtxt; | ||
use rustc_span::sym; | ||
|
||
/// Check that a body annotated with `#[rustc_force_inline]` will not fail to inline based on its | ||
/// definition alone (irrespective of any specific caller). | ||
pub(crate) fn check_force_inline<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) { | ||
let def_id = body.source.def_id(); | ||
if !tcx.hir().body_owner_kind(def_id).is_fn_or_closure() || !def_id.is_local() { | ||
return; | ||
} | ||
let InlineAttr::Force { attr_span, .. } = tcx.codegen_fn_attrs(def_id).inline else { | ||
return; | ||
}; | ||
|
||
if let Err(reason) = | ||
is_inline_valid_on_fn(tcx, def_id).and_then(|_| is_inline_valid_on_body(tcx, body)) | ||
{ | ||
tcx.dcx().emit_err(crate::errors::InvalidForceInline { | ||
attr_span, | ||
callee_span: tcx.def_span(def_id), | ||
callee: tcx.def_path_str(def_id), | ||
reason, | ||
}); | ||
} | ||
} | ||
|
||
pub fn is_inline_valid_on_fn<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Result<(), &'static str> { | ||
let codegen_attrs = tcx.codegen_fn_attrs(def_id); | ||
if tcx.has_attr(def_id, sym::rustc_no_mir_inline) { | ||
return Err("#[rustc_no_mir_inline]"); | ||
} | ||
|
||
// FIXME(#127234): Coverage instrumentation currently doesn't handle inlined | ||
// MIR correctly when Modified Condition/Decision Coverage is enabled. | ||
if tcx.sess.instrument_coverage_mcdc() { | ||
return Err("incompatible with MC/DC coverage"); | ||
} | ||
|
||
let ty = tcx.type_of(def_id); | ||
if match ty.instantiate_identity().kind() { | ||
ty::FnDef(..) => tcx.fn_sig(def_id).instantiate_identity().c_variadic(), | ||
ty::Closure(_, args) => args.as_closure().sig().c_variadic(), | ||
_ => false, | ||
} { | ||
return Err("C variadic"); | ||
} | ||
|
||
if codegen_attrs.flags.contains(CodegenFnAttrFlags::COLD) { | ||
return Err("cold"); | ||
} | ||
|
||
// Intrinsic fallback bodies are automatically made cross-crate inlineable, | ||
// but at this stage we don't know whether codegen knows the intrinsic, | ||
// so just conservatively don't inline it. This also ensures that we do not | ||
// accidentally inline the body of an intrinsic that *must* be overridden. | ||
if tcx.has_attr(def_id, sym::rustc_intrinsic) { | ||
return Err("callee is an intrinsic"); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn is_inline_valid_on_body<'tcx>( | ||
_: TyCtxt<'tcx>, | ||
body: &Body<'tcx>, | ||
) -> Result<(), &'static str> { | ||
if body | ||
.basic_blocks | ||
.iter() | ||
.any(|bb| matches!(bb.terminator().kind, TerminatorKind::TailCall { .. })) | ||
{ | ||
return Err("can't inline functions with tail calls"); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.