1
+ use crate :: dep_graph:: DepKind ;
1
2
use crate :: ty:: context:: TyCtxt ;
2
3
use crate :: ty:: query:: plumbing:: CycleError ;
3
4
use crate :: ty:: query:: Query ;
@@ -7,7 +8,7 @@ use rustc_data_structures::fx::FxHashMap;
7
8
use rustc_span:: Span ;
8
9
9
10
use std:: marker:: PhantomData ;
10
- use std:: num:: NonZeroUsize ;
11
+ use std:: num:: NonZeroU32 ;
11
12
12
13
#[ cfg( parallel_compiler) ]
13
14
use {
@@ -31,19 +32,26 @@ pub struct QueryInfo<'tcx> {
31
32
pub query : Query < ' tcx > ,
32
33
}
33
34
34
- type QueryMap < ' tcx > = FxHashMap < QueryToken , QueryJobInfo < ' tcx > > ;
35
+ type QueryMap < ' tcx > = FxHashMap < QueryJobId , QueryJobInfo < ' tcx > > ;
36
+
37
+ /// A value uniquely identifiying an active query job within a shard in the query cache.
38
+ #[ derive( Copy , Clone , Eq , PartialEq , Hash ) ]
39
+ pub struct QueryShardJobId ( pub NonZeroU32 ) ;
35
40
36
41
/// A value uniquely identifiying an active query job.
37
- /// This value is created from a stack pointer in `get_query` and `force_query`
38
- /// which is alive while the query executes.
39
42
#[ derive( Copy , Clone , Eq , PartialEq , Hash ) ]
40
- pub struct QueryToken ( NonZeroUsize ) ;
43
+ pub struct QueryJobId {
44
+ /// Which job within a shard is this
45
+ pub job : QueryShardJobId ,
41
46
42
- impl QueryToken {
43
- pub fn from < T > ( v : & T ) -> Self {
44
- QueryToken ( NonZeroUsize :: new ( v as * const T as usize ) . unwrap ( ) )
45
- }
47
+ /// In which shard is this job
48
+ pub shard : u16 ,
49
+
50
+ /// What kind of query this job is
51
+ pub kind : DepKind ,
52
+ }
46
53
54
+ impl QueryJobId {
47
55
fn query < ' tcx > ( self , map : & QueryMap < ' tcx > ) -> Query < ' tcx > {
48
56
map. get ( & self ) . unwrap ( ) . info . query . clone ( )
49
57
}
@@ -54,7 +62,7 @@ impl QueryToken {
54
62
}
55
63
56
64
#[ cfg( parallel_compiler) ]
57
- fn parent ( self , map : & QueryMap < ' _ > ) -> Option < QueryToken > {
65
+ fn parent ( self , map : & QueryMap < ' _ > ) -> Option < QueryJobId > {
58
66
map. get ( & self ) . unwrap ( ) . job . parent
59
67
}
60
68
@@ -72,13 +80,13 @@ pub struct QueryJobInfo<'tcx> {
72
80
/// Represents an active query job.
73
81
#[ derive( Clone ) ]
74
82
pub struct QueryJob < ' tcx > {
75
- pub token : QueryToken ,
83
+ pub id : QueryShardJobId ,
76
84
77
85
/// The span corresponding to the reason for which this query was required.
78
86
pub span : Span ,
79
87
80
88
/// The parent query job which created this job and is implicitly waiting on it.
81
- pub parent : Option < QueryToken > ,
89
+ pub parent : Option < QueryJobId > ,
82
90
83
91
/// The latch that is used to wait on this job.
84
92
#[ cfg( parallel_compiler) ]
@@ -89,9 +97,9 @@ pub struct QueryJob<'tcx> {
89
97
90
98
impl < ' tcx > QueryJob < ' tcx > {
91
99
/// Creates a new query job.
92
- pub fn new ( token : QueryToken , span : Span , parent : Option < QueryToken > ) -> Self {
100
+ pub fn new ( id : QueryShardJobId , span : Span , parent : Option < QueryJobId > ) -> Self {
93
101
QueryJob {
94
- token ,
102
+ id ,
95
103
span,
96
104
parent,
97
105
#[ cfg( parallel_compiler) ]
@@ -101,16 +109,16 @@ impl<'tcx> QueryJob<'tcx> {
101
109
}
102
110
103
111
#[ cfg( parallel_compiler) ]
104
- pub ( super ) fn latch ( & mut self ) -> QueryLatch < ' tcx > {
112
+ pub ( super ) fn latch ( & mut self , _id : QueryJobId ) -> QueryLatch < ' tcx > {
105
113
if self . latch . is_none ( ) {
106
114
self . latch = Some ( QueryLatch :: new ( ) ) ;
107
115
}
108
116
self . latch . as_ref ( ) . unwrap ( ) . clone ( )
109
117
}
110
118
111
119
#[ cfg( not( parallel_compiler) ) ]
112
- pub ( super ) fn latch ( & mut self ) -> QueryLatch < ' tcx > {
113
- QueryLatch { token : self . token , dummy : PhantomData }
120
+ pub ( super ) fn latch ( & mut self , id : QueryJobId ) -> QueryLatch < ' tcx > {
121
+ QueryLatch { id , dummy : PhantomData }
114
122
}
115
123
116
124
/// Signals to waiters that the query is complete.
@@ -126,7 +134,7 @@ impl<'tcx> QueryJob<'tcx> {
126
134
#[ cfg( not( parallel_compiler) ) ]
127
135
#[ derive( Clone ) ]
128
136
pub ( super ) struct QueryLatch < ' tcx > {
129
- token : QueryToken ,
137
+ id : QueryJobId ,
130
138
dummy : PhantomData < & ' tcx ( ) > ,
131
139
}
132
140
@@ -143,7 +151,7 @@ impl<'tcx> QueryLatch<'tcx> {
143
151
let info = query_map. get ( & job) . unwrap ( ) ;
144
152
cycle. push ( info. info . clone ( ) ) ;
145
153
146
- if job == self . token {
154
+ if job == self . id {
147
155
cycle. reverse ( ) ;
148
156
149
157
// This is the end of the cycle
@@ -169,7 +177,7 @@ impl<'tcx> QueryLatch<'tcx> {
169
177
170
178
#[ cfg( parallel_compiler) ]
171
179
struct QueryWaiter < ' tcx > {
172
- query : Option < QueryToken > ,
180
+ query : Option < QueryJobId > ,
173
181
condvar : Condvar ,
174
182
span : Span ,
175
183
cycle : Lock < Option < CycleError < ' tcx > > > ,
@@ -270,7 +278,7 @@ impl<'tcx> QueryLatch<'tcx> {
270
278
271
279
/// A resumable waiter of a query. The usize is the index into waiters in the query's latch
272
280
#[ cfg( parallel_compiler) ]
273
- type Waiter = ( QueryToken , usize ) ;
281
+ type Waiter = ( QueryJobId , usize ) ;
274
282
275
283
/// Visits all the non-resumable and resumable waiters of a query.
276
284
/// Only waiters in a query are visited.
@@ -284,11 +292,11 @@ type Waiter = (QueryToken, usize);
284
292
#[ cfg( parallel_compiler) ]
285
293
fn visit_waiters < ' tcx , F > (
286
294
query_map : & QueryMap < ' tcx > ,
287
- query : QueryToken ,
295
+ query : QueryJobId ,
288
296
mut visit : F ,
289
297
) -> Option < Option < Waiter > >
290
298
where
291
- F : FnMut ( Span , QueryToken ) -> Option < Option < Waiter > > ,
299
+ F : FnMut ( Span , QueryJobId ) -> Option < Option < Waiter > > ,
292
300
{
293
301
// Visit the parent query which is a non-resumable waiter since it's on the same stack
294
302
if let Some ( parent) = query. parent ( query_map) {
@@ -319,10 +327,10 @@ where
319
327
#[ cfg( parallel_compiler) ]
320
328
fn cycle_check < ' tcx > (
321
329
query_map : & QueryMap < ' tcx > ,
322
- query : QueryToken ,
330
+ query : QueryJobId ,
323
331
span : Span ,
324
- stack : & mut Vec < ( Span , QueryToken ) > ,
325
- visited : & mut FxHashSet < QueryToken > ,
332
+ stack : & mut Vec < ( Span , QueryJobId ) > ,
333
+ visited : & mut FxHashSet < QueryJobId > ,
326
334
) -> Option < Option < Waiter > > {
327
335
if !visited. insert ( query) {
328
336
return if let Some ( p) = stack. iter ( ) . position ( |q| q. 1 == query) {
@@ -360,8 +368,8 @@ fn cycle_check<'tcx>(
360
368
#[ cfg( parallel_compiler) ]
361
369
fn connected_to_root < ' tcx > (
362
370
query_map : & QueryMap < ' tcx > ,
363
- query : QueryToken ,
364
- visited : & mut FxHashSet < QueryToken > ,
371
+ query : QueryJobId ,
372
+ visited : & mut FxHashSet < QueryJobId > ,
365
373
) -> bool {
366
374
// We already visited this or we're deliberately ignoring it
367
375
if !visited. insert ( query) {
@@ -381,7 +389,7 @@ fn connected_to_root<'tcx>(
381
389
382
390
// Deterministically pick an query from a list
383
391
#[ cfg( parallel_compiler) ]
384
- fn pick_query < ' a , ' tcx , T , F : Fn ( & T ) -> ( Span , QueryToken ) > (
392
+ fn pick_query < ' a , ' tcx , T , F : Fn ( & T ) -> ( Span , QueryJobId ) > (
385
393
query_map : & QueryMap < ' tcx > ,
386
394
tcx : TyCtxt < ' tcx > ,
387
395
queries : & ' a [ T ] ,
@@ -413,7 +421,7 @@ fn pick_query<'a, 'tcx, T, F: Fn(&T) -> (Span, QueryToken)>(
413
421
#[ cfg( parallel_compiler) ]
414
422
fn remove_cycle < ' tcx > (
415
423
query_map : & QueryMap < ' tcx > ,
416
- jobs : & mut Vec < QueryToken > ,
424
+ jobs : & mut Vec < QueryJobId > ,
417
425
wakelist : & mut Vec < Lrc < QueryWaiter < ' tcx > > > ,
418
426
tcx : TyCtxt < ' tcx > ,
419
427
) -> bool {
@@ -468,7 +476,7 @@ fn remove_cycle<'tcx>(
468
476
}
469
477
}
470
478
} )
471
- . collect :: < Vec < ( Span , QueryToken , Option < ( Span , QueryToken ) > ) > > ( ) ;
479
+ . collect :: < Vec < ( Span , QueryJobId , Option < ( Span , QueryJobId ) > ) > > ( ) ;
472
480
473
481
// Deterministically pick an entry point
474
482
let ( _, entry_point, usage) = pick_query ( query_map, tcx, & entry_points, |e| ( e. 0 , e. 1 ) ) ;
@@ -548,7 +556,7 @@ fn deadlock(tcx: TyCtxt<'_>, registry: &rayon_core::Registry) {
548
556
549
557
let mut wakelist = Vec :: new ( ) ;
550
558
let query_map = tcx. queries . try_collect_active_jobs ( ) . unwrap ( ) ;
551
- let mut jobs: Vec < QueryToken > = query_map. keys ( ) . cloned ( ) . collect ( ) ;
559
+ let mut jobs: Vec < QueryJobId > = query_map. keys ( ) . cloned ( ) . collect ( ) ;
552
560
553
561
let mut found_cycle = false ;
554
562
0 commit comments