Skip to content

Commit a21cb79

Browse files
committed
implement a new bridge trait Allocation
1 parent 7436b15 commit a21cb79

File tree

6 files changed

+51
-14
lines changed

6 files changed

+51
-14
lines changed

compiler/rustc_smir/src/rustc_smir/alloc.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use rustc_middle::mir::interpret::{
1010
};
1111
use rustc_middle::ty::{Ty, layout};
1212

13-
use super::SmirCtxt;
13+
use super::{SmirCtxt, Tables};
14+
use crate::rustc_smir::bridge::Allocation as _;
1415
use crate::rustc_smir::{Bridge, SmirError};
1516

1617
pub fn create_ty_and_layout<'tcx, B: Bridge>(
@@ -70,10 +71,12 @@ pub fn try_new_indirect<'tcx, B: Bridge>(
7071
}
7172

7273
/// Creates an `Allocation` only from information within the `AllocRange`.
73-
pub fn allocation_filter(
74+
pub fn allocation_filter<'tcx, B: Bridge>(
7475
alloc: &rustc_middle::mir::interpret::Allocation,
7576
alloc_range: AllocRange,
76-
) -> (Vec<Option<u8>>, Vec<(usize, AllocId)>) {
77+
tables: &mut Tables<'tcx, B>,
78+
cx: &SmirCtxt<'tcx, B>,
79+
) -> B::Allocation {
7780
let mut bytes: Vec<Option<u8>> = alloc
7881
.inspect_with_uninit_and_ptr_outside_interpreter(
7982
alloc_range.start.bytes_usize()..alloc_range.end().bytes_usize(),
@@ -97,5 +100,5 @@ pub fn allocation_filter(
97100
ptrs.push((offset.bytes_usize() - alloc_range.start.bytes_usize(), prov.alloc_id()));
98101
}
99102

100-
(bytes, ptrs)
103+
B::Allocation::new(bytes, ptrs, alloc.align.bytes(), alloc.mutability, tables, cx)
101104
}

compiler/rustc_smir/src/rustc_smir/bridge.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
77
use std::fmt::Debug;
88

9-
use super::Bridge;
9+
use super::context::SmirCtxt;
10+
use super::{Bridge, Tables};
1011

1112
pub trait SmirError {
1213
fn new(msg: String) -> Self;
@@ -17,6 +18,17 @@ pub trait Prov<B: Bridge> {
1718
fn new(aid: B::AllocId) -> Self;
1819
}
1920

21+
pub trait Allocation<B: Bridge> {
22+
fn new<'tcx>(
23+
bytes: Vec<Option<u8>>,
24+
ptrs: Vec<(usize, rustc_middle::mir::interpret::AllocId)>,
25+
align: u64,
26+
mutability: rustc_middle::mir::Mutability,
27+
tables: &mut Tables<'tcx, B>,
28+
cx: &SmirCtxt<'tcx, B>,
29+
) -> Self;
30+
}
31+
2032
macro_rules! make_bridge_trait {
2133
($name:ident) => {
2234
pub trait $name<B: Bridge> {

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ pub trait Bridge: Sized {
225225
type OpaqueDef: OpaqueDef<Self>;
226226
type Prov: Prov<Self>;
227227
type StaticDef: StaticDef<Self>;
228+
229+
type Allocation: Allocation<Self>;
228230
}
229231

230232
pub trait IndexedVal {

compiler/rustc_smir/src/stable_mir/alloc.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,5 @@ pub(super) fn allocation_filter<'tcx>(
7373
tables: &mut Tables<'tcx, BridgeTys>,
7474
cx: &SmirCtxt<'tcx, BridgeTys>,
7575
) -> Allocation {
76-
let (bytes, ptrs) = alloc::allocation_filter(alloc, alloc_range);
77-
let ptrs = ptrs.iter().map(|(i, aid)| (*i, tables.prov(*aid))).collect();
78-
Allocation {
79-
bytes,
80-
provenance: ProvenanceMap { ptrs },
81-
align: alloc.align.bytes(),
82-
mutability: alloc.mutability.stable(tables, cx),
83-
}
76+
alloc::allocation_filter(alloc, alloc_range, tables, cx)
8477
}

compiler/rustc_smir/src/stable_mir/compiler_interface.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ impl Bridge for BridgeTys {
6363
type OpaqueDef = stable_mir::ty::OpaqueDef;
6464
type Prov = stable_mir::ty::Prov;
6565
type StaticDef = stable_mir::mir::mono::StaticDef;
66+
67+
type Allocation = stable_mir::ty::Allocation;
6668
}
6769

6870
/// Stable public API for querying compiler information.

compiler/rustc_smir/src/stable_mir/mod.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@ use std::fmt::Debug;
2828
use std::{fmt, io};
2929

3030
pub(crate) use rustc_smir::IndexedVal;
31+
use rustc_smir::Tables;
32+
use rustc_smir::context::SmirCtxt;
3133
use serde::Serialize;
3234
use stable_mir::compiler_interface::with;
3335
pub use stable_mir::crate_def::{CrateDef, CrateDefItems, CrateDefType, DefId};
3436
pub use stable_mir::error::*;
3537
use stable_mir::mir::mono::StaticDef;
3638
use stable_mir::mir::{Body, Mutability};
37-
use stable_mir::ty::{AssocItem, FnDef, ForeignModuleDef, ImplDef, Span, TraitDef, Ty};
39+
use stable_mir::ty::{
40+
AssocItem, FnDef, ForeignModuleDef, ImplDef, ProvenanceMap, Span, TraitDef, Ty,
41+
};
42+
use stable_mir::unstable::Stable;
3843

3944
use crate::{rustc_smir, stable_mir};
4045

@@ -277,3 +282,23 @@ impl rustc_smir::bridge::Prov<compiler_interface::BridgeTys> for stable_mir::ty:
277282
Self(aid)
278283
}
279284
}
285+
286+
impl rustc_smir::bridge::Allocation<compiler_interface::BridgeTys> for stable_mir::ty::Allocation {
287+
fn new<'tcx>(
288+
bytes: Vec<Option<u8>>,
289+
ptrs: Vec<(usize, rustc_middle::mir::interpret::AllocId)>,
290+
align: u64,
291+
mutability: rustc_middle::mir::Mutability,
292+
tables: &mut Tables<'tcx, compiler_interface::BridgeTys>,
293+
cx: &SmirCtxt<'tcx, compiler_interface::BridgeTys>,
294+
) -> Self {
295+
Self {
296+
bytes,
297+
provenance: ProvenanceMap {
298+
ptrs: ptrs.iter().map(|(i, aid)| (*i, tables.prov(*aid))).collect(),
299+
},
300+
align,
301+
mutability: mutability.stable(tables, cx),
302+
}
303+
}
304+
}

0 commit comments

Comments
 (0)