33use cranelift_module:: * ;
44use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
55use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
6- use rustc_middle:: mir:: interpret:: {
7- read_target_uint, AllocId , ConstAllocation , ConstValue , ErrorHandled , GlobalAlloc , Scalar ,
8- } ;
6+ use rustc_middle:: mir:: interpret:: { read_target_uint, AllocId , ConstValue , GlobalAlloc , Scalar } ;
97
108use crate :: prelude:: * ;
119
@@ -32,16 +30,6 @@ impl ConstantCx {
3230 }
3331}
3432
35- pub ( crate ) fn check_constants ( fx : & mut FunctionCx < ' _ , ' _ , ' _ > ) -> bool {
36- let mut all_constants_ok = true ;
37- for constant in & fx. mir . required_consts {
38- if eval_mir_constant ( fx, constant) . is_none ( ) {
39- all_constants_ok = false ;
40- }
41- }
42- all_constants_ok
43- }
44-
4533pub ( crate ) fn codegen_static ( tcx : TyCtxt < ' _ > , module : & mut dyn Module , def_id : DefId ) {
4634 let mut constants_cx = ConstantCx :: new ( ) ;
4735 constants_cx. todo . push ( TodoItem :: Static ( def_id) ) ;
@@ -75,30 +63,20 @@ pub(crate) fn codegen_tls_ref<'tcx>(
7563pub ( crate ) fn eval_mir_constant < ' tcx > (
7664 fx : & FunctionCx < ' _ , ' _ , ' tcx > ,
7765 constant : & Constant < ' tcx > ,
78- ) -> Option < ( ConstValue < ' tcx > , Ty < ' tcx > ) > {
66+ ) -> ( ConstValue < ' tcx > , Ty < ' tcx > ) {
7967 let cv = fx. monomorphize ( constant. literal ) ;
68+ // This cannot fail because we checked all required_consts in advance.
8069 let val = cv
8170 . eval ( fx. tcx , ty:: ParamEnv :: reveal_all ( ) , Some ( constant. span ) )
82- . map_err ( |err| match err {
83- ErrorHandled :: Reported ( _) => {
84- fx. tcx . sess . span_err ( constant. span , "erroneous constant encountered" ) ;
85- }
86- ErrorHandled :: TooGeneric => {
87- span_bug ! ( constant. span, "codegen encountered polymorphic constant: {:?}" , err) ;
88- }
89- } )
90- . ok ( ) ;
91- val. map ( |val| ( val, cv. ty ( ) ) )
71+ . expect ( "erroneous constant not captured by required_consts" ) ;
72+ ( val, cv. ty ( ) )
9273}
9374
9475pub ( crate ) fn codegen_constant_operand < ' tcx > (
9576 fx : & mut FunctionCx < ' _ , ' _ , ' tcx > ,
9677 constant : & Constant < ' tcx > ,
9778) -> CValue < ' tcx > {
98- let ( const_val, ty) = eval_mir_constant ( fx, constant) . unwrap_or_else ( || {
99- span_bug ! ( constant. span, "erroneous constant not captured by required_consts" )
100- } ) ;
101-
79+ let ( const_val, ty) = eval_mir_constant ( fx, constant) ;
10280 codegen_const_value ( fx, const_val, ty)
10381}
10482
@@ -115,7 +93,7 @@ pub(crate) fn codegen_const_value<'tcx>(
11593 }
11694
11795 match const_val {
118- ConstValue :: ZeroSized => unreachable ! ( ) , // we already handles ZST above
96+ ConstValue :: ZeroSized => unreachable ! ( ) , // we already handled ZST above
11997 ConstValue :: Scalar ( x) => match x {
12098 Scalar :: Int ( int) => {
12199 if fx. clif_type ( layout. ty ) . is_some ( ) {
@@ -199,13 +177,14 @@ pub(crate) fn codegen_const_value<'tcx>(
199177 CValue :: by_val ( val, layout)
200178 }
201179 } ,
202- ConstValue :: ByRef { alloc , offset } => CValue :: by_ref (
203- pointer_for_allocation ( fx, alloc )
180+ ConstValue :: Indirect { alloc_id , offset } => CValue :: by_ref (
181+ pointer_for_allocation ( fx, alloc_id )
204182 . offset_i64 ( fx, i64:: try_from ( offset. bytes ( ) ) . unwrap ( ) ) ,
205183 layout,
206184 ) ,
207185 ConstValue :: Slice { data, start, end } => {
208- let ptr = pointer_for_allocation ( fx, data)
186+ let alloc_id = fx. tcx . reserve_and_set_memory_alloc ( data) ;
187+ let ptr = pointer_for_allocation ( fx, alloc_id)
209188 . offset_i64 ( fx, i64:: try_from ( start) . unwrap ( ) )
210189 . get_addr ( fx) ;
211190 let len = fx
@@ -219,9 +198,9 @@ pub(crate) fn codegen_const_value<'tcx>(
219198
220199fn pointer_for_allocation < ' tcx > (
221200 fx : & mut FunctionCx < ' _ , ' _ , ' tcx > ,
222- alloc : ConstAllocation < ' tcx > ,
201+ alloc_id : AllocId ,
223202) -> crate :: pointer:: Pointer {
224- let alloc_id = fx. tcx . create_memory_alloc ( alloc ) ;
203+ let alloc = fx. tcx . global_alloc ( alloc_id ) . unwrap_memory ( ) ;
225204 let data_id = data_id_for_alloc_id (
226205 & mut fx. constants_cx ,
227206 & mut * fx. module ,
@@ -352,6 +331,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
352331 unreachable ! ( )
353332 }
354333 } ;
334+ // FIXME: should we have a cache so we don't do this multiple times for the same `ConstAllocation`?
355335 let data_id = * cx. anon_allocs . entry ( alloc_id) . or_insert_with ( || {
356336 module. declare_anonymous_data ( alloc. inner ( ) . mutability . is_mut ( ) , false ) . unwrap ( )
357337 } ) ;
@@ -456,7 +436,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
456436 operand : & Operand < ' tcx > ,
457437) -> Option < ConstValue < ' tcx > > {
458438 match operand {
459- Operand :: Constant ( const_) => Some ( eval_mir_constant ( fx, const_) . unwrap ( ) . 0 ) ,
439+ Operand :: Constant ( const_) => Some ( eval_mir_constant ( fx, const_) . 0 ) ,
460440 // FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored
461441 // inside a temporary before being passed to the intrinsic requiring the const argument.
462442 // This code tries to find a single constant defining definition of the referenced local.
0 commit comments