Skip to content

Commit 316128c

Browse files
committed
optimized_mir
1 parent a841909 commit 316128c

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed

src/librustc_metadata/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
111111
bug!("coerce_unsized_info: `{:?}` is missing its info", def_id);
112112
})
113113
}
114-
optimized_mir => { cdata.get_optimized_mir(tcx, def_id.index) }
114+
optimized_mir => { tcx.arena.alloc(cdata.get_optimized_mir(tcx, def_id.index)) }
115115
promoted_mir => { cdata.get_promoted_mir(tcx, def_id.index) }
116116
mir_const_qualif => { cdata.mir_const_qualif(def_id.index) }
117117
fn_sig => { cdata.fn_sig(def_id.index, tcx) }

src/librustc_middle/arena.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ macro_rules! arena_types {
1414
[] layouts: rustc_target::abi::Layout, rustc_target::abi::Layout;
1515
// AdtDef are interned and compared by address
1616
[] adt_def: rustc_middle::ty::AdtDef, rustc_middle::ty::AdtDef;
17+
[] mir: rustc_middle::mir::Body<$tcx>, rustc_middle::mir::Body<$tcx>;
1718
[decode] tables: rustc_middle::ty::TypeckTables<$tcx>, rustc_middle::ty::TypeckTables<'_x>;
1819
[] const_allocs: rustc_middle::mir::interpret::Allocation, rustc_middle::mir::interpret::Allocation;
1920
// Required for the incremental on-disk cache

src/librustc_middle/query/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,13 @@ rustc_queries! {
244244

245245
/// MIR after our optimization passes have run. This is MIR that is ready
246246
/// for codegen. This is also the only query that can fetch non-local MIR, at present.
247-
query optimized_mir(key: DefId) -> mir::Body<'tcx> {
247+
query optimized_mir(key: DefId) -> &'tcx mir::Body<'tcx> {
248248
desc { |tcx| "optimizing MIR for `{}`", tcx.def_path_str(key) }
249-
storage(ArenaCacheSelector<'tcx>)
250249
cache_on_disk_if { key.is_local() }
251250
}
251+
query optimized_mir_of_const_arg(key: ty::WithOptParam<LocalDefId>) -> &'tcx mir::Body<'tcx> {
252+
desc { |tcx| "optimizing MIR for `{}`", tcx.def_path_str(key.did.to_def_id()) }
253+
}
252254

253255
/// Returns coverage summary info for a function, after executing the `InstrumentCoverage`
254256
/// MIR pass (assuming the -Zinstrument-coverage option is enabled).

src/librustc_middle/ty/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2880,7 +2880,13 @@ impl<'tcx> TyCtxt<'tcx> {
28802880
/// Returns the possibly-auto-generated MIR of a `(DefId, Subst)` pair.
28812881
pub fn instance_mir(self, instance: ty::InstanceDef<'tcx>) -> &'tcx Body<'tcx> {
28822882
match instance {
2883-
ty::InstanceDef::Item(def) => self.optimized_mir(def.did),
2883+
ty::InstanceDef::Item(def) => {
2884+
if let Some(def) = def.as_local() {
2885+
self.optimized_mir_of_const_arg(def)
2886+
} else {
2887+
self.optimized_mir(def.did)
2888+
}
2889+
}
28842890
ty::InstanceDef::VtableShim(..)
28852891
| ty::InstanceDef::ReifyShim(..)
28862892
| ty::InstanceDef::Intrinsic(..)

src/librustc_mir/transform/mod.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub(crate) fn provide(providers: &mut Providers) {
5252
mir_validated,
5353
mir_drops_elaborated_and_const_checked,
5454
optimized_mir,
55+
optimized_mir_of_const_arg,
5556
is_mir_available,
5657
promoted_mir,
5758
..*providers
@@ -424,19 +425,41 @@ fn run_optimization_passes<'tcx>(
424425
);
425426
}
426427

427-
fn optimized_mir(tcx: TyCtxt<'_>, def_id: DefId) -> Body<'_> {
428-
if tcx.is_constructor(def_id) {
428+
fn optimized_mir<'tcx>(tcx: TyCtxt<'tcx>, did: DefId) -> &'tcx Body<'tcx> {
429+
let did = did.expect_local();
430+
if let param_did @ Some(_) = tcx.opt_const_param_of(did) {
431+
tcx.optimized_mir_of_const_arg(ty::WithOptParam { did, param_did })
432+
} else {
433+
tcx.arena.alloc(inner_optimized_mir(tcx, ty::WithOptParam::dummy(did)))
434+
}
435+
}
436+
437+
fn optimized_mir_of_const_arg<'tcx>(
438+
tcx: TyCtxt<'tcx>,
439+
def: ty::WithOptParam<LocalDefId>,
440+
) -> &'tcx Body<'tcx> {
441+
if def.param_did.is_none() {
442+
if let param_did @ Some(_) = tcx.opt_const_param_of(def.did) {
443+
tcx.optimized_mir_of_const_arg(ty::WithOptParam { param_did, ..def })
444+
} else {
445+
tcx.optimized_mir(def.did)
446+
}
447+
} else {
448+
tcx.arena.alloc(inner_optimized_mir(tcx, def))
449+
}
450+
}
451+
452+
fn inner_optimized_mir(tcx: TyCtxt<'_>, def: ty::WithOptParam<LocalDefId>) -> Body<'_> {
453+
if tcx.is_constructor(def.did.to_def_id()) {
429454
// There's no reason to run all of the MIR passes on constructors when
430455
// we can just output the MIR we want directly. This also saves const
431456
// qualification and borrow checking the trouble of special casing
432457
// constructors.
433-
return shim::build_adt_ctor(tcx, def_id);
458+
return shim::build_adt_ctor(tcx, def.did.to_def_id());
434459
}
435460

436-
let def_id = def_id.expect_local();
437-
438-
let mut body = tcx.mir_drops_elaborated_and_const_checked(def_id).steal();
439-
run_optimization_passes(tcx, &mut body, def_id, None);
461+
let mut body = tcx.mir_drops_elaborated_and_const_checked(def.did).steal();
462+
run_optimization_passes(tcx, &mut body, def.did, None);
440463

441464
debug_assert!(!body.has_free_regions(), "Free regions in optimized MIR");
442465

0 commit comments

Comments
 (0)