@@ -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:: { 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 ,
@@ -72,6 +80,10 @@ pub struct QuerySystem<'tcx> {
7280 pub states : QueryStates < ' tcx > ,
7381 pub arenas : QueryArenas < ' tcx > ,
7482 pub caches : QueryCaches < ' tcx > ,
83+ #[ cfg( parallel_compiler) ]
84+ pub single_thread : bool ,
85+ #[ cfg( parallel_compiler) ]
86+ pub mt_caches : MtQueryCaches < ' tcx > ,
7587 pub dynamic_queries : DynamicQueries < ' tcx > ,
7688
7789 /// This provides access to the incremental compilation on-disk cache for query results.
@@ -138,6 +150,36 @@ impl<'tcx> TyCtxt<'tcx> {
138150 }
139151}
140152
153+ #[ macro_export]
154+ #[ cfg( not( parallel_compiler) ) ]
155+ macro_rules! with_query_caches {
156+ ( $func: ident( $( $params: expr, ) * : $tcx: expr, $name: ident, $( $rest: expr, ) * ) ) => {
157+ $func( $( $params, ) * & $tcx. query_system. caches. $name, $( $rest, ) * )
158+ } ;
159+ ( $tcx: expr, $name: ident, $func: ident( $( $params: expr, ) * ) ) => {
160+ $tcx. query_system. caches. $name. $func( $( $params, ) * )
161+ }
162+ }
163+
164+ #[ macro_export]
165+ #[ cfg( parallel_compiler) ]
166+ macro_rules! with_query_caches {
167+ ( $func: ident( $( $params: expr, ) * : $tcx: expr, $name: ident, $( $rest: expr, ) * ) ) => {
168+ if $tcx. query_system. single_thread {
169+ $func( $( $params, ) * & $tcx. query_system. caches. $name, $( $rest, ) * )
170+ } else {
171+ $func( $( $params, ) * & $tcx. query_system. mt_caches. $name, $( $rest, ) * )
172+ }
173+ } ;
174+ ( $tcx: expr, $name: ident, $func: ident( $( $params: expr, ) * ) ) => {
175+ if $tcx. query_system. single_thread {
176+ $tcx. query_system. caches. $name. $func( $( $params, ) * )
177+ } else {
178+ $tcx. query_system. mt_caches. $name. $func( $( $params, ) * )
179+ }
180+ }
181+ }
182+
141183#[ inline]
142184pub fn query_get_at < ' tcx , Cache > (
143185 tcx : TyCtxt < ' tcx > ,
@@ -286,6 +328,10 @@ macro_rules! define_callbacks {
286328 <$( $K) * as keys:: Key >:: CacheSelector as CacheSelector <' tcx, Erase <$V>>
287329 >:: Cache ;
288330
331+ pub type MtStorage <' tcx> = <
332+ <$( $K) * as keys:: Key >:: CacheSelector as CacheSelector <' tcx, Erase <$V>>
333+ >:: MtCache ;
334+
289335 // Ensure that keys grow no larger than 64 bytes
290336 #[ cfg( all( target_arch = "x86_64" , target_pointer_width = "64" ) ) ]
291337 const _: ( ) = {
@@ -339,31 +385,36 @@ macro_rules! define_callbacks {
339385 $( $( #[ $attr] ) * pub $name: queries:: $name:: Storage <' tcx>, ) *
340386 }
341387
388+ #[ derive( Default ) ]
389+ pub struct MtQueryCaches <' tcx> {
390+ $( $( #[ $attr] ) * pub $name: queries:: $name:: MtStorage <' tcx>, ) *
391+ }
392+
342393 impl <' tcx> TyCtxtEnsure <' tcx> {
343394 $( $( #[ $attr] ) *
344395 #[ inline( always) ]
345396 pub fn $name( self , key: query_helper_param_ty!( $( $K) * ) ) {
346- query_ensure(
397+ with_query_caches! ( query_ensure(
347398 self . tcx,
348399 self . tcx. query_system. fns. engine. $name,
349- & self . tcx. query_system . caches . $name,
400+ : self . tcx, $name,
350401 key. into_query_param( ) ,
351402 false ,
352- ) ;
403+ ) ) ;
353404 } ) *
354405 }
355406
356407 impl <' tcx> TyCtxtEnsureWithValue <' tcx> {
357408 $( $( #[ $attr] ) *
358409 #[ inline( always) ]
359410 pub fn $name( self , key: query_helper_param_ty!( $( $K) * ) ) {
360- query_ensure(
411+ with_query_caches! ( query_ensure(
361412 self . tcx,
362413 self . tcx. query_system. fns. engine. $name,
363- & self . tcx. query_system . caches . $name,
414+ : self . tcx, $name,
364415 key. into_query_param( ) ,
365416 true ,
366- ) ;
417+ ) ) ;
367418 } ) *
368419 }
369420
@@ -382,19 +433,19 @@ macro_rules! define_callbacks {
382433 #[ inline( always) ]
383434 pub fn $name( self , key: query_helper_param_ty!( $( $K) * ) ) -> $V
384435 {
385- restore:: <$V>( query_get_at(
436+ restore:: <$V>( with_query_caches! ( query_get_at(
386437 self . tcx,
387438 self . tcx. query_system. fns. engine. $name,
388- & self . tcx. query_system . caches . $name,
439+ : self . tcx, $name,
389440 self . span,
390441 key. into_query_param( ) ,
391- ) )
442+ ) ) )
392443 } ) *
393444 }
394445
395446 pub struct DynamicQueries <' tcx> {
396447 $(
397- pub $name: DynamicQuery <' tcx, queries:: $name:: Storage <' tcx>>,
448+ pub $name: DynamicQuery <' tcx, queries:: $name:: Storage <' tcx>, queries :: $name :: MtStorage < ' tcx> >,
398449 ) *
399450 }
400451
@@ -484,10 +535,9 @@ macro_rules! define_feedable {
484535 let tcx = self . tcx;
485536 let erased = queries:: $name:: provided_to_erased( tcx, value) ;
486537 let value = restore:: <$V>( erased) ;
487- let cache = & tcx. query_system. caches. $name;
488538
489539 let hasher: Option <fn ( & mut StableHashingContext <' _>, & _) -> _> = hash_result!( [ $( $modifiers) * ] ) ;
490- match try_get_cached( tcx, cache , & key) {
540+ match with_query_caches! ( try_get_cached( tcx, : tcx , $name , & key, ) ) {
491541 Some ( old) => {
492542 let old = restore:: <$V>( old) ;
493543 if let Some ( hasher) = hasher {
@@ -523,7 +573,7 @@ macro_rules! define_feedable {
523573 & value,
524574 hash_result!( [ $( $modifiers) * ] ) ,
525575 ) ;
526- cache . complete( key, erased, dep_node_index) ;
576+ with_query_caches! ( tcx , $name , complete( key, erased, dep_node_index, ) ) ;
527577 }
528578 }
529579 }
0 commit comments