@@ -3,6 +3,8 @@ use crate::dep_graph::DepKind;
33use crate :: query:: on_disk_cache:: CacheEncoder ;
44use crate :: query:: on_disk_cache:: EncodedDepNodeIndex ;
55use crate :: query:: on_disk_cache:: OnDiskCache ;
6+ #[ cfg( parallel_compiler) ]
7+ use crate :: query:: MtQueryCaches ;
68use crate :: query:: {
79 DynamicQueries , ExternProviders , Providers , QueryArenas , QueryCaches , QueryEngine , QueryStates ,
810} ;
@@ -20,6 +22,8 @@ pub(crate) use rustc_query_system::query::QueryJobId;
2022use rustc_query_system:: query:: * ;
2123use rustc_query_system:: HandleCycleError ;
2224use rustc_span:: { ErrorGuaranteed , Span , DUMMY_SP } ;
25+ #[ cfg( not( parallel_compiler) ) ]
26+ use std:: marker:: PhantomData ;
2327use std:: ops:: Deref ;
2428
2529pub struct QueryKeyStringCache {
@@ -32,13 +36,17 @@ impl QueryKeyStringCache {
3236 }
3337}
3438
35- pub struct DynamicQuery < ' tcx , C : QueryCache > {
39+ pub struct DynamicQuery < ' tcx , C : QueryCache , C2 : QueryCache < Key = C :: Key , Value = C :: Value > > {
3640 pub name : & ' static str ,
3741 pub eval_always : bool ,
3842 pub dep_kind : DepKind ,
3943 pub handle_cycle_error : HandleCycleError ,
4044 pub query_state : FieldOffset < QueryStates < ' tcx > , QueryState < C :: Key , DepKind > > ,
4145 pub query_cache : FieldOffset < QueryCaches < ' tcx > , C > ,
46+ #[ cfg( not( parallel_compiler) ) ]
47+ pub mt_query_cache : PhantomData < C2 > ,
48+ #[ cfg( parallel_compiler) ]
49+ pub mt_query_cache : FieldOffset < MtQueryCaches < ' tcx > , C2 > ,
4250 pub cache_on_disk : fn ( tcx : TyCtxt < ' tcx > , key : & C :: Key ) -> bool ,
4351 pub execute_query : fn ( tcx : TyCtxt < ' tcx > , k : C :: Key ) -> C :: Value ,
4452 pub compute : fn ( tcx : TyCtxt < ' tcx > , key : C :: Key ) -> C :: Value ,
@@ -73,6 +81,10 @@ pub struct QuerySystem<'tcx> {
7381 pub states : QueryStates < ' tcx > ,
7482 pub arenas : QueryArenas < ' tcx > ,
7583 pub caches : QueryCaches < ' tcx > ,
84+ #[ cfg( parallel_compiler) ]
85+ pub single_thread : bool ,
86+ #[ cfg( parallel_compiler) ]
87+ pub mt_caches : MtQueryCaches < ' tcx > ,
7688 pub dynamic_queries : DynamicQueries < ' tcx > ,
7789
7890 /// This provides access to the incremental compilation on-disk cache for query results.
@@ -139,6 +151,36 @@ impl<'tcx> TyCtxt<'tcx> {
139151 }
140152}
141153
154+ #[ macro_export]
155+ #[ cfg( not( parallel_compiler) ) ]
156+ macro_rules! with_query_caches {
157+ ( $func: ident( $( $params: expr, ) * : $tcx: expr, $name: ident, $( $rest: expr, ) * ) ) => {
158+ $func( $( $params, ) * & $tcx. query_system. caches. $name, $( $rest, ) * )
159+ } ;
160+ ( $tcx: expr, $name: ident, $func: ident( $( $params: expr, ) * ) ) => {
161+ $tcx. query_system. caches. $name. $func( $( $params, ) * )
162+ }
163+ }
164+
165+ #[ macro_export]
166+ #[ cfg( parallel_compiler) ]
167+ macro_rules! with_query_caches {
168+ ( $func: ident( $( $params: expr, ) * : $tcx: expr, $name: ident, $( $rest: expr, ) * ) ) => {
169+ if $tcx. query_system. single_thread {
170+ $func( $( $params, ) * & $tcx. query_system. caches. $name, $( $rest, ) * )
171+ } else {
172+ $func( $( $params, ) * & $tcx. query_system. mt_caches. $name, $( $rest, ) * )
173+ }
174+ } ;
175+ ( $tcx: expr, $name: ident, $func: ident( $( $params: expr, ) * ) ) => {
176+ if $tcx. query_system. single_thread {
177+ $tcx. query_system. caches. $name. $func( $( $params, ) * )
178+ } else {
179+ $tcx. query_system. mt_caches. $name. $func( $( $params, ) * )
180+ }
181+ }
182+ }
183+
142184#[ inline]
143185pub fn query_get_at < ' tcx , Cache > (
144186 tcx : TyCtxt < ' tcx > ,
@@ -287,6 +329,10 @@ macro_rules! define_callbacks {
287329 <$( $K) * as keys:: Key >:: CacheSelector as CacheSelector <' tcx, Erase <$V>>
288330 >:: Cache ;
289331
332+ pub type MtStorage <' tcx> = <
333+ <$( $K) * as keys:: Key >:: CacheSelector as CacheSelector <' tcx, Erase <$V>>
334+ >:: MtCache ;
335+
290336 // Ensure that keys grow no larger than 64 bytes
291337 #[ cfg( all( target_arch = "x86_64" , target_pointer_width = "64" ) ) ]
292338 const _: ( ) = {
@@ -340,31 +386,36 @@ macro_rules! define_callbacks {
340386 $( $( #[ $attr] ) * pub $name: queries:: $name:: Storage <' tcx>, ) *
341387 }
342388
389+ #[ derive( Default ) ]
390+ pub struct MtQueryCaches <' tcx> {
391+ $( $( #[ $attr] ) * pub $name: queries:: $name:: MtStorage <' tcx>, ) *
392+ }
393+
343394 impl <' tcx> TyCtxtEnsure <' tcx> {
344395 $( $( #[ $attr] ) *
345396 #[ inline( always) ]
346397 pub fn $name( self , key: query_helper_param_ty!( $( $K) * ) ) {
347- query_ensure(
398+ with_query_caches! ( query_ensure(
348399 self . tcx,
349400 self . tcx. query_system. fns. engine. $name,
350- & self . tcx. query_system . caches . $name,
401+ : self . tcx, $name,
351402 key. into_query_param( ) ,
352403 false ,
353- ) ;
404+ ) ) ;
354405 } ) *
355406 }
356407
357408 impl <' tcx> TyCtxtEnsureWithValue <' tcx> {
358409 $( $( #[ $attr] ) *
359410 #[ inline( always) ]
360411 pub fn $name( self , key: query_helper_param_ty!( $( $K) * ) ) {
361- query_ensure(
412+ with_query_caches! ( query_ensure(
362413 self . tcx,
363414 self . tcx. query_system. fns. engine. $name,
364- & self . tcx. query_system . caches . $name,
415+ : self . tcx, $name,
365416 key. into_query_param( ) ,
366417 true ,
367- ) ;
418+ ) ) ;
368419 } ) *
369420 }
370421
@@ -383,19 +434,19 @@ macro_rules! define_callbacks {
383434 #[ inline( always) ]
384435 pub fn $name( self , key: query_helper_param_ty!( $( $K) * ) ) -> $V
385436 {
386- restore:: <$V>( query_get_at(
437+ restore:: <$V>( with_query_caches! ( query_get_at(
387438 self . tcx,
388439 self . tcx. query_system. fns. engine. $name,
389- & self . tcx. query_system . caches . $name,
440+ : self . tcx, $name,
390441 self . span,
391442 key. into_query_param( ) ,
392- ) )
443+ ) ) )
393444 } ) *
394445 }
395446
396447 pub struct DynamicQueries <' tcx> {
397448 $(
398- pub $name: DynamicQuery <' tcx, queries:: $name:: Storage <' tcx>>,
449+ pub $name: DynamicQuery <' tcx, queries:: $name:: Storage <' tcx>, queries :: $name :: MtStorage < ' tcx> >,
399450 ) *
400451 }
401452
@@ -485,10 +536,9 @@ macro_rules! define_feedable {
485536 let tcx = self . tcx;
486537 let erased = queries:: $name:: provided_to_erased( tcx, value) ;
487538 let value = restore:: <$V>( erased) ;
488- let cache = & tcx. query_system. caches. $name;
489539
490540 let hasher: Option <fn ( & mut StableHashingContext <' _>, & _) -> _> = hash_result!( [ $( $modifiers) * ] ) ;
491- match try_get_cached( tcx, cache , & key) {
541+ match with_query_caches! ( try_get_cached( tcx, : tcx , $name , & key, ) ) {
492542 Some ( old) => {
493543 let old = restore:: <$V>( old) ;
494544 if let Some ( hasher) = hasher {
@@ -524,7 +574,7 @@ macro_rules! define_feedable {
524574 & value,
525575 hash_result!( [ $( $modifiers) * ] ) ,
526576 ) ;
527- cache . complete( key, erased, dep_node_index) ;
577+ with_query_caches! ( tcx , $name , complete( key, erased, dep_node_index, ) ) ;
528578 }
529579 }
530580 }
0 commit comments