@@ -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:: { 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 ,
@@ -72,6 +80,10 @@ pub struct QuerySystem<'tcx> {
72
80
pub states : QueryStates < ' tcx > ,
73
81
pub arenas : QueryArenas < ' tcx > ,
74
82
pub caches : QueryCaches < ' tcx > ,
83
+ #[ cfg( parallel_compiler) ]
84
+ pub single_thread : bool ,
85
+ #[ cfg( parallel_compiler) ]
86
+ pub mt_caches : MtQueryCaches < ' tcx > ,
75
87
pub dynamic_queries : DynamicQueries < ' tcx > ,
76
88
77
89
/// This provides access to the incremental compilation on-disk cache for query results.
@@ -138,6 +150,36 @@ impl<'tcx> TyCtxt<'tcx> {
138
150
}
139
151
}
140
152
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
+
141
183
#[ inline]
142
184
pub fn query_get_at < ' tcx , Cache > (
143
185
tcx : TyCtxt < ' tcx > ,
@@ -286,6 +328,10 @@ macro_rules! define_callbacks {
286
328
<$( $K) * as keys:: Key >:: CacheSelector as CacheSelector <' tcx, Erase <$V>>
287
329
>:: Cache ;
288
330
331
+ pub type MtStorage <' tcx> = <
332
+ <$( $K) * as keys:: Key >:: CacheSelector as CacheSelector <' tcx, Erase <$V>>
333
+ >:: MtCache ;
334
+
289
335
// Ensure that keys grow no larger than 64 bytes
290
336
#[ cfg( all( target_arch = "x86_64" , target_pointer_width = "64" ) ) ]
291
337
const _: ( ) = {
@@ -339,31 +385,36 @@ macro_rules! define_callbacks {
339
385
$( $( #[ $attr] ) * pub $name: queries:: $name:: Storage <' tcx>, ) *
340
386
}
341
387
388
+ #[ derive( Default ) ]
389
+ pub struct MtQueryCaches <' tcx> {
390
+ $( $( #[ $attr] ) * pub $name: queries:: $name:: MtStorage <' tcx>, ) *
391
+ }
392
+
342
393
impl <' tcx> TyCtxtEnsure <' tcx> {
343
394
$( $( #[ $attr] ) *
344
395
#[ inline( always) ]
345
396
pub fn $name( self , key: query_helper_param_ty!( $( $K) * ) ) {
346
- query_ensure(
397
+ with_query_caches! ( query_ensure(
347
398
self . tcx,
348
399
self . tcx. query_system. fns. engine. $name,
349
- & self . tcx. query_system . caches . $name,
400
+ : self . tcx, $name,
350
401
key. into_query_param( ) ,
351
402
false ,
352
- ) ;
403
+ ) ) ;
353
404
} ) *
354
405
}
355
406
356
407
impl <' tcx> TyCtxtEnsureWithValue <' tcx> {
357
408
$( $( #[ $attr] ) *
358
409
#[ inline( always) ]
359
410
pub fn $name( self , key: query_helper_param_ty!( $( $K) * ) ) {
360
- query_ensure(
411
+ with_query_caches! ( query_ensure(
361
412
self . tcx,
362
413
self . tcx. query_system. fns. engine. $name,
363
- & self . tcx. query_system . caches . $name,
414
+ : self . tcx, $name,
364
415
key. into_query_param( ) ,
365
416
true ,
366
- ) ;
417
+ ) ) ;
367
418
} ) *
368
419
}
369
420
@@ -382,19 +433,19 @@ macro_rules! define_callbacks {
382
433
#[ inline( always) ]
383
434
pub fn $name( self , key: query_helper_param_ty!( $( $K) * ) ) -> $V
384
435
{
385
- restore:: <$V>( query_get_at(
436
+ restore:: <$V>( with_query_caches! ( query_get_at(
386
437
self . tcx,
387
438
self . tcx. query_system. fns. engine. $name,
388
- & self . tcx. query_system . caches . $name,
439
+ : self . tcx, $name,
389
440
self . span,
390
441
key. into_query_param( ) ,
391
- ) )
442
+ ) ) )
392
443
} ) *
393
444
}
394
445
395
446
pub struct DynamicQueries <' tcx> {
396
447
$(
397
- pub $name: DynamicQuery <' tcx, queries:: $name:: Storage <' tcx>>,
448
+ pub $name: DynamicQuery <' tcx, queries:: $name:: Storage <' tcx>, queries :: $name :: MtStorage < ' tcx> >,
398
449
) *
399
450
}
400
451
@@ -484,10 +535,9 @@ macro_rules! define_feedable {
484
535
let tcx = self . tcx;
485
536
let erased = queries:: $name:: provided_to_erased( tcx, value) ;
486
537
let value = restore:: <$V>( erased) ;
487
- let cache = & tcx. query_system. caches. $name;
488
538
489
539
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, ) ) {
491
541
Some ( old) => {
492
542
let old = restore:: <$V>( old) ;
493
543
if let Some ( hasher) = hasher {
@@ -523,7 +573,7 @@ macro_rules! define_feedable {
523
573
& value,
524
574
hash_result!( [ $( $modifiers) * ] ) ,
525
575
) ;
526
- cache . complete( key, erased, dep_node_index) ;
576
+ with_query_caches! ( tcx , $name , complete( key, erased, dep_node_index, ) ) ;
527
577
}
528
578
}
529
579
}
0 commit comments