forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#122568 - RalfJung:mentioned-items, r=<try>
Draft: recursively evaluate the constants in everything that is 'mentioned' This is another attempt at fixing rust-lang#107503. The previous attempt at rust-lang#112879 seems stuck in figuring out where the [perf regression](https://perf.rust-lang.org/compare.html?start=c55d1ee8d4e3162187214692229a63c2cc5e0f31&end=ec8de1ebe0d698b109beeaaac83e60f4ef8bb7d1&stat=instructions:u) comes from. In rust-lang#122258 I learned some things, which informed the approach this PR is taking. r? `@ghost`
- Loading branch information
Showing
27 changed files
with
659 additions
and
151 deletions.
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,58 @@ | ||
use rustc_middle::mir::visit::Visitor; | ||
use rustc_middle::mir::{self, ConstOperand, Location, MentionedItem, MirPass}; | ||
use rustc_middle::ty::{self, TyCtxt}; | ||
use rustc_session::Session; | ||
use rustc_span::source_map::Spanned; | ||
|
||
pub struct MentionedItems; | ||
|
||
struct MentionedItemsVisitor<'a, 'tcx> { | ||
tcx: TyCtxt<'tcx>, | ||
body: &'a mir::Body<'tcx>, | ||
mentioned_items: &'a mut Vec<Spanned<MentionedItem<'tcx>>>, | ||
} | ||
|
||
impl<'tcx> MirPass<'tcx> for MentionedItems { | ||
fn is_enabled(&self, _sess: &Session) -> bool { | ||
// We need to do always do that as the collector will only check the 'mentioned' items of | ||
// 'mentioned' items, so if some item X gets compiled without optimizations but later called | ||
// from somewhere that has optimizations enabled, we need the `mentioned_items` of X to be | ||
// filled. | ||
// Furthermore, even debug builds use level 1, so special-casing level 0 is just not worth it. | ||
true | ||
} | ||
|
||
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut mir::Body<'tcx>) { | ||
debug_assert!(body.mentioned_items.is_empty()); | ||
let mut mentioned_items = Vec::new(); | ||
MentionedItemsVisitor { tcx, body, mentioned_items: &mut mentioned_items }.visit_body(body); | ||
body.mentioned_items = mentioned_items; | ||
} | ||
} | ||
|
||
impl<'tcx> Visitor<'tcx> for MentionedItemsVisitor<'_, 'tcx> { | ||
fn visit_constant(&mut self, constant: &ConstOperand<'tcx>, _: Location) { | ||
let const_ = constant.const_; | ||
// This is how function items get referenced: via constants of `FnDef` type. This handles | ||
// both functions that are called anf those that are just turned to function pointers. | ||
if let ty::FnDef(def_id, args) = const_.ty().kind() { | ||
debug!("adding to required_items: {def_id:?}"); | ||
self.mentioned_items | ||
.push(Spanned { node: MentionedItem::Fn(*def_id, args), span: constant.span }); | ||
} | ||
} | ||
|
||
fn visit_terminator(&mut self, terminator: &mir::Terminator<'tcx>, location: Location) { | ||
self.super_terminator(terminator, location); | ||
match terminator.kind { | ||
// We don't need to handle `Call` as we already handled all function type operands in | ||
// `visit_constant`. But we do need to handle `Drop`. | ||
mir::TerminatorKind::Drop { place, .. } => { | ||
let ty = place.ty(self.body, self.tcx).ty; | ||
let span = self.body.source_info(location).span; | ||
self.mentioned_items.push(Spanned { node: MentionedItem::Drop(ty), span }); | ||
} | ||
_ => {} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.