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 48b36c9

Browse files
committedMar 21, 2025
Auto merge of rust-lang#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#64291 and rust-lang#137560
2 parents be73c1f + e21502c commit 48b36c9

File tree

6 files changed

+57
-26
lines changed

6 files changed

+57
-26
lines changed
 

‎compiler/rustc_codegen_cranelift/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

‎compiler/rustc_codegen_ssa/src/back/write.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,12 @@ fn copy_all_cgu_workproducts_to_incr_comp_cache_dir(
546546
if let Some(path) = &module.bytecode {
547547
files.push((OutputType::Bitcode.extension(), path.as_path()));
548548
}
549-
if let Some((id, product)) =
550-
copy_cgu_workproduct_to_incr_comp_cache_dir(sess, &module.name, files.as_slice())
551-
{
549+
if let Some((id, product)) = copy_cgu_workproduct_to_incr_comp_cache_dir(
550+
sess,
551+
&module.name,
552+
files.as_slice(),
553+
&module.links_from_incr_cache,
554+
) {
552555
work_products.insert(id, product);
553556
}
554557
}
@@ -934,7 +937,9 @@ fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
934937
) -> WorkItemResult<B> {
935938
let incr_comp_session_dir = cgcx.incr_comp_session_dir.as_ref().unwrap();
936939

937-
let load_from_incr_comp_dir = |output_path: PathBuf, saved_path: &str| {
940+
let mut links_from_incr_cache = Vec::new();
941+
942+
let mut load_from_incr_comp_dir = |output_path: PathBuf, saved_path: &str| {
938943
let source_file = in_incr_comp_dir(incr_comp_session_dir, saved_path);
939944
debug!(
940945
"copying preexisting module `{}` from {:?} to {}",
@@ -943,7 +948,10 @@ fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
943948
output_path.display()
944949
);
945950
match link_or_copy(&source_file, &output_path) {
946-
Ok(_) => Some(output_path),
951+
Ok(_) => {
952+
links_from_incr_cache.push(source_file);
953+
Some(output_path)
954+
}
947955
Err(error) => {
948956
cgcx.create_dcx().handle().emit_err(errors::CopyPathBuf {
949957
source_file,
@@ -966,7 +974,7 @@ fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
966974
load_from_incr_comp_dir(dwarf_obj_out, saved_dwarf_object_file)
967975
});
968976

969-
let load_from_incr_cache = |perform, output_type: OutputType| {
977+
let mut load_from_incr_cache = |perform, output_type: OutputType| {
970978
if perform {
971979
let saved_file = module.source.saved_files.get(output_type.extension())?;
972980
let output_path = cgcx.output_filenames.temp_path(output_type, Some(&module.name));
@@ -986,6 +994,7 @@ fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
986994
}
987995

988996
WorkItemResult::Finished(CompiledModule {
997+
links_from_incr_cache,
989998
name: module.name,
990999
kind: ModuleKind::Regular,
9911000
object,

‎compiler/rustc_codegen_ssa/src/base.rs

+1
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
658658
bytecode: None,
659659
assembly: None,
660660
llvm_ir: None,
661+
links_from_incr_cache: Vec::new(),
661662
}
662663
})
663664
});

‎compiler/rustc_codegen_ssa/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ impl<M> ModuleCodegen<M> {
122122
bytecode,
123123
assembly,
124124
llvm_ir,
125+
links_from_incr_cache: Vec::new(),
125126
}
126127
}
127128
}
@@ -135,6 +136,7 @@ pub struct CompiledModule {
135136
pub bytecode: Option<PathBuf>,
136137
pub assembly: Option<PathBuf>, // --emit=asm
137138
pub llvm_ir: Option<PathBuf>, // --emit=llvm-ir, llvm-bc is in bytecode
139+
pub links_from_incr_cache: Vec<PathBuf>,
138140
}
139141

140142
impl CompiledModule {

‎compiler/rustc_fs_util/src/lib.rs

+19-13
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,31 @@ pub enum LinkOrCopy {
5555
Copy,
5656
}
5757

58-
/// Copies `p` into `q`, preferring to use hard-linking if possible. If
59-
/// `q` already exists, it is removed first.
58+
/// Copies `p` into `q`, preferring to use hard-linking if possible.
6059
/// The result indicates which of the two operations has been performed.
6160
pub fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(p: P, q: Q) -> io::Result<LinkOrCopy> {
61+
// Creating a hard-link will fail if the destination path already exists. We could defensively
62+
// call remove_file in this function, but that pessimizes callers who can avoid such calls.
63+
// Incremental compilation calls this function a lot, and is able to avoid calls that
64+
// would fail the first hard_link attempt.
65+
6266
let p = p.as_ref();
6367
let q = q.as_ref();
64-
match fs::remove_file(q) {
65-
Ok(()) => (),
66-
Err(err) if err.kind() == io::ErrorKind::NotFound => (),
67-
Err(err) => return Err(err),
68-
}
6968

70-
match fs::hard_link(p, q) {
71-
Ok(()) => Ok(LinkOrCopy::Link),
72-
Err(_) => match fs::copy(p, q) {
73-
Ok(_) => Ok(LinkOrCopy::Copy),
74-
Err(e) => Err(e),
75-
},
69+
let err = match fs::hard_link(p, q) {
70+
Ok(()) => return Ok(LinkOrCopy::Link),
71+
Err(err) => err,
72+
};
73+
74+
if err.kind() == io::ErrorKind::AlreadyExists {
75+
fs::remove_file(q)?;
76+
if fs::hard_link(p, q).is_ok() {
77+
return Ok(LinkOrCopy::Link);
78+
}
7679
}
80+
81+
// Hard linking failed, fall back to copying.
82+
fs::copy(p, q).map(|_| LinkOrCopy::Copy)
7783
}
7884

7985
#[cfg(any(unix, all(target_os = "wasi", target_env = "p1")))]

‎compiler/rustc_incremental/src/persist/work_product.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! [work products]: WorkProduct
44
55
use std::fs as std_fs;
6-
use std::path::Path;
6+
use std::path::{Path, PathBuf};
77

88
use rustc_data_structures::unord::UnordMap;
99
use rustc_fs_util::link_or_copy;
@@ -20,6 +20,7 @@ pub fn copy_cgu_workproduct_to_incr_comp_cache_dir(
2020
sess: &Session,
2121
cgu_name: &str,
2222
files: &[(&'static str, &Path)],
23+
known_links: &[PathBuf],
2324
) -> Option<(WorkProductId, WorkProduct)> {
2425
debug!(?cgu_name, ?files);
2526
sess.opts.incremental.as_ref()?;
@@ -28,6 +29,10 @@ pub fn copy_cgu_workproduct_to_incr_comp_cache_dir(
2829
for (ext, path) in files {
2930
let file_name = format!("{cgu_name}.{ext}");
3031
let path_in_incr_dir = in_incr_comp_dir_sess(sess, &file_name);
32+
if known_links.contains(&path_in_incr_dir) {
33+
let _ = saved_files.insert(ext.to_string(), file_name);
34+
continue;
35+
}
3136
match link_or_copy(path, &path_in_incr_dir) {
3237
Ok(_) => {
3338
let _ = saved_files.insert(ext.to_string(), file_name);

0 commit comments

Comments
 (0)
Failed to load comments.