Skip to content

Commit 2814198

Browse files
committed
[WIP] Per function mir -> clif ir caching
FIXME recompiling without changes results in undefined symbols
1 parent b7ce29e commit 2814198

File tree

4 files changed

+77
-37
lines changed

4 files changed

+77
-37
lines changed

build_system/build_sysroot.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,12 @@ fn build_clif_sysroot_for_triple(
216216
}
217217

218218
// Build sysroot
219-
let mut rustflags =
220-
vec!["-Zforce-unstable-if-unmarked".to_owned(), "-Zincremental-verify-ich".to_owned()];
219+
let mut rustflags = vec![
220+
"-Zforce-unstable-if-unmarked".to_owned(),
221+
"-Zincremental-verify-ich".to_owned(),
222+
"-Ccodegen-units=1".to_owned(),
223+
"-Csymbol-mangling-version=v0".to_owned(),
224+
];
221225
if !panic_unwind_support {
222226
rustflags.push("-Cpanic=abort".to_owned());
223227
}

src/driver/aot.rs

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ use std::path::{Path, PathBuf};
88
use std::sync::Arc;
99
use std::thread::JoinHandle;
1010

11+
use cranelift_codegen::incremental_cache::CacheKvStore;
1112
use cranelift_object::{ObjectBuilder, ObjectModule};
1213
use rustc_codegen_ssa::assert_module_sources::CguReuse;
1314
use rustc_codegen_ssa::back::link::ensure_removed;
1415
use rustc_codegen_ssa::base::determine_cgu_reuse;
1516
use rustc_codegen_ssa::{
1617
CodegenResults, CompiledModule, CrateInfo, ModuleKind, errors as ssa_errors,
1718
};
19+
use rustc_data_structures::fingerprint::Fingerprint;
1820
use rustc_data_structures::profiling::SelfProfilerRef;
1921
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2022
use rustc_data_structures::sync::{IntoDynSyncSend, par_map};
@@ -26,14 +28,15 @@ use rustc_middle::mir::mono::{
2628
};
2729
use rustc_session::Session;
2830
use rustc_session::config::{OutFileName, OutputFilenames, OutputType};
31+
use rustc_span::sym;
2932

3033
use crate::base::CodegenedFunction;
3134
use crate::concurrency_limiter::{ConcurrencyLimiter, ConcurrencyLimiterToken};
3235
use crate::debuginfo::TypeDebugContext;
3336
use crate::global_asm::{GlobalAsmConfig, GlobalAsmContext};
3437
use crate::prelude::*;
3538
use crate::serializable_module::SerializableModule;
36-
use crate::unwind_module::UnwindModule;
39+
use crate::unwind_module::{FileCache, UnwindModule};
3740

3841
fn disable_incr_cache() -> bool {
3942
env::var("CG_CLIF_DISABLE_INCR_CACHE").as_deref() == Ok("1")
@@ -513,7 +516,7 @@ fn codegen_cgu_content(
513516
tcx: TyCtxt<'_>,
514517
module: &mut dyn Module,
515518
cgu_name: rustc_span::Symbol,
516-
) -> (Vec<(SerializableModule, String)>, String) {
519+
) -> (Vec<SerializableModule>, String) {
517520
let _timer = tcx.prof.generic_activity_with_arg("codegen cgu", cgu_name.as_str());
518521

519522
let cgu = tcx.codegen_unit(cgu_name);
@@ -524,6 +527,7 @@ fn codegen_cgu_content(
524527
//let mut type_dbg = TypeDebugContext::default();
525528
super::predefine_mono_items(tcx, module, &mono_items);
526529
let mut codegened_functions = vec![];
530+
let isa = crate::build_isa(tcx.sess, false);
527531
for (mono_item, item_data) in mono_items {
528532
match mono_item {
529533
MonoItem::Fn(instance) => {
@@ -545,40 +549,64 @@ fn codegen_cgu_content(
545549
continue;
546550
}
547551

548-
/*let dep_node = mono_item.codegen_dep_node(tcx);
549-
let ((ser_module, global_asm), _) = tcx.dep_graph.with_task(
552+
let dep_node = mono_item.codegen_dep_node(tcx);
553+
let mut hasher = StableHasher::new();
554+
tcx.with_stable_hashing_context(|mut hcx| {
555+
instance.hash_stable(&mut hcx, &mut hasher);
556+
});
557+
let cache_key: Fingerprint = hasher.finish();
558+
559+
if tcx.dep_graph.is_fully_enabled() && tcx.try_mark_green(&dep_node) {
560+
let data = FileCache.get(&cache_key.to_le_bytes()).unwrap();
561+
codegened_functions.push(SerializableModule::deserialize(&data, isa.clone()));
562+
continue;
563+
};
564+
565+
let (ser_module, _) = tcx.dep_graph.with_task(
550566
dep_node,
551567
tcx,
552-
(cgu.name(), instance),
553-
|tcx, (cgu_name, instance)| {*/
554-
let mut ser_module = SerializableModule::new(crate::build_isa(tcx.sess, false));
555-
let codegened_function = crate::base::codegen_fn(
556-
tcx,
557-
cgu_name,
558-
//debug_context.as_mut(),
559-
//&mut type_dbg,
560-
Function::new(),
561-
&mut ser_module,
562568
instance,
563-
);
564-
let mut cached_context = Context::new();
565-
let mut global_asm = String::new();
566-
crate::base::compile_fn(
567-
&tcx.prof,
568-
&tcx.output_filenames(()),
569-
crate::pretty_clif::should_write_ir(tcx.sess),
570-
&mut cached_context,
571-
&mut ser_module,
572-
//debug_context.as_mut(),
573-
&mut global_asm,
574-
codegened_function,
575-
);
576-
/*(ser_module, global_asm)
569+
|tcx, instance| {
570+
let mut ser_module =
571+
SerializableModule::new(crate::build_isa(tcx.sess, false));
572+
let codegened_function = crate::base::codegen_fn(
573+
tcx,
574+
tcx.crate_name(LOCAL_CRATE),
575+
//debug_context.as_mut(),
576+
//&mut type_dbg,
577+
Function::new(),
578+
&mut ser_module,
579+
instance,
580+
);
581+
let mut cached_context = Context::new();
582+
let mut global_asm = String::new();
583+
crate::base::compile_fn(
584+
&tcx.prof,
585+
&tcx.output_filenames(()),
586+
crate::pretty_clif::should_write_ir(tcx.sess),
587+
&mut cached_context,
588+
&mut ser_module,
589+
//debug_context.as_mut(),
590+
&mut global_asm,
591+
codegened_function,
592+
);
593+
ser_module.add_global_asm(&global_asm);
594+
595+
let mut hasher = StableHasher::new();
596+
tcx.with_stable_hashing_context(|mut hcx| {
597+
instance.hash_stable(&mut hcx, &mut hasher);
598+
});
599+
let cache_key: Fingerprint = hasher.finish();
600+
601+
let data = ser_module.serialize();
602+
FileCache.insert(&cache_key.to_le_bytes(), data);
603+
604+
ser_module
577605
},
578606
Some(rustc_middle::dep_graph::hash_result),
579-
);*/
607+
);
580608

581-
codegened_functions.push((ser_module, global_asm));
609+
codegened_functions.push(ser_module);
582610
}
583611
MonoItem::Static(def_id) => {
584612
let data_id = crate::constant::codegen_static(tcx, module, def_id);
@@ -624,8 +652,8 @@ fn module_codegen(
624652
profiler.clone(),
625653
)));
626654

627-
for (codegened_func, asm) in codegened_functions {
628-
codegened_func.apply_to(&mut module);
655+
for codegened_func in codegened_functions {
656+
let asm = codegened_func.apply_to(&mut module);
629657
global_asm.push_str(&asm);
630658
}
631659
});

src/serializable_module.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct SerializableModuleInner {
2323
declarations: ModuleDeclarations,
2424
functions: BTreeMap<FuncId, Function>,
2525
data_objects: BTreeMap<DataId, DataDescription>,
26+
global_asm: String,
2627
}
2728

2829
impl<CTX> HashStable<CTX> for SerializableModule {
@@ -44,6 +45,7 @@ impl SerializableModule {
4445
declarations: ModuleDeclarations::default(),
4546
functions: BTreeMap::new(),
4647
data_objects: BTreeMap::new(),
48+
global_asm: String::new(),
4749
},
4850
serialized: OnceLock::new(),
4951
}
@@ -62,7 +64,11 @@ impl SerializableModule {
6264
}
6365
}
6466

65-
pub(crate) fn apply_to(self, module: &mut dyn Module) {
67+
pub(crate) fn add_global_asm(&mut self, asm: &str) {
68+
self.inner.global_asm.push_str(asm);
69+
}
70+
71+
pub(crate) fn apply_to(self, module: &mut dyn Module) -> String {
6672
let mut function_map: SecondaryMap<FuncId, Option<FuncId>> = SecondaryMap::new();
6773
let mut data_object_map: SecondaryMap<DataId, Option<DataId>> = SecondaryMap::new();
6874

@@ -165,7 +171,7 @@ impl SerializableModule {
165171
module.define_data(data_id, &data).unwrap();
166172
}
167173

168-
//todo!();
174+
self.inner.global_asm
169175
}
170176
}
171177

@@ -231,6 +237,8 @@ impl Module for SerializableModule {
231237
ctx.verify_if(&*self.isa)?;
232238
ctx.optimize(&*self.isa, ctrl_plane)?;
233239

240+
// FIXME compile to machine code
241+
234242
self.inner.functions.insert(func_id, ctx.func.clone());
235243

236244
Ok(())

src/unwind_module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<T: Module> Module for UnwindModule<T> {
149149
}
150150
}
151151

152-
struct FileCache;
152+
pub(crate) struct FileCache;
153153

154154
impl FileCache {
155155
fn file_for_key(&self, key: &[u8]) -> String {

0 commit comments

Comments
 (0)