@@ -4,18 +4,13 @@ use super::query::DepGraphQuery;
4
4
use super :: { DepKind , DepNode , DepNodeIndex } ;
5
5
use rustc_data_structures:: fingerprint:: Fingerprint ;
6
6
use rustc_data_structures:: fx:: FxHashMap ;
7
- use rustc_data_structures:: sync:: { AtomicU32 , Lock , Lrc , Ordering } ;
8
- use rustc_index:: vec:: IndexVec ;
7
+ use rustc_data_structures:: sync:: { Lock , Lrc } ;
8
+ use rustc_index:: vec:: { Idx , IndexVec } ;
9
9
use rustc_serialize:: opaque:: { self , FileEncodeResult , FileEncoder , IntEncodedWithFixedSize } ;
10
10
use rustc_serialize:: { Decodable , Decoder , Encodable } ;
11
11
use smallvec:: SmallVec ;
12
12
use std:: convert:: TryInto ;
13
13
14
- #[ cfg( parallel_compiler) ]
15
- use {
16
- rustc_data_structures:: sync:: WorkerLocal , rustc_index:: vec:: Idx , std:: sync:: mpsc, std:: thread,
17
- } ;
18
-
19
14
// The maximum value of `SerializedDepNodeIndex` leaves the upper two bits
20
15
// unused so that we can store multiple index types in `CompressedHybridIndex`,
21
16
// and use those bits to encode which index type it contains.
@@ -146,12 +141,8 @@ fn encode_node<K: DepKind>(
146
141
) -> FileEncodeResult {
147
142
#[ cfg( debug_assertions) ]
148
143
if let Some ( record_graph) = & _record_graph {
149
- if let Some ( record_graph) = & mut if cfg ! ( parallel_compiler) {
150
- Some ( record_graph. lock ( ) )
151
- } else {
152
- // Do not ICE when a query is called from within `with_query`.
153
- record_graph. try_lock ( )
154
- } {
144
+ // Do not ICE when a query is called from within `with_query`.
145
+ if let Some ( record_graph) = & mut record_graph. try_lock ( ) {
155
146
record_graph. push ( _index, node. node , & node. edges ) ;
156
147
}
157
148
}
@@ -190,19 +181,8 @@ fn encode_counts(
190
181
encoder. flush ( )
191
182
}
192
183
193
- #[ cfg( not( parallel_compiler) ) ]
194
184
pub struct GraphEncoder < K : DepKind > {
195
- status : Lock < ( FileEncoder , usize , FileEncodeResult ) > ,
196
- counter : AtomicU32 ,
197
- record_graph : Option < Lrc < Lock < DepGraphQuery < K > > > > ,
198
- record_stats : Option < Lrc < Lock < Stats < K > > > > ,
199
- }
200
-
201
- #[ cfg( parallel_compiler) ]
202
- pub struct GraphEncoder < K : DepKind > {
203
- send : WorkerLocal < mpsc:: Sender < ( DepNodeIndex , NodeInfo < K > ) > > ,
204
- thread : thread:: JoinHandle < FileEncodeResult > ,
205
- counter : AtomicU32 ,
185
+ status : Lock < ( FileEncoder , DepNodeIndex , usize , FileEncodeResult ) > ,
206
186
record_graph : Option < Lrc < Lock < DepGraphQuery < K > > > > ,
207
187
record_stats : Option < Lrc < Lock < Stats < K > > > > ,
208
188
}
@@ -228,29 +208,8 @@ impl<K: DepKind + Encodable<FileEncoder>> GraphEncoder<K> {
228
208
} else {
229
209
None
230
210
} ;
231
- let counter = AtomicU32 :: new ( 0 ) ;
232
-
233
- #[ cfg( not( parallel_compiler) ) ]
234
- {
235
- let status = Lock :: new ( ( encoder, 0 , Ok ( ( ) ) ) ) ;
236
- GraphEncoder { status, counter, record_graph, record_stats }
237
- }
238
- #[ cfg( parallel_compiler) ]
239
- {
240
- let ( send, recv) = mpsc:: channel ( ) ;
241
- let thread = {
242
- let record_graph = record_graph. clone ( ) ;
243
- let record_stats = record_stats. clone ( ) ;
244
- thread:: spawn ( move || {
245
- encode_graph ( encoder, recv, |encoder, index, node| {
246
- encode_node ( encoder, index, node, & record_graph, & record_stats)
247
- } )
248
- } )
249
- } ;
250
- let send = WorkerLocal :: new ( move |_| send. clone ( ) ) ;
251
-
252
- GraphEncoder { send, thread, counter, record_graph, record_stats }
253
- }
211
+ let status = Lock :: new ( ( encoder, DepNodeIndex :: new ( 0 ) , 0 , Ok ( ( ) ) ) ) ;
212
+ GraphEncoder { status, record_graph, record_stats }
254
213
}
255
214
256
215
pub ( crate ) fn with_query ( & self , f : impl Fn ( & DepGraphQuery < K > ) ) {
@@ -314,19 +273,17 @@ impl<K: DepKind + Encodable<FileEncoder>> GraphEncoder<K> {
314
273
eprintln ! ( "[incremental]" ) ;
315
274
}
316
275
}
317
- }
318
276
319
- #[ cfg( not( parallel_compiler) ) ]
320
- impl < K : DepKind + Encodable < FileEncoder > > GraphEncoder < K > {
321
277
pub ( crate ) fn send (
322
278
& self ,
323
279
node : DepNode < K > ,
324
280
fingerprint : Fingerprint ,
325
281
edges : SmallVec < [ DepNodeIndex ; 8 ] > ,
326
282
) -> DepNodeIndex {
327
- let index = self . counter . fetch_add ( 1 , Ordering :: SeqCst ) ;
328
- let index = DepNodeIndex :: from_u32 ( index) ;
329
- let & mut ( ref mut encoder, ref mut edge_count, ref mut result) = & mut * self . status . lock ( ) ;
283
+ let & mut ( ref mut encoder, ref mut next_index, ref mut edge_count, ref mut result) =
284
+ & mut * self . status . lock ( ) ;
285
+ let index = next_index. clone ( ) ;
286
+ next_index. increment_by ( 1 ) ;
330
287
* edge_count += edges. len ( ) ;
331
288
* result = std:: mem:: replace ( result, Ok ( ( ) ) ) . and_then ( |( ) | {
332
289
let node = NodeInfo { node, fingerprint, edges } ;
@@ -336,89 +293,10 @@ impl<K: DepKind + Encodable<FileEncoder>> GraphEncoder<K> {
336
293
}
337
294
338
295
pub fn finish ( self ) -> FileEncodeResult {
339
- let ( encoder, edge_count, result) = self . status . into_inner ( ) ;
296
+ let ( encoder, node_count , edge_count, result) = self . status . into_inner ( ) ;
340
297
let ( ) = result?;
341
- let node_count = self . counter . into_inner ( ) as usize ;
298
+ let node_count = node_count . index ( ) ;
342
299
343
300
encode_counts ( encoder, node_count, edge_count)
344
301
}
345
302
}
346
-
347
- #[ cfg( parallel_compiler) ]
348
- impl < K : DepKind + Encodable < FileEncoder > > GraphEncoder < K > {
349
- pub ( crate ) fn send (
350
- & self ,
351
- node : DepNode < K > ,
352
- fingerprint : Fingerprint ,
353
- edges : SmallVec < [ DepNodeIndex ; 8 ] > ,
354
- ) -> DepNodeIndex {
355
- let node = NodeInfo { node, fingerprint, edges } ;
356
- let index = self . counter . fetch_add ( 1 , Ordering :: SeqCst ) ;
357
- let index = DepNodeIndex :: from_u32 ( index) ;
358
- self . send . send ( ( index, node) ) . unwrap ( ) ;
359
- index
360
- }
361
-
362
- pub fn finish ( self ) -> FileEncodeResult {
363
- std:: mem:: drop ( self . send ) ;
364
- self . thread . join ( ) . unwrap ( )
365
- }
366
- }
367
-
368
- #[ cfg( parallel_compiler) ]
369
- #[ instrument( skip( encoder, recv, process) ) ]
370
- fn encode_graph < K : DepKind + Encodable < FileEncoder > > (
371
- mut encoder : FileEncoder ,
372
- recv : mpsc:: Receiver < ( DepNodeIndex , NodeInfo < K > ) > ,
373
- process : impl Fn ( & mut FileEncoder , DepNodeIndex , & NodeInfo < K > ) -> FileEncodeResult ,
374
- ) -> FileEncodeResult {
375
- let mut edge_count: usize = 0 ;
376
- let node_count: usize = ordered_recv ( recv, |index, node| {
377
- edge_count += node. edges . len ( ) ;
378
- process ( & mut encoder, index, node)
379
- } ) ?;
380
-
381
- encode_counts ( encoder, node_count, edge_count)
382
- }
383
-
384
- /// Since there are multiple producers assigning the DepNodeIndex using an atomic,
385
- /// the messages may not arrive in order. This function sorts them as they come.
386
- #[ cfg( parallel_compiler) ]
387
- fn ordered_recv < K : DepKind + Encodable < opaque:: FileEncoder > > (
388
- recv : mpsc:: Receiver < ( DepNodeIndex , NodeInfo < K > ) > ,
389
- mut f : impl FnMut ( DepNodeIndex , & NodeInfo < K > ) -> FileEncodeResult ,
390
- ) -> Result < usize , std:: io:: Error > {
391
- let mut pending = Vec :: < ( DepNodeIndex , _ ) > :: new ( ) ;
392
- let mut expected = DepNodeIndex :: new ( 0 ) ;
393
-
394
- // INVARIANT: No message can arrive with an index less than `expected`.
395
- ' outer: loop {
396
- pending. sort_by_key ( |n| n. 0 ) ;
397
- for ( index, node) in pending. drain_filter ( |( index, _) | {
398
- if * index == expected {
399
- expected. increment_by ( 1 ) ;
400
- true
401
- } else {
402
- false
403
- }
404
- } ) {
405
- f ( index, & node) ?;
406
- }
407
-
408
- while let Ok ( ( index, node) ) = recv. recv ( ) {
409
- if index > expected {
410
- pending. push ( ( index, node) ) ;
411
- } else if index == expected {
412
- f ( index, & node) ?;
413
- expected. increment_by ( 1 ) ;
414
- continue ' outer;
415
- } else {
416
- panic ! ( "Unexpected index {:?} while waiting for {:?}" , index, expected) ;
417
- }
418
- }
419
-
420
- break ;
421
- }
422
-
423
- Ok ( expected. as_u32 ( ) as usize )
424
- }
0 commit comments