Skip to content

Commit

Permalink
Summary cache tweaks (#17)
Browse files Browse the repository at this point in the history
* Tweak comments

* Specialize type of aliased func ref

* Cleanup visit_function_reference calls
  • Loading branch information
hermanventer authored Dec 5, 2024
1 parent 938d702 commit fc3dd65
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 138 deletions.
Binary file modified binaries/summary_store.tar
Binary file not shown.
113 changes: 53 additions & 60 deletions checker/src/block_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ impl<'block, 'analysis, 'compilation, 'tcx> BlockVisitor<'block, 'analysis, 'com
&actual_argument_types,
);
let fun_ty = tcx.type_of(destructor.did).skip_binder();
// since ty is specialized, fun_ty and args should be specialized as well.
let func_const = self
.visit_function_reference(destructor.did, fun_ty, Some(args))
.clone();
Expand Down Expand Up @@ -661,10 +662,9 @@ impl<'block, 'analysis, 'compilation, 'tcx> BlockVisitor<'block, 'analysis, 'com
if utils::is_concrete(arg_ty.kind()) {
arg_ty
} else {
let specialized_ty = self.type_visitor().specialize_generic_argument_type(
arg_ty,
&self.type_visitor().generic_argument_map,
);
let specialized_ty = self
.type_visitor()
.specialize_type(arg_ty, &self.type_visitor().generic_argument_map);
if utils::is_concrete(specialized_ty.kind()) {
specialized_ty
} else {
Expand Down Expand Up @@ -1012,11 +1012,9 @@ impl<'block, 'analysis, 'compilation, 'tcx> BlockVisitor<'block, 'analysis, 'com
let closure_ty = self
.type_visitor()
.get_path_rustc_type(path, self.bv.current_span);
let mut specialized_closure_ty =
self.type_visitor().specialize_generic_argument_type(
closure_ty,
&self.type_visitor().generic_argument_map,
);
let mut specialized_closure_ty = self
.type_visitor()
.specialize_type(closure_ty, &self.type_visitor().generic_argument_map);
if let TyKind::Alias(
rustc_middle::ty::Opaque,
rustc_middle::ty::AliasTy { def_id, args, .. },
Expand All @@ -1030,9 +1028,7 @@ impl<'block, 'analysis, 'compilation, 'tcx> BlockVisitor<'block, 'analysis, 'com
let map = self
.type_visitor()
.get_generic_arguments_map(*def_id, args, &[]);
specialized_closure_ty = self
.type_visitor()
.specialize_generic_argument_type(closure_ty, &map);
specialized_closure_ty = self.type_visitor().specialize_type(closure_ty, &map);
}
match specialized_closure_ty.kind() {
TyKind::Closure(def_id, args)
Expand All @@ -1045,17 +1041,17 @@ impl<'block, 'analysis, 'compilation, 'tcx> BlockVisitor<'block, 'analysis, 'com
));
}
TyKind::Ref(_, ty, _) => {
let specialized_closure_ty =
self.type_visitor().specialize_generic_argument_type(
*ty,
&self.type_visitor().generic_argument_map,
);
let specialized_closure_ty = self
.type_visitor()
.specialize_type(*ty, &self.type_visitor().generic_argument_map);
if let TyKind::Closure(def_id, args) | TyKind::FnDef(def_id, args) =
specialized_closure_ty.kind()
{
let fun_ty = self.bv.tcx.type_of(def_id).skip_binder();
// since specialized_closure_ty is specialized, fun_ty and args should be specialized as well.
return extract_func_ref(self.visit_function_reference(
*def_id,
specialized_closure_ty,
fun_ty,
Some(args),
));
}
Expand Down Expand Up @@ -1746,10 +1742,9 @@ impl<'block, 'analysis, 'compilation, 'tcx> BlockVisitor<'block, 'analysis, 'com
self.visit_len(path, place);
}
mir::Rvalue::Cast(cast_kind, operand, ty) => {
let specialized_ty = self.type_visitor().specialize_generic_argument_type(
*ty,
&self.type_visitor().generic_argument_map,
);
let specialized_ty = self
.type_visitor()
.specialize_type(*ty, &self.type_visitor().generic_argument_map);
self.visit_cast(path, *cast_kind, operand, specialized_ty);
}
mir::Rvalue::BinaryOp(bin_op, box (left_operand, right_operand)) => {
Expand All @@ -1760,10 +1755,9 @@ impl<'block, 'analysis, 'compilation, 'tcx> BlockVisitor<'block, 'analysis, 'com
};
}
mir::Rvalue::NullaryOp(null_op, ty) => {
let specialized_ty = self.type_visitor().specialize_generic_argument_type(
*ty,
&self.type_visitor().generic_argument_map,
);
let specialized_ty = self
.type_visitor()
.specialize_type(*ty, &self.type_visitor().generic_argument_map);
self.visit_nullary_op(path, null_op, specialized_ty);
}
mir::Rvalue::UnaryOp(unary_op, operand) => {
Expand Down Expand Up @@ -2669,9 +2663,15 @@ impl<'block, 'analysis, 'compilation, 'tcx> BlockVisitor<'block, 'analysis, 'com
mir::AggregateKind::Closure(def_id, args)
| mir::AggregateKind::Coroutine(def_id, args)
| mir::AggregateKind::CoroutineClosure(def_id, args) => {
//| mir::AggregateKind::CoroutineClosure(def_id, args) => {
let ty = self.bv.tcx.type_of(*def_id).skip_binder();
let func_const = self.visit_function_reference(*def_id, ty, Some(args));
let specialized_ty = self
.type_visitor()
.specialize_type(ty, &self.type_visitor().generic_argument_map);
let specialized_args = self
.type_visitor()
.specialize_generic_args(args, &self.type_visitor().generic_argument_map);
let func_const =
self.visit_function_reference(*def_id, specialized_ty, Some(specialized_args));
let func_val = Rc::new(func_const.clone().into());
self.bv.update_value_at(path.clone(), func_val);
for (i, operand) in operands.iter().enumerate() {
Expand Down Expand Up @@ -2720,7 +2720,7 @@ impl<'block, 'analysis, 'compilation, 'tcx> BlockVisitor<'block, 'analysis, 'com
let value_path = Path::new_field(Path::new_field(Path::new_field(path, 0), 0), 0);
let ty = self
.type_visitor()
.specialize_generic_argument_type(ty, &self.type_visitor().generic_argument_map);
.specialize_type(ty, &self.type_visitor().generic_argument_map);
self.type_visitor_mut()
.set_path_rustc_type(value_path.clone(), ty);
// todo: set value_path to boxed type
Expand Down Expand Up @@ -3606,23 +3606,18 @@ impl<'block, 'analysis, 'compilation, 'tcx> BlockVisitor<'block, 'analysis, 'com
bytes_left_deserialize
}
// todo: bytes is the serialization of the captured state of a closure/generator
// deserialize that and return an heap block that represents the closure state + func ptr
// deserialize that and return a heap block that represents the closure state + func ptr
TyKind::Closure(def_id, args)
| TyKind::FnDef(def_id, args)
| TyKind::Coroutine(def_id, args, ..)
| TyKind::Alias(
rustc_middle::ty::Opaque,
rustc_middle::ty::AliasTy { def_id, args, .. },
) => {
let specialized_ty = self.type_visitor().specialize_generic_argument_type(
ty,
&self.type_visitor().generic_argument_map,
);
let specialized_args = self
.type_visitor()
.specialize_generic_args(args, &self.type_visitor().generic_argument_map);
let fun_ty = self.bv.tcx.type_of(def_id).skip_binder();
// since ty is specialized, fun_ty and args should be specialized as well.
let func_val = Rc::new(
self.visit_function_reference(*def_id, specialized_ty, Some(specialized_args))
self.visit_function_reference(*def_id, fun_ty, Some(args))
.clone()
.into(),
);
Expand Down Expand Up @@ -3910,10 +3905,9 @@ impl<'block, 'analysis, 'compilation, 'tcx> BlockVisitor<'block, 'analysis, 'com
TyKind::Uint(..) => self.bv.cv.constant_value_cache.get_u128_for(data),
TyKind::RawPtr(..) => self.bv.cv.constant_value_cache.get_u128_for(data),
TyKind::Closure(def_id, args) => {
let specialized_ty = self.type_visitor().specialize_generic_argument_type(
ty,
&self.type_visitor().generic_argument_map,
);
let specialized_ty = self
.type_visitor()
.specialize_type(ty, &self.type_visitor().generic_argument_map);
let specialized_args = self
.type_visitor()
.specialize_generic_args(args, &self.type_visitor().generic_argument_map);
Expand All @@ -3938,10 +3932,11 @@ impl<'block, 'analysis, 'compilation, 'tcx> BlockVisitor<'block, 'analysis, 'com
}
TyKind::FnDef(def_id, args) => {
debug_assert!(size == 0 && data == 0);
let specialized_ty = self.type_visitor().specialize_generic_argument_type(
ty,
&self.type_visitor().generic_argument_map,
);
// guard against the possibility that ty is not specialized with respect the actual
// type arguments of the current function.
let specialized_ty = self
.type_visitor()
.specialize_type(ty, &self.type_visitor().generic_argument_map);
let specialized_args = self
.type_visitor()
.specialize_generic_args(args, &self.type_visitor().generic_argument_map);
Expand Down Expand Up @@ -3973,7 +3968,7 @@ impl<'block, 'analysis, 'compilation, 'tcx> BlockVisitor<'block, 'analysis, 'com
)
}

/// The anonymous type of a function declaration/definition. Each
/// The anonymous type, of a function declaration/definition. Each
/// function has a unique type, which is output (for a function
/// named `foo` returning an `i32`) as `fn() -> i32 {foo}`.
///
Expand All @@ -3990,8 +3985,8 @@ impl<'block, 'analysis, 'compilation, 'tcx> BlockVisitor<'block, 'analysis, 'com
ty: Ty<'tcx>,
generic_args: Option<GenericArgsRef<'tcx>>,
) -> &ConstantDomain {
//todo: is def_id unique enough? Perhaps add ty?
if let Some(generic_args) = generic_args {
//todo: is def_id unique enough? Perhaps add fn_ty?
self.bv.cv.generic_args_cache.insert(def_id, generic_args);
}
self.bv.cv.constant_value_cache.get_function_constant_for(
Expand Down Expand Up @@ -4116,23 +4111,22 @@ impl<'block, 'analysis, 'compilation, 'tcx> BlockVisitor<'block, 'analysis, 'com
rustc_middle::ty::Opaque,
rustc_middle::ty::AliasTy { def_id, .. },
) => {
let aliased_ty = self.bv.tcx.type_of(*def_id).skip_binder();
// since ty is specialized, aliased_ty should be specialized as well.
if let TyKind::Closure(def_id, generic_args)
| TyKind::Coroutine(def_id, generic_args) =
self.bv.tcx.type_of(*def_id).skip_binder().kind()
| TyKind::Coroutine(def_id, generic_args) = aliased_ty.kind()
{
let fun_ty = self.bv.tcx.type_of(*def_id).skip_binder();
// since aliased_ty is specialized, fun_ty and generic_args should be specialized as well.
let func_const =
self.visit_function_reference(*def_id, ty, Some(generic_args));
self.visit_function_reference(*def_id, fun_ty, Some(generic_args));
let func_val = Rc::new(func_const.clone().into());
self.bv
.update_value_at(Path::new_function(base_path.clone()), func_val);
}
}
TyKind::FnDef(def_id, generic_args) => {
let func_const = self.visit_function_reference(
*def_id,
ty,
Some(generic_args.as_closure().args),
);
let func_const = self.visit_function_reference(*def_id, ty, Some(generic_args));
let func_val = Rc::new(func_const.clone().into());
self.bv
.update_value_at(Path::new_function(base_path.clone()), func_val);
Expand Down Expand Up @@ -4178,10 +4172,9 @@ impl<'block, 'analysis, 'compilation, 'tcx> BlockVisitor<'block, 'analysis, 'com
continue;
}
mir::ProjectionElem::Field(_, field_ty) => {
ty = self.type_visitor().specialize_generic_argument_type(
*field_ty,
&self.type_visitor().generic_argument_map,
);
ty = self
.type_visitor()
.specialize_type(*field_ty, &self.type_visitor().generic_argument_map);
if let TyKind::Adt(def, ..) = ty.kind() {
let ty_name = self.bv.cv.known_names_cache.get(tcx, def.did());
if ty_name == KnownNames::StdMarkerPhantomData {
Expand Down
Loading

0 comments on commit fc3dd65

Please sign in to comment.