@@ -8,13 +8,15 @@ use std::path::{Path, PathBuf};
88use std:: sync:: Arc ;
99use std:: thread:: JoinHandle ;
1010
11+ use cranelift_codegen:: incremental_cache:: CacheKvStore ;
1112use cranelift_object:: { ObjectBuilder , ObjectModule } ;
1213use rustc_codegen_ssa:: assert_module_sources:: CguReuse ;
1314use rustc_codegen_ssa:: back:: link:: ensure_removed;
1415use rustc_codegen_ssa:: base:: determine_cgu_reuse;
1516use rustc_codegen_ssa:: {
1617 CodegenResults , CompiledModule , CrateInfo , ModuleKind , errors as ssa_errors,
1718} ;
19+ use rustc_data_structures:: fingerprint:: Fingerprint ;
1820use rustc_data_structures:: profiling:: SelfProfilerRef ;
1921use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
2022use rustc_data_structures:: sync:: { IntoDynSyncSend , par_map} ;
@@ -26,14 +28,15 @@ use rustc_middle::mir::mono::{
2628} ;
2729use rustc_session:: Session ;
2830use rustc_session:: config:: { OutFileName , OutputFilenames , OutputType } ;
31+ use rustc_span:: sym;
2932
3033use crate :: base:: CodegenedFunction ;
3134use crate :: concurrency_limiter:: { ConcurrencyLimiter , ConcurrencyLimiterToken } ;
3235use crate :: debuginfo:: TypeDebugContext ;
3336use crate :: global_asm:: { GlobalAsmConfig , GlobalAsmContext } ;
3437use crate :: prelude:: * ;
3538use crate :: serializable_module:: SerializableModule ;
36- use crate :: unwind_module:: UnwindModule ;
39+ use crate :: unwind_module:: { FileCache , UnwindModule } ;
3740
3841fn 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 } ) ;
0 commit comments