Skip to content

Commit 9e8b1bf

Browse files
committed
refactor: remove the tcx field in Tables
the only functionality of `Tables` is caching results. this commit moves calls to rustc queries from `Tables` to `SmirCtxt`.
1 parent 6d81378 commit 9e8b1bf

File tree

8 files changed

+100
-120
lines changed

8 files changed

+100
-120
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ where
127127
F: FnOnce() -> T,
128128
{
129129
let smir_cx = RefCell::new(SmirCtxt::new(tcx));
130-
let container = SmirContainer { tables: RefCell::new(Tables::new(tcx)), cx: smir_cx };
130+
let container = SmirContainer { tables: RefCell::new(Tables::default()), cx: smir_cx };
131131

132132
stable_mir::compiler_interface::run(&container, || init(&container, f))
133133
}

compiler/rustc_smir/src/rustc_smir/alloc.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,28 @@ use rustc_abi::{Size, TyAndLayout};
88
use rustc_middle::mir::interpret::{
99
AllocId, AllocInit, AllocRange, Allocation, ConstAllocation, Pointer, Scalar, alloc_range,
1010
};
11-
use rustc_middle::ty::Ty;
11+
use rustc_middle::ty::{Ty, layout};
1212

13-
use crate::rustc_smir::{Bridge, SmirError, Tables};
13+
use super::SmirCtxt;
14+
use crate::rustc_smir::{Bridge, SmirError};
15+
16+
pub fn create_ty_and_layout<'tcx, B: Bridge>(
17+
cx: &SmirCtxt<'tcx, B>,
18+
ty: Ty<'tcx>,
19+
) -> Result<TyAndLayout<'tcx, Ty<'tcx>>, &'tcx layout::LayoutError<'tcx>> {
20+
use crate::rustc_smir::context::SmirTypingEnv;
21+
cx.tcx.layout_of(cx.fully_monomorphized().as_query_input(ty))
22+
}
1423

1524
pub fn try_new_scalar<'tcx, B: Bridge>(
1625
layout: TyAndLayout<'tcx, Ty<'tcx>>,
1726
scalar: Scalar,
18-
tables: &mut Tables<'tcx, B>,
27+
cx: &SmirCtxt<'tcx, B>,
1928
) -> Result<Allocation, B::Error> {
2029
let size = scalar.size();
2130
let mut allocation = Allocation::new(size, layout.align.abi, AllocInit::Uninit, ());
2231
allocation
23-
.write_scalar(&tables.tcx, alloc_range(Size::ZERO, size), scalar)
32+
.write_scalar(&cx.tcx, alloc_range(Size::ZERO, size), scalar)
2433
.map_err(|e| B::Error::from_internal(e))?;
2534

2635
Ok(allocation)
@@ -30,24 +39,20 @@ pub fn try_new_slice<'tcx, B: Bridge>(
3039
layout: TyAndLayout<'tcx, Ty<'tcx>>,
3140
data: ConstAllocation<'tcx>,
3241
meta: u64,
33-
tables: &mut Tables<'tcx, B>,
42+
cx: &SmirCtxt<'tcx, B>,
3443
) -> Result<Allocation, B::Error> {
35-
let alloc_id = tables.tcx.reserve_and_set_memory_alloc(data);
44+
let alloc_id = cx.tcx.reserve_and_set_memory_alloc(data);
3645
let ptr = Pointer::new(alloc_id.into(), Size::ZERO);
3746
let scalar_ptr = Scalar::from_pointer(ptr, &tables.tcx);
3847
let scalar_meta: Scalar = Scalar::from_target_usize(meta, &tables.tcx);
3948
let mut allocation = Allocation::new(layout.size, layout.align.abi, AllocInit::Uninit, ());
4049
allocation
41-
.write_scalar(
42-
&tables.tcx,
43-
alloc_range(Size::ZERO, tables.tcx.data_layout.pointer_size),
44-
scalar_ptr,
45-
)
50+
.write_scalar(&cx.tcx, alloc_range(Size::ZERO, cx.tcx.data_layout.pointer_size), scalar_ptr)
4651
.map_err(|e| B::Error::from_internal(e))?;
4752
allocation
4853
.write_scalar(
49-
&tables.tcx,
50-
alloc_range(tables.tcx.data_layout.pointer_size, scalar_meta.size()),
54+
&cx.tcx,
55+
alloc_range(cx.tcx.data_layout.pointer_size, scalar_meta.size()),
5156
scalar_meta,
5257
)
5358
.map_err(|e| B::Error::from_internal(e))?;
@@ -57,9 +62,9 @@ pub fn try_new_slice<'tcx, B: Bridge>(
5762

5863
pub fn try_new_indirect<'tcx, B: Bridge>(
5964
alloc_id: AllocId,
60-
tables: &mut Tables<'tcx, B>,
65+
cx: &SmirCtxt<'tcx, B>,
6166
) -> ConstAllocation<'tcx> {
62-
let alloc = tables.tcx.global_alloc(alloc_id).unwrap_memory();
67+
let alloc = cx.tcx.global_alloc(alloc_id).unwrap_memory();
6368

6469
alloc
6570
}

compiler/rustc_smir/src/rustc_smir/builder.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ use rustc_middle::mir;
99
use rustc_middle::mir::visit::MutVisitor;
1010
use rustc_middle::ty::{self, TyCtxt};
1111

12-
use crate::rustc_smir::{Bridge, Tables};
13-
1412
/// Builds a monomorphic body for a given instance.
1513
pub(crate) struct BodyBuilder<'tcx> {
1614
tcx: TyCtxt<'tcx>,
@@ -30,16 +28,16 @@ impl<'tcx> BodyBuilder<'tcx> {
3028
/// Build a stable monomorphic body for a given instance based on the MIR body.
3129
///
3230
/// All constants are also evaluated.
33-
pub(crate) fn build<B: Bridge>(mut self, tables: &mut Tables<'tcx, B>) -> mir::Body<'tcx> {
34-
let body = tables.tcx.instance_mir(self.instance.def).clone();
31+
pub(crate) fn build(mut self) -> mir::Body<'tcx> {
32+
let body = self.tcx.instance_mir(self.instance.def).clone();
3533
let mono_body = if !self.instance.args.is_empty()
3634
// Without the `generic_const_exprs` feature gate, anon consts in signatures do not
3735
// get generic parameters. Which is wrong, but also not a problem without
3836
// generic_const_exprs
3937
|| self.tcx.def_kind(self.instance.def_id()) != DefKind::AnonConst
4038
{
4139
let mut mono_body = self.instance.instantiate_mir_and_normalize_erasing_regions(
42-
tables.tcx,
40+
self.tcx,
4341
ty::TypingEnv::fully_monomorphized(),
4442
ty::EarlyBinder::bind(body),
4543
);

compiler/rustc_smir/src/rustc_smir/context/impls.rs

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,46 @@ impl<'tcx, B: Bridge> SmirCtxt<'tcx, B> {
143143
self.tcx.valtree_to_const_val(key)
144144
}
145145

146+
/// Return whether the instance as a body available.
147+
///
148+
/// Items and intrinsics may have a body available from its definition.
149+
/// Shims body may be generated depending on their type.
150+
pub(crate) fn instance_has_body(&self, instance: Instance<'tcx>) -> bool {
151+
let def_id = instance.def_id();
152+
self.item_has_body(def_id)
153+
|| !matches!(
154+
instance.def,
155+
ty::InstanceKind::Virtual(..)
156+
| ty::InstanceKind::Intrinsic(..)
157+
| ty::InstanceKind::Item(..)
158+
)
159+
}
160+
161+
/// Return whether the item has a body defined by the user.
162+
///
163+
/// Note that intrinsics may have a placeholder body that shouldn't be used in practice.
164+
/// In StableMIR, we handle this case as if the body is not available.
165+
pub(crate) fn item_has_body(&self, def_id: DefId) -> bool {
166+
let must_override = if let Some(intrinsic) = self.tcx.intrinsic(def_id) {
167+
intrinsic.must_be_overridden
168+
} else {
169+
false
170+
};
171+
!must_override && self.tcx.is_mir_available(def_id)
172+
}
173+
174+
fn filter_fn_def(&self, def_id: DefId) -> Option<DefId> {
175+
if matches!(self.tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
176+
Some(def_id)
177+
} else {
178+
None
179+
}
180+
}
181+
182+
fn filter_static_def(&self, def_id: DefId) -> Option<DefId> {
183+
matches!(self.tcx.def_kind(def_id), DefKind::Static { .. }).then(|| def_id)
184+
}
185+
146186
pub fn target_endian(&self) -> Endian {
147187
self.tcx.data_layout.endian
148188
}
@@ -167,22 +207,22 @@ impl<'tcx, B: Bridge> SmirCtxt<'tcx, B> {
167207
}
168208

169209
/// Check whether the body of a function is available.
170-
pub fn has_body(&self, def: DefId, tables: &mut Tables<'_, B>) -> bool {
171-
tables.item_has_body(def)
210+
pub fn has_body(&self, def: DefId) -> bool {
211+
self.item_has_body(def)
172212
}
173213

174214
pub fn foreign_modules(&self, crate_num: CrateNum) -> Vec<DefId> {
175215
self.tcx.foreign_modules(crate_num).keys().map(|mod_def_id| *mod_def_id).collect()
176216
}
177217

178218
/// Retrieve all functions defined in this crate.
179-
pub fn crate_functions(&self, crate_num: CrateNum, tables: &mut Tables<'_, B>) -> Vec<DefId> {
180-
filter_def_ids(self.tcx, crate_num, |def_id| tables.filter_fn_def(def_id))
219+
pub fn crate_functions(&self, crate_num: CrateNum) -> Vec<DefId> {
220+
filter_def_ids(self.tcx, crate_num, |def_id| self.filter_fn_def(def_id))
181221
}
182222

183223
/// Retrieve all static items defined in this crate.
184-
pub fn crate_statics(&self, crate_num: CrateNum, tables: &mut Tables<'_, B>) -> Vec<DefId> {
185-
filter_def_ids(self.tcx, crate_num, |def_id| tables.filter_static_def(def_id))
224+
pub fn crate_statics(&self, crate_num: CrateNum) -> Vec<DefId> {
225+
filter_def_ids(self.tcx, crate_num, |def_id| self.filter_static_def(def_id))
186226
}
187227

188228
pub fn foreign_module(&self, mod_def: DefId) -> &ForeignModule {
@@ -574,14 +614,8 @@ impl<'tcx, B: Bridge> SmirCtxt<'tcx, B> {
574614
}
575615

576616
/// Get the body of an Instance which is already monomorphized.
577-
pub fn instance_body(
578-
&self,
579-
instance: ty::Instance<'tcx>,
580-
tables: &mut Tables<'tcx, B>,
581-
) -> Option<Body<'tcx>> {
582-
tables
583-
.instance_has_body(instance)
584-
.then(|| BodyBuilder::new(self.tcx, instance).build(tables))
617+
pub fn instance_body(&self, instance: ty::Instance<'tcx>) -> Option<Body<'tcx>> {
618+
self.instance_has_body(instance).then(|| BodyBuilder::new(self.tcx, instance).build())
585619
}
586620

587621
/// Get the instance type with generic instantiations applied and lifetimes erased.
@@ -599,18 +633,13 @@ impl<'tcx, B: Bridge> SmirCtxt<'tcx, B> {
599633
pub fn instance_abi(
600634
&self,
601635
instance: ty::Instance<'tcx>,
602-
tables: &mut Tables<'tcx, B>,
603636
) -> Result<&FnAbi<'tcx, Ty<'tcx>>, B::Error> {
604-
Ok(tables.fn_abi_of_instance(instance, List::empty())?)
637+
Ok(self.fn_abi_of_instance(instance, List::empty())?)
605638
}
606639

607640
/// Get the ABI of a function pointer.
608-
pub fn fn_ptr_abi(
609-
&self,
610-
sig: PolyFnSig<'tcx>,
611-
tables: &mut Tables<'tcx, B>,
612-
) -> Result<&FnAbi<'tcx, Ty<'tcx>>, B::Error> {
613-
Ok(tables.fn_abi_of_fn_ptr(sig, List::empty())?)
641+
pub fn fn_ptr_abi(&self, sig: PolyFnSig<'tcx>) -> Result<&FnAbi<'tcx, Ty<'tcx>>, B::Error> {
642+
Ok(self.fn_abi_of_fn_ptr(sig, List::empty())?)
614643
}
615644

616645
/// Get the instance.
@@ -737,12 +766,8 @@ impl<'tcx, B: Bridge> SmirCtxt<'tcx, B> {
737766
}
738767

739768
/// Get the layout of a type.
740-
pub fn ty_layout(
741-
&self,
742-
ty: Ty<'tcx>,
743-
tables: &mut Tables<'tcx, B>,
744-
) -> Result<Layout<'tcx>, B::Error> {
745-
let layout = tables.layout_of(ty)?.layout;
769+
pub fn ty_layout(&self, ty: Ty<'tcx>) -> Result<Layout<'tcx>, B::Error> {
770+
let layout = self.layout_of(ty)?.layout;
746771
Ok(layout)
747772
}
748773

compiler/rustc_smir/src/rustc_smir/context/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::ty;
99
use rustc_middle::ty::layout::{FnAbiOfHelpers, HasTyCtxt, HasTypingEnv, LayoutOfHelpers};
1010
use rustc_middle::ty::{Ty, TyCtxt};
1111

12-
use crate::rustc_smir::{Bridge, SmirError, Tables};
12+
use crate::rustc_smir::{Bridge, SmirError};
1313

1414
mod impls;
1515
mod traits;
@@ -21,7 +21,7 @@ pub use traits::*;
2121
/// The [`crate::stable_mir::compiler_interface::SmirInterface`] must go through
2222
/// this context to obtain rustc-level information.
2323
pub struct SmirCtxt<'tcx, B: Bridge> {
24-
tcx: TyCtxt<'tcx>,
24+
pub(crate) tcx: TyCtxt<'tcx>,
2525
_marker: PhantomData<B>,
2626
}
2727

@@ -32,7 +32,7 @@ impl<'tcx, B: Bridge> SmirCtxt<'tcx, B> {
3232
}
3333

3434
/// Implement error handling for extracting function ABI information.
35-
impl<'tcx, B: Bridge> FnAbiOfHelpers<'tcx> for Tables<'tcx, B> {
35+
impl<'tcx, B: Bridge> FnAbiOfHelpers<'tcx> for SmirCtxt<'tcx, B> {
3636
type FnAbiOfResult = Result<&'tcx rustc_target::callconv::FnAbi<'tcx, Ty<'tcx>>, B::Error>;
3737

3838
#[inline]
@@ -46,7 +46,7 @@ impl<'tcx, B: Bridge> FnAbiOfHelpers<'tcx> for Tables<'tcx, B> {
4646
}
4747
}
4848

49-
impl<'tcx, B: Bridge> LayoutOfHelpers<'tcx> for Tables<'tcx, B> {
49+
impl<'tcx, B: Bridge> LayoutOfHelpers<'tcx> for SmirCtxt<'tcx, B> {
5050
type LayoutOfResult = Result<ty::layout::TyAndLayout<'tcx>, B::Error>;
5151

5252
#[inline]
@@ -60,19 +60,19 @@ impl<'tcx, B: Bridge> LayoutOfHelpers<'tcx> for Tables<'tcx, B> {
6060
}
6161
}
6262

63-
impl<'tcx, B: Bridge> HasTypingEnv<'tcx> for Tables<'tcx, B> {
63+
impl<'tcx, B: Bridge> HasTypingEnv<'tcx> for SmirCtxt<'tcx, B> {
6464
fn typing_env(&self) -> ty::TypingEnv<'tcx> {
6565
ty::TypingEnv::fully_monomorphized()
6666
}
6767
}
6868

69-
impl<'tcx, B: Bridge> HasTyCtxt<'tcx> for Tables<'tcx, B> {
69+
impl<'tcx, B: Bridge> HasTyCtxt<'tcx> for SmirCtxt<'tcx, B> {
7070
fn tcx(&self) -> TyCtxt<'tcx> {
7171
self.tcx
7272
}
7373
}
7474

75-
impl<'tcx, B: Bridge> HasDataLayout for Tables<'tcx, B> {
75+
impl<'tcx, B: Bridge> HasDataLayout for SmirCtxt<'tcx, B> {
7676
fn data_layout(&self) -> &rustc_abi::TargetDataLayout {
7777
self.tcx.data_layout()
7878
}

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ use std::cell::RefCell;
1111
use std::fmt::Debug;
1212

1313
use context::SmirCtxt;
14-
use rustc_hir::def::DefKind;
1514
use rustc_middle::mir;
1615
use rustc_middle::mir::interpret::AllocId;
17-
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
16+
use rustc_middle::ty::{self, Ty, TyCtxt};
1817
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
1918

2019
use crate::rustc_internal::IndexMap;
@@ -30,7 +29,6 @@ pub struct SmirContainer<'tcx, B: Bridge> {
3029
}
3130

3231
pub struct Tables<'tcx, B: Bridge> {
33-
tcx: TyCtxt<'tcx>,
3432
pub(crate) def_ids: IndexMap<DefId, B::DefId>,
3533
pub(crate) alloc_ids: IndexMap<AllocId, B::AllocId>,
3634
pub(crate) spans: IndexMap<rustc_span::Span, B::Span>,
@@ -41,10 +39,9 @@ pub struct Tables<'tcx, B: Bridge> {
4139
pub(crate) layouts: IndexMap<rustc_abi::Layout<'tcx>, B::Layout>,
4240
}
4341

44-
impl<'tcx, B: Bridge> Tables<'tcx, B> {
45-
pub(crate) fn new(tcx: TyCtxt<'tcx>) -> Self {
42+
impl<'tcx, B: Bridge> Default for Tables<'tcx, B> {
43+
fn default() -> Self {
4644
Self {
47-
tcx,
4845
def_ids: IndexMap::default(),
4946
alloc_ids: IndexMap::default(),
5047
spans: IndexMap::default(),
@@ -69,46 +66,6 @@ impl<'tcx, B: Bridge> Tables<'tcx, B> {
6966
pub(crate) fn intern_mir_const(&mut self, constant: mir::Const<'tcx>) -> B::MirConstId {
7067
self.mir_consts.create_or_fetch(constant)
7168
}
72-
73-
/// Return whether the instance as a body available.
74-
///
75-
/// Items and intrinsics may have a body available from its definition.
76-
/// Shims body may be generated depending on their type.
77-
pub(crate) fn instance_has_body(&self, instance: Instance<'tcx>) -> bool {
78-
let def_id = instance.def_id();
79-
self.item_has_body(def_id)
80-
|| !matches!(
81-
instance.def,
82-
ty::InstanceKind::Virtual(..)
83-
| ty::InstanceKind::Intrinsic(..)
84-
| ty::InstanceKind::Item(..)
85-
)
86-
}
87-
88-
/// Return whether the item has a body defined by the user.
89-
///
90-
/// Note that intrinsics may have a placeholder body that shouldn't be used in practice.
91-
/// In StableMIR, we handle this case as if the body is not available.
92-
pub(crate) fn item_has_body(&self, def_id: DefId) -> bool {
93-
let must_override = if let Some(intrinsic) = self.tcx.intrinsic(def_id) {
94-
intrinsic.must_be_overridden
95-
} else {
96-
false
97-
};
98-
!must_override && self.tcx.is_mir_available(def_id)
99-
}
100-
101-
fn filter_fn_def(&mut self, def_id: DefId) -> Option<DefId> {
102-
if matches!(self.tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
103-
Some(def_id)
104-
} else {
105-
None
106-
}
107-
}
108-
109-
fn filter_static_def(&mut self, def_id: DefId) -> Option<DefId> {
110-
matches!(self.tcx.def_kind(def_id), DefKind::Static { .. }).then(|| def_id)
111-
}
11269
}
11370

11471
/// A trait defining types that are used to emulate StableMIR components, which is really

0 commit comments

Comments
 (0)