Auto merge of #16290 - Veykril:chalk, r=Veykril

internal: Reduce vec cloning in mir lowering/eval
This commit is contained in:
bors 2024-01-06 22:13:09 +00:00
commit f595e60b6d
12 changed files with 308 additions and 256 deletions

View File

@ -142,15 +142,15 @@ pub fn intern_const_ref(
LiteralConstRef::Int(i) => { LiteralConstRef::Int(i) => {
// FIXME: We should handle failure of layout better. // FIXME: We should handle failure of layout better.
let size = layout.map(|it| it.size.bytes_usize()).unwrap_or(16); let size = layout.map(|it| it.size.bytes_usize()).unwrap_or(16);
ConstScalar::Bytes(i.to_le_bytes()[0..size].to_vec(), MemoryMap::default()) ConstScalar::Bytes(i.to_le_bytes()[0..size].into(), MemoryMap::default())
} }
LiteralConstRef::UInt(i) => { LiteralConstRef::UInt(i) => {
let size = layout.map(|it| it.size.bytes_usize()).unwrap_or(16); let size = layout.map(|it| it.size.bytes_usize()).unwrap_or(16);
ConstScalar::Bytes(i.to_le_bytes()[0..size].to_vec(), MemoryMap::default()) ConstScalar::Bytes(i.to_le_bytes()[0..size].into(), MemoryMap::default())
} }
LiteralConstRef::Bool(b) => ConstScalar::Bytes(vec![*b as u8], MemoryMap::default()), LiteralConstRef::Bool(b) => ConstScalar::Bytes(Box::new([*b as u8]), MemoryMap::default()),
LiteralConstRef::Char(c) => { LiteralConstRef::Char(c) => {
ConstScalar::Bytes((*c as u32).to_le_bytes().to_vec(), MemoryMap::default()) ConstScalar::Bytes((*c as u32).to_le_bytes().into(), MemoryMap::default())
} }
LiteralConstRef::Unknown => ConstScalar::Unknown, LiteralConstRef::Unknown => ConstScalar::Unknown,
}; };

View File

@ -515,7 +515,7 @@ fn render_const_scalar(
TyKind::Dyn(_) => { TyKind::Dyn(_) => {
let addr = usize::from_le_bytes(b[0..b.len() / 2].try_into().unwrap()); let addr = usize::from_le_bytes(b[0..b.len() / 2].try_into().unwrap());
let ty_id = usize::from_le_bytes(b[b.len() / 2..].try_into().unwrap()); let ty_id = usize::from_le_bytes(b[b.len() / 2..].try_into().unwrap());
let Ok(t) = memory_map.vtable.ty(ty_id) else { let Ok(t) = memory_map.vtable_ty(ty_id) else {
return f.write_str("<ty-missing-in-vtable-map>"); return f.write_str("<ty-missing-in-vtable-map>");
}; };
let Ok(layout) = f.db.layout_of_ty(t.clone(), trait_env) else { let Ok(layout) = f.db.layout_of_ty(t.clone(), trait_env) else {

View File

@ -1,6 +1,6 @@
//! Inference of closure parameter types based on the closure's expected type. //! Inference of closure parameter types based on the closure's expected type.
use std::{cmp, collections::HashMap, convert::Infallible, mem}; use std::{cmp, convert::Infallible, mem};
use chalk_ir::{ use chalk_ir::{
cast::Cast, cast::Cast,
@ -778,7 +778,7 @@ impl InferenceContext<'_> {
fn minimize_captures(&mut self) { fn minimize_captures(&mut self) {
self.current_captures.sort_by_key(|it| it.place.projections.len()); self.current_captures.sort_by_key(|it| it.place.projections.len());
let mut hash_map = HashMap::<HirPlace, usize>::new(); let mut hash_map = FxHashMap::<HirPlace, usize>::default();
let result = mem::take(&mut self.current_captures); let result = mem::take(&mut self.current_captures);
for item in result { for item in result {
let mut lookup_place = HirPlace { local: item.place.local, projections: vec![] }; let mut lookup_place = HirPlace { local: item.place.local, projections: vec![] };

View File

@ -1,9 +1,15 @@
//! Implementation of the Chalk `Interner` trait, which allows customizing the //! Implementation of the Chalk `Interner` trait, which allows customizing the
//! representation of the various objects Chalk deals with (types, goals etc.). //! representation of the various objects Chalk deals with (types, goals etc.).
use crate::{chalk_db, tls, ConstScalar, GenericArg}; use crate::{
chalk_db, tls, AliasTy, CanonicalVarKind, CanonicalVarKinds, ClosureId, Const, ConstData,
ConstScalar, Constraint, Constraints, FnDefId, GenericArg, GenericArgData, Goal, GoalData,
Goals, InEnvironment, Lifetime, LifetimeData, OpaqueTy, OpaqueTyId, ProgramClause,
ProgramClauseData, ProgramClauses, ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses,
Substitution, Ty, TyData, TyKind, VariableKind, VariableKinds,
};
use base_db::salsa::InternId; use base_db::salsa::InternId;
use chalk_ir::{Goal, GoalData}; use chalk_ir::{ProgramClauseImplication, SeparatorTraitRef, Variance};
use hir_def::TypeAliasId; use hir_def::TypeAliasId;
use intern::{impl_internable, Interned}; use intern::{impl_internable, Interned};
use smallvec::SmallVec; use smallvec::SmallVec;
@ -31,36 +37,37 @@ impl<T> std::ops::Deref for InternedWrapper<T> {
} }
impl_internable!( impl_internable!(
InternedWrapper<Vec<chalk_ir::VariableKind<Interner>>>, InternedWrapper<Vec<VariableKind>>,
InternedWrapper<SmallVec<[GenericArg; 2]>>, InternedWrapper<SmallVec<[GenericArg; 2]>>,
InternedWrapper<chalk_ir::TyData<Interner>>, InternedWrapper<TyData>,
InternedWrapper<chalk_ir::LifetimeData<Interner>>, InternedWrapper<LifetimeData>,
InternedWrapper<chalk_ir::ConstData<Interner>>, InternedWrapper<ConstData>,
InternedWrapper<ConstScalar>, InternedWrapper<ConstScalar>,
InternedWrapper<Vec<chalk_ir::CanonicalVarKind<Interner>>>, InternedWrapper<Vec<CanonicalVarKind>>,
InternedWrapper<Vec<chalk_ir::ProgramClause<Interner>>>, InternedWrapper<Vec<ProgramClause>>,
InternedWrapper<Vec<chalk_ir::QuantifiedWhereClause<Interner>>>, InternedWrapper<Vec<QuantifiedWhereClause>>,
InternedWrapper<Vec<chalk_ir::Variance>>, InternedWrapper<SmallVec<[Variance; 16]>>,
); );
impl chalk_ir::interner::Interner for Interner { impl chalk_ir::interner::Interner for Interner {
type InternedType = Interned<InternedWrapper<chalk_ir::TyData<Self>>>; type InternedType = Interned<InternedWrapper<TyData>>;
type InternedLifetime = Interned<InternedWrapper<chalk_ir::LifetimeData<Self>>>; type InternedLifetime = Interned<InternedWrapper<LifetimeData>>;
type InternedConst = Interned<InternedWrapper<chalk_ir::ConstData<Self>>>; type InternedConst = Interned<InternedWrapper<ConstData>>;
type InternedConcreteConst = ConstScalar; type InternedConcreteConst = ConstScalar;
type InternedGenericArg = chalk_ir::GenericArgData<Self>; type InternedGenericArg = GenericArgData;
type InternedGoal = Arc<GoalData<Self>>; // We could do the following, but that saves "only" 20mb on self while increasing inferecene
type InternedGoals = Vec<Goal<Self>>; // time by ~2.5%
// type InternedGoal = Interned<InternedWrapper<GoalData>>;
type InternedGoal = Arc<GoalData>;
type InternedGoals = Vec<Goal>;
type InternedSubstitution = Interned<InternedWrapper<SmallVec<[GenericArg; 2]>>>; type InternedSubstitution = Interned<InternedWrapper<SmallVec<[GenericArg; 2]>>>;
type InternedProgramClauses = Interned<InternedWrapper<Vec<chalk_ir::ProgramClause<Self>>>>; type InternedProgramClauses = Interned<InternedWrapper<Vec<ProgramClause>>>;
type InternedProgramClause = chalk_ir::ProgramClauseData<Self>; type InternedProgramClause = ProgramClauseData;
type InternedQuantifiedWhereClauses = type InternedQuantifiedWhereClauses = Interned<InternedWrapper<Vec<QuantifiedWhereClause>>>;
Interned<InternedWrapper<Vec<chalk_ir::QuantifiedWhereClause<Self>>>>; type InternedVariableKinds = Interned<InternedWrapper<Vec<VariableKind>>>;
type InternedVariableKinds = Interned<InternedWrapper<Vec<chalk_ir::VariableKind<Interner>>>>; type InternedCanonicalVarKinds = Interned<InternedWrapper<Vec<CanonicalVarKind>>>;
type InternedCanonicalVarKinds = type InternedConstraints = Vec<InEnvironment<Constraint>>;
Interned<InternedWrapper<Vec<chalk_ir::CanonicalVarKind<Self>>>>; type InternedVariances = SmallVec<[Variance; 16]>;
type InternedConstraints = Vec<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>>;
type InternedVariances = Interned<InternedWrapper<Vec<chalk_ir::Variance>>>;
type DefId = InternId; type DefId = InternId;
type InternedAdtId = hir_def::AdtId; type InternedAdtId = hir_def::AdtId;
type Identifier = TypeAliasId; type Identifier = TypeAliasId;
@ -88,68 +95,51 @@ impl chalk_ir::interner::Interner for Interner {
} }
fn debug_opaque_ty_id( fn debug_opaque_ty_id(
opaque_ty_id: chalk_ir::OpaqueTyId<Self>, opaque_ty_id: OpaqueTyId,
fmt: &mut fmt::Formatter<'_>, fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> { ) -> Option<fmt::Result> {
Some(write!(fmt, "OpaqueTy#{}", opaque_ty_id.0)) Some(write!(fmt, "OpaqueTy#{}", opaque_ty_id.0))
} }
fn debug_fn_def_id( fn debug_fn_def_id(fn_def_id: FnDefId, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
fn_def_id: chalk_ir::FnDefId<Self>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
tls::with_current_program(|prog| Some(prog?.debug_fn_def_id(fn_def_id, fmt))) tls::with_current_program(|prog| Some(prog?.debug_fn_def_id(fn_def_id, fmt)))
} }
fn debug_closure_id( fn debug_closure_id(
_fn_def_id: chalk_ir::ClosureId<Self>, _fn_def_id: ClosureId,
_fmt: &mut fmt::Formatter<'_>, _fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> { ) -> Option<fmt::Result> {
None None
} }
fn debug_alias( fn debug_alias(alias: &AliasTy, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
alias: &chalk_ir::AliasTy<Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
use std::fmt::Debug; use std::fmt::Debug;
match alias { match alias {
chalk_ir::AliasTy::Projection(projection_ty) => { AliasTy::Projection(projection_ty) => Interner::debug_projection_ty(projection_ty, fmt),
Interner::debug_projection_ty(projection_ty, fmt) AliasTy::Opaque(opaque_ty) => Some(opaque_ty.fmt(fmt)),
}
chalk_ir::AliasTy::Opaque(opaque_ty) => Some(opaque_ty.fmt(fmt)),
} }
} }
fn debug_projection_ty( fn debug_projection_ty(
proj: &chalk_ir::ProjectionTy<Interner>, proj: &ProjectionTy,
fmt: &mut fmt::Formatter<'_>, fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> { ) -> Option<fmt::Result> {
tls::with_current_program(|prog| Some(prog?.debug_projection_ty(proj, fmt))) tls::with_current_program(|prog| Some(prog?.debug_projection_ty(proj, fmt)))
} }
fn debug_opaque_ty( fn debug_opaque_ty(opaque_ty: &OpaqueTy, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
opaque_ty: &chalk_ir::OpaqueTy<Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", opaque_ty.opaque_ty_id)) Some(write!(fmt, "{:?}", opaque_ty.opaque_ty_id))
} }
fn debug_ty(ty: &chalk_ir::Ty<Interner>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { fn debug_ty(ty: &Ty, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", ty.data(Interner))) Some(write!(fmt, "{:?}", ty.data(Interner)))
} }
fn debug_lifetime( fn debug_lifetime(lifetime: &Lifetime, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
lifetime: &chalk_ir::Lifetime<Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", lifetime.data(Interner))) Some(write!(fmt, "{:?}", lifetime.data(Interner)))
} }
fn debug_const( fn debug_const(constant: &Const, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
constant: &chalk_ir::Const<Self>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", constant.data(Interner))) Some(write!(fmt, "{:?}", constant.data(Interner)))
} }
@ -161,102 +151,99 @@ impl chalk_ir::interner::Interner for Interner {
} }
fn debug_variable_kinds( fn debug_variable_kinds(
variable_kinds: &chalk_ir::VariableKinds<Self>, variable_kinds: &VariableKinds,
fmt: &mut fmt::Formatter<'_>, fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> { ) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", variable_kinds.as_slice(Interner))) Some(write!(fmt, "{:?}", variable_kinds.as_slice(Interner)))
} }
fn debug_variable_kinds_with_angles( fn debug_variable_kinds_with_angles(
variable_kinds: &chalk_ir::VariableKinds<Self>, variable_kinds: &VariableKinds,
fmt: &mut fmt::Formatter<'_>, fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> { ) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", variable_kinds.inner_debug(Interner))) Some(write!(fmt, "{:?}", variable_kinds.inner_debug(Interner)))
} }
fn debug_canonical_var_kinds( fn debug_canonical_var_kinds(
canonical_var_kinds: &chalk_ir::CanonicalVarKinds<Self>, canonical_var_kinds: &CanonicalVarKinds,
fmt: &mut fmt::Formatter<'_>, fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> { ) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", canonical_var_kinds.as_slice(Interner))) Some(write!(fmt, "{:?}", canonical_var_kinds.as_slice(Interner)))
} }
fn debug_goal(goal: &Goal<Interner>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { fn debug_goal(goal: &Goal, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
let goal_data = goal.data(Interner); let goal_data = goal.data(Interner);
Some(write!(fmt, "{goal_data:?}")) Some(write!(fmt, "{goal_data:?}"))
} }
fn debug_goals( fn debug_goals(goals: &Goals, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
goals: &chalk_ir::Goals<Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", goals.debug(Interner))) Some(write!(fmt, "{:?}", goals.debug(Interner)))
} }
fn debug_program_clause_implication( fn debug_program_clause_implication(
pci: &chalk_ir::ProgramClauseImplication<Interner>, pci: &ProgramClauseImplication<Self>,
fmt: &mut fmt::Formatter<'_>, fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> { ) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", pci.debug(Interner))) Some(write!(fmt, "{:?}", pci.debug(Interner)))
} }
fn debug_program_clause( fn debug_program_clause(
clause: &chalk_ir::ProgramClause<Self>, clause: &ProgramClause,
fmt: &mut fmt::Formatter<'_>, fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> { ) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", clause.data(Interner))) Some(write!(fmt, "{:?}", clause.data(Interner)))
} }
fn debug_program_clauses( fn debug_program_clauses(
clauses: &chalk_ir::ProgramClauses<Self>, clauses: &ProgramClauses,
fmt: &mut fmt::Formatter<'_>, fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> { ) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", clauses.as_slice(Interner))) Some(write!(fmt, "{:?}", clauses.as_slice(Interner)))
} }
fn debug_substitution( fn debug_substitution(
substitution: &chalk_ir::Substitution<Interner>, substitution: &Substitution,
fmt: &mut fmt::Formatter<'_>, fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> { ) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", substitution.debug(Interner))) Some(write!(fmt, "{:?}", substitution.debug(Interner)))
} }
fn debug_separator_trait_ref( fn debug_separator_trait_ref(
separator_trait_ref: &chalk_ir::SeparatorTraitRef<'_, Interner>, separator_trait_ref: &SeparatorTraitRef<'_, Interner>,
fmt: &mut fmt::Formatter<'_>, fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> { ) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", separator_trait_ref.debug(Interner))) Some(write!(fmt, "{:?}", separator_trait_ref.debug(Interner)))
} }
fn debug_quantified_where_clauses( fn debug_quantified_where_clauses(
clauses: &chalk_ir::QuantifiedWhereClauses<Self>, clauses: &QuantifiedWhereClauses,
fmt: &mut fmt::Formatter<'_>, fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> { ) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", clauses.as_slice(Interner))) Some(write!(fmt, "{:?}", clauses.as_slice(Interner)))
} }
fn debug_constraints( fn debug_constraints(
_clauses: &chalk_ir::Constraints<Self>, _clauses: &Constraints,
_fmt: &mut fmt::Formatter<'_>, _fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> { ) -> Option<fmt::Result> {
None None
} }
fn intern_ty(self, kind: chalk_ir::TyKind<Self>) -> Self::InternedType { fn intern_ty(self, kind: TyKind) -> Self::InternedType {
let flags = kind.compute_flags(self); let flags = kind.compute_flags(self);
Interned::new(InternedWrapper(chalk_ir::TyData { kind, flags })) Interned::new(InternedWrapper(TyData { kind, flags }))
} }
fn ty_data(self, ty: &Self::InternedType) -> &chalk_ir::TyData<Self> { fn ty_data(self, ty: &Self::InternedType) -> &TyData {
&ty.0 &ty.0
} }
fn intern_lifetime(self, lifetime: chalk_ir::LifetimeData<Self>) -> Self::InternedLifetime { fn intern_lifetime(self, lifetime: LifetimeData) -> Self::InternedLifetime {
Interned::new(InternedWrapper(lifetime)) Interned::new(InternedWrapper(lifetime))
} }
fn lifetime_data(self, lifetime: &Self::InternedLifetime) -> &chalk_ir::LifetimeData<Self> { fn lifetime_data(self, lifetime: &Self::InternedLifetime) -> &LifetimeData {
&lifetime.0 &lifetime.0
} }
fn intern_const(self, constant: chalk_ir::ConstData<Self>) -> Self::InternedConst { fn intern_const(self, constant: ConstData) -> Self::InternedConst {
Interned::new(InternedWrapper(constant)) Interned::new(InternedWrapper(constant))
} }
fn const_data(self, constant: &Self::InternedConst) -> &chalk_ir::ConstData<Self> { fn const_data(self, constant: &Self::InternedConst) -> &ConstData {
&constant.0 &constant.0
} }
@ -269,36 +256,33 @@ impl chalk_ir::interner::Interner for Interner {
!matches!(c1, ConstScalar::Bytes(..)) || !matches!(c2, ConstScalar::Bytes(..)) || (c1 == c2) !matches!(c1, ConstScalar::Bytes(..)) || !matches!(c2, ConstScalar::Bytes(..)) || (c1 == c2)
} }
fn intern_generic_arg( fn intern_generic_arg(self, parameter: GenericArgData) -> Self::InternedGenericArg {
self,
parameter: chalk_ir::GenericArgData<Self>,
) -> Self::InternedGenericArg {
parameter parameter
} }
fn generic_arg_data( fn generic_arg_data(self, parameter: &Self::InternedGenericArg) -> &GenericArgData {
self,
parameter: &Self::InternedGenericArg,
) -> &chalk_ir::GenericArgData<Self> {
parameter parameter
} }
fn intern_goal(self, goal: GoalData<Self>) -> Self::InternedGoal { fn intern_goal(self, goal: GoalData) -> Self::InternedGoal {
Arc::new(goal) Arc::new(goal)
} }
fn goal_data(self, goal: &Self::InternedGoal) -> &GoalData<Self> { fn goal_data(self, goal: &Self::InternedGoal) -> &GoalData {
goal goal
} }
fn intern_goals<E>( fn intern_goals<E>(
self, self,
data: impl IntoIterator<Item = Result<Goal<Self>, E>>, data: impl IntoIterator<Item = Result<Goal, E>>,
) -> Result<Self::InternedGoals, E> { ) -> Result<Self::InternedGoals, E> {
// let hash =
// std::hash::BuildHasher::hash_one(&BuildHasherDefault::<FxHasher>::default(), &goal);
// Interned::new(InternedWrapper(PreHashedWrapper(goal, hash)))
data.into_iter().collect() data.into_iter().collect()
} }
fn goals_data(self, goals: &Self::InternedGoals) -> &[Goal<Interner>] { fn goals_data(self, goals: &Self::InternedGoals) -> &[Goal] {
goals goals
} }
@ -313,37 +297,28 @@ impl chalk_ir::interner::Interner for Interner {
&substitution.as_ref().0 &substitution.as_ref().0
} }
fn intern_program_clause( fn intern_program_clause(self, data: ProgramClauseData) -> Self::InternedProgramClause {
self,
data: chalk_ir::ProgramClauseData<Self>,
) -> Self::InternedProgramClause {
data data
} }
fn program_clause_data( fn program_clause_data(self, clause: &Self::InternedProgramClause) -> &ProgramClauseData {
self,
clause: &Self::InternedProgramClause,
) -> &chalk_ir::ProgramClauseData<Self> {
clause clause
} }
fn intern_program_clauses<E>( fn intern_program_clauses<E>(
self, self,
data: impl IntoIterator<Item = Result<chalk_ir::ProgramClause<Self>, E>>, data: impl IntoIterator<Item = Result<ProgramClause, E>>,
) -> Result<Self::InternedProgramClauses, E> { ) -> Result<Self::InternedProgramClauses, E> {
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?))) Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
} }
fn program_clauses_data( fn program_clauses_data(self, clauses: &Self::InternedProgramClauses) -> &[ProgramClause] {
self,
clauses: &Self::InternedProgramClauses,
) -> &[chalk_ir::ProgramClause<Self>] {
clauses clauses
} }
fn intern_quantified_where_clauses<E>( fn intern_quantified_where_clauses<E>(
self, self,
data: impl IntoIterator<Item = Result<chalk_ir::QuantifiedWhereClause<Self>, E>>, data: impl IntoIterator<Item = Result<QuantifiedWhereClause, E>>,
) -> Result<Self::InternedQuantifiedWhereClauses, E> { ) -> Result<Self::InternedQuantifiedWhereClauses, E> {
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?))) Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
} }
@ -351,27 +326,24 @@ impl chalk_ir::interner::Interner for Interner {
fn quantified_where_clauses_data( fn quantified_where_clauses_data(
self, self,
clauses: &Self::InternedQuantifiedWhereClauses, clauses: &Self::InternedQuantifiedWhereClauses,
) -> &[chalk_ir::QuantifiedWhereClause<Self>] { ) -> &[QuantifiedWhereClause] {
clauses clauses
} }
fn intern_generic_arg_kinds<E>( fn intern_generic_arg_kinds<E>(
self, self,
data: impl IntoIterator<Item = Result<chalk_ir::VariableKind<Self>, E>>, data: impl IntoIterator<Item = Result<VariableKind, E>>,
) -> Result<Self::InternedVariableKinds, E> { ) -> Result<Self::InternedVariableKinds, E> {
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?))) Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
} }
fn variable_kinds_data( fn variable_kinds_data(self, parameter_kinds: &Self::InternedVariableKinds) -> &[VariableKind] {
self,
parameter_kinds: &Self::InternedVariableKinds,
) -> &[chalk_ir::VariableKind<Self>] {
&parameter_kinds.as_ref().0 &parameter_kinds.as_ref().0
} }
fn intern_canonical_var_kinds<E>( fn intern_canonical_var_kinds<E>(
self, self,
data: impl IntoIterator<Item = Result<chalk_ir::CanonicalVarKind<Self>, E>>, data: impl IntoIterator<Item = Result<CanonicalVarKind, E>>,
) -> Result<Self::InternedCanonicalVarKinds, E> { ) -> Result<Self::InternedCanonicalVarKinds, E> {
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?))) Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
} }
@ -379,30 +351,30 @@ impl chalk_ir::interner::Interner for Interner {
fn canonical_var_kinds_data( fn canonical_var_kinds_data(
self, self,
canonical_var_kinds: &Self::InternedCanonicalVarKinds, canonical_var_kinds: &Self::InternedCanonicalVarKinds,
) -> &[chalk_ir::CanonicalVarKind<Self>] { ) -> &[CanonicalVarKind] {
canonical_var_kinds canonical_var_kinds
} }
fn intern_constraints<E>( fn intern_constraints<E>(
self, self,
data: impl IntoIterator<Item = Result<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>, E>>, data: impl IntoIterator<Item = Result<InEnvironment<Constraint>, E>>,
) -> Result<Self::InternedConstraints, E> { ) -> Result<Self::InternedConstraints, E> {
data.into_iter().collect() data.into_iter().collect()
} }
fn constraints_data( fn constraints_data(
self, self,
constraints: &Self::InternedConstraints, constraints: &Self::InternedConstraints,
) -> &[chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>] { ) -> &[InEnvironment<Constraint>] {
constraints constraints
} }
fn intern_variances<E>( fn intern_variances<E>(
self, self,
data: impl IntoIterator<Item = Result<chalk_ir::Variance, E>>, data: impl IntoIterator<Item = Result<Variance, E>>,
) -> Result<Self::InternedVariances, E> { ) -> Result<Self::InternedVariances, E> {
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?))) data.into_iter().collect::<Result<_, _>>()
} }
fn variances_data(self, variances: &Self::InternedVariances) -> &[chalk_ir::Variance] { fn variances_data(self, variances: &Self::InternedVariances) -> &[Variance] {
variances variances
} }
} }

View File

@ -37,22 +37,22 @@ mod tests;
mod test_db; mod test_db;
use std::{ use std::{
collections::{hash_map::Entry, HashMap}, collections::hash_map::Entry,
hash::Hash, hash::{BuildHasherDefault, Hash},
}; };
use chalk_ir::{ use chalk_ir::{
fold::{Shift, TypeFoldable}, fold::{Shift, TypeFoldable},
interner::HasInterner, interner::HasInterner,
visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor}, visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor},
NoSolution, TyData, NoSolution,
}; };
use either::Either; use either::Either;
use hir_def::{hir::ExprId, type_ref::Rawness, GeneralConstId, TypeOrConstParamId}; use hir_def::{hir::ExprId, type_ref::Rawness, GeneralConstId, TypeOrConstParamId};
use hir_expand::name; use hir_expand::name;
use la_arena::{Arena, Idx}; use la_arena::{Arena, Idx};
use mir::{MirEvalError, VTableMap}; use mir::{MirEvalError, VTableMap};
use rustc_hash::FxHashSet; use rustc_hash::{FxHashMap, FxHashSet};
use syntax::ast::{make, ConstArg}; use syntax::ast::{make, ConstArg};
use traits::FnTrait; use traits::FnTrait;
use triomphe::Arc; use triomphe::Arc;
@ -152,32 +152,64 @@ pub type DomainGoal = chalk_ir::DomainGoal<Interner>;
pub type Goal = chalk_ir::Goal<Interner>; pub type Goal = chalk_ir::Goal<Interner>;
pub type AliasEq = chalk_ir::AliasEq<Interner>; pub type AliasEq = chalk_ir::AliasEq<Interner>;
pub type Solution = chalk_solve::Solution<Interner>; pub type Solution = chalk_solve::Solution<Interner>;
pub type Constraint = chalk_ir::Constraint<Interner>;
pub type Constraints = chalk_ir::Constraints<Interner>;
pub type ConstrainedSubst = chalk_ir::ConstrainedSubst<Interner>; pub type ConstrainedSubst = chalk_ir::ConstrainedSubst<Interner>;
pub type Guidance = chalk_solve::Guidance<Interner>; pub type Guidance = chalk_solve::Guidance<Interner>;
pub type WhereClause = chalk_ir::WhereClause<Interner>; pub type WhereClause = chalk_ir::WhereClause<Interner>;
pub type CanonicalVarKind = chalk_ir::CanonicalVarKind<Interner>;
pub type GoalData = chalk_ir::GoalData<Interner>;
pub type Goals = chalk_ir::Goals<Interner>;
pub type ProgramClauseData = chalk_ir::ProgramClauseData<Interner>;
pub type ProgramClause = chalk_ir::ProgramClause<Interner>;
pub type ProgramClauses = chalk_ir::ProgramClauses<Interner>;
pub type TyData = chalk_ir::TyData<Interner>;
pub type Variances = chalk_ir::Variances<Interner>;
/// A constant can have reference to other things. Memory map job is holding /// A constant can have reference to other things. Memory map job is holding
/// the necessary bits of memory of the const eval session to keep the constant /// the necessary bits of memory of the const eval session to keep the constant
/// meaningful. /// meaningful.
#[derive(Debug, Default, Clone, PartialEq, Eq)] #[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct MemoryMap { pub enum MemoryMap {
pub memory: HashMap<usize, Vec<u8>>, #[default]
pub vtable: VTableMap, Empty,
Simple(Box<[u8]>),
Complex(Box<ComplexMemoryMap>),
} }
impl MemoryMap { #[derive(Debug, Default, Clone, PartialEq, Eq)]
fn insert(&mut self, addr: usize, x: Vec<u8>) { pub struct ComplexMemoryMap {
memory: FxHashMap<usize, Box<[u8]>>,
vtable: VTableMap,
}
impl ComplexMemoryMap {
fn insert(&mut self, addr: usize, val: Box<[u8]>) {
match self.memory.entry(addr) { match self.memory.entry(addr) {
Entry::Occupied(mut e) => { Entry::Occupied(mut e) => {
if e.get().len() < x.len() { if e.get().len() < val.len() {
e.insert(x); e.insert(val);
} }
} }
Entry::Vacant(e) => { Entry::Vacant(e) => {
e.insert(x); e.insert(val);
} }
} }
} }
}
impl MemoryMap {
pub fn vtable_ty(&self, id: usize) -> Result<&Ty, MirEvalError> {
match self {
MemoryMap::Empty | MemoryMap::Simple(_) => Err(MirEvalError::InvalidVTableId(id)),
MemoryMap::Complex(cm) => cm.vtable.ty(id),
}
}
fn simple(v: Box<[u8]>) -> Self {
MemoryMap::Simple(v)
}
/// This functions convert each address by a function `f` which gets the byte intervals and assign an address /// This functions convert each address by a function `f` which gets the byte intervals and assign an address
/// to them. It is useful when you want to load a constant with a memory map in a new memory. You can pass an /// to them. It is useful when you want to load a constant with a memory map in a new memory. You can pass an
@ -185,22 +217,33 @@ impl MemoryMap {
fn transform_addresses( fn transform_addresses(
&self, &self,
mut f: impl FnMut(&[u8], usize) -> Result<usize, MirEvalError>, mut f: impl FnMut(&[u8], usize) -> Result<usize, MirEvalError>,
) -> Result<HashMap<usize, usize>, MirEvalError> { ) -> Result<FxHashMap<usize, usize>, MirEvalError> {
self.memory let mut transform = |(addr, val): (&usize, &Box<[u8]>)| {
.iter() let addr = *addr;
.map(|x| { let align = if addr == 0 { 64 } else { (addr - (addr & (addr - 1))).min(64) };
let addr = *x.0; f(val, align).and_then(|it| Ok((addr, it)))
let align = if addr == 0 { 64 } else { (addr - (addr & (addr - 1))).min(64) }; };
Ok((addr, f(x.1, align)?)) match self {
}) MemoryMap::Empty => Ok(Default::default()),
.collect() MemoryMap::Simple(m) => transform((&0, m)).map(|(addr, val)| {
let mut map = FxHashMap::with_capacity_and_hasher(1, BuildHasherDefault::default());
map.insert(addr, val);
map
}),
MemoryMap::Complex(cm) => cm.memory.iter().map(transform).collect(),
}
} }
fn get<'a>(&'a self, addr: usize, size: usize) -> Option<&'a [u8]> { fn get(&self, addr: usize, size: usize) -> Option<&[u8]> {
if size == 0 { if size == 0 {
Some(&[]) Some(&[])
} else { } else {
self.memory.get(&addr)?.get(0..size) match self {
MemoryMap::Empty => Some(&[]),
MemoryMap::Simple(m) if addr == 0 => m.get(0..size),
MemoryMap::Simple(_) => None,
MemoryMap::Complex(cm) => cm.memory.get(&addr)?.get(0..size),
}
} }
} }
} }
@ -208,7 +251,7 @@ impl MemoryMap {
/// A concrete constant value /// A concrete constant value
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConstScalar { pub enum ConstScalar {
Bytes(Vec<u8>, MemoryMap), Bytes(Box<[u8]>, MemoryMap),
// FIXME: this is a hack to get around chalk not being able to represent unevaluatable // FIXME: this is a hack to get around chalk not being able to represent unevaluatable
// constants // constants
UnevaluatedConst(GeneralConstId, Substitution), UnevaluatedConst(GeneralConstId, Substitution),

View File

@ -98,16 +98,16 @@ pub enum Operand {
} }
impl Operand { impl Operand {
fn from_concrete_const(data: Vec<u8>, memory_map: MemoryMap, ty: Ty) -> Self { fn from_concrete_const(data: Box<[u8]>, memory_map: MemoryMap, ty: Ty) -> Self {
Operand::Constant(intern_const_scalar(ConstScalar::Bytes(data, memory_map), ty)) Operand::Constant(intern_const_scalar(ConstScalar::Bytes(data, memory_map), ty))
} }
fn from_bytes(data: Vec<u8>, ty: Ty) -> Self { fn from_bytes(data: Box<[u8]>, ty: Ty) -> Self {
Operand::from_concrete_const(data, MemoryMap::default(), ty) Operand::from_concrete_const(data, MemoryMap::default(), ty)
} }
fn const_zst(ty: Ty) -> Operand { fn const_zst(ty: Ty) -> Operand {
Self::from_bytes(vec![], ty) Self::from_bytes(Box::default(), ty)
} }
fn from_fn( fn from_fn(
@ -118,7 +118,7 @@ impl Operand {
let ty = let ty =
chalk_ir::TyKind::FnDef(CallableDefId::FunctionId(func_id).to_chalk(db), generic_args) chalk_ir::TyKind::FnDef(CallableDefId::FunctionId(func_id).to_chalk(db), generic_args)
.intern(Interner); .intern(Interner);
Operand::from_bytes(vec![], ty) Operand::from_bytes(Box::default(), ty)
} }
} }

View File

@ -1,13 +1,6 @@
//! This module provides a MIR interpreter, which is used in const eval. //! This module provides a MIR interpreter, which is used in const eval.
use std::{ use std::{borrow::Cow, cell::RefCell, fmt::Write, iter, mem, ops::Range};
borrow::Cow,
cell::RefCell,
collections::{HashMap, HashSet},
fmt::Write,
iter, mem,
ops::Range,
};
use base_db::{CrateId, FileId}; use base_db::{CrateId, FileId};
use chalk_ir::{cast::Cast, Mutability}; use chalk_ir::{cast::Cast, Mutability};
@ -40,8 +33,8 @@ use crate::{
name, static_lifetime, name, static_lifetime,
traits::FnTrait, traits::FnTrait,
utils::{detect_variant_from_bytes, ClosureSubst}, utils::{detect_variant_from_bytes, ClosureSubst},
CallableDefId, ClosureId, Const, ConstScalar, FnDefId, Interner, MemoryMap, Substitution, CallableDefId, ClosureId, ComplexMemoryMap, Const, ConstScalar, FnDefId, Interner, MemoryMap,
TraitEnvironment, Ty, TyBuilder, TyExt, TyKind, Substitution, TraitEnvironment, Ty, TyBuilder, TyExt, TyKind,
}; };
use super::{ use super::{
@ -98,6 +91,15 @@ impl VTableMap {
let id = from_bytes!(usize, bytes); let id = from_bytes!(usize, bytes);
self.ty(id) self.ty(id)
} }
pub fn shrink_to_fit(&mut self) {
self.id_to_ty.shrink_to_fit();
self.ty_to_id.shrink_to_fit();
}
fn is_empty(&self) -> bool {
self.id_to_ty.is_empty() && self.ty_to_id.is_empty()
}
} }
#[derive(Debug, Default, Clone, PartialEq, Eq)] #[derive(Debug, Default, Clone, PartialEq, Eq)]
@ -251,13 +253,6 @@ impl From<Interval> for IntervalOrOwned {
} }
impl IntervalOrOwned { impl IntervalOrOwned {
pub(crate) fn to_vec(self, memory: &Evaluator<'_>) -> Result<Vec<u8>> {
Ok(match self {
IntervalOrOwned::Owned(o) => o,
IntervalOrOwned::Borrowed(b) => b.get(memory)?.to_vec(),
})
}
fn get<'a>(&'a self, memory: &'a Evaluator<'a>) -> Result<&'a [u8]> { fn get<'a>(&'a self, memory: &'a Evaluator<'a>) -> Result<&'a [u8]> {
Ok(match self { Ok(match self {
IntervalOrOwned::Owned(o) => o, IntervalOrOwned::Owned(o) => o,
@ -291,8 +286,8 @@ impl Address {
} }
} }
fn to_bytes(&self) -> Vec<u8> { fn to_bytes(&self) -> [u8; mem::size_of::<usize>()] {
usize::to_le_bytes(self.to_usize()).to_vec() usize::to_le_bytes(self.to_usize())
} }
fn to_usize(&self) -> usize { fn to_usize(&self) -> usize {
@ -510,6 +505,20 @@ struct Locals {
drop_flags: DropFlags, drop_flags: DropFlags,
} }
pub struct MirOutput {
stdout: Vec<u8>,
stderr: Vec<u8>,
}
impl MirOutput {
pub fn stdout(&self) -> Cow<'_, str> {
String::from_utf8_lossy(&self.stdout)
}
pub fn stderr(&self) -> Cow<'_, str> {
String::from_utf8_lossy(&self.stderr)
}
}
pub fn interpret_mir( pub fn interpret_mir(
db: &dyn HirDatabase, db: &dyn HirDatabase,
body: Arc<MirBody>, body: Arc<MirBody>,
@ -520,27 +529,31 @@ pub fn interpret_mir(
// (and probably should) do better here, for example by excluding bindings outside of the target expression. // (and probably should) do better here, for example by excluding bindings outside of the target expression.
assert_placeholder_ty_is_unused: bool, assert_placeholder_ty_is_unused: bool,
trait_env: Option<Arc<TraitEnvironment>>, trait_env: Option<Arc<TraitEnvironment>>,
) -> (Result<Const>, String, String) { ) -> (Result<Const>, MirOutput) {
let ty = body.locals[return_slot()].ty.clone(); let ty = body.locals[return_slot()].ty.clone();
let mut evaluator = Evaluator::new(db, body.owner, assert_placeholder_ty_is_unused, trait_env); let mut evaluator = Evaluator::new(db, body.owner, assert_placeholder_ty_is_unused, trait_env);
let it: Result<Const> = (|| { let it: Result<Const> = (|| {
if evaluator.ptr_size() != std::mem::size_of::<usize>() { if evaluator.ptr_size() != std::mem::size_of::<usize>() {
not_supported!("targets with different pointer size from host"); not_supported!("targets with different pointer size from host");
} }
let bytes = evaluator.interpret_mir(body.clone(), None.into_iter())?; let interval = evaluator.interpret_mir(body.clone(), None.into_iter())?;
let bytes = interval.get(&evaluator)?;
let mut memory_map = evaluator.create_memory_map( let mut memory_map = evaluator.create_memory_map(
&bytes, bytes,
&ty, &ty,
&Locals { ptr: ArenaMap::new(), body, drop_flags: DropFlags::default() }, &Locals { ptr: ArenaMap::new(), body, drop_flags: DropFlags::default() },
)?; )?;
memory_map.vtable = evaluator.vtable_map.clone(); let bytes = bytes.into();
let memory_map = if memory_map.memory.is_empty() && evaluator.vtable_map.is_empty() {
MemoryMap::Empty
} else {
memory_map.vtable = mem::take(&mut evaluator.vtable_map);
memory_map.vtable.shrink_to_fit();
MemoryMap::Complex(Box::new(memory_map))
};
return Ok(intern_const_scalar(ConstScalar::Bytes(bytes, memory_map), ty)); return Ok(intern_const_scalar(ConstScalar::Bytes(bytes, memory_map), ty));
})(); })();
( (it, MirOutput { stdout: evaluator.stdout, stderr: evaluator.stderr })
it,
String::from_utf8_lossy(&evaluator.stdout).into_owned(),
String::from_utf8_lossy(&evaluator.stderr).into_owned(),
)
} }
#[cfg(test)] #[cfg(test)]
@ -562,7 +575,7 @@ impl Evaluator<'_> {
code_stack: vec![], code_stack: vec![],
vtable_map: VTableMap::default(), vtable_map: VTableMap::default(),
thread_local_storage: TlsData::default(), thread_local_storage: TlsData::default(),
static_locations: HashMap::default(), static_locations: Default::default(),
db, db,
random_state: oorandom::Rand64::new(0), random_state: oorandom::Rand64::new(0),
trait_env: trait_env.unwrap_or_else(|| db.trait_environment_for_body(owner)), trait_env: trait_env.unwrap_or_else(|| db.trait_environment_for_body(owner)),
@ -573,11 +586,11 @@ impl Evaluator<'_> {
stack_depth_limit: 100, stack_depth_limit: 100,
execution_limit: EXECUTION_LIMIT, execution_limit: EXECUTION_LIMIT,
memory_limit: 1000_000_000, // 2GB, 1GB for stack and 1GB for heap memory_limit: 1000_000_000, // 2GB, 1GB for stack and 1GB for heap
layout_cache: RefCell::new(HashMap::default()), layout_cache: RefCell::new(Default::default()),
projected_ty_cache: RefCell::new(HashMap::default()), projected_ty_cache: RefCell::new(Default::default()),
not_special_fn_cache: RefCell::new(HashSet::default()), not_special_fn_cache: RefCell::new(Default::default()),
mir_or_dyn_index_cache: RefCell::new(HashMap::default()), mir_or_dyn_index_cache: RefCell::new(Default::default()),
unused_locals_store: RefCell::new(HashMap::default()), unused_locals_store: RefCell::new(Default::default()),
cached_ptr_size: match db.target_data_layout(crate_id) { cached_ptr_size: match db.target_data_layout(crate_id) {
Some(it) => it.pointer_size.bytes_usize(), Some(it) => it.pointer_size.bytes_usize(),
None => 8, None => 8,
@ -803,11 +816,11 @@ impl Evaluator<'_> {
}) })
} }
fn interpret_mir( fn interpret_mir<'slf>(
&mut self, &'slf mut self,
body: Arc<MirBody>, body: Arc<MirBody>,
args: impl Iterator<Item = IntervalOrOwned>, args: impl Iterator<Item = IntervalOrOwned>,
) -> Result<Vec<u8>> { ) -> Result<Interval> {
if let Some(it) = self.stack_depth_limit.checked_sub(1) { if let Some(it) = self.stack_depth_limit.checked_sub(1) {
self.stack_depth_limit = it; self.stack_depth_limit = it;
} else { } else {
@ -837,8 +850,8 @@ impl Evaluator<'_> {
match &statement.kind { match &statement.kind {
StatementKind::Assign(l, r) => { StatementKind::Assign(l, r) => {
let addr = self.place_addr(l, &locals)?; let addr = self.place_addr(l, &locals)?;
let result = self.eval_rvalue(r, &mut locals)?.to_vec(&self)?; let result = self.eval_rvalue(r, &mut locals)?;
self.write_memory(addr, &result)?; self.copy_from_interval_or_owned(addr, result)?;
locals locals
.drop_flags .drop_flags
.add_place(l.clone(), &locals.body.projection_store); .add_place(l.clone(), &locals.body.projection_store);
@ -957,7 +970,7 @@ impl Evaluator<'_> {
None => { None => {
self.code_stack = prev_code_stack; self.code_stack = prev_code_stack;
self.stack_depth_limit += 1; self.stack_depth_limit += 1;
return Ok(return_interval.get(self)?.to_vec()); return Ok(return_interval);
} }
Some(bb) => { Some(bb) => {
// We don't support const promotion, so we can't truncate the stack yet. // We don't support const promotion, so we can't truncate the stack yet.
@ -1050,7 +1063,7 @@ impl Evaluator<'_> {
Rvalue::Use(it) => Borrowed(self.eval_operand(it, locals)?), Rvalue::Use(it) => Borrowed(self.eval_operand(it, locals)?),
Rvalue::Ref(_, p) => { Rvalue::Ref(_, p) => {
let (addr, _, metadata) = self.place_addr_and_ty_and_metadata(p, locals)?; let (addr, _, metadata) = self.place_addr_and_ty_and_metadata(p, locals)?;
let mut r = addr.to_bytes(); let mut r = addr.to_bytes().to_vec();
if let Some(metadata) = metadata { if let Some(metadata) = metadata {
r.extend(metadata.get(self)?); r.extend(metadata.get(self)?);
} }
@ -1283,7 +1296,7 @@ impl Evaluator<'_> {
not_supported!("unsized box initialization"); not_supported!("unsized box initialization");
}; };
let addr = self.heap_allocate(size, align)?; let addr = self.heap_allocate(size, align)?;
Owned(addr.to_bytes()) Owned(addr.to_bytes().to_vec())
} }
Rvalue::CopyForDeref(_) => not_supported!("copy for deref"), Rvalue::CopyForDeref(_) => not_supported!("copy for deref"),
Rvalue::Aggregate(kind, values) => { Rvalue::Aggregate(kind, values) => {
@ -1715,7 +1728,18 @@ impl Evaluator<'_> {
} }
let addr = self.heap_allocate(size, align)?; let addr = self.heap_allocate(size, align)?;
self.write_memory(addr, &v)?; self.write_memory(addr, &v)?;
self.patch_addresses(&patch_map, &memory_map.vtable, addr, ty, locals)?; self.patch_addresses(
&patch_map,
|bytes| match &memory_map {
MemoryMap::Empty | MemoryMap::Simple(_) => {
Err(MirEvalError::InvalidVTableId(from_bytes!(usize, bytes)))
}
MemoryMap::Complex(cm) => cm.vtable.ty_of_bytes(bytes),
},
addr,
ty,
locals,
)?;
Ok(Interval::new(addr, size)) Ok(Interval::new(addr, size))
} }
@ -1767,6 +1791,13 @@ impl Evaluator<'_> {
Ok(()) Ok(())
} }
fn copy_from_interval_or_owned(&mut self, addr: Address, r: IntervalOrOwned) -> Result<()> {
match r {
IntervalOrOwned::Borrowed(r) => self.copy_from_interval(addr, r),
IntervalOrOwned::Owned(r) => self.write_memory(addr, &r),
}
}
fn copy_from_interval(&mut self, addr: Address, r: Interval) -> Result<()> { fn copy_from_interval(&mut self, addr: Address, r: Interval) -> Result<()> {
if r.size == 0 { if r.size == 0 {
return Ok(()); return Ok(());
@ -1887,13 +1918,18 @@ impl Evaluator<'_> {
} }
} }
fn create_memory_map(&self, bytes: &[u8], ty: &Ty, locals: &Locals) -> Result<MemoryMap> { fn create_memory_map(
&self,
bytes: &[u8],
ty: &Ty,
locals: &Locals,
) -> Result<ComplexMemoryMap> {
fn rec( fn rec(
this: &Evaluator<'_>, this: &Evaluator<'_>,
bytes: &[u8], bytes: &[u8],
ty: &Ty, ty: &Ty,
locals: &Locals, locals: &Locals,
mm: &mut MemoryMap, mm: &mut ComplexMemoryMap,
) -> Result<()> { ) -> Result<()> {
match ty.kind(Interner) { match ty.kind(Interner) {
TyKind::Ref(_, _, t) => { TyKind::Ref(_, _, t) => {
@ -1903,7 +1939,7 @@ impl Evaluator<'_> {
let addr_usize = from_bytes!(usize, bytes); let addr_usize = from_bytes!(usize, bytes);
mm.insert( mm.insert(
addr_usize, addr_usize,
this.read_memory(Address::from_usize(addr_usize), size)?.to_vec(), this.read_memory(Address::from_usize(addr_usize), size)?.into(),
) )
} }
None => { None => {
@ -1929,7 +1965,7 @@ impl Evaluator<'_> {
let size = element_size * count; let size = element_size * count;
let addr = Address::from_bytes(addr)?; let addr = Address::from_bytes(addr)?;
let b = this.read_memory(addr, size)?; let b = this.read_memory(addr, size)?;
mm.insert(addr.to_usize(), b.to_vec()); mm.insert(addr.to_usize(), b.into());
if let Some(ty) = check_inner { if let Some(ty) = check_inner {
for i in 0..count { for i in 0..count {
let offset = element_size * i; let offset = element_size * i;
@ -2002,15 +2038,15 @@ impl Evaluator<'_> {
} }
Ok(()) Ok(())
} }
let mut mm = MemoryMap::default(); let mut mm = ComplexMemoryMap::default();
rec(self, bytes, ty, locals, &mut mm)?; rec(&self, bytes, ty, locals, &mut mm)?;
Ok(mm) Ok(mm)
} }
fn patch_addresses( fn patch_addresses<'vtable>(
&mut self, &mut self,
patch_map: &HashMap<usize, usize>, patch_map: &FxHashMap<usize, usize>,
old_vtable: &VTableMap, ty_of_bytes: impl Fn(&[u8]) -> Result<&'vtable Ty> + Copy,
addr: Address, addr: Address,
ty: &Ty, ty: &Ty,
locals: &Locals, locals: &Locals,
@ -2037,7 +2073,7 @@ impl Evaluator<'_> {
} }
} }
TyKind::Function(_) => { TyKind::Function(_) => {
let ty = old_vtable.ty_of_bytes(self.read_memory(addr, my_size)?)?.clone(); let ty = ty_of_bytes(self.read_memory(addr, my_size)?)?.clone();
let new_id = self.vtable_map.id(ty); let new_id = self.vtable_map.id(ty);
self.write_memory(addr, &new_id.to_le_bytes())?; self.write_memory(addr, &new_id.to_le_bytes())?;
} }
@ -2048,7 +2084,7 @@ impl Evaluator<'_> {
let ty = ty.clone().substitute(Interner, subst); let ty = ty.clone().substitute(Interner, subst);
self.patch_addresses( self.patch_addresses(
patch_map, patch_map,
old_vtable, ty_of_bytes,
addr.offset(offset), addr.offset(offset),
&ty, &ty,
locals, locals,
@ -2070,7 +2106,7 @@ impl Evaluator<'_> {
let ty = ty.clone().substitute(Interner, subst); let ty = ty.clone().substitute(Interner, subst);
self.patch_addresses( self.patch_addresses(
patch_map, patch_map,
old_vtable, ty_of_bytes,
addr.offset(offset), addr.offset(offset),
&ty, &ty,
locals, locals,
@ -2083,7 +2119,7 @@ impl Evaluator<'_> {
for (id, ty) in subst.iter(Interner).enumerate() { for (id, ty) in subst.iter(Interner).enumerate() {
let ty = ty.assert_ty_ref(Interner); // Tuple only has type argument let ty = ty.assert_ty_ref(Interner); // Tuple only has type argument
let offset = layout.fields.offset(id).bytes_usize(); let offset = layout.fields.offset(id).bytes_usize();
self.patch_addresses(patch_map, old_vtable, addr.offset(offset), ty, locals)?; self.patch_addresses(patch_map, ty_of_bytes, addr.offset(offset), ty, locals)?;
} }
} }
TyKind::Array(inner, len) => { TyKind::Array(inner, len) => {
@ -2095,7 +2131,7 @@ impl Evaluator<'_> {
for i in 0..len { for i in 0..len {
self.patch_addresses( self.patch_addresses(
patch_map, patch_map,
old_vtable, ty_of_bytes,
addr.offset(i * size), addr.offset(i * size),
inner, inner,
locals, locals,
@ -2166,14 +2202,14 @@ impl Evaluator<'_> {
.map_err(|it| MirEvalError::MirLowerErrorForClosure(closure, it))?; .map_err(|it| MirEvalError::MirLowerErrorForClosure(closure, it))?;
let closure_data = if mir_body.locals[mir_body.param_locals[0]].ty.as_reference().is_some() let closure_data = if mir_body.locals[mir_body.param_locals[0]].ty.as_reference().is_some()
{ {
closure_data.addr.to_bytes() closure_data.addr.to_bytes().to_vec()
} else { } else {
closure_data.get(self)?.to_owned() closure_data.get(self)?.to_owned()
}; };
let arg_bytes = iter::once(Ok(closure_data)) let arg_bytes = iter::once(Ok(closure_data))
.chain(args.iter().map(|it| Ok(it.get(&self)?.to_owned()))) .chain(args.iter().map(|it| Ok(it.get(&self)?.to_owned())))
.collect::<Result<Vec<_>>>()?; .collect::<Result<Vec<_>>>()?;
let bytes = self let interval = self
.interpret_mir(mir_body, arg_bytes.into_iter().map(IntervalOrOwned::Owned)) .interpret_mir(mir_body, arg_bytes.into_iter().map(IntervalOrOwned::Owned))
.map_err(|e| { .map_err(|e| {
MirEvalError::InFunction( MirEvalError::InFunction(
@ -2181,7 +2217,7 @@ impl Evaluator<'_> {
vec![(Either::Right(closure), span, locals.body.owner)], vec![(Either::Right(closure), span, locals.body.owner)],
) )
})?; })?;
destination.write_from_bytes(self, &bytes)?; destination.write_from_interval(self, interval)?;
Ok(None) Ok(None)
} }
@ -2374,7 +2410,7 @@ impl Evaluator<'_> {
vec![(Either::Left(def), span, locals.body.owner)], vec![(Either::Left(def), span, locals.body.owner)],
) )
})?; })?;
destination.write_from_bytes(self, &result)?; destination.write_from_interval(self, result)?;
None None
}) })
} }
@ -2552,7 +2588,7 @@ impl Evaluator<'_> {
body, body,
locals, locals,
drop_fn, drop_fn,
[IntervalOrOwned::Owned(addr.to_bytes())].into_iter(), iter::once(IntervalOrOwned::Owned(addr.to_bytes().to_vec())),
span, span,
Interval { addr: Address::Invalid(0), size: 0 }, Interval { addr: Address::Invalid(0), size: 0 },
None, None,
@ -2680,11 +2716,12 @@ pub fn render_const_using_debug_impl(
) else { ) else {
not_supported!("std::fmt::format not found"); not_supported!("std::fmt::format not found");
}; };
let message_string = evaluator.interpret_mir( let interval = evaluator.interpret_mir(
db.mir_body(format_fn.into()).map_err(|e| MirEvalError::MirLowerError(format_fn, e))?, db.mir_body(format_fn.into()).map_err(|e| MirEvalError::MirLowerError(format_fn, e))?,
[IntervalOrOwned::Borrowed(Interval { addr: a3, size: evaluator.ptr_size() * 6 })] [IntervalOrOwned::Borrowed(Interval { addr: a3, size: evaluator.ptr_size() * 6 })]
.into_iter(), .into_iter(),
)?; )?;
let message_string = interval.get(&evaluator)?;
let addr = let addr =
Address::from_bytes(&message_string[evaluator.ptr_size()..2 * evaluator.ptr_size()])?; Address::from_bytes(&message_string[evaluator.ptr_size()..2 * evaluator.ptr_size()])?;
let size = from_bytes!(usize, message_string[2 * evaluator.ptr_size()..]); let size = from_bytes!(usize, message_string[2 * evaluator.ptr_size()..]);

View File

@ -322,12 +322,13 @@ impl Evaluator<'_> {
let hir_def::resolver::ValueNs::FunctionId(format_fn) = format_fn else { let hir_def::resolver::ValueNs::FunctionId(format_fn) = format_fn else {
not_supported!("std::fmt::format is not a function") not_supported!("std::fmt::format is not a function")
}; };
let message_string = self.interpret_mir( let interval = self.interpret_mir(
self.db self.db
.mir_body(format_fn.into()) .mir_body(format_fn.into())
.map_err(|e| MirEvalError::MirLowerError(format_fn, e))?, .map_err(|e| MirEvalError::MirLowerError(format_fn, e))?,
args.map(|x| IntervalOrOwned::Owned(x.clone())), args.map(|x| IntervalOrOwned::Owned(x.clone())),
)?; )?;
let message_string = interval.get(self)?;
let addr = let addr =
Address::from_bytes(&message_string[self.ptr_size()..2 * self.ptr_size()])?; Address::from_bytes(&message_string[self.ptr_size()..2 * self.ptr_size()])?;
let size = from_bytes!(usize, message_string[2 * self.ptr_size()..]); let size = from_bytes!(usize, message_string[2 * self.ptr_size()..]);

View File

@ -31,9 +31,9 @@ fn eval_main(db: &TestDB, file_id: FileId) -> Result<(String, String), MirEvalEr
db.trait_environment(func_id.into()), db.trait_environment(func_id.into()),
) )
.map_err(|e| MirEvalError::MirLowerError(func_id.into(), e))?; .map_err(|e| MirEvalError::MirLowerError(func_id.into(), e))?;
let (result, stdout, stderr) = interpret_mir(db, body, false, None); let (result, output) = interpret_mir(db, body, false, None);
result?; result?;
Ok((stdout, stderr)) Ok((output.stdout().into_owned(), output.stderr().into_owned()))
} }
fn check_pass(ra_fixture: &str) { fn check_pass(ra_fixture: &str) {

View File

@ -540,7 +540,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
self.write_bytes_to_place( self.write_bytes_to_place(
then_target, then_target,
place.clone(), place.clone(),
vec![1], Box::new([1]),
TyBuilder::bool(), TyBuilder::bool(),
MirSpan::Unknown, MirSpan::Unknown,
)?; )?;
@ -548,7 +548,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
self.write_bytes_to_place( self.write_bytes_to_place(
else_target, else_target,
place, place,
vec![0], Box::new([0]),
TyBuilder::bool(), TyBuilder::bool(),
MirSpan::Unknown, MirSpan::Unknown,
)?; )?;
@ -602,7 +602,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
generic_args, generic_args,
) )
.intern(Interner); .intern(Interner);
let func = Operand::from_bytes(vec![], ty); let func = Operand::from_bytes(Box::default(), ty);
return self.lower_call_and_args( return self.lower_call_and_args(
func, func,
iter::once(*callee).chain(args.iter().copied()), iter::once(*callee).chain(args.iter().copied()),
@ -615,7 +615,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
let callee_ty = self.expr_ty_after_adjustments(*callee); let callee_ty = self.expr_ty_after_adjustments(*callee);
match &callee_ty.kind(Interner) { match &callee_ty.kind(Interner) {
chalk_ir::TyKind::FnDef(..) => { chalk_ir::TyKind::FnDef(..) => {
let func = Operand::from_bytes(vec![], callee_ty.clone()); let func = Operand::from_bytes(Box::default(), callee_ty.clone());
self.lower_call_and_args( self.lower_call_and_args(
func, func,
args.iter().copied(), args.iter().copied(),
@ -1113,7 +1113,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
Some("start") => lp.take(), Some("start") => lp.take(),
Some("end") => rp.take(), Some("end") => rp.take(),
Some("exhausted") => { Some("exhausted") => {
Some(Operand::from_bytes(vec![0], TyBuilder::bool())) Some(Operand::from_bytes(Box::new([0]), TyBuilder::bool()))
} }
_ => None, _ => None,
}; };
@ -1395,46 +1395,43 @@ impl<'ctx> MirLowerCtx<'ctx> {
} }
fn lower_literal_to_operand(&mut self, ty: Ty, l: &Literal) -> Result<Operand> { fn lower_literal_to_operand(&mut self, ty: Ty, l: &Literal) -> Result<Operand> {
let size = self let size = || {
.db self.db
.layout_of_ty(ty.clone(), self.db.trait_environment_for_body(self.owner))? .layout_of_ty(ty.clone(), self.db.trait_environment_for_body(self.owner))
.size .map(|it| it.size.bytes_usize())
.bytes_usize(); };
let bytes = match l { const USIZE_SIZE: usize = mem::size_of::<usize>();
let bytes: Box<[_]> = match l {
hir_def::hir::Literal::String(b) => { hir_def::hir::Literal::String(b) => {
let b = b.as_bytes(); let mut data = [0; { 2 * USIZE_SIZE }];
let mut data = Vec::with_capacity(mem::size_of::<usize>() * 2); data[..USIZE_SIZE].copy_from_slice(&0usize.to_le_bytes());
data.extend(0usize.to_le_bytes()); data[USIZE_SIZE..].copy_from_slice(&b.len().to_le_bytes());
data.extend(b.len().to_le_bytes()); let mm = MemoryMap::simple(b.as_bytes().into());
let mut mm = MemoryMap::default(); return Ok(Operand::from_concrete_const(Box::new(data), mm, ty));
mm.insert(0, b.to_vec());
return Ok(Operand::from_concrete_const(data, mm, ty));
} }
hir_def::hir::Literal::CString(b) => { hir_def::hir::Literal::CString(b) => {
let bytes = b.iter().copied().chain(iter::once(0)).collect::<Vec<_>>(); let bytes = b.iter().copied().chain(iter::once(0)).collect::<Box<_>>();
let mut data = Vec::with_capacity(mem::size_of::<usize>() * 2); let mut data = [0; { 2 * USIZE_SIZE }];
data.extend(0usize.to_le_bytes()); data[..USIZE_SIZE].copy_from_slice(&0usize.to_le_bytes());
data.extend(bytes.len().to_le_bytes()); data[USIZE_SIZE..].copy_from_slice(&bytes.len().to_le_bytes());
let mut mm = MemoryMap::default(); let mm = MemoryMap::simple(bytes);
mm.insert(0, bytes); return Ok(Operand::from_concrete_const(Box::new(data), mm, ty));
return Ok(Operand::from_concrete_const(data, mm, ty));
} }
hir_def::hir::Literal::ByteString(b) => { hir_def::hir::Literal::ByteString(b) => {
let mut data = Vec::with_capacity(mem::size_of::<usize>() * 2); let mut data = [0; { 2 * USIZE_SIZE }];
data.extend(0usize.to_le_bytes()); data[..USIZE_SIZE].copy_from_slice(&0usize.to_le_bytes());
data.extend(b.len().to_le_bytes()); data[USIZE_SIZE..].copy_from_slice(&b.len().to_le_bytes());
let mut mm = MemoryMap::default(); let mm = MemoryMap::simple(b.clone());
mm.insert(0, b.to_vec()); return Ok(Operand::from_concrete_const(Box::new(data), mm, ty));
return Ok(Operand::from_concrete_const(data, mm, ty));
} }
hir_def::hir::Literal::Char(c) => u32::from(*c).to_le_bytes().into(), hir_def::hir::Literal::Char(c) => Box::new(u32::from(*c).to_le_bytes()),
hir_def::hir::Literal::Bool(b) => vec![*b as u8], hir_def::hir::Literal::Bool(b) => Box::new([*b as u8]),
hir_def::hir::Literal::Int(it, _) => it.to_le_bytes()[0..size].into(), hir_def::hir::Literal::Int(it, _) => Box::from(&it.to_le_bytes()[0..size()?]),
hir_def::hir::Literal::Uint(it, _) => it.to_le_bytes()[0..size].into(), hir_def::hir::Literal::Uint(it, _) => Box::from(&it.to_le_bytes()[0..size()?]),
hir_def::hir::Literal::Float(f, _) => match size { hir_def::hir::Literal::Float(f, _) => match size()? {
8 => f.into_f64().to_le_bytes().into(), 8 => Box::new(f.into_f64().to_le_bytes()),
4 => f.into_f32().to_le_bytes().into(), 4 => Box::new(f.into_f32().to_le_bytes()),
_ => { _ => {
return Err(MirLowerError::TypeError("float with size other than 4 or 8 bytes")) return Err(MirLowerError::TypeError("float with size other than 4 or 8 bytes"))
} }
@ -1483,7 +1480,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
&mut self, &mut self,
prev_block: BasicBlockId, prev_block: BasicBlockId,
place: Place, place: Place,
cv: Vec<u8>, cv: Box<[u8]>,
ty: Ty, ty: Ty,
span: MirSpan, span: MirSpan,
) -> Result<()> { ) -> Result<()> {

View File

@ -244,7 +244,7 @@ impl MirLowerCtx<'_> {
); );
} else { } else {
let c = Operand::from_concrete_const( let c = Operand::from_concrete_const(
pattern_len.to_le_bytes().to_vec(), pattern_len.to_le_bytes().into(),
MemoryMap::default(), MemoryMap::default(),
TyBuilder::usize(), TyBuilder::usize(),
); );

View File

@ -2183,7 +2183,7 @@ impl Function {
return r; return r;
} }
}; };
let (result, stdout, stderr) = interpret_mir(db, body, false, None); let (result, output) = interpret_mir(db, body, false, None);
let mut text = match result { let mut text = match result {
Ok(_) => "pass".to_string(), Ok(_) => "pass".to_string(),
Err(e) => { Err(e) => {
@ -2192,10 +2192,12 @@ impl Function {
r r
} }
}; };
let stdout = output.stdout().into_owned();
if !stdout.is_empty() { if !stdout.is_empty() {
text += "\n--------- stdout ---------\n"; text += "\n--------- stdout ---------\n";
text += &stdout; text += &stdout;
} }
let stderr = output.stdout().into_owned();
if !stderr.is_empty() { if !stderr.is_empty() {
text += "\n--------- stderr ---------\n"; text += "\n--------- stderr ---------\n";
text += &stderr; text += &stderr;