Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1f5fb3e

Browse files
committed
Differentiate between the availability of ctfe MIR and runtime MIR
1 parent cccd40f commit 1f5fb3e

File tree

8 files changed

+38
-29
lines changed

8 files changed

+38
-29
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,9 +1160,12 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11601160
}
11611161
}
11621162

1163+
fn is_ctfe_mir_available(&self, id: DefIndex) -> bool {
1164+
self.root.tables.mir_for_ctfe.get(self, id).is_some()
1165+
}
1166+
11631167
fn is_item_mir_available(&self, id: DefIndex) -> bool {
11641168
self.root.tables.mir.get(self, id).is_some()
1165-
|| self.root.tables.mir_for_ctfe.get(self, id).is_some()
11661169
}
11671170

11681171
fn module_expansion(&self, id: DefIndex, sess: &Session) -> ExpnId {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
146146
impl_parent => { cdata.get_parent_impl(def_id.index) }
147147
trait_of_item => { cdata.get_trait_of_item(def_id.index) }
148148
is_mir_available => { cdata.is_item_mir_available(def_id.index) }
149+
is_ctfe_mir_available => { cdata.is_ctfe_mir_available(def_id.index) }
149150

150151
dylib_dependency_formats => { cdata.get_dylib_dependency_formats(tcx) }
151152
is_panic_runtime => { cdata.root.panic_runtime }

compiler/rustc_middle/src/mir/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ impl<'tcx> TyCtxt<'tcx> {
444444
def: ty::WithOptConstParam<DefId>,
445445
) -> &'tcx Body<'tcx> {
446446
if let Some((did, param_did)) = def.as_const_arg() {
447-
self.optimized_mir_of_const_arg((did, param_did))
447+
self.mir_for_ctfe_of_const_arg((did, param_did))
448448
} else {
449449
self.optimized_mir(def.did)
450450
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,6 @@ rustc_queries! {
346346
cache_on_disk_if { key.is_local() }
347347
}
348348

349-
// FIXME: now that we have `mir_for_ctfe_of_const_arg` can we get
350-
// rid of this query?
351-
query optimized_mir_of_const_arg(key: (LocalDefId, DefId)) -> &'tcx mir::Body<'tcx> {
352-
desc {
353-
|tcx| "optimizing MIR for the const argument `{}`",
354-
tcx.def_path_str(key.0.to_def_id())
355-
}
356-
}
357-
358349
/// Returns coverage summary info for a function, after executing the `InstrumentCoverage`
359350
/// MIR pass (assuming the -Zinstrument-coverage option is enabled).
360351
query coverageinfo(key: DefId) -> mir::CoverageInfo {
@@ -944,6 +935,10 @@ rustc_queries! {
944935
}
945936

946937
Codegen {
938+
// FIXME: remove after figuring out how to make miri able to detect non-Rust function calls
939+
query is_ctfe_mir_available(key: DefId) -> bool {
940+
desc { |tcx| "checking if item has ctfe mir available: `{}`", tcx.def_path_str(key) }
941+
}
947942
query is_mir_available(key: DefId) -> bool {
948943
desc { |tcx| "checking if item has mir available: `{}`", tcx.def_path_str(key) }
949944
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3010,7 +3010,16 @@ impl<'tcx> TyCtxt<'tcx> {
30103010
/// Returns the possibly-auto-generated MIR of a `(DefId, Subst)` pair.
30113011
pub fn instance_mir(self, instance: ty::InstanceDef<'tcx>) -> &'tcx Body<'tcx> {
30123012
match instance {
3013-
ty::InstanceDef::Item(def) => self.optimized_mir_opt_const_arg(def),
3013+
ty::InstanceDef::Item(def) => match self.def_kind(def.did) {
3014+
DefKind::Const
3015+
| DefKind::Static
3016+
| DefKind::AssocConst
3017+
| DefKind::Ctor(..)
3018+
| DefKind::AnonConst => self.mir_for_ctfe_opt_const_arg(def),
3019+
// If the caller wants `mir_for_ctfe` they should not be using `instance_mir`, so
3020+
// we'll assume const fn also wants the optimized version.
3021+
_ => self.optimized_mir_opt_const_arg(def),
3022+
},
30143023
ty::InstanceDef::VtableShim(..)
30153024
| ty::InstanceDef::ReifyShim(..)
30163025
| ty::InstanceDef::Intrinsic(..)

compiler/rustc_mir/src/interpret/eval_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
479479
}
480480
match instance {
481481
ty::InstanceDef::Item(def) => {
482-
if self.tcx.is_mir_available(def.did) {
482+
if self.tcx.is_ctfe_mir_available(def.did) {
483483
Ok(self.tcx.mir_for_ctfe_opt_const_arg(def))
484484
} else {
485485
throw_unsup!(NoMirFor(def.did))

compiler/rustc_mir/src/monomorphize/polymorphize.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,17 @@ fn unused_generic_params(tcx: TyCtxt<'_>, def_id: DefId) -> FiniteBitSet<u32> {
5454
}
5555

5656
// Exit early when there is no MIR available.
57-
if !tcx.is_mir_available(def_id) {
58-
debug!("unused_generic_params: (no mir available) def_id={:?}", def_id);
59-
return FiniteBitSet::new_empty();
57+
let context = tcx.hir().body_const_context(def_id.expect_local());
58+
match context {
59+
None if !tcx.is_mir_available(def_id) => {
60+
debug!("unused_generic_params: (no mir available) def_id={:?}", def_id);
61+
return FiniteBitSet::new_empty();
62+
}
63+
Some(_) if !tcx.is_ctfe_mir_available(def_id) => {
64+
debug!("unused_generic_params: (no ctfe mir available) def_id={:?}", def_id);
65+
return FiniteBitSet::new_empty();
66+
}
67+
_ => {}
6068
}
6169

6270
// Create a bitset with N rightmost ones for each parameter.
@@ -69,8 +77,11 @@ fn unused_generic_params(tcx: TyCtxt<'_>, def_id: DefId) -> FiniteBitSet<u32> {
6977
debug!("unused_generic_params: (after default) unused_parameters={:?}", unused_parameters);
7078

7179
// Visit MIR and accumululate used generic parameters.
72-
let body = match tcx.hir().body_const_context(def_id.expect_local()) {
80+
let body = match context {
7381
None => tcx.optimized_mir(def_id),
82+
// FIXME(oli-obk): since this is solely used for codegen (I think?), should we keep using
83+
// the optimized MIR for `const fn`? Need to adjust the above `is_mir_available` check
84+
// in that case.
7485
Some(_) => tcx.mir_for_ctfe(def_id),
7586
};
7687
let mut vis = MarkUsedGenericParams { tcx, def_id, unused_parameters: &mut unused_parameters };

compiler/rustc_mir/src/transform/mod.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ pub(crate) fn provide(providers: &mut Providers) {
7474
mir_for_ctfe,
7575
mir_for_ctfe_of_const_arg,
7676
optimized_mir,
77-
optimized_mir_of_const_arg,
7877
is_mir_available,
78+
is_ctfe_mir_available: |tcx, did| is_mir_available(tcx, did),
7979
promoted_mir: |tcx, def_id| {
8080
let def_id = def_id.expect_local();
8181
if let Some(def) = ty::WithOptConstParam::try_lookup(def_id, tcx) {
@@ -518,22 +518,12 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
518518
fn optimized_mir<'tcx>(tcx: TyCtxt<'tcx>, did: DefId) -> &'tcx Body<'tcx> {
519519
let did = did.expect_local();
520520
if let Some(def) = ty::WithOptConstParam::try_lookup(did, tcx) {
521-
tcx.optimized_mir_of_const_arg(def)
521+
tcx.mir_for_ctfe_of_const_arg(def)
522522
} else {
523523
tcx.arena.alloc(inner_optimized_mir(tcx, ty::WithOptConstParam::unknown(did)))
524524
}
525525
}
526526

527-
fn optimized_mir_of_const_arg<'tcx>(
528-
tcx: TyCtxt<'tcx>,
529-
(did, param_did): (LocalDefId, DefId),
530-
) -> &'tcx Body<'tcx> {
531-
tcx.arena.alloc(inner_optimized_mir(
532-
tcx,
533-
ty::WithOptConstParam { did, const_param_did: Some(param_did) },
534-
))
535-
}
536-
537527
fn inner_optimized_mir(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_> {
538528
if tcx.is_constructor(def.did.to_def_id()) {
539529
// There's no reason to run all of the MIR passes on constructors when

0 commit comments

Comments
 (0)