Skip to content

Commit c5ca816

Browse files
committed
Auto merge of rust-lang#15228 - HKalbasi:mir, r=HKalbasi
Implement recursion in mir interpreter without recursion This enables interpreting functions with deep stack + profiling. I also applied some changes to make it faster based on the profiling result.
2 parents 954a341 + 4a444e7 commit c5ca816

File tree

15 files changed

+432
-264
lines changed

15 files changed

+432
-264
lines changed

crates/hir-def/src/db.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
generics::GenericParams,
1919
import_map::ImportMap,
2020
item_tree::{AttrOwner, ItemTree},
21-
lang_item::{LangItem, LangItemTarget, LangItems},
21+
lang_item::{self, LangItem, LangItemTarget, LangItems},
2222
nameres::{diagnostics::DefDiagnostic, DefMap},
2323
visibility::{self, Visibility},
2424
AttrDefId, BlockId, BlockLoc, ConstBlockId, ConstBlockLoc, ConstId, ConstLoc, DefWithBodyId,
@@ -204,6 +204,9 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
204204
#[salsa::invoke(AttrsWithOwner::attrs_query)]
205205
fn attrs(&self, def: AttrDefId) -> Attrs;
206206

207+
#[salsa::invoke(lang_item::lang_attr_query)]
208+
fn lang_attr(&self, def: AttrDefId) -> Option<LangItem>;
209+
207210
#[salsa::transparent]
208211
#[salsa::invoke(AttrsWithOwner::attrs_with_owner)]
209212
fn attrs_with_owner(&self, def: AttrDefId) -> AttrsWithOwner;

crates/hir-def/src/lang_item.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,15 @@ impl LangItems {
180180
T: Into<AttrDefId> + Copy,
181181
{
182182
let _p = profile::span("collect_lang_item");
183-
if let Some(lang_item) = lang_attr(db, item) {
183+
if let Some(lang_item) = db.lang_attr(item.into()) {
184184
self.items.entry(lang_item).or_insert_with(|| constructor(item));
185185
}
186186
}
187187
}
188188

189-
pub fn lang_attr(db: &dyn DefDatabase, item: impl Into<AttrDefId> + Copy) -> Option<LangItem> {
190-
let attrs = db.attrs(item.into());
191-
attrs.by_key("lang").string_value().cloned().and_then(|it| LangItem::from_str(&it))
189+
pub(crate) fn lang_attr_query(db: &dyn DefDatabase, item: AttrDefId) -> Option<LangItem> {
190+
let attrs = db.attrs(item);
191+
attrs.by_key("lang").string_value().and_then(|it| LangItem::from_str(&it))
192192
}
193193

194194
pub enum GenericRequirement {

crates/hir-ty/src/chalk_db.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait};
1111
use base_db::CrateId;
1212
use hir_def::{
1313
hir::Movability,
14-
lang_item::{lang_attr, LangItem, LangItemTarget},
14+
lang_item::{LangItem, LangItemTarget},
1515
AssocItemId, BlockId, GenericDefId, HasModule, ItemContainerId, Lookup, TypeAliasId,
1616
};
1717
use hir_expand::name::name;
@@ -565,7 +565,7 @@ pub(crate) fn trait_datum_query(
565565
let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars);
566566
let associated_ty_ids = trait_data.associated_types().map(to_assoc_type_id).collect();
567567
let trait_datum_bound = rust_ir::TraitDatumBound { where_clauses };
568-
let well_known = lang_attr(db.upcast(), trait_).and_then(well_known_trait_from_lang_item);
568+
let well_known = db.lang_attr(trait_.into()).and_then(well_known_trait_from_lang_item);
569569
let trait_datum = TraitDatum {
570570
id: trait_id,
571571
binders: make_binders(db, &generic_params, trait_datum_bound),

crates/hir-ty/src/consteval.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ pub(crate) fn const_eval_query(
228228
}
229229
GeneralConstId::InTypeConstId(c) => db.mir_body(c.into())?,
230230
};
231-
let c = interpret_mir(db, &body, false).0?;
231+
let c = interpret_mir(db, body, false).0?;
232232
Ok(c)
233233
}
234234

@@ -241,7 +241,7 @@ pub(crate) fn const_eval_static_query(
241241
Substitution::empty(Interner),
242242
db.trait_environment_for_body(def.into()),
243243
)?;
244-
let c = interpret_mir(db, &body, false).0?;
244+
let c = interpret_mir(db, body, false).0?;
245245
Ok(c)
246246
}
247247

@@ -268,7 +268,7 @@ pub(crate) fn const_eval_discriminant_variant(
268268
Substitution::empty(Interner),
269269
db.trait_environment_for_body(def),
270270
)?;
271-
let c = interpret_mir(db, &mir_body, false).0?;
271+
let c = interpret_mir(db, mir_body, false).0?;
272272
let c = try_const_usize(db, &c).unwrap() as i128;
273273
Ok(c)
274274
}
@@ -293,7 +293,7 @@ pub(crate) fn eval_to_const(
293293
}
294294
let infer = ctx.clone().resolve_all();
295295
if let Ok(mir_body) = lower_to_mir(ctx.db, ctx.owner, &ctx.body, &infer, expr) {
296-
if let Ok(result) = interpret_mir(db, &mir_body, true).0 {
296+
if let Ok(result) = interpret_mir(db, Arc::new(mir_body), true).0 {
297297
return result;
298298
}
299299
}

crates/hir-ty/src/consteval/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod intrinsics;
1616

1717
fn simplify(e: ConstEvalError) -> ConstEvalError {
1818
match e {
19-
ConstEvalError::MirEvalError(MirEvalError::InFunction(_, e, _, _)) => {
19+
ConstEvalError::MirEvalError(MirEvalError::InFunction(e, _)) => {
2020
simplify(ConstEvalError::MirEvalError(*e))
2121
}
2222
_ => e,
@@ -2471,7 +2471,7 @@ fn exec_limits() {
24712471
}
24722472
const GOAL: i32 = f(0);
24732473
"#,
2474-
|e| e == ConstEvalError::MirEvalError(MirEvalError::StackOverflow),
2474+
|e| e == ConstEvalError::MirEvalError(MirEvalError::ExecutionLimitExceeded),
24752475
);
24762476
// Reasonable code should still work
24772477
check_number(

crates/hir-ty/src/db.rs

+8
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
110110
#[salsa::invoke(crate::layout::target_data_layout_query)]
111111
fn target_data_layout(&self, krate: CrateId) -> Option<Arc<TargetDataLayout>>;
112112

113+
#[salsa::invoke(crate::method_resolution::lookup_impl_method_query)]
114+
fn lookup_impl_method(
115+
&self,
116+
env: Arc<crate::TraitEnvironment>,
117+
func: FunctionId,
118+
fn_subst: Substitution,
119+
) -> (FunctionId, Substitution);
120+
113121
#[salsa::invoke(crate::lower::callable_item_sig)]
114122
fn callable_item_signature(&self, def: CallableDefId) -> PolyFnSig;
115123

crates/hir-ty/src/lower.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use hir_def::{
2323
generics::{
2424
TypeOrConstParamData, TypeParamProvenance, WherePredicate, WherePredicateTypeTarget,
2525
},
26-
lang_item::{lang_attr, LangItem},
26+
lang_item::LangItem,
2727
nameres::MacroSubNs,
2828
path::{GenericArg, GenericArgs, ModPath, Path, PathKind, PathSegment, PathSegments},
2929
resolver::{HasResolver, Resolver, TypeNs},
@@ -1012,7 +1012,7 @@ impl<'a> TyLoweringContext<'a> {
10121012
// (So ideally, we'd only ignore `~const Drop` here)
10131013
// - `Destruct` impls are built-in in 1.62 (current nightly as of 08-04-2022), so until
10141014
// the builtin impls are supported by Chalk, we ignore them here.
1015-
if let Some(lang) = lang_attr(self.db.upcast(), tr.hir_trait_id()) {
1015+
if let Some(lang) = self.db.lang_attr(tr.hir_trait_id().into()) {
10161016
if matches!(lang, LangItem::Drop | LangItem::Destruct) {
10171017
return false;
10181018
}

crates/hir-ty/src/method_resolution.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ pub fn is_dyn_method(
682682
/// Looks up the impl method that actually runs for the trait method `func`.
683683
///
684684
/// Returns `func` if it's not a method defined in a trait or the lookup failed.
685-
pub fn lookup_impl_method(
685+
pub(crate) fn lookup_impl_method_query(
686686
db: &dyn HirDatabase,
687687
env: Arc<TraitEnvironment>,
688688
func: FunctionId,

0 commit comments

Comments
 (0)