Skip to content

Commit 92cfd20

Browse files
committed
Move some logic using rustc datastructures to the rustc_smir module
1 parent 03b03f9 commit 92cfd20

File tree

3 files changed

+135
-131
lines changed

3 files changed

+135
-131
lines changed

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

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
use rustc_middle::mir::interpret::{alloc_range, AllocRange, ConstValue, Pointer};
2+
3+
use crate::{
4+
rustc_internal::opaque,
5+
rustc_smir::{Stable, Tables},
6+
stable_mir::mir::Mutability,
7+
stable_mir::ty::{Allocation, ProvenanceMap},
8+
};
9+
10+
/// Creates new empty `Allocation` from given `Align`.
11+
fn new_empty_allocation(align: rustc_target::abi::Align) -> Allocation {
12+
Allocation {
13+
bytes: Vec::new(),
14+
provenance: ProvenanceMap { ptrs: Vec::new() },
15+
align: align.bytes(),
16+
mutability: Mutability::Not,
17+
}
18+
}
19+
20+
// We need this method instead of a Stable implementation
21+
// because we need to get `Ty` of the const we are trying to create, to do that
22+
// we need to have access to `ConstantKind` but we can't access that inside Stable impl.
23+
#[allow(rustc::usage_of_qualified_ty)]
24+
pub fn new_allocation<'tcx>(
25+
ty: rustc_middle::ty::Ty<'tcx>,
26+
const_value: ConstValue<'tcx>,
27+
tables: &mut Tables<'tcx>,
28+
) -> Allocation {
29+
match const_value {
30+
ConstValue::Scalar(scalar) => {
31+
let size = scalar.size();
32+
let align = tables
33+
.tcx
34+
.layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(ty))
35+
.unwrap()
36+
.align;
37+
let mut allocation = rustc_middle::mir::interpret::Allocation::uninit(size, align.abi);
38+
allocation
39+
.write_scalar(&tables.tcx, alloc_range(rustc_target::abi::Size::ZERO, size), scalar)
40+
.unwrap();
41+
allocation.stable(tables)
42+
}
43+
ConstValue::ZeroSized => {
44+
let align =
45+
tables.tcx.layout_of(rustc_middle::ty::ParamEnv::empty().and(ty)).unwrap().align;
46+
new_empty_allocation(align.abi)
47+
}
48+
ConstValue::Slice { data, start, end } => {
49+
let alloc_id = tables.tcx.create_memory_alloc(data);
50+
let ptr = Pointer::new(alloc_id, rustc_target::abi::Size::from_bytes(start));
51+
let scalar_ptr = rustc_middle::mir::interpret::Scalar::from_pointer(ptr, &tables.tcx);
52+
let scalar_len = rustc_middle::mir::interpret::Scalar::from_target_usize(
53+
(end - start) as u64,
54+
&tables.tcx,
55+
);
56+
let layout =
57+
tables.tcx.layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(ty)).unwrap();
58+
let mut allocation =
59+
rustc_middle::mir::interpret::Allocation::uninit(layout.size, layout.align.abi);
60+
allocation
61+
.write_scalar(
62+
&tables.tcx,
63+
alloc_range(rustc_target::abi::Size::ZERO, tables.tcx.data_layout.pointer_size),
64+
scalar_ptr,
65+
)
66+
.unwrap();
67+
allocation
68+
.write_scalar(
69+
&tables.tcx,
70+
alloc_range(tables.tcx.data_layout.pointer_size, scalar_len.size()),
71+
scalar_len,
72+
)
73+
.unwrap();
74+
allocation.stable(tables)
75+
}
76+
ConstValue::ByRef { alloc, offset } => {
77+
let ty_size = tables
78+
.tcx
79+
.layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(ty))
80+
.unwrap()
81+
.size;
82+
allocation_filter(&alloc.0, alloc_range(offset, ty_size), tables)
83+
}
84+
}
85+
}
86+
87+
/// Creates an `Allocation` only from information within the `AllocRange`.
88+
pub(super) fn allocation_filter<'tcx>(
89+
alloc: &rustc_middle::mir::interpret::Allocation,
90+
alloc_range: AllocRange,
91+
tables: &mut Tables<'tcx>,
92+
) -> Allocation {
93+
let mut bytes: Vec<Option<u8>> = alloc
94+
.inspect_with_uninit_and_ptr_outside_interpreter(
95+
alloc_range.start.bytes_usize()..alloc_range.end().bytes_usize(),
96+
)
97+
.iter()
98+
.copied()
99+
.map(Some)
100+
.collect();
101+
for (i, b) in bytes.iter_mut().enumerate() {
102+
if !alloc
103+
.init_mask()
104+
.get(rustc_target::abi::Size::from_bytes(i + alloc_range.start.bytes_usize()))
105+
{
106+
*b = None;
107+
}
108+
}
109+
let mut ptrs = Vec::new();
110+
for (offset, prov) in alloc
111+
.provenance()
112+
.ptrs()
113+
.iter()
114+
.filter(|a| a.0 >= alloc_range.start && a.0 <= alloc_range.end())
115+
{
116+
ptrs.push((offset.bytes_usize() - alloc_range.start.bytes_usize(), opaque(prov)));
117+
}
118+
Allocation {
119+
bytes: bytes,
120+
provenance: ProvenanceMap { ptrs },
121+
align: alloc.align.bytes(),
122+
mutability: alloc.mutability.stable(tables),
123+
}
124+
}

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

+10-7
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
1010
use crate::rustc_internal::{self, opaque};
1111
use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx};
12-
use crate::stable_mir::ty::{
13-
allocation_filter, new_allocation, FloatTy, GenericParamDef, IntTy, Movability, RigidTy,
14-
TyKind, UintTy,
15-
};
12+
use crate::stable_mir::ty::{FloatTy, GenericParamDef, IntTy, Movability, RigidTy, TyKind, UintTy};
1613
use crate::stable_mir::{self, Context};
1714
use rustc_hir as hir;
1815
use rustc_middle::mir::interpret::alloc_range;
@@ -22,6 +19,8 @@ use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
2219
use rustc_target::abi::FieldIdx;
2320
use tracing::debug;
2421

22+
mod alloc;
23+
2524
impl<'tcx> Context for Tables<'tcx> {
2625
fn local_crate(&self) -> stable_mir::Crate {
2726
smir_crate(self.tcx, LOCAL_CRATE)
@@ -1085,7 +1084,7 @@ impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {
10851084
literal: match self.kind() {
10861085
ty::Value(val) => {
10871086
let const_val = tables.tcx.valtree_to_const_val((self.ty(), val));
1088-
stable_mir::ty::ConstantKind::Allocated(new_allocation(
1087+
stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation(
10891088
self.ty(),
10901089
const_val,
10911090
tables,
@@ -1130,7 +1129,11 @@ impl<'tcx> Stable<'tcx> for mir::interpret::Allocation {
11301129
type T = stable_mir::ty::Allocation;
11311130

11321131
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1133-
allocation_filter(self, alloc_range(rustc_target::abi::Size::ZERO, self.size()), tables)
1132+
alloc::allocation_filter(
1133+
self,
1134+
alloc_range(rustc_target::abi::Size::ZERO, self.size()),
1135+
tables,
1136+
)
11341137
}
11351138
}
11361139

@@ -1188,7 +1191,7 @@ impl<'tcx> Stable<'tcx> for rustc_middle::mir::ConstantKind<'tcx> {
11881191
})
11891192
}
11901193
ConstantKind::Val(val, ty) => {
1191-
stable_mir::ty::ConstantKind::Allocated(new_allocation(ty, val, tables))
1194+
stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation(ty, val, tables))
11921195
}
11931196
}
11941197
}

Diff for: compiler/rustc_smir/src/stable_mir/ty.rs

+1-124
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
use rustc_middle::mir::interpret::{alloc_range, AllocRange, ConstValue, Pointer};
2-
31
use super::{mir::Mutability, mir::Safety, with, DefId};
4-
use crate::{
5-
rustc_internal::{opaque, Opaque},
6-
rustc_smir::{Stable, Tables},
7-
};
2+
use crate::rustc_internal::Opaque;
83

94
#[derive(Copy, Clone, Debug)]
105
pub struct Ty(pub usize);
@@ -286,124 +281,6 @@ pub struct Allocation {
286281
pub mutability: Mutability,
287282
}
288283

289-
impl Allocation {
290-
/// Creates new empty `Allocation` from given `Align`.
291-
fn new_empty_allocation(align: rustc_target::abi::Align) -> Allocation {
292-
Allocation {
293-
bytes: Vec::new(),
294-
provenance: ProvenanceMap { ptrs: Vec::new() },
295-
align: align.bytes(),
296-
mutability: Mutability::Not,
297-
}
298-
}
299-
}
300-
301-
// We need this method instead of a Stable implementation
302-
// because we need to get `Ty` of the const we are trying to create, to do that
303-
// we need to have access to `ConstantKind` but we can't access that inside Stable impl.
304-
#[allow(rustc::usage_of_qualified_ty)]
305-
pub fn new_allocation<'tcx>(
306-
ty: rustc_middle::ty::Ty<'tcx>,
307-
const_value: ConstValue<'tcx>,
308-
tables: &mut Tables<'tcx>,
309-
) -> Allocation {
310-
match const_value {
311-
ConstValue::Scalar(scalar) => {
312-
let size = scalar.size();
313-
let align = tables
314-
.tcx
315-
.layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(ty))
316-
.unwrap()
317-
.align;
318-
let mut allocation = rustc_middle::mir::interpret::Allocation::uninit(size, align.abi);
319-
allocation
320-
.write_scalar(&tables.tcx, alloc_range(rustc_target::abi::Size::ZERO, size), scalar)
321-
.unwrap();
322-
allocation.stable(tables)
323-
}
324-
ConstValue::ZeroSized => {
325-
let align =
326-
tables.tcx.layout_of(rustc_middle::ty::ParamEnv::empty().and(ty)).unwrap().align;
327-
Allocation::new_empty_allocation(align.abi)
328-
}
329-
ConstValue::Slice { data, start, end } => {
330-
let alloc_id = tables.tcx.create_memory_alloc(data);
331-
let ptr = Pointer::new(alloc_id, rustc_target::abi::Size::from_bytes(start));
332-
let scalar_ptr = rustc_middle::mir::interpret::Scalar::from_pointer(ptr, &tables.tcx);
333-
let scalar_len = rustc_middle::mir::interpret::Scalar::from_target_usize(
334-
(end - start) as u64,
335-
&tables.tcx,
336-
);
337-
let layout =
338-
tables.tcx.layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(ty)).unwrap();
339-
let mut allocation =
340-
rustc_middle::mir::interpret::Allocation::uninit(layout.size, layout.align.abi);
341-
allocation
342-
.write_scalar(
343-
&tables.tcx,
344-
alloc_range(rustc_target::abi::Size::ZERO, tables.tcx.data_layout.pointer_size),
345-
scalar_ptr,
346-
)
347-
.unwrap();
348-
allocation
349-
.write_scalar(
350-
&tables.tcx,
351-
alloc_range(tables.tcx.data_layout.pointer_size, scalar_len.size()),
352-
scalar_len,
353-
)
354-
.unwrap();
355-
allocation.stable(tables)
356-
}
357-
ConstValue::ByRef { alloc, offset } => {
358-
let ty_size = tables
359-
.tcx
360-
.layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(ty))
361-
.unwrap()
362-
.size;
363-
allocation_filter(&alloc.0, alloc_range(offset, ty_size), tables)
364-
}
365-
}
366-
}
367-
368-
/// Creates an `Allocation` only from information within the `AllocRange`.
369-
pub fn allocation_filter<'tcx>(
370-
alloc: &rustc_middle::mir::interpret::Allocation,
371-
alloc_range: AllocRange,
372-
tables: &mut Tables<'tcx>,
373-
) -> Allocation {
374-
let mut bytes: Vec<Option<u8>> = alloc
375-
.inspect_with_uninit_and_ptr_outside_interpreter(
376-
alloc_range.start.bytes_usize()..alloc_range.end().bytes_usize(),
377-
)
378-
.iter()
379-
.copied()
380-
.map(Some)
381-
.collect();
382-
for (i, b) in bytes.iter_mut().enumerate() {
383-
if !alloc
384-
.init_mask()
385-
.get(rustc_target::abi::Size::from_bytes(i + alloc_range.start.bytes_usize()))
386-
{
387-
*b = None;
388-
}
389-
}
390-
let mut ptrs = Vec::new();
391-
for (offset, prov) in alloc
392-
.provenance()
393-
.ptrs()
394-
.iter()
395-
.filter(|a| a.0 >= alloc_range.start && a.0 <= alloc_range.end())
396-
{
397-
ptrs.push((offset.bytes_usize() - alloc_range.start.bytes_usize(), opaque(prov)));
398-
}
399-
Allocation {
400-
bytes: bytes,
401-
provenance: ProvenanceMap { ptrs },
402-
align: alloc.align.bytes(),
403-
mutability: alloc.mutability.stable(tables),
404-
}
405-
}
406-
407284
#[derive(Clone, Debug)]
408285
pub enum ConstantKind {
409286
Allocated(Allocation),

0 commit comments

Comments
 (0)