add stable provenance

This commit is contained in:
Eric Mark Martin 2023-09-02 01:53:06 -04:00
parent f9ba43ce14
commit cb7d020fb6
5 changed files with 28 additions and 7 deletions

View File

@ -14,6 +14,7 @@ use crate::{
};
use rustc_driver::{Callbacks, Compilation, RunCompiler};
use rustc_interface::{interface, Queries};
use rustc_middle::mir::interpret::AllocId;
use rustc_middle::ty::TyCtxt;
use rustc_session::EarlyErrorHandler;
pub use rustc_span::def_id::{CrateNum, DefId};
@ -134,6 +135,10 @@ impl<'tcx> Tables<'tcx> {
stable_mir::ty::ImplDef(self.create_def_id(did))
}
pub fn prov(&mut self, aid: AllocId) -> stable_mir::ty::Prov {
stable_mir::ty::Prov(self.create_alloc_id(aid))
}
fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId {
// FIXME: this becomes inefficient when we have too many ids
for (i, &d) in self.def_ids.iter().enumerate() {
@ -145,6 +150,16 @@ impl<'tcx> Tables<'tcx> {
self.def_ids.push(did);
stable_mir::DefId(id)
}
fn create_alloc_id(&mut self, aid: AllocId) -> stable_mir::AllocId {
// FIXME: this becomes inefficient when we have too many ids
if let Some(i) = self.alloc_ids.iter().position(|a| *a == aid) {
return stable_mir::AllocId(i);
};
let id = self.def_ids.len();
self.alloc_ids.push(aid);
stable_mir::AllocId(id)
}
}
pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
@ -152,7 +167,7 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
}
pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
crate::stable_mir::run(Tables { tcx, def_ids: vec![], types: vec![] }, f);
crate::stable_mir::run(Tables { tcx, def_ids: vec![], alloc_ids: vec![], types: vec![] }, f);
}
/// A type that provides internal information but that can still be used for debug purpose.

View File

@ -1,7 +1,6 @@
use rustc_middle::mir::interpret::{alloc_range, AllocRange, ConstValue, Pointer};
use crate::{
rustc_internal::opaque,
rustc_smir::{Stable, Tables},
stable_mir::mir::Mutability,
stable_mir::ty::{Allocation, ProvenanceMap},
@ -113,7 +112,7 @@ pub(super) fn allocation_filter<'tcx>(
.iter()
.filter(|a| a.0 >= alloc_range.start && a.0 <= alloc_range.end())
{
ptrs.push((offset.bytes_usize() - alloc_range.start.bytes_usize(), opaque(prov)));
ptrs.push((offset.bytes_usize() - alloc_range.start.bytes_usize(), tables.prov(*prov)));
}
Allocation {
bytes: bytes,

View File

@ -1,5 +1,5 @@
//! Module that implements what will become the rustc side of Stable MIR.
//!
//! This module is responsible for building Stable MIR components from internal components.
//!
//! This module is not intended to be invoked directly by users. It will eventually
@ -12,7 +12,7 @@ use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx}
use crate::stable_mir::ty::{FloatTy, GenericParamDef, IntTy, Movability, RigidTy, TyKind, UintTy};
use crate::stable_mir::{self, Context};
use rustc_hir as hir;
use rustc_middle::mir::interpret::alloc_range;
use rustc_middle::mir::interpret::{alloc_range, AllocId};
use rustc_middle::mir::{self, ConstantKind};
use rustc_middle::ty::{self, Ty, TyCtxt, Variance};
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
@ -125,6 +125,7 @@ impl<'tcx> Context for Tables<'tcx> {
pub struct Tables<'tcx> {
pub tcx: TyCtxt<'tcx>,
pub def_ids: Vec<DefId>,
pub alloc_ids: Vec<AllocId>,
pub types: Vec<Ty<'tcx>>,
}

View File

@ -31,6 +31,10 @@ pub type CrateNum = usize;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct DefId(pub(crate) usize);
/// A unique identification number for each provenance
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct AllocId(pub(crate) usize);
/// A list of crate items.
pub type CrateItems = Vec<CrateItem>;

View File

@ -1,4 +1,4 @@
use super::{mir::Mutability, mir::Safety, with, DefId};
use super::{mir::Mutability, mir::Safety, with, AllocId, DefId};
use crate::rustc_internal::Opaque;
#[derive(Copy, Clone, Debug)]
@ -260,7 +260,9 @@ pub struct BoundTy {
pub type Bytes = Vec<Option<u8>>;
pub type Size = usize;
pub type Prov = Opaque;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Prov(pub(crate) AllocId);
pub type Align = u64;
pub type Promoted = u32;
pub type InitMaskMaterialized = Vec<u64>;