From cb7d020fb65c090cd086cbdabf1f3e0b8a721bca Mon Sep 17 00:00:00 2001 From: Eric Mark Martin Date: Sat, 2 Sep 2023 01:53:06 -0400 Subject: [PATCH] add stable provenance --- compiler/rustc_smir/src/rustc_internal/mod.rs | 17 ++++++++++++++++- compiler/rustc_smir/src/rustc_smir/alloc.rs | 3 +-- compiler/rustc_smir/src/rustc_smir/mod.rs | 5 +++-- compiler/rustc_smir/src/stable_mir/mod.rs | 4 ++++ compiler/rustc_smir/src/stable_mir/ty.rs | 6 ++++-- 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_internal/mod.rs b/compiler/rustc_smir/src/rustc_internal/mod.rs index 5334a75dc0649..34db35e40ad4c 100644 --- a/compiler/rustc_smir/src/rustc_internal/mod.rs +++ b/compiler/rustc_smir/src/rustc_internal/mod.rs @@ -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. diff --git a/compiler/rustc_smir/src/rustc_smir/alloc.rs b/compiler/rustc_smir/src/rustc_smir/alloc.rs index 33c75250adcd6..166c8bda9e10a 100644 --- a/compiler/rustc_smir/src/rustc_smir/alloc.rs +++ b/compiler/rustc_smir/src/rustc_smir/alloc.rs @@ -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, diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index d36835d37f54b..822a6e4865862 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -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, + pub alloc_ids: Vec, pub types: Vec>, } diff --git a/compiler/rustc_smir/src/stable_mir/mod.rs b/compiler/rustc_smir/src/stable_mir/mod.rs index 1b8346281757d..8344dc954d2fb 100644 --- a/compiler/rustc_smir/src/stable_mir/mod.rs +++ b/compiler/rustc_smir/src/stable_mir/mod.rs @@ -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; diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index d49f3243777ff..1db6b1e3d28eb 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -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>; 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;