@@ -8,13 +8,15 @@ use std::path::{Path, PathBuf};
8
8
use std:: sync:: Arc ;
9
9
use std:: thread:: JoinHandle ;
10
10
11
+ use cranelift_codegen:: incremental_cache:: CacheKvStore ;
11
12
use cranelift_object:: { ObjectBuilder , ObjectModule } ;
12
13
use rustc_codegen_ssa:: assert_module_sources:: CguReuse ;
13
14
use rustc_codegen_ssa:: back:: link:: ensure_removed;
14
15
use rustc_codegen_ssa:: base:: determine_cgu_reuse;
15
16
use rustc_codegen_ssa:: {
16
17
CodegenResults , CompiledModule , CrateInfo , ModuleKind , errors as ssa_errors,
17
18
} ;
19
+ use rustc_data_structures:: fingerprint:: Fingerprint ;
18
20
use rustc_data_structures:: profiling:: SelfProfilerRef ;
19
21
use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
20
22
use rustc_data_structures:: sync:: { IntoDynSyncSend , par_map} ;
@@ -26,14 +28,15 @@ use rustc_middle::mir::mono::{
26
28
} ;
27
29
use rustc_session:: Session ;
28
30
use rustc_session:: config:: { OutFileName , OutputFilenames , OutputType } ;
31
+ use rustc_span:: sym;
29
32
30
33
use crate :: base:: CodegenedFunction ;
31
34
use crate :: concurrency_limiter:: { ConcurrencyLimiter , ConcurrencyLimiterToken } ;
32
35
use crate :: debuginfo:: TypeDebugContext ;
33
36
use crate :: global_asm:: { GlobalAsmConfig , GlobalAsmContext } ;
34
37
use crate :: prelude:: * ;
35
38
use crate :: serializable_module:: SerializableModule ;
36
- use crate :: unwind_module:: UnwindModule ;
39
+ use crate :: unwind_module:: { FileCache , UnwindModule } ;
37
40
38
41
fn disable_incr_cache ( ) -> bool {
39
42
env:: var ( "CG_CLIF_DISABLE_INCR_CACHE" ) . as_deref ( ) == Ok ( "1" )
@@ -513,7 +516,7 @@ fn codegen_cgu_content(
513
516
tcx : TyCtxt < ' _ > ,
514
517
module : & mut dyn Module ,
515
518
cgu_name : rustc_span:: Symbol ,
516
- ) -> ( Vec < ( SerializableModule , String ) > , String ) {
519
+ ) -> ( Vec < SerializableModule > , String ) {
517
520
let _timer = tcx. prof . generic_activity_with_arg ( "codegen cgu" , cgu_name. as_str ( ) ) ;
518
521
519
522
let cgu = tcx. codegen_unit ( cgu_name) ;
@@ -524,6 +527,7 @@ fn codegen_cgu_content(
524
527
//let mut type_dbg = TypeDebugContext::default();
525
528
super :: predefine_mono_items ( tcx, module, & mono_items) ;
526
529
let mut codegened_functions = vec ! [ ] ;
530
+ let isa = crate :: build_isa ( tcx. sess , false ) ;
527
531
for ( mono_item, item_data) in mono_items {
528
532
match mono_item {
529
533
MonoItem :: Fn ( instance) => {
@@ -545,40 +549,64 @@ fn codegen_cgu_content(
545
549
continue ;
546
550
}
547
551
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 (
550
566
dep_node,
551
567
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,
562
568
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
577
605
} ,
578
606
Some ( rustc_middle:: dep_graph:: hash_result) ,
579
- );*/
607
+ ) ;
580
608
581
- codegened_functions. push ( ( ser_module, global_asm ) ) ;
609
+ codegened_functions. push ( ser_module) ;
582
610
}
583
611
MonoItem :: Static ( def_id) => {
584
612
let data_id = crate :: constant:: codegen_static ( tcx, module, def_id) ;
@@ -624,8 +652,8 @@ fn module_codegen(
624
652
profiler. clone ( ) ,
625
653
) ) ) ;
626
654
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) ;
629
657
global_asm. push_str ( & asm) ;
630
658
}
631
659
} ) ;
0 commit comments