Skip to content

Commit 381511a

Browse files
committed
Auto merge of rust-lang#132566 - saethlin:querify-mir-collection, r=<try>
Querify mir collection Factored out of rust-lang#131650, these changes are required for post-mono MIR opts but I want to benchmark them on their own so that I can tune the implementation. r? ghost
2 parents 43c7805 + 1add34f commit 381511a

File tree

13 files changed

+207
-104
lines changed

13 files changed

+207
-104
lines changed

Diff for: compiler/rustc_middle/src/mir/mono.rs

+48-25
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_target::spec::SymbolVisibility;
1919
use tracing::debug;
2020

2121
use crate::dep_graph::{DepNode, WorkProduct, WorkProductId};
22+
use crate::query::Providers;
2223
use crate::ty::{GenericArgs, Instance, InstanceKind, SymbolName, TyCtxt};
2324

2425
/// Describes how a monomorphization will be instantiated in object files.
@@ -46,7 +47,7 @@ pub enum InstantiationMode {
4647
LocalCopy,
4748
}
4849

49-
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash, HashStable)]
50+
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash, HashStable, TyEncodable, TyDecodable)]
5051
pub enum MonoItem<'tcx> {
5152
Fn(Instance<'tcx>),
5253
Static(DefId),
@@ -62,30 +63,6 @@ impl<'tcx> MonoItem<'tcx> {
6263
}
6364
}
6465

65-
// Note: if you change how item size estimates work, you might need to
66-
// change NON_INCR_MIN_CGU_SIZE as well.
67-
pub fn size_estimate(&self, tcx: TyCtxt<'tcx>) -> usize {
68-
match *self {
69-
MonoItem::Fn(instance) => {
70-
match instance.def {
71-
// "Normal" functions size estimate: the number of
72-
// statements, plus one for the terminator.
73-
InstanceKind::Item(..)
74-
| InstanceKind::DropGlue(..)
75-
| InstanceKind::AsyncDropGlueCtorShim(..) => {
76-
let mir = tcx.instance_mir(instance.def);
77-
mir.basic_blocks.iter().map(|bb| bb.statements.len() + 1).sum()
78-
}
79-
// Other compiler-generated shims size estimate: 1
80-
_ => 1,
81-
}
82-
}
83-
// Conservatively estimate the size of a static declaration or
84-
// assembly item to be 1.
85-
MonoItem::Static(_) | MonoItem::GlobalAsm(_) => 1,
86-
}
87-
}
88-
8966
pub fn is_generic_fn(&self) -> bool {
9067
match self {
9168
MonoItem::Fn(instance) => instance.args.non_erasable_generics().next().is_some(),
@@ -556,3 +533,49 @@ impl<'tcx> CodegenUnitNameBuilder<'tcx> {
556533
Symbol::intern(&cgu_name)
557534
}
558535
}
536+
537+
/// See module-level docs on some contect for "mentioned" items.
538+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable)]
539+
pub enum CollectionMode {
540+
/// Collect items that are used, i.e., actually needed for codegen.
541+
///
542+
/// Which items are used can depend on optimization levels, as MIR optimizations can remove
543+
/// uses.
544+
UsedItems,
545+
/// Collect items that are mentioned. The goal of this mode is that it is independent of
546+
/// optimizations: the set of "mentioned" items is computed before optimizations are run.
547+
///
548+
/// The exact contents of this set are *not* a stable guarantee. (For instance, it is currently
549+
/// computed after drop-elaboration. If we ever do some optimizations even in debug builds, we
550+
/// might decide to run them before computing mentioned items.) The key property of this set is
551+
/// that it is optimization-independent.
552+
MentionedItems,
553+
}
554+
555+
// Note: if you change how item size estimates work, you might need to
556+
// change NON_INCR_MIN_CGU_SIZE as well.
557+
fn size_estimate<'tcx>(tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>) -> usize {
558+
match item {
559+
MonoItem::Fn(instance) => {
560+
match instance.def {
561+
// "Normal" functions size estimate: the number of
562+
// statements, plus one for the terminator.
563+
InstanceKind::Item(..)
564+
| InstanceKind::DropGlue(..)
565+
| InstanceKind::AsyncDropGlueCtorShim(..) => {
566+
let mir = tcx.instance_mir(instance.def);
567+
mir.basic_blocks.iter().map(|bb| bb.statements.len() + 1).sum()
568+
}
569+
// Other compiler-generated shims size estimate: 1
570+
_ => 1,
571+
}
572+
}
573+
// Conservatively estimate the size of a static declaration or
574+
// assembly item to be 1.
575+
MonoItem::Static(_) | MonoItem::GlobalAsm(_) => 1,
576+
}
577+
}
578+
579+
pub fn provide(providers: &mut Providers) {
580+
providers.size_estimate = size_estimate;
581+
}

Diff for: compiler/rustc_middle/src/query/erase.rs

+4
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ impl<T0, T1> EraseType for (&'_ T0, &'_ [T1]) {
216216
type Result = [u8; size_of::<(&'static (), &'static [()])>()];
217217
}
218218

219+
impl<T0, T1> EraseType for (&'_ [T0], &'_ [T1]) {
220+
type Result = [u8; size_of::<(&'static [()], &'static [()])>()];
221+
}
222+
219223
impl<T0> EraseType for (&'_ T0, Result<(), ErrorGuaranteed>) {
220224
type Result = [u8; size_of::<(&'static (), Result<(), ErrorGuaranteed>)>()];
221225
}

Diff for: compiler/rustc_middle/src/query/keys.rs

+18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use rustc_span::symbol::{Ident, Symbol};
77
use rustc_span::{DUMMY_SP, Span};
88

99
use crate::infer::canonical::CanonicalQueryInput;
10+
use crate::mir::mono::CollectionMode;
11+
use crate::query::MonoItem;
1012
use crate::ty::fast_reject::SimplifiedType;
1113
use crate::ty::layout::{TyAndLayout, ValidityRequirement};
1214
use crate::ty::{self, GenericArg, GenericArgsRef, Ty, TyCtxt};
@@ -85,6 +87,14 @@ impl<'tcx> Key for ty::Instance<'tcx> {
8587
}
8688
}
8789

90+
impl<'tcx> Key for MonoItem<'tcx> {
91+
type Cache<V> = DefaultCache<Self, V>;
92+
93+
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
94+
tcx.def_span(self.def_id())
95+
}
96+
}
97+
8898
impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
8999
type Cache<V> = DefaultCache<Self, V>;
90100

@@ -590,3 +600,11 @@ impl<'tcx> Key for (ValidityRequirement, ty::ParamEnvAnd<'tcx, Ty<'tcx>>) {
590600
}
591601
}
592602
}
603+
604+
impl<'tcx> Key for (ty::Instance<'tcx>, CollectionMode) {
605+
type Cache<V> = DefaultCache<Self, V>;
606+
607+
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
608+
self.0.default_span(tcx)
609+
}
610+
}

Diff for: compiler/rustc_middle/src/query/mod.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use rustc_session::cstore::{
4040
};
4141
use rustc_session::lint::LintExpectationId;
4242
use rustc_span::def_id::LOCAL_CRATE;
43+
use rustc_span::source_map::Spanned;
4344
use rustc_span::symbol::Symbol;
4445
use rustc_span::{DUMMY_SP, Span};
4546
use rustc_target::spec::PanicStrategy;
@@ -59,7 +60,7 @@ use crate::mir::interpret::{
5960
EvalStaticInitializerRawResult, EvalToAllocationRawResult, EvalToConstValueResult,
6061
EvalToValTreeResult, GlobalId, LitToConstError, LitToConstInput,
6162
};
62-
use crate::mir::mono::CodegenUnit;
63+
use crate::mir::mono::{CodegenUnit, CollectionMode, MonoItem};
6364
use crate::query::erase::{Erase, erase, restore};
6465
use crate::query::plumbing::{
6566
CyclePlaceholder, DynamicQuery, query_ensure, query_ensure_error_guaranteed, query_get_at,
@@ -2314,6 +2315,16 @@ rustc_queries! {
23142315
desc { "whether the item should be made inlinable across crates" }
23152316
separate_provide_extern
23162317
}
2318+
2319+
query items_of_instance(key: (ty::Instance<'tcx>, CollectionMode)) -> (&'tcx [Spanned<MonoItem<'tcx>>], &'tcx [Spanned<MonoItem<'tcx>>]) {
2320+
desc { "collecting items used by `{}`", key.0 }
2321+
cache_on_disk_if { true }
2322+
}
2323+
2324+
query size_estimate(key: MonoItem<'tcx>) -> usize {
2325+
desc { "estimating codegen size of `{}`", key }
2326+
cache_on_disk_if { true }
2327+
}
23172328
}
23182329

23192330
rustc_query_append! { define_callbacks! }

Diff for: compiler/rustc_middle/src/query/on_disk_cache.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_index::{Idx, IndexVec};
1212
use rustc_macros::{Decodable, Encodable};
1313
use rustc_middle::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
1414
use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
15+
use rustc_middle::mir::mono::MonoItem;
1516
use rustc_middle::mir::{self, interpret};
1617
use rustc_middle::ty::codec::{RefDecodable, TyDecoder, TyEncoder};
1718
use rustc_middle::ty::{self, Ty, TyCtxt};
@@ -22,7 +23,7 @@ use rustc_session::Session;
2223
use rustc_span::hygiene::{
2324
ExpnId, HygieneDecodeContext, HygieneEncodeContext, SyntaxContext, SyntaxContextData,
2425
};
25-
use rustc_span::source_map::SourceMap;
26+
use rustc_span::source_map::{SourceMap, Spanned};
2627
use rustc_span::{
2728
BytePos, CachingSourceMapView, ExpnData, ExpnHash, Pos, RelativeBytePos, SourceFile, Span,
2829
SpanDecoder, SpanEncoder, StableSourceFileId, Symbol,
@@ -773,6 +774,13 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [rustc_ast::InlineAsm
773774
}
774775
}
775776

777+
impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [Spanned<MonoItem<'tcx>>] {
778+
#[inline]
779+
fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
780+
RefDecodable::decode(d)
781+
}
782+
}
783+
776784
impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>>
777785
for &'tcx crate::traits::specialization_graph::Graph
778786
{

Diff for: compiler/rustc_middle/src/ty/codec.rs

+11
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ use std::marker::DiscriminantKind;
1313
use rustc_abi::{FieldIdx, VariantIdx};
1414
use rustc_data_structures::fx::FxHashMap;
1515
use rustc_hir::def_id::LocalDefId;
16+
use rustc_middle::mir::mono::MonoItem;
1617
use rustc_middle::ty::TyCtxt;
1718
use rustc_serialize::{Decodable, Encodable};
1819
use rustc_span::Span;
20+
use rustc_span::source_map::Spanned;
1921
pub use rustc_type_ir::{TyDecoder, TyEncoder};
2022

2123
use crate::arena::ArenaAllocatable;
@@ -397,6 +399,15 @@ impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D>
397399
}
398400
}
399401

402+
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D> for [Spanned<MonoItem<'tcx>>] {
403+
fn decode(decoder: &mut D) -> &'tcx Self {
404+
decoder
405+
.interner()
406+
.arena
407+
.alloc_from_iter((0..decoder.read_usize()).map(|_| Decodable::decode(decoder)))
408+
}
409+
}
410+
400411
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D>
401412
for ty::List<ty::BoundVariableKind>
402413
{

Diff for: compiler/rustc_middle/src/ty/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2154,6 +2154,7 @@ pub fn provide(providers: &mut Providers) {
21542154
print::provide(providers);
21552155
super::util::bug::provide(providers);
21562156
super::middle::provide(providers);
2157+
super::mir::mono::provide(providers);
21572158
*providers = Providers {
21582159
trait_impls_of: trait_def::trait_impls_of_provider,
21592160
incoherent_impls: trait_def::incoherent_impls_provider,

0 commit comments

Comments
 (0)