-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Create a separate query for required and mentioned items instead of tracking them in the MIR body #123488
Closed
Closed
Create a separate query for required and mentioned items instead of tracking them in the MIR body #123488
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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 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,68 @@ | ||
use rustc_span::source_map::Spanned; | ||
|
||
use crate::ty::{self, Ty, TyCtxt}; | ||
|
||
use super::ConstOperand; | ||
|
||
#[derive(Debug, HashStable, TyEncodable, TyDecodable, Default)] | ||
pub struct RequiredAndMentionedItems<'tcx> { | ||
/// Constants that are required to evaluate successfully for this MIR to be well-formed. | ||
/// We hold in this field all the constants we are not able to evaluate yet. | ||
/// | ||
/// This is soundness-critical, we make a guarantee that all consts syntactically mentioned in a | ||
/// function have successfully evaluated if the function ever gets executed at runtime. | ||
pub required_consts: Vec<ConstOperand<'tcx>>, | ||
|
||
/// Further items that were mentioned in this function and hence *may* become monomorphized, | ||
/// depending on optimizations. We use this to avoid optimization-dependent compile errors: the | ||
/// collector recursively traverses all "mentioned" items and evaluates all their | ||
/// `required_consts`. | ||
/// | ||
/// This is *not* soundness-critical and the contents of this list are *not* a stable guarantee. | ||
/// All that's relevant is that this set is optimization-level-independent, and that it includes | ||
/// everything that the collector would consider "used". (For example, we currently compute this | ||
/// set after drop elaboration, so some drop calls that can never be reached are not considered | ||
/// "mentioned".) See the documentation of `CollectionMode` in | ||
/// `compiler/rustc_monomorphize/src/collector.rs` for more context. | ||
pub mentioned_items: Vec<Spanned<MentionedItem<'tcx>>>, | ||
} | ||
|
||
/// Some item that needs to monomorphize successfully for a MIR body to be considered well-formed. | ||
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, HashStable, TyEncodable, TyDecodable)] | ||
#[derive(TypeFoldable, TypeVisitable)] | ||
pub enum MentionedItem<'tcx> { | ||
/// A function that gets called. We don't necessarily know its precise type yet, since it can be | ||
/// hidden behind a generic. | ||
Fn(Ty<'tcx>), | ||
/// A type that has its drop shim called. | ||
Drop(Ty<'tcx>), | ||
/// Unsizing casts might require vtables, so we have to record them. | ||
UnsizeCast { source_ty: Ty<'tcx>, target_ty: Ty<'tcx> }, | ||
/// A closure that is coerced to a function pointer. | ||
Closure(Ty<'tcx>), | ||
} | ||
|
||
impl<'tcx> TyCtxt<'tcx> { | ||
pub fn required_and_mentioned_items( | ||
self, | ||
key: ty::InstanceDef<'tcx>, | ||
) -> &'tcx RequiredAndMentionedItems<'tcx> { | ||
match key { | ||
ty::InstanceDef::Item(id) => self.required_and_mentioned_items_of_item(id), | ||
ty::InstanceDef::Intrinsic(_) | ||
| ty::InstanceDef::VTableShim(_) | ||
| ty::InstanceDef::ReifyShim(..) | ||
| ty::InstanceDef::FnPtrShim(_, _) | ||
| ty::InstanceDef::Virtual(_, _) | ||
| ty::InstanceDef::ClosureOnceShim { .. } | ||
| ty::InstanceDef::ConstructCoroutineInClosureShim { .. } | ||
| ty::InstanceDef::CoroutineKindShim { .. } | ||
| ty::InstanceDef::ThreadLocalShim(_) | ||
| ty::InstanceDef::DropGlue(_, _) | ||
| ty::InstanceDef::CloneShim(_, _) | ||
| ty::InstanceDef::FnPtrAddrShim(_, _) => { | ||
self.required_and_mentioned_items_of_shim(key) | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 |
---|---|---|
|
@@ -566,8 +566,7 @@ impl<'tcx> Inliner<'tcx> { | |
mut callee_body: Body<'tcx>, | ||
) { | ||
let terminator = caller_body[callsite.block].terminator.take().unwrap(); | ||
let TerminatorKind::Call { func, args, destination, unwind, target, .. } = terminator.kind | ||
else { | ||
let TerminatorKind::Call { args, destination, unwind, target, .. } = terminator.kind else { | ||
bug!("unexpected terminator kind {:?}", terminator.kind); | ||
}; | ||
|
||
|
@@ -706,37 +705,6 @@ impl<'tcx> Inliner<'tcx> { | |
source_info: callsite.source_info, | ||
kind: TerminatorKind::Goto { target: integrator.map_block(START_BLOCK) }, | ||
}); | ||
|
||
// Copy only unevaluated constants from the callee_body into the caller_body. | ||
// Although we are only pushing `ConstKind::Unevaluated` consts to | ||
// `required_consts`, here we may not only have `ConstKind::Unevaluated` | ||
// because we are calling `instantiate_and_normalize_erasing_regions`. | ||
caller_body.required_consts.extend(callee_body.required_consts.iter().copied().filter( | ||
|&ct| match ct.const_ { | ||
Const::Ty(_) => { | ||
bug!("should never encounter ty::UnevaluatedConst in `required_consts`") | ||
} | ||
Const::Val(..) | Const::Unevaluated(..) => true, | ||
}, | ||
)); | ||
// Now that we incorporated the callee's `required_consts`, we can remove the callee from | ||
// `mentioned_items` -- but we have to take their `mentioned_items` in return. This does | ||
// some extra work here to save the monomorphization collector work later. It helps a lot, | ||
// since monomorphization can avoid a lot of work when the "mentioned items" are similar to | ||
// the actually used items. By doing this we can entirely avoid visiting the callee! | ||
// We need to reconstruct the `required_item` for the callee so that we can find and | ||
// remove it. | ||
let callee_item = MentionedItem::Fn(func.ty(caller_body, self.tcx)); | ||
if let Some(idx) = | ||
caller_body.mentioned_items.iter().position(|item| item.node == callee_item) | ||
{ | ||
// We found the callee, so remove it and add its items instead. | ||
caller_body.mentioned_items.remove(idx); | ||
caller_body.mentioned_items.extend(callee_body.mentioned_items); | ||
} else { | ||
// If we can't find the callee, there's no point in adding its items. | ||
// Probably it already got removed by being inlined elsewhere in the same function. | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing this optimization will also not be great for perf, as it means the "mentioned items" traversal has to visit all the functions that got inlined. |
||
} | ||
|
||
fn make_call_args( | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not incorporating inlined consts can lead to ICEs in the interpreter: we may be inlining a function that contains a const that fails. The interpreter will evaluate all required_consts (which would not include the inlined const) and then assert that all const-eval during function evaluation succeeds.