Skip to content

Commit adeb3f4

Browse files
committed
Deduce param attrs from codegen MIR
1 parent 7964969 commit adeb3f4

File tree

6 files changed

+13
-51
lines changed

6 files changed

+13
-51
lines changed

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -282,20 +282,6 @@ provide! { tcx, def_id, other, cdata,
282282
.unwrap_or_else(|| panic!("{def_id:?} does not have eval_static_initializer")))
283283
}
284284
trait_def => { table }
285-
deduced_param_attrs => {
286-
// FIXME: `deduced_param_attrs` has some sketchy encoding settings,
287-
// where we don't encode unless we're optimizing, doing codegen,
288-
// and not incremental (see `encoder.rs`). I don't think this is right!
289-
cdata
290-
.root
291-
.tables
292-
.deduced_param_attrs
293-
.get(cdata, def_id.index)
294-
.map(|lazy| {
295-
&*tcx.arena.alloc_from_iter(lazy.decode((cdata, tcx)))
296-
})
297-
.unwrap_or_default()
298-
}
299285
opaque_ty_origin => { table }
300286
assumed_wf_types_for_rpitit => { table }
301287
collect_return_position_impl_trait_in_trait_tys => {

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc_middle::ty::codec::TyEncoder;
2727
use rustc_middle::ty::fast_reject::{self, TreatParams};
2828
use rustc_middle::{bug, span_bug};
2929
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder, opaque};
30-
use rustc_session::config::{CrateType, OptLevel, TargetModifier};
30+
use rustc_session::config::{CrateType, TargetModifier};
3131
use rustc_span::hygiene::HygieneEncodeContext;
3232
use rustc_span::{
3333
ByteSymbol, ExternalSource, FileName, SourceFile, SpanData, SpanEncoder, StableSourceFileId,
@@ -1822,21 +1822,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
18221822
record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses);
18231823
}
18241824
}
1825-
1826-
// Encode all the deduced parameter attributes for everything that has MIR, even for items
1827-
// that can't be inlined. But don't if we aren't optimizing in non-incremental mode, to
1828-
// save the query traffic.
1829-
if tcx.sess.opts.output_types.should_codegen()
1830-
&& tcx.sess.opts.optimize != OptLevel::No
1831-
&& tcx.sess.opts.incremental.is_none()
1832-
{
1833-
for &local_def_id in tcx.mir_keys(()) {
1834-
if let DefKind::AssocFn | DefKind::Fn = tcx.def_kind(local_def_id) {
1835-
record_array!(self.tables.deduced_param_attrs[local_def_id.to_def_id()] <-
1836-
self.tcx.deduced_param_attrs(local_def_id.to_def_id()));
1837-
}
1838-
}
1839-
}
18401825
}
18411826

18421827
#[instrument(level = "debug", skip(self))]

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_middle::middle::lib_features::FeatureStability;
2929
use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
3030
use rustc_middle::mir;
3131
use rustc_middle::ty::fast_reject::SimplifiedType;
32-
use rustc_middle::ty::{self, DeducedParamAttrs, Ty, TyCtxt, UnusedGenericParams};
32+
use rustc_middle::ty::{self, Ty, TyCtxt, UnusedGenericParams};
3333
use rustc_middle::util::Providers;
3434
use rustc_serialize::opaque::FileEncoder;
3535
use rustc_session::config::{SymbolManglingVersion, TargetModifier};
@@ -462,7 +462,6 @@ define_tables! {
462462
assoc_container: Table<DefIndex, ty::AssocItemContainer>,
463463
macro_definition: Table<DefIndex, LazyValue<ast::DelimArgs>>,
464464
proc_macro: Table<DefIndex, MacroKind>,
465-
deduced_param_attrs: Table<DefIndex, LazyArray<DeducedParamAttrs>>,
466465
trait_impl_trait_tys: Table<DefIndex, LazyValue<DefIdMap<ty::EarlyBinder<'static, Ty<'static>>>>>,
467466
doc_link_resolutions: Table<DefIndex, LazyValue<DocLinkResMap>>,
468467
doc_link_traits_in_scope: Table<DefIndex, LazyArray<DefId>>,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,9 +2632,8 @@ rustc_queries! {
26322632
return_result_from_ensure_ok
26332633
}
26342634

2635-
query deduced_param_attrs(def_id: DefId) -> &'tcx [ty::DeducedParamAttrs] {
2636-
desc { |tcx| "deducing parameter attributes for {}", tcx.def_path_str(def_id) }
2637-
separate_provide_extern
2635+
query deduced_param_attrs(instance: ty::Instance<'tcx>) -> &'tcx [ty::DeducedParamAttrs] {
2636+
desc { |tcx| "deducing parameter attributes for {}", instance }
26382637
}
26392638

26402639
query doc_link_resolutions(def_id: DefId) -> &'tcx DocLinkResMap {

compiler/rustc_mir_transform/src/deduce_param_attrs.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
//! purposes on a best-effort basis. We compute them here and store them into the crate metadata so
66
//! dependent crates can use them.
77
8-
use rustc_hir::def_id::LocalDefId;
98
use rustc_index::bit_set::DenseBitSet;
109
use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
1110
use rustc_middle::mir::{Body, Location, Operand, Place, RETURN_PLACE, Terminator, TerminatorKind};
12-
use rustc_middle::ty::{self, DeducedParamAttrs, Ty, TyCtxt};
11+
use rustc_middle::ty::{self, DeducedParamAttrs, Instance, Ty, TyCtxt};
1312
use rustc_session::config::OptLevel;
1413

1514
/// A visitor that determines which arguments have been mutated. We can't use the mutability field
@@ -123,7 +122,7 @@ fn type_will_always_be_passed_directly(ty: Ty<'_>) -> bool {
123122
/// dependent crates can use them.
124123
pub(super) fn deduced_param_attrs<'tcx>(
125124
tcx: TyCtxt<'tcx>,
126-
def_id: LocalDefId,
125+
instance: Instance<'tcx>,
127126
) -> &'tcx [DeducedParamAttrs] {
128127
// This computation is unfortunately rather expensive, so don't do it unless we're optimizing.
129128
// Also skip it in incremental mode.
@@ -138,7 +137,7 @@ pub(super) fn deduced_param_attrs<'tcx>(
138137

139138
// Codegen won't use this information for anything if all the function parameters are passed
140139
// directly. Detect that and bail, for compilation speed.
141-
let fn_ty = tcx.type_of(def_id).instantiate_identity();
140+
let fn_ty = tcx.type_of(instance.def_id()).instantiate_identity();
142141
if matches!(fn_ty.kind(), ty::FnDef(..))
143142
&& fn_ty
144143
.fn_sig(tcx)
@@ -151,13 +150,8 @@ pub(super) fn deduced_param_attrs<'tcx>(
151150
return &[];
152151
}
153152

154-
// Don't deduce any attributes for functions that have no MIR.
155-
if !tcx.is_mir_available(def_id) {
156-
return &[];
157-
}
158-
159-
// Grab the optimized MIR. Analyze it to determine which arguments have been mutated.
160-
let body: &Body<'tcx> = tcx.optimized_mir(def_id);
153+
// Grab the codegen MIR. Analyze it to determine which arguments have been mutated.
154+
let body: &Body<'tcx> = tcx.build_codegen_mir(instance);
161155
let mut deduce_read_only = DeduceReadOnly::new(body.arg_count);
162156
deduce_read_only.visit_body(body);
163157

compiler/rustc_ty_utils/src/abi.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ use rustc_middle::query::Providers;
99
use rustc_middle::ty::layout::{
1010
FnAbiError, HasTyCtxt, HasTypingEnv, LayoutCx, LayoutOf, TyAndLayout, fn_can_unwind,
1111
};
12-
use rustc_middle::ty::{self, InstanceKind, Ty, TyCtxt};
12+
use rustc_middle::ty::{self, Instance, InstanceKind, Ty, TyCtxt};
1313
use rustc_session::config::OptLevel;
1414
use rustc_span::DUMMY_SP;
15-
use rustc_span::def_id::DefId;
1615
use rustc_target::callconv::{
1716
AbiMap, ArgAbi, ArgAttribute, ArgAttributes, ArgExtension, FnAbi, PassMode,
1817
};
@@ -573,7 +572,7 @@ fn fn_abi_new_uncached<'tcx>(
573572
// functions from vtable. And for a tls shim, passing the `fn_def_id` would refer to
574573
// the underlying static. Internally, `deduced_param_attrs` attempts to infer attributes
575574
// by visit the function body.
576-
determined_fn_def_id,
575+
instance,
577576
);
578577
debug!("fn_abi_new_uncached = {:?}", fn_abi);
579578
fn_abi_sanity_check(cx, &fn_abi, sig.abi);
@@ -585,7 +584,7 @@ fn fn_abi_adjust_for_abi<'tcx>(
585584
cx: &LayoutCx<'tcx>,
586585
fn_abi: &mut FnAbi<'tcx, Ty<'tcx>>,
587586
abi: ExternAbi,
588-
fn_def_id: Option<DefId>,
587+
instance: Option<Instance<'tcx>>,
589588
) {
590589
if abi == ExternAbi::Unadjusted {
591590
// The "unadjusted" ABI passes aggregates in "direct" mode. That's fragile but needed for
@@ -619,7 +618,7 @@ fn fn_abi_adjust_for_abi<'tcx>(
619618
// as appropriate.
620619
let deduced_param_attrs =
621620
if tcx.sess.opts.optimize != OptLevel::No && tcx.sess.opts.incremental.is_none() {
622-
fn_def_id.map(|fn_def_id| tcx.deduced_param_attrs(fn_def_id)).unwrap_or_default()
621+
instance.map(|instance| tcx.deduced_param_attrs(instance)).unwrap_or_default()
623622
} else {
624623
&[]
625624
};

0 commit comments

Comments
 (0)