Skip to content

Commit dc01c8c

Browse files
committed
Add support to global allocation to stable-mir
1 parent 7bd385d commit dc01c8c

File tree

8 files changed

+150
-24
lines changed

8 files changed

+150
-24
lines changed

compiler/rustc_smir/src/rustc_internal/internal.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
use crate::rustc_smir::Tables;
88
use rustc_middle::ty::{self as rustc_ty, Ty as InternalTy};
99
use rustc_span::Symbol;
10+
use stable_mir::mir::alloc::AllocId;
1011
use stable_mir::mir::mono::{Instance, MonoItem, StaticDef};
1112
use stable_mir::ty::{
1213
AdtDef, Binder, BoundRegionKind, BoundTyKind, BoundVariableKind, ClosureKind, Const, FloatTy,
1314
GenericArgKind, GenericArgs, IntTy, Region, RigidTy, TraitRef, Ty, UintTy,
1415
};
15-
use stable_mir::{AllocId, CrateItem, DefId};
16+
use stable_mir::{CrateItem, DefId};
1617

1718
use super::RustcInternal;
1819

compiler/rustc_smir/src/rustc_internal/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'tcx> Tables<'tcx> {
118118
self.def_ids.create_or_fetch(did)
119119
}
120120

121-
fn create_alloc_id(&mut self, aid: AllocId) -> stable_mir::AllocId {
121+
fn create_alloc_id(&mut self, aid: AllocId) -> stable_mir::mir::alloc::AllocId {
122122
self.alloc_ids.create_or_fetch(aid)
123123
}
124124

compiler/rustc_smir/src/rustc_smir/mod.rs

+68-5
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ use rustc_middle::mir::mono::MonoItem;
1717
use rustc_middle::ty::{self, Instance, ParamEnv, ScalarInt, Ty, TyCtxt, Variance};
1818
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
1919
use rustc_target::abi::FieldIdx;
20-
use stable_mir::mir::mono::InstanceDef;
20+
use stable_mir::mir::alloc::GlobalAlloc;
21+
use stable_mir::mir::mono::{InstanceDef, StaticDef};
2122
use stable_mir::mir::{Body, CopyNonOverlapping, Statement, UserTypeProjection, VariantIdx};
2223
use stable_mir::ty::{
23-
AdtDef, AdtKind, ClosureDef, ClosureKind, Const, ConstId, ConstantKind, EarlyParamRegion,
24-
FloatTy, FnDef, GenericArgs, GenericParamDef, IntTy, LineInfo, Movability, RigidTy, Span,
25-
TyKind, UintTy,
24+
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, ConstId, ConstantKind,
25+
EarlyParamRegion, FloatTy, FnDef, GenericArgs, GenericParamDef, IntTy, LineInfo, Movability,
26+
RigidTy, Span, TyKind, UintTy,
2627
};
2728
use stable_mir::{self, opaque, Context, CrateItem, Error, Filename, ItemKind};
2829
use std::cell::RefCell;
@@ -317,6 +318,18 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
317318
.ok_or_else(|| Error::new(format!("Const `{cnst:?}` cannot be encoded as u64")))
318319
}
319320

321+
fn eval_static_initializer(&self, def: StaticDef) -> Result<Allocation, Error> {
322+
let mut tables = self.0.borrow_mut();
323+
let def_id = def.0.internal(&mut *tables);
324+
tables.tcx.eval_static_initializer(def_id).stable(&mut *tables)
325+
}
326+
327+
fn global_alloc(&self, alloc: stable_mir::mir::alloc::AllocId) -> GlobalAlloc {
328+
let mut tables = self.0.borrow_mut();
329+
let alloc_id = alloc.internal(&mut *tables);
330+
tables.tcx.global_alloc(alloc_id).stable(&mut *tables)
331+
}
332+
320333
fn usize_to_const(&self, val: u64) -> Result<Const, Error> {
321334
let mut tables = self.0.borrow_mut();
322335
let ty = tables.tcx.types.usize;
@@ -341,7 +354,7 @@ pub(crate) struct TablesWrapper<'tcx>(pub(crate) RefCell<Tables<'tcx>>);
341354
pub struct Tables<'tcx> {
342355
pub(crate) tcx: TyCtxt<'tcx>,
343356
pub(crate) def_ids: IndexMap<DefId, stable_mir::DefId>,
344-
pub(crate) alloc_ids: IndexMap<AllocId, stable_mir::AllocId>,
357+
pub(crate) alloc_ids: IndexMap<AllocId, stable_mir::mir::alloc::AllocId>,
345358
pub(crate) spans: IndexMap<rustc_span::Span, Span>,
346359
pub(crate) types: IndexMap<Ty<'tcx>, stable_mir::ty::Ty>,
347360
pub(crate) instances: IndexMap<ty::Instance<'tcx>, InstanceDef>,
@@ -1539,6 +1552,14 @@ impl<'tcx> Stable<'tcx> for ty::BoundTy {
15391552
}
15401553
}
15411554

1555+
impl<'tcx> Stable<'tcx> for mir::interpret::ConstAllocation<'tcx> {
1556+
type T = Allocation;
1557+
1558+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1559+
self.inner().stable(tables)
1560+
}
1561+
}
1562+
15421563
impl<'tcx> Stable<'tcx> for mir::interpret::Allocation {
15431564
type T = stable_mir::ty::Allocation;
15441565

@@ -1551,6 +1572,25 @@ impl<'tcx> Stable<'tcx> for mir::interpret::Allocation {
15511572
}
15521573
}
15531574

1575+
impl<'tcx> Stable<'tcx> for mir::interpret::GlobalAlloc<'tcx> {
1576+
type T = GlobalAlloc;
1577+
1578+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1579+
match self {
1580+
mir::interpret::GlobalAlloc::Function(instance) => {
1581+
GlobalAlloc::Function(instance.stable(tables))
1582+
}
1583+
mir::interpret::GlobalAlloc::VTable(ty, trait_ref) => {
1584+
GlobalAlloc::VTable(ty.stable(tables), trait_ref.stable(tables))
1585+
}
1586+
mir::interpret::GlobalAlloc::Static(def) => {
1587+
GlobalAlloc::Static(tables.static_def(*def))
1588+
}
1589+
mir::interpret::GlobalAlloc::Memory(alloc) => GlobalAlloc::Memory(alloc.stable(tables)),
1590+
}
1591+
}
1592+
}
1593+
15541594
impl<'tcx> Stable<'tcx> for ty::trait_def::TraitSpecializationKind {
15551595
type T = stable_mir::ty::TraitSpecializationKind;
15561596
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
@@ -1938,6 +1978,14 @@ impl<'tcx> Stable<'tcx> for MonoItem<'tcx> {
19381978
}
19391979
}
19401980

1981+
impl<'tcx> Stable<'tcx> for mir::interpret::ErrorHandled {
1982+
type T = Error;
1983+
1984+
fn stable(&self, _tables: &mut Tables<'tcx>) -> Self::T {
1985+
Error::new(format!("{self:?}"))
1986+
}
1987+
}
1988+
19411989
impl<'tcx, T> Stable<'tcx> for &T
19421990
where
19431991
T: Stable<'tcx>,
@@ -1959,3 +2007,18 @@ where
19592007
self.as_ref().map(|value| value.stable(tables))
19602008
}
19612009
}
2010+
2011+
impl<'tcx, T, E> Stable<'tcx> for Result<T, E>
2012+
where
2013+
T: Stable<'tcx>,
2014+
E: Stable<'tcx>,
2015+
{
2016+
type T = Result<T::T, E::T>;
2017+
2018+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
2019+
match self {
2020+
Ok(val) => Ok(val.stable(tables)),
2021+
Err(error) => Err(error.stable(tables)),
2022+
}
2023+
}
2024+
}

compiler/stable_mir/src/lib.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! The goal is to eventually be published on
1818
//! [crates.io](https://crates.io).
1919
20-
use crate::mir::mono::InstanceDef;
20+
use crate::mir::mono::{InstanceDef, StaticDef};
2121
use crate::mir::Body;
2222
use std::fmt;
2323
use std::fmt::Debug;
@@ -37,9 +37,10 @@ pub mod mir;
3737
pub mod ty;
3838
pub mod visitor;
3939

40+
use crate::mir::alloc::{AllocId, GlobalAlloc};
4041
use crate::mir::pretty::function_name;
4142
use crate::mir::Mutability;
42-
use crate::ty::{AdtDef, AdtKind, ClosureDef, ClosureKind, Const, RigidTy};
43+
use crate::ty::{AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, RigidTy};
4344
pub use error::*;
4445
use mir::mono::Instance;
4546
use ty::{FnDef, GenericArgs};
@@ -73,19 +74,6 @@ impl IndexedVal for DefId {
7374
}
7475
}
7576

76-
/// A unique identification number for each provenance
77-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
78-
pub struct AllocId(usize);
79-
80-
impl IndexedVal for AllocId {
81-
fn to_val(index: usize) -> Self {
82-
AllocId(index)
83-
}
84-
fn to_index(&self) -> usize {
85-
self.0
86-
}
87-
}
88-
8977
/// A list of crate items.
9078
pub type CrateItems = Vec<CrateItem>;
9179

@@ -141,6 +129,10 @@ impl CrateItem {
141129
with(|cx| cx.def_ty(self.0))
142130
}
143131

132+
pub fn is_foreign_item(&self) -> bool {
133+
with(|cx| cx.is_foreign_item(*self))
134+
}
135+
144136
pub fn dump<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
145137
writeln!(w, "{}", function_name(*self))?;
146138
self.body().dump(w)
@@ -190,6 +182,8 @@ pub fn trait_impl(trait_impl: &ImplDef) -> ImplTrait {
190182
with(|cx| cx.trait_impl(trait_impl))
191183
}
192184

185+
/// This trait defines the interface between stable_mir and the Rust compiler.
186+
/// Do not use this directly.
193187
pub trait Context {
194188
fn entry_fn(&self) -> Option<CrateItem>;
195189
/// Retrieve all items of the local crate that have a MIR associated with them.
@@ -291,6 +285,12 @@ pub trait Context {
291285
args: &GenericArgs,
292286
kind: ClosureKind,
293287
) -> Option<Instance>;
288+
289+
/// Evaluate a static's initializer.
290+
fn eval_static_initializer(&self, def: StaticDef) -> Result<Allocation, Error>;
291+
292+
/// Retrieve global allocation for the given allocation ID.
293+
fn global_alloc(&self, id: AllocId) -> GlobalAlloc;
294294
}
295295

296296
// A thread local variable that stores a pointer to the tables mapping between TyCtxt

compiler/stable_mir/src/mir.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod alloc;
12
mod body;
23
pub mod mono;
34
pub mod pretty;

compiler/stable_mir/src/mir/alloc.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use crate::mir::mono::{Instance, StaticDef};
2+
use crate::ty::{Allocation, Binder, ExistentialTraitRef, IndexedVal, Ty};
3+
use crate::with;
4+
5+
/// An allocation in the SMIR global memory can be either a function pointer,
6+
/// a static, or a "real" allocation with some data in it.
7+
#[derive(Debug, Clone, Eq, PartialEq)]
8+
pub enum GlobalAlloc {
9+
/// The alloc ID is used as a function pointer.
10+
Function(Instance),
11+
/// This alloc ID points to a symbolic (not-reified) vtable.
12+
VTable(Ty, Option<Binder<ExistentialTraitRef>>),
13+
/// The alloc ID points to a "lazy" static variable that did not get computed (yet).
14+
/// This is also used to break the cycle in recursive statics.
15+
Static(StaticDef),
16+
/// The alloc ID points to memory.
17+
Memory(Allocation),
18+
}
19+
20+
impl From<AllocId> for GlobalAlloc {
21+
fn from(value: AllocId) -> Self {
22+
with(|cx| cx.global_alloc(value))
23+
}
24+
}
25+
26+
/// A unique identification number for each provenance
27+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
28+
pub struct AllocId(usize);
29+
30+
impl IndexedVal for AllocId {
31+
fn to_val(index: usize) -> Self {
32+
AllocId(index)
33+
}
34+
fn to_index(&self) -> usize {
35+
self.0
36+
}
37+
}

compiler/stable_mir/src/mir/mono.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::mir::Body;
2-
use crate::ty::{ClosureDef, ClosureKind, FnDef, GenericArgs, IndexedVal, Ty};
2+
use crate::ty::{Allocation, ClosureDef, ClosureKind, FnDef, GenericArgs, IndexedVal, Ty};
33
use crate::{with, CrateItem, DefId, Error, ItemKind, Opaque};
44
use std::fmt::{Debug, Formatter};
55

@@ -37,6 +37,11 @@ impl Instance {
3737
with(|context| context.instance_body(self.def))
3838
}
3939

40+
pub fn is_foreign_item(&self) -> bool {
41+
let item = CrateItem::try_from(*self);
42+
item.as_ref().map_or(false, CrateItem::is_foreign_item)
43+
}
44+
4045
/// Get the instance type with generic substitutions applied and lifetimes erased.
4146
pub fn ty(&self) -> Ty {
4247
with(|context| context.instance_ty(self.def))
@@ -128,6 +133,18 @@ impl From<Instance> for MonoItem {
128133
}
129134
}
130135

136+
impl From<StaticDef> for MonoItem {
137+
fn from(value: StaticDef) -> Self {
138+
MonoItem::Static(value)
139+
}
140+
}
141+
142+
impl From<StaticDef> for CrateItem {
143+
fn from(value: StaticDef) -> Self {
144+
CrateItem(value.0)
145+
}
146+
}
147+
131148
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
132149
pub struct InstanceDef(usize);
133150

@@ -147,9 +164,15 @@ impl TryFrom<CrateItem> for StaticDef {
147164
}
148165

149166
impl StaticDef {
167+
/// Return the type of this static definition.
150168
pub fn ty(&self) -> Ty {
151169
with(|cx| cx.def_ty(self.0))
152170
}
171+
172+
/// Evaluate a static's initializer, returning the allocation of the initializer's memory.
173+
pub fn eval_initializer(&self) -> Result<Allocation, Error> {
174+
with(|cx| cx.eval_static_initializer(*self))
175+
}
153176
}
154177

155178
impl IndexedVal for InstanceDef {

compiler/stable_mir/src/ty.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use super::{
22
mir::Safety,
33
mir::{Body, Mutability},
4-
with, AllocId, DefId, Error, Symbol,
4+
with, DefId, Error, Symbol,
55
};
6+
use crate::mir::alloc::AllocId;
67
use crate::{Filename, Opaque};
78
use std::fmt::{self, Debug, Display, Formatter};
89

0 commit comments

Comments
 (0)