Skip to content

Commit 691e953

Browse files
committed
Save/restore more items in cache with incremental compilation
1 parent 914e43a commit 691e953

File tree

8 files changed

+97
-5
lines changed

8 files changed

+97
-5
lines changed

compiler/rustc_codegen_cranelift/src/driver/aot.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ fn emit_cgu(
341341
object: Some(global_asm_object_file),
342342
dwarf_object: None,
343343
bytecode: None,
344+
assembly: None,
345+
llvm_ir: None,
344346
}),
345347
existing_work_product: None,
346348
})
@@ -378,7 +380,15 @@ fn emit_module(
378380

379381
prof.artifact_size("object_file", &*name, file.metadata().unwrap().len());
380382

381-
Ok(CompiledModule { name, kind, object: Some(tmp_file), dwarf_object: None, bytecode: None })
383+
Ok(CompiledModule {
384+
name,
385+
kind,
386+
object: Some(tmp_file),
387+
dwarf_object: None,
388+
bytecode: None,
389+
assembly: None,
390+
llvm_ir: None,
391+
})
382392
}
383393

384394
fn reuse_workproduct_for_cgu(
@@ -426,13 +436,17 @@ fn reuse_workproduct_for_cgu(
426436
object: Some(obj_out_regular),
427437
dwarf_object: None,
428438
bytecode: None,
439+
assembly: None,
440+
llvm_ir: None,
429441
},
430442
module_global_asm: has_global_asm.then(|| CompiledModule {
431443
name: cgu.name().to_string(),
432444
kind: ModuleKind::Regular,
433445
object: Some(obj_out_global_asm),
434446
dwarf_object: None,
435447
bytecode: None,
448+
assembly: None,
449+
llvm_ir: None,
436450
}),
437451
existing_work_product: Some((cgu.work_product_id(), work_product)),
438452
})
@@ -678,6 +692,8 @@ pub(crate) fn run_aot(
678692
object: Some(tmp_file),
679693
dwarf_object: None,
680694
bytecode: None,
695+
assembly: None,
696+
llvm_ir: None,
681697
})
682698
} else {
683699
None

compiler/rustc_codegen_gcc/src/back/write.rs

+2
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ pub(crate) unsafe fn codegen(
158158
config.emit_obj != EmitObj::None,
159159
cgcx.target_can_use_split_dwarf && cgcx.split_debuginfo == SplitDebuginfo::Unpacked,
160160
config.emit_bc,
161+
config.emit_asm,
162+
config.emit_ir,
161163
&cgcx.output_filenames,
162164
))
163165
}

compiler/rustc_codegen_llvm/src/back/write.rs

+2
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,8 @@ pub(crate) unsafe fn codegen(
880880
config.emit_obj != EmitObj::None,
881881
dwarf_object_emitted,
882882
config.emit_bc,
883+
config.emit_asm,
884+
config.emit_ir,
883885
&cgcx.output_filenames,
884886
))
885887
}

compiler/rustc_codegen_ssa/src/back/write.rs

+27-3
Original file line numberDiff line numberDiff line change
@@ -528,12 +528,20 @@ fn copy_all_cgu_workproducts_to_incr_comp_cache_dir(
528528
for module in compiled_modules.modules.iter().filter(|m| m.kind == ModuleKind::Regular) {
529529
let mut files = Vec::new();
530530
if let Some(object_file_path) = &module.object {
531-
files.push(("o", object_file_path.as_path()));
531+
files.push((OutputType::Object.extension(), object_file_path.as_path()));
532532
}
533533
if let Some(dwarf_object_file_path) = &module.dwarf_object {
534534
files.push(("dwo", dwarf_object_file_path.as_path()));
535535
}
536-
536+
if let Some(path) = &module.assembly {
537+
files.push((OutputType::Assembly.extension(), path.as_path()));
538+
}
539+
if let Some(path) = &module.llvm_ir {
540+
files.push((OutputType::LlvmAssembly.extension(), path.as_path()));
541+
}
542+
if let Some(path) = &module.bytecode {
543+
files.push((OutputType::Bitcode.extension(), path.as_path()));
544+
}
537545
if let Some((id, product)) =
538546
copy_cgu_workproduct_to_incr_comp_cache_dir(sess, &module.name, files.as_slice())
539547
{
@@ -937,12 +945,28 @@ fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
937945
load_from_incr_comp_dir(dwarf_obj_out, saved_dwarf_object_file)
938946
});
939947

948+
let load_from_incr_cache = |perform, output_type: OutputType| {
949+
if perform {
950+
let saved_file = module.source.saved_files.get(output_type.extension())?;
951+
let output_path = cgcx.output_filenames.temp_path(output_type, Some(&module.name));
952+
load_from_incr_comp_dir(output_path, &saved_file)
953+
} else {
954+
None
955+
}
956+
};
957+
958+
let assembly = load_from_incr_cache(module_config.emit_asm, OutputType::Assembly);
959+
let llvm_ir = load_from_incr_cache(module_config.emit_ir, OutputType::LlvmAssembly);
960+
let bytecode = load_from_incr_cache(module_config.emit_bc, OutputType::Bitcode);
961+
940962
WorkItemResult::Finished(CompiledModule {
941963
name: module.name,
942964
kind: ModuleKind::Regular,
943965
object,
944966
dwarf_object,
945-
bytecode: None,
967+
bytecode,
968+
assembly,
969+
llvm_ir,
946970
})
947971
}
948972

compiler/rustc_codegen_ssa/src/base.rs

+2
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,8 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
656656
object: Some(file_name),
657657
dwarf_object: None,
658658
bytecode: None,
659+
assembly: None,
660+
llvm_ir: None,
659661
}
660662
})
661663
});

compiler/rustc_codegen_ssa/src/lib.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,26 @@ impl<M> ModuleCodegen<M> {
7979
emit_obj: bool,
8080
emit_dwarf_obj: bool,
8181
emit_bc: bool,
82+
emit_asm: bool,
83+
emit_ir: bool,
8284
outputs: &OutputFilenames,
8385
) -> CompiledModule {
8486
let object = emit_obj.then(|| outputs.temp_path(OutputType::Object, Some(&self.name)));
8587
let dwarf_object = emit_dwarf_obj.then(|| outputs.temp_path_dwo(Some(&self.name)));
8688
let bytecode = emit_bc.then(|| outputs.temp_path(OutputType::Bitcode, Some(&self.name)));
89+
let assembly = emit_asm.then(|| outputs.temp_path(OutputType::Assembly, Some(&self.name)));
90+
let llvm_ir =
91+
emit_ir.then(|| outputs.temp_path(OutputType::LlvmAssembly, Some(&self.name)));
8792

88-
CompiledModule { name: self.name.clone(), kind: self.kind, object, dwarf_object, bytecode }
93+
CompiledModule {
94+
name: self.name.clone(),
95+
kind: self.kind,
96+
object,
97+
dwarf_object,
98+
bytecode,
99+
assembly,
100+
llvm_ir,
101+
}
89102
}
90103
}
91104

@@ -96,6 +109,8 @@ pub struct CompiledModule {
96109
pub object: Option<PathBuf>,
97110
pub dwarf_object: Option<PathBuf>,
98111
pub bytecode: Option<PathBuf>,
112+
pub assembly: Option<PathBuf>, // --emit=asm
113+
pub llvm_ir: Option<PathBuf>, // --emit=llvm-ir, llvm-bc is in bytecode
99114
}
100115

101116
pub struct CachedModuleCodegen {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![crate_name = "foo"]
2+
3+
#[inline(never)]
4+
pub fn add(a: u32, b: u32) -> u32 {
5+
a + b
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// rustc should be able to emit required files (asm, llvm-*, etc) during incremental
2+
// compilation on the first pass by running the code gen as well as on subsequent runs -
3+
// extracting them from the cache
4+
//
5+
// Fixes: rust-lang/rust#89149
6+
// Fixes: rust-lang/rust#88829
7+
// Also see discussion at
8+
// <https://internals.rust-lang.org/t/interaction-between-incremental-compilation-and-emit/20551>
9+
10+
extern crate run_make_support;
11+
12+
use run_make_support::{rustc, tmp_dir};
13+
14+
fn main() {
15+
let inc_dir = tmp_dir();
16+
17+
for _ in 0..=1 {
18+
rustc()
19+
.input("lib.rs")
20+
.crate_type("lib")
21+
.emit("obj,asm,dep-info,link,mir,llvm-ir,llvm-bc")
22+
.incremental(&inc_dir)
23+
.run();
24+
}
25+
}

0 commit comments

Comments
 (0)