Skip to content

Commit deb6928

Browse files
adpaco-awstedinski
authored andcommitted
Conform with recent changes in rustc (alloc id, scalar ptr, vtable method) (rust-lang#361)
* Refactor for alloc_id iterators * Access scalar pointer values via `into_parts` * Trait compile fixes * Vtable field name workaround * Remove `unused doc comment` warning
1 parent 9c7c058 commit deb6928

File tree

5 files changed

+42
-47
lines changed

5 files changed

+42
-47
lines changed

compiler/rustc_codegen_llvm/src/gotoc/metadata.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,12 @@ impl<'tcx> GotocCtx<'tcx> {
9696
/// functions can share the same name, we need to use the index of the entry in the
9797
/// vtable. This is the same index that will be passed in virtual function calls as
9898
/// InstanceDef::Virtual(def_id, idx). We could use solely the index as a key into
99-
/// the vtable struct, but we add the trait and function names for debugging
100-
/// readability.
101-
/// Example: 3_Shape::vol
102-
pub fn vtable_field_name(&self, def_id: DefId, idx: usize) -> String {
103-
format!("{}_{}", idx, with_no_trimmed_paths(|| self.tcx.def_path_str(def_id)))
99+
/// the vtable struct, but we add the method name for debugging readability.
100+
/// Example: 3_vol
101+
pub fn vtable_field_name(&self, _def_id: DefId, idx: usize) -> String {
102+
// format!("{}_{}", idx, with_no_trimmed_paths(|| self.tcx.item_name(def_id)))
103+
// TODO: use def_id https://github.com/model-checking/rmc/issues/364
104+
idx.to_string()
104105
}
105106

106107
/// A human readable name in Rust for reference, should not be used as a key.

compiler/rustc_codegen_llvm/src/gotoc/monomorphize/collector.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ fn collect_items_rec<'tcx>(
225225
recursion_depth_reset = None;
226226

227227
if let Ok(alloc) = tcx.eval_static_initializer(def_id) {
228-
for &((), id) in alloc.relocations().values() {
228+
for &id in alloc.relocations().values() {
229229
collect_miri(tcx, id, &mut neighbors);
230230
}
231231
}
@@ -815,16 +815,13 @@ fn create_mono_items_for_vtable_methods<'tcx>(
815815
.iter()
816816
.cloned()
817817
.filter_map(|entry| match entry {
818-
VtblEntry::Method(def_id, substs) => ty::Instance::resolve_for_vtable(
819-
tcx,
820-
ty::ParamEnv::reveal_all(),
821-
def_id,
822-
substs,
823-
),
818+
VtblEntry::Method(instance) => Some(instance),
824819
VtblEntry::MetadataDropInPlace
825820
| VtblEntry::MetadataSize
826821
| VtblEntry::MetadataAlign
827822
| VtblEntry::Vacant => None,
823+
// Super trait vtable entries already handled by now
824+
VtblEntry::TraitVPtr(..) => None,
828825
})
829826
.filter(|&instance| should_codegen_locally(tcx, &instance))
830827
.map(|item| create_fn_mono_item(item, source));
@@ -1039,7 +1036,7 @@ fn collect_miri<'tcx>(
10391036
}
10401037
GlobalAlloc::Memory(alloc) => {
10411038
trace!("collecting {:?} with {:#?}", alloc_id, alloc);
1042-
for &((), inner) in alloc.relocations().values() {
1039+
for &inner in alloc.relocations().values() {
10431040
rustc_data_structures::stack::ensure_sufficient_stack(|| {
10441041
collect_miri(tcx, inner, output);
10451042
});
@@ -1073,9 +1070,12 @@ fn collect_const_value<'tcx>(
10731070
output: &mut Vec<Spanned<MonoItem<'tcx>>>,
10741071
) {
10751072
match value {
1076-
ConstValue::Scalar(Scalar::Ptr(ptr)) => collect_miri(tcx, ptr.alloc_id, output),
1073+
ConstValue::Scalar(Scalar::Ptr(ptr, _size)) => {
1074+
let (alloc_id, _offset) = ptr.into_parts();
1075+
collect_miri(tcx, alloc_id, output);
1076+
}
10771077
ConstValue::Slice { data: alloc, start: _, end: _ } | ConstValue::ByRef { alloc, .. } => {
1078-
for &((), id) in alloc.relocations().values() {
1078+
for &id in alloc.relocations().values() {
10791079
collect_miri(tcx, id, output);
10801080
}
10811081
}

compiler/rustc_codegen_llvm/src/gotoc/operand.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,10 @@ impl<'tcx> GotocCtx<'tcx> {
265265
&self.symbol_table,
266266
)
267267
}
268-
(Scalar::Ptr(ptr), _) => {
268+
(Scalar::Ptr(ptr, _size), _) => {
269269
let res_t = self.codegen_ty(ty);
270-
self.codegen_alloc_pointer(res_t, ptr.alloc_id, ptr.offset, span)
270+
let (alloc_id, offset) = ptr.into_parts();
271+
self.codegen_alloc_pointer(res_t, alloc_id, offset, span)
271272
}
272273
_ => unimplemented!(),
273274
}
@@ -382,7 +383,7 @@ impl<'tcx> GotocCtx<'tcx> {
382383
let pointer_size = self.ptr_width() as usize / 8;
383384

384385
let mut next_offset = 0;
385-
for &(offset, ((), alloc_id)) in alloc.relocations().iter() {
386+
for &(offset, alloc_id) in alloc.relocations().iter() {
386387
let offset = offset.bytes_usize();
387388
if offset > next_offset {
388389
let bytes =

compiler/rustc_codegen_llvm/src/gotoc/rvalue.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@ use crate::btree_string_map;
1010
use num::bigint::BigInt;
1111
use rustc_middle::mir::{AggregateKind, BinOp, CastKind, NullOp, Operand, Place, Rvalue, UnOp};
1212
use rustc_middle::ty::adjustment::PointerCast;
13-
use rustc_middle::ty::subst::InternalSubsts;
14-
use rustc_middle::ty::{
15-
self, Binder, GenericParamDefKind, Instance, IntTy, TraitRef, Ty, UintTy, VtblEntry,
16-
COMMON_VTABLE_ENTRIES,
17-
};
18-
use rustc_span::def_id::DefId;
13+
use rustc_middle::ty::{self, Instance, IntTy, Ty, UintTy, VtblEntry, COMMON_VTABLE_ENTRIES};
1914
use rustc_target::abi::{FieldsShape, LayoutOf, Primitive, TagEncoding, Variants};
2015
use tracing::{debug, warn};
2116

@@ -702,26 +697,18 @@ impl<'tcx> GotocCtx<'tcx> {
702697

703698
fn codegen_vtable_method_field(
704699
&mut self,
705-
def_id: DefId,
706-
substs: ty::subst::SubstsRef<'tcx>,
700+
instance: Instance<'tcx>,
707701
t: Ty<'tcx>,
708702
idx: usize,
709703
) -> Expr {
710-
let vtable_field_name = self.vtable_field_name(def_id, idx);
704+
let vtable_field_name = self.vtable_field_name(instance.def_id(), idx);
711705
let vtable_type_name = aggr_name(&self.vtable_name(t));
712706
let field_type = self
713707
.symbol_table
714708
.lookup_field_type(&vtable_type_name, &vtable_field_name)
715709
.cloned()
716710
.unwrap();
717711

718-
// We use Instance::resolve to more closely match Rust proper behavior. The comment
719-
// there says "used to find the precise code that will run for a trait method invocation"
720-
// and it is used (in a more indirect way) to generate vtables.
721-
let instance = Instance::resolve(self.tcx, ty::ParamEnv::reveal_all(), def_id, substs)
722-
.unwrap()
723-
.unwrap();
724-
725712
// Lookup in the symbol table using the full symbol table name/key
726713
let fn_name = self.symbol_name(instance);
727714
if let Some(fn_symbol) = self.symbol_table.lookup(&fn_name) {
@@ -858,8 +845,11 @@ impl<'tcx> GotocCtx<'tcx> {
858845
VtblEntry::MetadataSize => Some(vt_size.clone()),
859846
VtblEntry::MetadataAlign => Some(vt_align.clone()),
860847
VtblEntry::Vacant => None,
861-
VtblEntry::Method(def_id, substs) => {
862-
Some(ctx.codegen_vtable_method_field(*def_id, substs, trait_type, idx))
848+
// TODO: trait upcasting
849+
// https://github.com/model-checking/rmc/issues/358
850+
VtblEntry::TraitVPtr(_trait_ref) => None,
851+
VtblEntry::Method(instance) => {
852+
Some(ctx.codegen_vtable_method_field(*instance, trait_type, idx))
863853
}
864854
})
865855
.collect();

compiler/rustc_codegen_llvm/src/gotoc/typ.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_index::vec::IndexVec;
99
use rustc_middle::mir::{HasLocalDecls, Local};
1010
use rustc_middle::ty::print::with_no_trimmed_paths;
1111
use rustc_middle::ty::print::FmtPrinter;
12-
use rustc_middle::ty::subst::{InternalSubsts, SubstsRef};
12+
use rustc_middle::ty::subst::InternalSubsts;
1313
use rustc_middle::ty::{
1414
self, AdtDef, FloatTy, Instance, IntTy, PolyFnSig, Ty, TyS, UintTy, VariantDef, VtblEntry,
1515
};
@@ -117,22 +117,17 @@ impl<'tcx> GotocCtx<'tcx> {
117117
/// In particular, these fields are function pointers.
118118
fn trait_method_vtable_field_type(
119119
&mut self,
120-
def_id: DefId,
121-
substs: SubstsRef<'tcx>,
120+
instance: Instance<'tcx>,
122121
idx: usize,
123122
) -> DatatypeComponent {
124-
let instance = Instance::resolve(self.tcx, ty::ParamEnv::reveal_all(), def_id, substs)
125-
.unwrap()
126-
.unwrap();
127-
128123
// gives a binder with function signature
129124
let sig = self.fn_sig_of_instance(instance);
130125

131126
// gives an Irep Pointer object for the signature
132127
let fnptr = self.codegen_dynamic_function_sig(sig).to_pointer();
133128

134-
// vtable field name, i.e., 3_Shape::vol (idx_Trait::method)
135-
let vtable_field_name = self.vtable_field_name(def_id, idx);
129+
// vtable field name, i.e., 3_vol (idx_method)
130+
let vtable_field_name = self.vtable_field_name(instance.def_id(), idx);
136131

137132
let ins_ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
138133
let _layout = self.layout_of(ins_ty);
@@ -198,9 +193,12 @@ impl<'tcx> GotocCtx<'tcx> {
198193
.cloned()
199194
.enumerate()
200195
.filter_map(|(idx, entry)| match entry {
201-
VtblEntry::Method(def_id, substs) => {
202-
Some(self.trait_method_vtable_field_type(def_id, substs, idx))
196+
VtblEntry::Method(instance) => {
197+
Some(self.trait_method_vtable_field_type(instance, idx))
203198
}
199+
// TODO: trait upcasting
200+
// https://github.com/model-checking/rmc/issues/358
201+
VtblEntry::TraitVPtr(..) => None,
204202
VtblEntry::MetadataDropInPlace
205203
| VtblEntry::MetadataSize
206204
| VtblEntry::MetadataAlign
@@ -225,6 +223,11 @@ impl<'tcx> GotocCtx<'tcx> {
225223

226224
/// Gives the vtable name for a type.
227225
/// In some cases, we have &T, in other cases T, so normalize.
226+
///
227+
/// TODO: to handle trait upcasting, this will need to use a
228+
/// poly existential trait type as a part of the key as well.
229+
/// See compiler/rustc_middle/src/ty/vtable.rs
230+
/// https://github.com/model-checking/rmc/issues/358
228231
pub fn vtable_name(&self, t: Ty<'tcx>) -> String {
229232
self.normalized_trait_name(t) + "::vtable"
230233
}

0 commit comments

Comments
 (0)