Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1986ad7

Browse files
committedMar 21, 2025
Auto merge of #128320 - saethlin:link-me-maybe, r=compiler-errors
Avoid no-op unlink+link dances in incr comp Incremental compilation scales quite poorly with the number of CGUs. This PR improves one reason for that. The incr comp process hard-links all the files from an old session into a new one, then it runs the backend, which may just hard-link the new session files into the output directory. Then codegen hard-links all the output files back to the new session directory. This PR (perhaps unimaginatively) fixes the silliness that ensues in the last step. The old `link_or_copy` implementation would be passed pairs of paths which are already the same inode, then it would blindly delete the destination and re-create the hard-link that it just deleted. This PR lets us skip both those operations. We don't skip the other two hard-links. `cargo +stage1 b && touch crates/core/main.rs && strace -cfw -elink,linkat,unlink,unlinkat cargo +stage1 b` before and then after on `ripgrep-13.0.0`: ``` % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 52.56 0.024950 25 978 485 unlink 34.38 0.016318 22 727 linkat 13.06 0.006200 24 249 unlinkat ------ ----------- ----------- --------- --------- ---------------- 100.00 0.047467 24 1954 485 total ``` ``` % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 42.83 0.014521 57 252 unlink 38.41 0.013021 26 486 linkat 18.77 0.006362 25 249 unlinkat ------ ----------- ----------- --------- --------- ---------------- 100.00 0.033904 34 987 total ``` This reduces the number of hard-links that are causing perf troubles, noted in rust-lang/rust#64291 and rust-lang/rust#137560
2 parents 3270d71 + af9f916 commit 1986ad7

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed
 

‎src/driver/aot.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,14 @@ impl OngoingCodegen {
103103
("o", &module_regular.object.as_ref().unwrap()),
104104
("asm.o", &module_global_asm.object.as_ref().unwrap()),
105105
],
106+
&[],
106107
)
107108
} else {
108109
rustc_incremental::copy_cgu_workproduct_to_incr_comp_cache_dir(
109110
sess,
110111
&module_regular.name,
111112
&[("o", &module_regular.object.as_ref().unwrap())],
113+
&[],
112114
)
113115
};
114116
if let Some((work_product_id, work_product)) = work_product {
@@ -381,6 +383,7 @@ fn emit_cgu(
381383
bytecode: None,
382384
assembly: None,
383385
llvm_ir: None,
386+
links_from_incr_cache: Vec::new(),
384387
}),
385388
existing_work_product: None,
386389
})
@@ -437,6 +440,7 @@ fn emit_module(
437440
bytecode: None,
438441
assembly: None,
439442
llvm_ir: None,
443+
links_from_incr_cache: Vec::new(),
440444
})
441445
}
442446

@@ -460,22 +464,23 @@ fn reuse_workproduct_for_cgu(
460464
err
461465
));
462466
}
467+
463468
let obj_out_global_asm =
464469
crate::global_asm::add_file_stem_postfix(obj_out_regular.clone(), ".asm");
465-
let has_global_asm = if let Some(asm_o) = work_product.saved_files.get("asm.o") {
470+
let source_file_global_asm = if let Some(asm_o) = work_product.saved_files.get("asm.o") {
466471
let source_file_global_asm = rustc_incremental::in_incr_comp_dir_sess(&tcx.sess, asm_o);
467472
if let Err(err) = rustc_fs_util::link_or_copy(&source_file_global_asm, &obj_out_global_asm)
468473
{
469474
return Err(format!(
470475
"unable to copy {} to {}: {}",
471-
source_file_regular.display(),
472-
obj_out_regular.display(),
476+
source_file_global_asm.display(),
477+
obj_out_global_asm.display(),
473478
err
474479
));
475480
}
476-
true
481+
Some(source_file_global_asm)
477482
} else {
478-
false
483+
None
479484
};
480485

481486
Ok(ModuleCodegenResult {
@@ -487,15 +492,17 @@ fn reuse_workproduct_for_cgu(
487492
bytecode: None,
488493
assembly: None,
489494
llvm_ir: None,
495+
links_from_incr_cache: vec![source_file_regular],
490496
},
491-
module_global_asm: has_global_asm.then(|| CompiledModule {
497+
module_global_asm: source_file_global_asm.map(|source_file| CompiledModule {
492498
name: cgu.name().to_string(),
493499
kind: ModuleKind::Regular,
494500
object: Some(obj_out_global_asm),
495501
dwarf_object: None,
496502
bytecode: None,
497503
assembly: None,
498504
llvm_ir: None,
505+
links_from_incr_cache: vec![source_file],
499506
}),
500507
existing_work_product: Some((cgu.work_product_id(), work_product)),
501508
})
@@ -637,6 +644,7 @@ fn emit_metadata_module(tcx: TyCtxt<'_>, metadata: &EncodedMetadata) -> Compiled
637644
bytecode: None,
638645
assembly: None,
639646
llvm_ir: None,
647+
links_from_incr_cache: Vec::new(),
640648
}
641649
}
642650

0 commit comments

Comments
 (0)
Failed to load comments.