@@ -8,10 +8,11 @@ use crate::query::config::{QueryDescription, QueryVtable};
8
8
use crate :: query:: job:: { report_cycle, QueryInfo , QueryJob , QueryJobId , QueryJobInfo } ;
9
9
use crate :: query:: { QueryContext , QueryMap , QuerySideEffects , QueryStackFrame } ;
10
10
use rustc_data_structures:: fingerprint:: Fingerprint ;
11
- use rustc_data_structures:: fx:: { FxHashMap , FxHasher } ;
11
+ use rustc_data_structures:: fx:: FxHashMap ;
12
12
#[ cfg( parallel_compiler) ]
13
13
use rustc_data_structures:: profiling:: TimingGuard ;
14
- use rustc_data_structures:: sharded:: { get_shard_index_by_hash, Sharded } ;
14
+ #[ cfg( parallel_compiler) ]
15
+ use rustc_data_structures:: sharded:: Sharded ;
15
16
use rustc_data_structures:: sync:: Lock ;
16
17
use rustc_data_structures:: thin_vec:: ThinVec ;
17
18
use rustc_errors:: { DiagnosticBuilder , FatalError } ;
@@ -20,21 +21,15 @@ use rustc_span::{Span, DUMMY_SP};
20
21
use std:: cell:: Cell ;
21
22
use std:: collections:: hash_map:: Entry ;
22
23
use std:: fmt:: Debug ;
23
- use std:: hash:: { Hash , Hasher } ;
24
+ use std:: hash:: Hash ;
24
25
use std:: mem;
25
26
use std:: ptr;
26
27
27
- // We compute the key's hash once and then use it for both the
28
- // shard lookup and the hashmap lookup. This relies on the fact
29
- // that both of them use `FxHasher`.
30
- fn hash_for_shard < K : Hash > ( key : & K ) -> u64 {
31
- let mut hasher = FxHasher :: default ( ) ;
32
- key. hash ( & mut hasher) ;
33
- hasher. finish ( )
34
- }
35
-
36
28
pub struct QueryState < K > {
37
- shards : Sharded < FxHashMap < K , QueryResult > > ,
29
+ #[ cfg( parallel_compiler) ]
30
+ active : Sharded < FxHashMap < K , QueryResult > > ,
31
+ #[ cfg( not( parallel_compiler) ) ]
32
+ active : Lock < FxHashMap < K , QueryResult > > ,
38
33
}
39
34
40
35
/// Indicates the state of a query for a given key in a query map.
52
47
K : Eq + Hash + Clone + Debug ,
53
48
{
54
49
pub fn all_inactive ( & self ) -> bool {
55
- let shards = self . shards . lock_shards ( ) ;
56
- shards. iter ( ) . all ( |shard| shard. is_empty ( ) )
50
+ #[ cfg( parallel_compiler) ]
51
+ {
52
+ let shards = self . active . lock_shards ( ) ;
53
+ shards. iter ( ) . all ( |shard| shard. is_empty ( ) )
54
+ }
55
+ #[ cfg( not( parallel_compiler) ) ]
56
+ {
57
+ self . active . lock ( ) . is_empty ( )
58
+ }
57
59
}
58
60
59
61
pub fn try_collect_active_jobs < CTX : Copy > (
@@ -62,11 +64,27 @@ where
62
64
make_query : fn ( CTX , K ) -> QueryStackFrame ,
63
65
jobs : & mut QueryMap ,
64
66
) -> Option < ( ) > {
65
- // We use try_lock_shards here since we are called from the
66
- // deadlock handler, and this shouldn't be locked.
67
- let shards = self . shards . try_lock_shards ( ) ?;
68
- for shard in shards. iter ( ) {
69
- for ( k, v) in shard. iter ( ) {
67
+ #[ cfg( parallel_compiler) ]
68
+ {
69
+ // We use try_lock_shards here since we are called from the
70
+ // deadlock handler, and this shouldn't be locked.
71
+ let shards = self . active . try_lock_shards ( ) ?;
72
+ for shard in shards. iter ( ) {
73
+ for ( k, v) in shard. iter ( ) {
74
+ if let QueryResult :: Started ( ref job) = * v {
75
+ let query = make_query ( tcx, k. clone ( ) ) ;
76
+ jobs. insert ( job. id , QueryJobInfo { query, job : job. clone ( ) } ) ;
77
+ }
78
+ }
79
+ }
80
+ }
81
+ #[ cfg( not( parallel_compiler) ) ]
82
+ {
83
+ // We use try_lock here since we are called from the
84
+ // deadlock handler, and this shouldn't be locked.
85
+ // (FIXME: Is this relevant for non-parallel compilers? It doesn't
86
+ // really hurt much.)
87
+ for ( k, v) in self . active . try_lock ( ) ?. iter ( ) {
70
88
if let QueryResult :: Started ( ref job) = * v {
71
89
let query = make_query ( tcx, k. clone ( ) ) ;
72
90
jobs. insert ( job. id , QueryJobInfo { query, job : job. clone ( ) } ) ;
80
98
81
99
impl < K > Default for QueryState < K > {
82
100
fn default ( ) -> QueryState < K > {
83
- QueryState { shards : Default :: default ( ) }
101
+ QueryState { active : Default :: default ( ) }
84
102
}
85
103
}
86
104
@@ -135,7 +153,10 @@ where
135
153
where
136
154
CTX : QueryContext ,
137
155
{
138
- let mut state_lock = state. shards . get_shard_by_value ( & key) . lock ( ) ;
156
+ #[ cfg( parallel_compiler) ]
157
+ let mut state_lock = state. active . get_shard_by_value ( & key) . lock ( ) ;
158
+ #[ cfg( not( parallel_compiler) ) ]
159
+ let mut state_lock = state. active . lock ( ) ;
139
160
let lock = & mut * state_lock;
140
161
141
162
match lock. entry ( key) {
@@ -206,10 +227,11 @@ where
206
227
mem:: forget ( self ) ;
207
228
208
229
let ( job, result) = {
209
- let key_hash = hash_for_shard ( & key) ;
210
- let shard = get_shard_index_by_hash ( key_hash) ;
211
230
let job = {
212
- let mut lock = state. shards . get_shard_by_index ( shard) . lock ( ) ;
231
+ #[ cfg( parallel_compiler) ]
232
+ let mut lock = state. active . get_shard_by_value ( & key) . lock ( ) ;
233
+ #[ cfg( not( parallel_compiler) ) ]
234
+ let mut lock = state. active . lock ( ) ;
213
235
match lock. remove ( & key) . unwrap ( ) {
214
236
QueryResult :: Started ( job) => job,
215
237
QueryResult :: Poisoned => panic ! ( ) ,
@@ -233,9 +255,11 @@ where
233
255
fn drop ( & mut self ) {
234
256
// Poison the query so jobs waiting on it panic.
235
257
let state = self . state ;
236
- let shard = state. shards . get_shard_by_value ( & self . key ) ;
237
258
let job = {
238
- let mut shard = shard. lock ( ) ;
259
+ #[ cfg( parallel_compiler) ]
260
+ let mut shard = state. active . get_shard_by_value ( & self . key ) . lock ( ) ;
261
+ #[ cfg( not( parallel_compiler) ) ]
262
+ let mut shard = state. active . lock ( ) ;
239
263
let job = match shard. remove ( & self . key ) . unwrap ( ) {
240
264
QueryResult :: Started ( job) => job,
241
265
QueryResult :: Poisoned => panic ! ( ) ,
0 commit comments