From 729b649fb0d20880a05ccae925b4ef5fa04c8382 Mon Sep 17 00:00:00 2001 From: Michael Baikov Date: Fri, 29 Mar 2024 20:47:55 -0400 Subject: [PATCH] Save more items into incremental cache Without it compiler skips codengen stage and generating those intermediate byproducts and then fails when it comes to copying them to stable locations: export RUSTFLAGS=--emit=asm cargo new --lib foo && cd foo cargo build touch src/lib.rs cargo build --- .../rustc_codegen_cranelift/src/driver/aot.rs | 18 ++++++- compiler/rustc_codegen_gcc/src/back/write.rs | 2 + compiler/rustc_codegen_llvm/src/back/write.rs | 2 + compiler/rustc_codegen_ssa/src/back/write.rs | 47 ++++++++++++++++++- compiler/rustc_codegen_ssa/src/base.rs | 2 + compiler/rustc_codegen_ssa/src/lib.rs | 19 +++++++- tests/run-make/asm-incr-cache/lib.rs | 6 +++ tests/run-make/asm-incr-cache/rmake.rs | 16 +++++++ 8 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 tests/run-make/asm-incr-cache/lib.rs create mode 100644 tests/run-make/asm-incr-cache/rmake.rs diff --git a/compiler/rustc_codegen_cranelift/src/driver/aot.rs b/compiler/rustc_codegen_cranelift/src/driver/aot.rs index 75268341a4fe0..e8c96486041b1 100644 --- a/compiler/rustc_codegen_cranelift/src/driver/aot.rs +++ b/compiler/rustc_codegen_cranelift/src/driver/aot.rs @@ -341,6 +341,8 @@ fn emit_cgu( object: Some(global_asm_object_file), dwarf_object: None, bytecode: None, + assembly: None, + llvm_ir: None, }), existing_work_product: None, }) @@ -378,7 +380,15 @@ fn emit_module( prof.artifact_size("object_file", &*name, file.metadata().unwrap().len()); - Ok(CompiledModule { name, kind, object: Some(tmp_file), dwarf_object: None, bytecode: None }) + Ok(CompiledModule { + name, + kind, + object: Some(tmp_file), + dwarf_object: None, + bytecode: None, + assembly: None, + llvm_ir: None, + }) } fn reuse_workproduct_for_cgu( @@ -426,6 +436,8 @@ fn reuse_workproduct_for_cgu( object: Some(obj_out_regular), dwarf_object: None, bytecode: None, + assembly: None, + llvm_ir: None, }, module_global_asm: has_global_asm.then(|| CompiledModule { name: cgu.name().to_string(), @@ -433,6 +445,8 @@ fn reuse_workproduct_for_cgu( object: Some(obj_out_global_asm), dwarf_object: None, bytecode: None, + assembly: None, + llvm_ir: None, }), existing_work_product: Some((cgu.work_product_id(), work_product)), }) @@ -678,6 +692,8 @@ pub(crate) fn run_aot( object: Some(tmp_file), dwarf_object: None, bytecode: None, + assembly: None, + llvm_ir: None, }) } else { None diff --git a/compiler/rustc_codegen_gcc/src/back/write.rs b/compiler/rustc_codegen_gcc/src/back/write.rs index 76a619a1af782..3ea5be1ee562b 100644 --- a/compiler/rustc_codegen_gcc/src/back/write.rs +++ b/compiler/rustc_codegen_gcc/src/back/write.rs @@ -158,6 +158,8 @@ pub(crate) unsafe fn codegen( config.emit_obj != EmitObj::None, cgcx.target_can_use_split_dwarf && cgcx.split_debuginfo == SplitDebuginfo::Unpacked, config.emit_bc, + config.emit_asm, + config.emit_ir, &cgcx.output_filenames, )) } diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index 4efea66a7f1e1..8dafee4fdb28c 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -880,6 +880,8 @@ pub(crate) unsafe fn codegen( config.emit_obj != EmitObj::None, dwarf_object_emitted, config.emit_bc, + config.emit_asm, + config.emit_ir, &cgcx.output_filenames, )) } diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index b7bcaac3b18f6..eb9eaaeb17452 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -533,7 +533,15 @@ fn copy_all_cgu_workproducts_to_incr_comp_cache_dir( if let Some(dwarf_object_file_path) = &module.dwarf_object { files.push(("dwo", dwarf_object_file_path.as_path())); } - + if let Some(path) = &module.assembly { + files.push(("s", path.as_path())); + } + if let Some(path) = &module.llvm_ir { + files.push(("ll", path.as_path())); + } + if let Some(path) = &module.bytecode { + files.push(("bc", path.as_path())); + } if let Some((id, product)) = copy_cgu_workproduct_to_incr_comp_cache_dir(sess, &module.name, files.as_slice()) { @@ -937,12 +945,47 @@ fn execute_copy_from_cache_work_item( load_from_incr_comp_dir(dwarf_obj_out, saved_dwarf_object_file) }); + let assembly = module_config + .emit_asm + .then(|| { + module.source.saved_files.get("s").as_ref().and_then(|saved_asm_file| { + let output_path = + cgcx.output_filenames.temp_path(OutputType::Assembly, Some(&module.name)); + load_from_incr_comp_dir(output_path, &saved_asm_file) + }) + }) + .flatten(); + + let llvm_ir = module_config + .emit_ir + .then(|| { + module.source.saved_files.get("ll").as_ref().and_then(|saved_ir_file| { + let output_path = + cgcx.output_filenames.temp_path(OutputType::LlvmAssembly, Some(&module.name)); + load_from_incr_comp_dir(output_path, &saved_ir_file) + }) + }) + .flatten(); + + let bytecode = module_config + .emit_bc + .then(|| { + module.source.saved_files.get("bc").as_ref().and_then(|saved_bc_file| { + let output_path = + cgcx.output_filenames.temp_path(OutputType::Bitcode, Some(&module.name)); + load_from_incr_comp_dir(output_path, &saved_bc_file) + }) + }) + .flatten(); + WorkItemResult::Finished(CompiledModule { name: module.name, kind: ModuleKind::Regular, object, dwarf_object, - bytecode: None, + bytecode, + assembly, + llvm_ir, }) } diff --git a/compiler/rustc_codegen_ssa/src/base.rs b/compiler/rustc_codegen_ssa/src/base.rs index f7f2bfca838ea..ae438fc6426f3 100644 --- a/compiler/rustc_codegen_ssa/src/base.rs +++ b/compiler/rustc_codegen_ssa/src/base.rs @@ -629,6 +629,8 @@ pub fn codegen_crate( object: Some(file_name), dwarf_object: None, bytecode: None, + assembly: None, + llvm_ir: None, } }) }); diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index 9be8dcf166d40..d791add9c2ef6 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -79,13 +79,26 @@ impl ModuleCodegen { emit_obj: bool, emit_dwarf_obj: bool, emit_bc: bool, + emit_asm: bool, + emit_ir: bool, outputs: &OutputFilenames, ) -> CompiledModule { let object = emit_obj.then(|| outputs.temp_path(OutputType::Object, Some(&self.name))); let dwarf_object = emit_dwarf_obj.then(|| outputs.temp_path_dwo(Some(&self.name))); let bytecode = emit_bc.then(|| outputs.temp_path(OutputType::Bitcode, Some(&self.name))); + let assembly = emit_asm.then(|| outputs.temp_path(OutputType::Assembly, Some(&self.name))); + let llvm_ir = + emit_ir.then(|| outputs.temp_path(OutputType::LlvmAssembly, Some(&self.name))); - CompiledModule { name: self.name.clone(), kind: self.kind, object, dwarf_object, bytecode } + CompiledModule { + name: self.name.clone(), + kind: self.kind, + object, + dwarf_object, + bytecode, + assembly, + llvm_ir, + } } } @@ -96,6 +109,10 @@ pub struct CompiledModule { pub object: Option, pub dwarf_object: Option, pub bytecode: Option, + + /// items created to satisfy --emit + pub assembly: Option, // --emit=asm + pub llvm_ir: Option, // --emit=llvm-ir, llvm-bc is in bytecode } pub struct CachedModuleCodegen { diff --git a/tests/run-make/asm-incr-cache/lib.rs b/tests/run-make/asm-incr-cache/lib.rs new file mode 100644 index 0000000000000..fa4048594e369 --- /dev/null +++ b/tests/run-make/asm-incr-cache/lib.rs @@ -0,0 +1,6 @@ +#![crate_name = "foo"] + +#[inline(never)] +pub fn add(a: u32, b: u32) -> u32 { + a + b +} diff --git a/tests/run-make/asm-incr-cache/rmake.rs b/tests/run-make/asm-incr-cache/rmake.rs new file mode 100644 index 0000000000000..c7baab4d0c2fa --- /dev/null +++ b/tests/run-make/asm-incr-cache/rmake.rs @@ -0,0 +1,16 @@ +extern crate run_make_support; + +use run_make_support::{rustc, tmp_dir}; + +fn main() { + let inc_dir = tmp_dir().join("asm-incr-cache"); + + for _ in 0..=1 { + rustc() + .input("lib.rs") + .arg("--emit=obj,asm,dep-info,link,mir,llvm-ir,llvm-bc") + .arg("--crate-type=lib") + .arg(format!("-Cincremental={inc_dir:?}").as_str()) + .run(); + } +}