@@ -3,6 +3,8 @@ use crate::dep_graph::DepKind;
3
3
use crate :: query:: on_disk_cache:: CacheEncoder ;
4
4
use crate :: query:: on_disk_cache:: EncodedDepNodeIndex ;
5
5
use crate :: query:: on_disk_cache:: OnDiskCache ;
6
+ #[ cfg( parallel_compiler) ]
7
+ use crate :: query:: MtQueryCaches ;
6
8
use crate :: query:: {
7
9
DynamicQueries , ExternProviders , Providers , QueryArenas , QueryCaches , QueryEngine , QueryStates ,
8
10
} ;
@@ -20,6 +22,8 @@ pub(crate) use rustc_query_system::query::QueryJobId;
20
22
use rustc_query_system:: query:: * ;
21
23
use rustc_query_system:: HandleCycleError ;
22
24
use rustc_span:: { ErrorGuaranteed , Span , DUMMY_SP } ;
25
+ #[ cfg( not( parallel_compiler) ) ]
26
+ use std:: marker:: PhantomData ;
23
27
use std:: ops:: Deref ;
24
28
25
29
pub struct QueryKeyStringCache {
@@ -32,13 +36,17 @@ impl QueryKeyStringCache {
32
36
}
33
37
}
34
38
35
- pub struct DynamicQuery < ' tcx , C : QueryCache > {
39
+ pub struct DynamicQuery < ' tcx , C : QueryCache , C2 : QueryCache < Key = C :: Key , Value = C :: Value > > {
36
40
pub name : & ' static str ,
37
41
pub eval_always : bool ,
38
42
pub dep_kind : DepKind ,
39
43
pub handle_cycle_error : HandleCycleError ,
40
44
pub query_state : FieldOffset < QueryStates < ' tcx > , QueryState < C :: Key , DepKind > > ,
41
45
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 > ,
42
50
pub cache_on_disk : fn ( tcx : TyCtxt < ' tcx > , key : & C :: Key ) -> bool ,
43
51
pub execute_query : fn ( tcx : TyCtxt < ' tcx > , k : C :: Key ) -> C :: Value ,
44
52
pub compute : fn ( tcx : TyCtxt < ' tcx > , key : C :: Key ) -> C :: Value ,
@@ -73,6 +81,10 @@ pub struct QuerySystem<'tcx> {
73
81
pub states : QueryStates < ' tcx > ,
74
82
pub arenas : QueryArenas < ' tcx > ,
75
83
pub caches : QueryCaches < ' tcx > ,
84
+ #[ cfg( parallel_compiler) ]
85
+ pub single_thread : bool ,
86
+ #[ cfg( parallel_compiler) ]
87
+ pub mt_caches : MtQueryCaches < ' tcx > ,
76
88
pub dynamic_queries : DynamicQueries < ' tcx > ,
77
89
78
90
/// This provides access to the incremental compilation on-disk cache for query results.
@@ -139,6 +151,36 @@ impl<'tcx> TyCtxt<'tcx> {
139
151
}
140
152
}
141
153
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
+
142
184
#[ inline]
143
185
pub fn query_get_at < ' tcx , Cache > (
144
186
tcx : TyCtxt < ' tcx > ,
@@ -287,6 +329,10 @@ macro_rules! define_callbacks {
287
329
<$( $K) * as keys:: Key >:: CacheSelector as CacheSelector <' tcx, Erase <$V>>
288
330
>:: Cache ;
289
331
332
+ pub type MtStorage <' tcx> = <
333
+ <$( $K) * as keys:: Key >:: CacheSelector as CacheSelector <' tcx, Erase <$V>>
334
+ >:: MtCache ;
335
+
290
336
// Ensure that keys grow no larger than 64 bytes
291
337
#[ cfg( all( target_arch = "x86_64" , target_pointer_width = "64" ) ) ]
292
338
const _: ( ) = {
@@ -340,31 +386,36 @@ macro_rules! define_callbacks {
340
386
$( $( #[ $attr] ) * pub $name: queries:: $name:: Storage <' tcx>, ) *
341
387
}
342
388
389
+ #[ derive( Default ) ]
390
+ pub struct MtQueryCaches <' tcx> {
391
+ $( $( #[ $attr] ) * pub $name: queries:: $name:: MtStorage <' tcx>, ) *
392
+ }
393
+
343
394
impl <' tcx> TyCtxtEnsure <' tcx> {
344
395
$( $( #[ $attr] ) *
345
396
#[ inline( always) ]
346
397
pub fn $name( self , key: query_helper_param_ty!( $( $K) * ) ) {
347
- query_ensure(
398
+ with_query_caches! ( query_ensure(
348
399
self . tcx,
349
400
self . tcx. query_system. fns. engine. $name,
350
- & self . tcx. query_system . caches . $name,
401
+ : self . tcx, $name,
351
402
key. into_query_param( ) ,
352
403
false ,
353
- ) ;
404
+ ) ) ;
354
405
} ) *
355
406
}
356
407
357
408
impl <' tcx> TyCtxtEnsureWithValue <' tcx> {
358
409
$( $( #[ $attr] ) *
359
410
#[ inline( always) ]
360
411
pub fn $name( self , key: query_helper_param_ty!( $( $K) * ) ) {
361
- query_ensure(
412
+ with_query_caches! ( query_ensure(
362
413
self . tcx,
363
414
self . tcx. query_system. fns. engine. $name,
364
- & self . tcx. query_system . caches . $name,
415
+ : self . tcx, $name,
365
416
key. into_query_param( ) ,
366
417
true ,
367
- ) ;
418
+ ) ) ;
368
419
} ) *
369
420
}
370
421
@@ -383,19 +434,19 @@ macro_rules! define_callbacks {
383
434
#[ inline( always) ]
384
435
pub fn $name( self , key: query_helper_param_ty!( $( $K) * ) ) -> $V
385
436
{
386
- restore:: <$V>( query_get_at(
437
+ restore:: <$V>( with_query_caches! ( query_get_at(
387
438
self . tcx,
388
439
self . tcx. query_system. fns. engine. $name,
389
- & self . tcx. query_system . caches . $name,
440
+ : self . tcx, $name,
390
441
self . span,
391
442
key. into_query_param( ) ,
392
- ) )
443
+ ) ) )
393
444
} ) *
394
445
}
395
446
396
447
pub struct DynamicQueries <' tcx> {
397
448
$(
398
- pub $name: DynamicQuery <' tcx, queries:: $name:: Storage <' tcx>>,
449
+ pub $name: DynamicQuery <' tcx, queries:: $name:: Storage <' tcx>, queries :: $name :: MtStorage < ' tcx> >,
399
450
) *
400
451
}
401
452
@@ -485,10 +536,9 @@ macro_rules! define_feedable {
485
536
let tcx = self . tcx;
486
537
let erased = queries:: $name:: provided_to_erased( tcx, value) ;
487
538
let value = restore:: <$V>( erased) ;
488
- let cache = & tcx. query_system. caches. $name;
489
539
490
540
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, ) ) {
492
542
Some ( old) => {
493
543
let old = restore:: <$V>( old) ;
494
544
if let Some ( hasher) = hasher {
@@ -524,7 +574,7 @@ macro_rules! define_feedable {
524
574
& value,
525
575
hash_result!( [ $( $modifiers) * ] ) ,
526
576
) ;
527
- cache . complete( key, erased, dep_node_index) ;
577
+ with_query_caches! ( tcx , $name , complete( key, erased, dep_node_index, ) ) ;
528
578
}
529
579
}
530
580
}
0 commit comments