@@ -319,30 +319,70 @@ fn merge_codegen_units<'tcx>(
319
319
// - we have more CGUs than the upper limit, or
320
320
// - (Non-incremental builds only) the user didn't specify a CGU count, and
321
321
// there are multiple CGUs, and some are below the minimum size.
322
+ // - njn: update this comment
322
323
//
323
324
// The "didn't specify a CGU count" condition is because when an explicit
324
325
// count is requested we observe it as closely as possible. For example,
325
326
// the `compiler_builtins` crate sets `codegen-units = 10000` and it's
326
327
// critical they aren't merged. Also, some tests use explicit small values
327
328
// and likewise won't work if small CGUs are merged.
328
- while codegen_units. len ( ) > cx. tcx . sess . codegen_units ( ) . as_usize ( )
329
- || ( cx. tcx . sess . opts . incremental . is_none ( )
330
- && matches ! ( cx. tcx. sess. codegen_units( ) , CodegenUnits :: Default ( _) )
331
- && codegen_units. len ( ) > 1
332
- && codegen_units. iter ( ) . any ( |cgu| cgu. size_estimate ( ) < NON_INCR_MIN_CGU_SIZE ) )
333
- {
329
+ //eprintln!("-----");
330
+ loop {
331
+ // njn: where to put this?
334
332
// Sort small cgus to the back.
335
333
codegen_units. sort_by_cached_key ( |cgu| cmp:: Reverse ( cgu. size_estimate ( ) ) ) ;
336
334
335
+ //eprintln!("cgus: {:?}", codegen_units.iter().map(|cgu| cgu.size_estimate()).collect::<Vec<_>>());
336
+
337
+ let merge1 = codegen_units. len ( ) > cx. tcx . sess . codegen_units ( ) . as_usize ( ) ;
338
+
339
+ let merge2 = cx. tcx . sess . opts . incremental . is_none ( )
340
+ && matches ! ( cx. tcx. sess. codegen_units( ) , CodegenUnits :: Default ( _) )
341
+ && codegen_units. len ( ) >= 2
342
+ && codegen_units. iter ( ) . any ( |cgu| cgu. size_estimate ( ) < NON_INCR_MIN_CGU_SIZE ) ;
343
+
344
+ // njn: addition is an imperfect measure, could be overlap
345
+ let merge3 = cx. tcx . sess . opts . incremental . is_none ( )
346
+ && matches ! ( cx. tcx. sess. codegen_units( ) , CodegenUnits :: Default ( _) )
347
+ && codegen_units. len ( ) >= 3
348
+ && {
349
+ // eprintln!(
350
+ // "sz: {} >= {} + {}?",
351
+ // codegen_units[0].size_estimate(),
352
+ // codegen_units[codegen_units.len() - 2].size_estimate(),
353
+ // codegen_units[codegen_units.len() - 1].size_estimate());
354
+
355
+ codegen_units[ 0 ] . size_estimate ( )
356
+ >= codegen_units[ codegen_units. len ( ) - 2 ] . size_estimate ( )
357
+ + codegen_units[ codegen_units. len ( ) - 1 ] . size_estimate ( )
358
+ } ;
359
+
360
+ if !( merge1 || merge2 || merge3) {
361
+ break ;
362
+ }
363
+
337
364
let mut smallest = codegen_units. pop ( ) . unwrap ( ) ;
338
365
let second_smallest = codegen_units. last_mut ( ) . unwrap ( ) ;
339
366
367
+ // let sm_size = smallest.size_estimate();
368
+ // let sec_sm_size = second_smallest.size_estimate();
369
+
340
370
// Move the items from `smallest` to `second_smallest`. Some of them
341
371
// may be duplicate inlined items, in which case the destination CGU is
342
372
// unaffected. Recalculate size estimates afterwards.
343
373
second_smallest. items_mut ( ) . extend ( smallest. items_mut ( ) . drain ( ) ) ;
344
374
second_smallest. create_size_estimate ( cx. tcx ) ;
345
375
376
+ // eprintln!(
377
+ // "merge: {} {} {}: {} + {} -> {}",
378
+ // merge1,
379
+ // merge2,
380
+ // merge3,
381
+ // sec_sm_size,
382
+ // sm_size,
383
+ // second_smallest.size_estimate()
384
+ // );
385
+
346
386
// Record that `second_smallest` now contains all the stuff that was
347
387
// in `smallest` before.
348
388
let mut consumed_cgu_names = cgu_contents. remove ( & smallest. name ( ) ) . unwrap ( ) ;
0 commit comments