Skip to content

Commit e21504a

Browse files
committed
refactor: implement the new SmirContainer struct
1 parent 8afa3fb commit e21504a

File tree

3 files changed

+275
-47
lines changed

3 files changed

+275
-47
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ use stable_mir::compiler_interface::SmirInterface;
2222
use stable_mir::ty::IndexedVal;
2323

2424
use crate::rustc_smir::context::SmirCtxt;
25-
use crate::rustc_smir::{Stable, Tables};
25+
use crate::rustc_smir::{Bridge, IndexedVal, SmirContainer, Tables};
2626
use crate::stable_mir;
2727

28-
mod internal;
2928
pub mod pretty;
3029

3130
/// Convert an internal Rust compiler item into its stable counterpart, if one exists.
@@ -40,7 +39,7 @@ pub mod pretty;
4039
///
4140
/// This function will panic if StableMIR has not been properly initialized.
4241
pub fn stable<'tcx, S: Stable<'tcx>>(item: S) -> S::T {
43-
with_tables(|tables| item.stable(tables))
42+
with_container(|tables, cx| item.stable(tables, cx))
4443
}
4544

4645
/// Convert a stable item into its internal Rust compiler counterpart, if one exists.
@@ -54,12 +53,11 @@ pub fn stable<'tcx, S: Stable<'tcx>>(item: S) -> S::T {
5453
/// # Panics
5554
///
5655
/// This function will panic if StableMIR has not been properly initialized.
57-
pub fn internal<'tcx, S>(tcx: TyCtxt<'tcx>, item: S) -> S::T<'tcx>
56+
pub fn internal<'tcx, S>(item: S) -> S::T<'tcx>
5857
where
5958
S: RustcInternal,
6059
{
61-
// The tcx argument ensures that the item won't outlive the type context.
62-
with_tables(|tables| item.internal(tables, tcx))
60+
with_container(|tables, cx| item.internal(tables, cx))
6361
}
6462

6563
impl<'tcx, B: Bridge> Index<B::DefId> for Tables<'tcx, B> {
@@ -101,49 +99,39 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
10199
// datastructures and stable MIR datastructures
102100
scoped_thread_local! (static TLV: Cell<*const ()>);
103101

104-
pub(crate) fn init<'tcx, F, T>(cx: &SmirCtxt<'tcx>, f: F) -> T
102+
pub(crate) fn init<'tcx, F, T, B: Bridge>(container: &SmirContainer<'tcx, B>, f: F) -> T
105103
where
106104
F: FnOnce() -> T,
107105
{
108106
assert!(!TLV.is_set());
109-
let ptr = cx as *const _ as *const ();
107+
let ptr = container as *const _ as *const ();
110108
TLV.set(&Cell::new(ptr), || f())
111109
}
112110

113111
/// Loads the current context and calls a function with it.
114112
/// Do not nest these, as that will ICE.
115-
pub(crate) fn with_tables<R>(f: impl for<'tcx> FnOnce(&mut Tables<'tcx>) -> R) -> R {
113+
pub(crate) fn with_container<'tcx, R, B: Bridge>(
114+
f: impl FnOnce(&mut Tables<'tcx, B>, &SmirCtxt<'tcx, B>) -> R,
115+
) -> R {
116116
assert!(TLV.is_set());
117117
TLV.with(|tlv| {
118118
let ptr = tlv.get();
119119
assert!(!ptr.is_null());
120-
let context = ptr as *const SmirCtxt<'_>;
121-
let mut tables = unsafe { (*context).0.borrow_mut() };
122-
f(&mut *tables)
120+
let container = ptr as *const SmirContainer<'tcx, B>;
121+
let mut tables = unsafe { (*container).tables.borrow_mut() };
122+
let cx = unsafe { (*container).cx.borrow() };
123+
f(&mut *tables, &*cx)
123124
})
124125
}
125126

126127
pub fn run<F, T>(tcx: TyCtxt<'_>, f: F) -> Result<T, Error>
127128
where
128129
F: FnOnce() -> T,
129130
{
130-
let tables = SmirCtxt(RefCell::new(Tables {
131-
tcx,
132-
def_ids: IndexMap::default(),
133-
alloc_ids: IndexMap::default(),
134-
spans: IndexMap::default(),
135-
types: IndexMap::default(),
136-
instances: IndexMap::default(),
137-
ty_consts: IndexMap::default(),
138-
mir_consts: IndexMap::default(),
139-
layouts: IndexMap::default(),
140-
}));
141-
142-
let interface = SmirInterface { cx: tables };
143-
144-
// Pass the `SmirInterface` to compiler_interface::run
145-
// and initialize the rustc-specific TLS with tables.
146-
stable_mir::compiler_interface::run(&interface, || init(&interface.cx, f))
131+
let smir_cx = RefCell::new(SmirCtxt::new(tcx));
132+
let container = SmirContainer { tables: RefCell::new(Tables::new(tcx)), cx: smir_cx };
133+
134+
stable_mir::compiler_interface::run(&container, || init(&container, f))
147135
}
148136

149137
/// Instantiate and run the compiler with the provided arguments and callback.

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,17 @@
77
//!
88
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
10-
use std::ops::RangeInclusive;
10+
use std::cell::RefCell;
11+
use std::fmt::Debug;
1112

13+
use context::SmirCtxt;
1214
use rustc_hir::def::DefKind;
1315
use rustc_middle::mir;
1416
use rustc_middle::mir::interpret::AllocId;
1517
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
1618
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
17-
use stable_mir::abi::Layout;
18-
use stable_mir::mir::mono::{InstanceDef, StaticDef};
19-
use stable_mir::ty::{FnDef, MirConstId, Span, TyConstId};
20-
use stable_mir::{CtorKind, ItemKind};
21-
use tracing::debug;
2219

2320
use crate::rustc_internal::IndexMap;
24-
use crate::stable_mir;
2521

2622
mod alloc;
2723
mod builder;

0 commit comments

Comments
 (0)