Skip to content

Commit

Permalink
add stable provenance
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmarkmartin committed Sep 2, 2023
1 parent f9ba43c commit cb7d020
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
17 changes: 16 additions & 1 deletion compiler/rustc_smir/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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() {
Expand All @@ -145,14 +150,24 @@ 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 {
item.id.into()
}

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.
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_smir/src/rustc_smir/alloc.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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};
Expand Down Expand Up @@ -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>>,
}

Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_smir/src/stable_mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_smir/src/stable_mir/ty.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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>;
Expand Down

0 comments on commit cb7d020

Please sign in to comment.