Skip to content

Commit 35a20de

Browse files
committed
Implement SmirInterface
- With `Context` wrapped by `SmirInterface`, the stable-mir's TLV stores a pointer to `SmirInterface`, while the rustc-specific TLV stores a pointer to tables. - This PR make the `rustc_smir` mod public.
1 parent 18a029c commit 35a20de

File tree

5 files changed

+495
-207
lines changed

5 files changed

+495
-207
lines changed

Diff for: compiler/rustc_smir/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
pub mod rustc_internal;
2222

23-
// Make this module private for now since external users should not call these directly.
24-
mod rustc_smir;
23+
pub mod rustc_smir;
2524

2625
pub mod stable_mir;

Diff for: compiler/rustc_smir/src/rustc_internal/mod.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ use rustc_span::def_id::{CrateNum, DefId};
1818
use scoped_tls::scoped_thread_local;
1919
use stable_mir::Error;
2020
use stable_mir::abi::Layout;
21+
use stable_mir::compiler_interface::SmirInterface;
2122
use stable_mir::ty::IndexedVal;
2223

23-
use crate::rustc_smir::context::TablesWrapper;
24+
use crate::rustc_smir::context::Context;
2425
use crate::rustc_smir::{Stable, Tables};
2526
use crate::stable_mir;
2627

@@ -196,12 +197,12 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
196197
// datastructures and stable MIR datastructures
197198
scoped_thread_local! (static TLV: Cell<*const ()>);
198199

199-
pub(crate) fn init<'tcx, F, T>(tables: &TablesWrapper<'tcx>, f: F) -> T
200+
pub(crate) fn init<'tcx, F, T>(cx: &Context<'tcx>, f: F) -> T
200201
where
201202
F: FnOnce() -> T,
202203
{
203204
assert!(!TLV.is_set());
204-
let ptr = tables as *const _ as *const ();
205+
let ptr = cx as *const _ as *const ();
205206
TLV.set(&Cell::new(ptr), || f())
206207
}
207208

@@ -212,8 +213,8 @@ pub(crate) fn with_tables<R>(f: impl for<'tcx> FnOnce(&mut Tables<'tcx>) -> R) -
212213
TLV.with(|tlv| {
213214
let ptr = tlv.get();
214215
assert!(!ptr.is_null());
215-
let wrapper = ptr as *const TablesWrapper<'_>;
216-
let mut tables = unsafe { (*wrapper).0.borrow_mut() };
216+
let context = ptr as *const Context<'_>;
217+
let mut tables = unsafe { (*context).0.borrow_mut() };
217218
f(&mut *tables)
218219
})
219220
}
@@ -222,7 +223,7 @@ pub fn run<F, T>(tcx: TyCtxt<'_>, f: F) -> Result<T, Error>
222223
where
223224
F: FnOnce() -> T,
224225
{
225-
let tables = TablesWrapper(RefCell::new(Tables {
226+
let tables = Context(RefCell::new(Tables {
226227
tcx,
227228
def_ids: IndexMap::default(),
228229
alloc_ids: IndexMap::default(),
@@ -233,7 +234,12 @@ where
233234
mir_consts: IndexMap::default(),
234235
layouts: IndexMap::default(),
235236
}));
236-
stable_mir::compiler_interface::run(&tables, || init(&tables, f))
237+
238+
let interface = SmirInterface { cx: tables };
239+
240+
// Pass the `SmirInterface` to compiler_interface::run
241+
// and initialize the rustc-specific TLS with tables.
242+
stable_mir::compiler_interface::run(&interface, || init(&interface.cx, f))
237243
}
238244

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

0 commit comments

Comments
 (0)