@@ -10,6 +10,7 @@ use std::process;
1010use either:: Either ;
1111use rand:: rngs:: StdRng ;
1212use rand:: SeedableRng ;
13+ use rand:: Rng ;
1314
1415use rustc_ast:: ast:: Mutability ;
1516use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
@@ -531,6 +532,8 @@ pub struct MiriMachine<'mir, 'tcx> {
531532 /// The spans we will use to report where an allocation was created and deallocated in
532533 /// diagnostics.
533534 pub ( crate ) allocation_spans : RefCell < FxHashMap < AllocId , ( Span , Option < Span > ) > > ,
535+
536+ const_cache : RefCell < FxHashMap < ( ConstAllocation < ' tcx > , Instance < ' tcx > ) , Vec < AllocId > > > ,
534537}
535538
536539impl < ' mir , ' tcx > MiriMachine < ' mir , ' tcx > {
@@ -656,6 +659,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
656659 stack_size,
657660 collect_leak_backtraces : config. collect_leak_backtraces ,
658661 allocation_spans : RefCell :: new ( FxHashMap :: default ( ) ) ,
662+ const_cache : RefCell :: new ( FxHashMap :: default ( ) ) ,
659663 }
660664 }
661665
@@ -841,6 +845,7 @@ impl VisitProvenance for MiriMachine<'_, '_> {
841845 stack_size : _,
842846 collect_leak_backtraces : _,
843847 allocation_spans : _,
848+ const_cache : _,
844849 } = self ;
845850
846851 threads. visit_provenance ( visit) ;
@@ -1450,4 +1455,20 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
14501455 ecx. machine . allocation_spans . borrow_mut ( ) . insert ( alloc_id, ( span, None ) ) ;
14511456 Ok ( ( ) )
14521457 }
1458+
1459+ fn const_alloc_id (
1460+ ecx : & InterpCx < ' mir , ' tcx , Self > ,
1461+ alloc : ConstAllocation < ' tcx > ,
1462+ ) -> AllocId {
1463+ let instance = ecx. active_thread_stack ( ) . last ( ) . unwrap ( ) . instance ;
1464+ let mut cache = ecx. machine . const_cache . borrow_mut ( ) ;
1465+ let contents = cache. entry ( ( alloc, instance) ) . or_insert_with ( Vec :: new) ;
1466+ if contents. len ( ) < 16 {
1467+ let new = ecx. tcx . reserve_and_set_memory_alloc ( alloc) ;
1468+ contents. push ( new) ;
1469+ new
1470+ } else {
1471+ contents[ ecx. machine . rng . borrow_mut ( ) . gen :: < u8 > ( ) as usize % 16 ]
1472+ }
1473+ }
14531474}
0 commit comments