Skip to content

Commit cae7c76

Browse files
committed
Avoid no-op unlink+link dances in incr comp
1 parent 9af8985 commit cae7c76

File tree

6 files changed

+50
-21
lines changed

6 files changed

+50
-21
lines changed

Diff for: compiler/rustc_codegen_cranelift/src/driver/aot.rs

+7-1
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

@@ -487,6 +491,7 @@ fn reuse_workproduct_for_cgu(
487491
bytecode: None,
488492
assembly: None,
489493
llvm_ir: None,
494+
links_from_incr_cache: Vec::new(),
490495
},
491496
module_global_asm: has_global_asm.then(|| CompiledModule {
492497
name: cgu.name().to_string(),
@@ -496,6 +501,7 @@ fn reuse_workproduct_for_cgu(
496501
bytecode: None,
497502
assembly: None,
498503
llvm_ir: None,
504+
links_from_incr_cache: Vec::new(),
499505
}),
500506
existing_work_product: Some((cgu.work_product_id(), work_product)),
501507
})
@@ -637,6 +643,7 @@ fn emit_metadata_module(tcx: TyCtxt<'_>, metadata: &EncodedMetadata) -> Compiled
637643
bytecode: None,
638644
assembly: None,
639645
llvm_ir: None,
646+
links_from_incr_cache: Vec::new(),
640647
}
641648
}
642649

@@ -745,7 +752,6 @@ pub(crate) fn run_aot(
745752

746753
let metadata_module =
747754
if need_metadata_module { Some(emit_metadata_module(tcx, &metadata)) } else { None };
748-
749755
Box::new(OngoingCodegen {
750756
modules,
751757
allocator_module,

Diff for: compiler/rustc_codegen_ssa/src/back/write.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,12 @@ fn copy_all_cgu_workproducts_to_incr_comp_cache_dir(
540540
if let Some(path) = &module.bytecode {
541541
files.push((OutputType::Bitcode.extension(), path.as_path()));
542542
}
543-
if let Some((id, product)) =
544-
copy_cgu_workproduct_to_incr_comp_cache_dir(sess, &module.name, files.as_slice())
545-
{
543+
if let Some((id, product)) = copy_cgu_workproduct_to_incr_comp_cache_dir(
544+
sess,
545+
&module.name,
546+
files.as_slice(),
547+
&module.links_from_incr_cache,
548+
) {
546549
work_products.insert(id, product);
547550
}
548551
}
@@ -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,

Diff for: compiler/rustc_codegen_ssa/src/base.rs

+1
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
656656
bytecode: None,
657657
assembly: None,
658658
llvm_ir: None,
659+
links_from_incr_cache: Vec::new(),
659660
}
660661
})
661662
});

Diff for: compiler/rustc_codegen_ssa/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ impl<M> ModuleCodegen<M> {
102102
bytecode,
103103
assembly,
104104
llvm_ir,
105+
links_from_incr_cache: Vec::new(),
105106
}
106107
}
107108
}
@@ -115,6 +116,7 @@ pub struct CompiledModule {
115116
pub bytecode: Option<PathBuf>,
116117
pub assembly: Option<PathBuf>, // --emit=asm
117118
pub llvm_ir: Option<PathBuf>, // --emit=llvm-ir, llvm-bc is in bytecode
119+
pub links_from_incr_cache: Vec<PathBuf>,
118120
}
119121

120122
impl CompiledModule {

Diff for: 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")))]

Diff for: 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)