1
- use rustc_abi:: { Align , Size } ;
2
- use rustc_middle:: mir:: ConstValue ;
3
- use rustc_middle:: mir:: interpret:: { AllocInit , AllocRange , Pointer , alloc_range} ;
4
- use stable_mir:: Error ;
5
- use stable_mir:: mir:: Mutability ;
6
- use stable_mir:: ty:: { Allocation , ProvenanceMap } ;
1
+ //! Internal memory allocator implementation for StableMIR.
2
+ //!
3
+ //! This module handles all direct interactions with rustc queries and performs
4
+ //! the actual memory allocations. The stable interface in `stable_mir::alloc`
5
+ //! delegates all query-related operations to this implementation.
7
6
8
- use crate :: rustc_smir:: { Stable , Tables } ;
9
- use crate :: stable_mir;
7
+ use rustc_abi:: { Size , TyAndLayout } ;
8
+ use rustc_middle:: mir:: interpret:: {
9
+ AllocId , AllocInit , AllocRange , Allocation , ConstAllocation , Pointer , Scalar , alloc_range,
10
+ } ;
11
+ use rustc_middle:: ty:: Ty ;
10
12
11
- /// Creates new empty `Allocation` from given `Align`.
12
- fn new_empty_allocation ( align : Align ) -> Allocation {
13
- Allocation {
14
- bytes : Vec :: new ( ) ,
15
- provenance : ProvenanceMap { ptrs : Vec :: new ( ) } ,
16
- align : align. bytes ( ) ,
17
- mutability : Mutability :: Not ,
18
- }
13
+ use crate :: rustc_smir:: { Bridge , SmirError , Tables } ;
14
+
15
+ pub fn try_new_scalar < ' tcx , B : Bridge > (
16
+ layout : TyAndLayout < ' tcx , Ty < ' tcx > > ,
17
+ scalar : Scalar ,
18
+ tables : & mut Tables < ' tcx , B > ,
19
+ ) -> Result < Allocation , B :: Error > {
20
+ let size = scalar. size ( ) ;
21
+ let mut allocation = Allocation :: new ( size, layout. align . abi , AllocInit :: Uninit , ( ) ) ;
22
+ allocation
23
+ . write_scalar ( & tables. tcx , alloc_range ( Size :: ZERO , size) , scalar)
24
+ . map_err ( |e| B :: Error :: from_internal ( e) ) ?;
25
+
26
+ Ok ( allocation)
19
27
}
20
28
21
- // We need this method instead of a Stable implementation
22
- // because we need to get `Ty` of the const we are trying to create, to do that
23
- // we need to have access to `ConstantKind` but we can't access that inside Stable impl.
24
- #[ allow( rustc:: usage_of_qualified_ty) ]
25
- pub ( crate ) fn new_allocation < ' tcx > (
26
- ty : rustc_middle:: ty:: Ty < ' tcx > ,
27
- const_value : ConstValue < ' tcx > ,
28
- tables : & mut Tables < ' tcx > ,
29
- ) -> Allocation {
30
- try_new_allocation ( ty, const_value, tables)
31
- . unwrap_or_else ( |_| panic ! ( "Failed to convert: {const_value:?} to {ty:?}" ) )
29
+ pub fn try_new_slice < ' tcx , B : Bridge > (
30
+ layout : TyAndLayout < ' tcx , Ty < ' tcx > > ,
31
+ data : ConstAllocation < ' tcx > ,
32
+ meta : u64 ,
33
+ tables : & mut Tables < ' tcx , B > ,
34
+ ) -> Result < Allocation , B :: Error > {
35
+ let alloc_id = tables. tcx . reserve_and_set_memory_alloc ( data) ;
36
+ let ptr = Pointer :: new ( alloc_id. into ( ) , Size :: ZERO ) ;
37
+ let scalar_ptr = Scalar :: from_pointer ( ptr, & tables. tcx ) ;
38
+ let scalar_meta: Scalar = Scalar :: from_target_usize ( meta, & tables. tcx ) ;
39
+ let mut allocation = Allocation :: new ( layout. size , layout. align . abi , AllocInit :: Uninit , ( ) ) ;
40
+ allocation
41
+ . write_scalar (
42
+ & tables. tcx ,
43
+ alloc_range ( Size :: ZERO , tables. tcx . data_layout . pointer_size ) ,
44
+ scalar_ptr,
45
+ )
46
+ . map_err ( |e| B :: Error :: from_internal ( e) ) ?;
47
+ allocation
48
+ . write_scalar (
49
+ & tables. tcx ,
50
+ alloc_range ( tables. tcx . data_layout . pointer_size , scalar_meta. size ( ) ) ,
51
+ scalar_meta,
52
+ )
53
+ . map_err ( |e| B :: Error :: from_internal ( e) ) ?;
54
+
55
+ Ok ( allocation)
32
56
}
33
57
34
- #[ allow( rustc:: usage_of_qualified_ty) ]
35
- pub ( crate ) fn try_new_allocation < ' tcx > (
36
- ty : rustc_middle:: ty:: Ty < ' tcx > ,
37
- const_value : ConstValue < ' tcx > ,
38
- tables : & mut Tables < ' tcx > ,
39
- ) -> Result < Allocation , Error > {
40
- let layout = tables
41
- . tcx
42
- . layout_of ( rustc_middle:: ty:: TypingEnv :: fully_monomorphized ( ) . as_query_input ( ty) )
43
- . map_err ( |e| e. stable ( tables) ) ?;
44
- Ok ( match const_value {
45
- ConstValue :: Scalar ( scalar) => {
46
- let size = scalar. size ( ) ;
47
- let mut allocation = rustc_middle:: mir:: interpret:: Allocation :: new (
48
- size,
49
- layout. align . abi ,
50
- AllocInit :: Uninit ,
51
- ( ) ,
52
- ) ;
53
- allocation
54
- . write_scalar ( & tables. tcx , alloc_range ( Size :: ZERO , size) , scalar)
55
- . map_err ( |e| e. stable ( tables) ) ?;
56
- allocation. stable ( tables)
57
- }
58
- ConstValue :: ZeroSized => new_empty_allocation ( layout. align . abi ) ,
59
- ConstValue :: Slice { data, meta } => {
60
- let alloc_id = tables. tcx . reserve_and_set_memory_alloc ( data) ;
61
- let ptr = Pointer :: new ( alloc_id. into ( ) , Size :: ZERO ) ;
62
- let scalar_ptr = rustc_middle:: mir:: interpret:: Scalar :: from_pointer ( ptr, & tables. tcx ) ;
63
- let scalar_meta =
64
- rustc_middle:: mir:: interpret:: Scalar :: from_target_usize ( meta, & tables. tcx ) ;
65
- let mut allocation = rustc_middle:: mir:: interpret:: Allocation :: new (
66
- layout. size ,
67
- layout. align . abi ,
68
- AllocInit :: Uninit ,
69
- ( ) ,
70
- ) ;
71
- allocation
72
- . write_scalar (
73
- & tables. tcx ,
74
- alloc_range ( Size :: ZERO , tables. tcx . data_layout . pointer_size ) ,
75
- scalar_ptr,
76
- )
77
- . map_err ( |e| e. stable ( tables) ) ?;
78
- allocation
79
- . write_scalar (
80
- & tables. tcx ,
81
- alloc_range ( tables. tcx . data_layout . pointer_size , scalar_meta. size ( ) ) ,
82
- scalar_meta,
83
- )
84
- . map_err ( |e| e. stable ( tables) ) ?;
85
- allocation. stable ( tables)
86
- }
87
- ConstValue :: Indirect { alloc_id, offset } => {
88
- let alloc = tables. tcx . global_alloc ( alloc_id) . unwrap_memory ( ) ;
89
- allocation_filter ( & alloc. 0 , alloc_range ( offset, layout. size ) , tables)
90
- }
91
- } )
58
+ pub fn try_new_indirect < ' tcx , B : Bridge > (
59
+ alloc_id : AllocId ,
60
+ tables : & mut Tables < ' tcx , B > ,
61
+ ) -> ConstAllocation < ' tcx > {
62
+ let alloc = tables. tcx . global_alloc ( alloc_id) . unwrap_memory ( ) ;
63
+
64
+ alloc
92
65
}
93
66
94
67
/// Creates an `Allocation` only from information within the `AllocRange`.
95
- pub ( super ) fn allocation_filter < ' tcx > (
68
+ pub fn allocation_filter (
96
69
alloc : & rustc_middle:: mir:: interpret:: Allocation ,
97
70
alloc_range : AllocRange ,
98
- tables : & mut Tables < ' tcx > ,
99
- ) -> Allocation {
71
+ ) -> ( Vec < Option < u8 > > , Vec < ( usize , AllocId ) > ) {
100
72
let mut bytes: Vec < Option < u8 > > = alloc
101
73
. inspect_with_uninit_and_ptr_outside_interpreter (
102
74
alloc_range. start . bytes_usize ( ) ..alloc_range. end ( ) . bytes_usize ( ) ,
@@ -117,15 +89,8 @@ pub(super) fn allocation_filter<'tcx>(
117
89
. iter ( )
118
90
. filter ( |a| a. 0 >= alloc_range. start && a. 0 <= alloc_range. end ( ) )
119
91
{
120
- ptrs. push ( (
121
- offset. bytes_usize ( ) - alloc_range. start . bytes_usize ( ) ,
122
- tables. prov ( prov. alloc_id ( ) ) ,
123
- ) ) ;
124
- }
125
- Allocation {
126
- bytes,
127
- provenance : ProvenanceMap { ptrs } ,
128
- align : alloc. align . bytes ( ) ,
129
- mutability : alloc. mutability . stable ( tables) ,
92
+ ptrs. push ( ( offset. bytes_usize ( ) - alloc_range. start . bytes_usize ( ) , prov. alloc_id ( ) ) ) ;
130
93
}
94
+
95
+ ( bytes, ptrs)
131
96
}
0 commit comments