@@ -463,15 +463,18 @@ fn thin_lto(
463
463
// If previous imports have been deleted, or we get an IO error
464
464
// reading the file storing them, then we'll just use `None` as the
465
465
// prev_import_map, which will force the code to be recompiled.
466
- let prev =
467
- if path. exists ( ) { ThinLTOImports :: load_from_file ( & path) . ok ( ) } else { None } ;
468
- let curr = ThinLTOImports :: from_thin_lto_data ( data) ;
466
+ let prev = if path. exists ( ) {
467
+ ThinLTOImportMaps :: load_from_file ( & path) . ok ( )
468
+ } else {
469
+ None
470
+ } ;
471
+ let curr = ThinLTOImportMaps :: from_thin_lto_data ( data) ;
469
472
( Some ( path) , prev, curr)
470
473
} else {
471
474
// If we don't compile incrementally, we don't need to load the
472
475
// import data from LLVM.
473
476
assert ! ( green_modules. is_empty( ) ) ;
474
- let curr = ThinLTOImports :: default ( ) ;
477
+ let curr = ThinLTOImportMaps :: default ( ) ;
475
478
( None , None , curr)
476
479
} ;
477
480
info ! ( "thin LTO import map loaded" ) ;
@@ -497,10 +500,14 @@ fn thin_lto(
497
500
let module_name = module_name_to_str ( module_name) ;
498
501
499
502
// If (1.) the module hasn't changed, and (2.) none of the modules
500
- // it imports from has changed, *and* (3.) the import-set itself has
501
- // not changed from the previous compile when it was last
502
- // ThinLTO'ed, then we can re-use the post-ThinLTO version of the
503
- // module. Otherwise, freshly perform LTO optimization.
503
+ // it imports from nor exports to have changed, *and* (3.) the
504
+ // import and export sets themselves have not changed from the
505
+ // previous compile when it was last ThinLTO'ed, then we can re-use
506
+ // the post-ThinLTO version of the module. Otherwise, freshly
507
+ // perform LTO optimization.
508
+ //
509
+ // (Note that globally, the export set is just the inverse of the
510
+ // import set.)
504
511
//
505
512
// This strategy means we can always save the computed imports as
506
513
// canon: when we reuse the post-ThinLTO version, condition (3.)
@@ -509,16 +516,18 @@ fn thin_lto(
509
516
// version, the current import set *is* the correct one, since we
510
517
// are doing the ThinLTO in this current compilation cycle.)
511
518
//
512
- // See rust-lang/rust#59535.
519
+ // For more discussion, see rust-lang/rust#59535 (where the import
520
+ // issue was discovered) and rust-lang/rust#69798 (where the
521
+ // analogous export issue was discovered).
513
522
if let ( Some ( prev_import_map) , true ) =
514
523
( prev_import_map. as_ref ( ) , green_modules. contains_key ( module_name) )
515
524
{
516
525
assert ! ( cgcx. incr_comp_session_dir. is_some( ) ) ;
517
526
518
- let prev_imports = prev_import_map. modules_imported_by ( module_name) ;
519
- let curr_imports = curr_import_map. modules_imported_by ( module_name) ;
520
- let prev_exports = prev_import_map. modules_exported_by ( module_name) ;
521
- let curr_exports = curr_import_map. modules_exported_by ( module_name) ;
527
+ let prev_imports = prev_import_map. imports_of ( module_name) ;
528
+ let curr_imports = curr_import_map. imports_of ( module_name) ;
529
+ let prev_exports = prev_import_map. exports_of ( module_name) ;
530
+ let curr_exports = curr_import_map. exports_of ( module_name) ;
522
531
let imports_all_green = curr_imports
523
532
. iter ( )
524
533
. all ( |imported_module| green_modules. contains_key ( imported_module) ) ;
@@ -890,20 +899,29 @@ pub unsafe fn optimize_thin_module(
890
899
Ok ( module)
891
900
}
892
901
902
+ /// Summarizes module import/export relationships used by LLVM's ThinLTO pass.
903
+ ///
904
+ /// Note that we tend to have two such instances of `ThinLTOImportMaps` in use:
905
+ /// one loaded from a file that represents the relationships used during the
906
+ /// compilation associated with the incremetnal build artifacts we are
907
+ /// attempting to reuse, and another constructed via `from_thin_lto_data`, which
908
+ /// captures the relationships of ThinLTO in the current compilation.
893
909
#[ derive( Debug , Default ) ]
894
- pub struct ThinLTOImports {
910
+ pub struct ThinLTOImportMaps {
895
911
// key = llvm name of importing module, value = list of modules it imports from
896
912
imports : FxHashMap < String , Vec < String > > ,
897
913
// key = llvm name of exporting module, value = list of modules it exports to
898
914
exports : FxHashMap < String , Vec < String > > ,
899
915
}
900
916
901
- impl ThinLTOImports {
902
- fn modules_imported_by ( & self , llvm_module_name : & str ) -> & [ String ] {
917
+ impl ThinLTOImportMaps {
918
+ /// Returns modules imported by `llvm_module_name` during some ThinLTO pass.
919
+ fn imports_of ( & self , llvm_module_name : & str ) -> & [ String ] {
903
920
self . imports . get ( llvm_module_name) . map ( |v| & v[ ..] ) . unwrap_or ( & [ ] )
904
921
}
905
922
906
- fn modules_exported_by ( & self , llvm_module_name : & str ) -> & [ String ] {
923
+ /// Returns modules exported by `llvm_module_name` during some ThinLTO pass.
924
+ fn exports_of ( & self , llvm_module_name : & str ) -> & [ String ] {
907
925
self . exports . get ( llvm_module_name) . map ( |v| & v[ ..] ) . unwrap_or ( & [ ] )
908
926
}
909
927
@@ -921,7 +939,7 @@ impl ThinLTOImports {
921
939
Ok ( ( ) )
922
940
}
923
941
924
- fn load_from_file ( path : & Path ) -> io:: Result < ThinLTOImports > {
942
+ fn load_from_file ( path : & Path ) -> io:: Result < ThinLTOImportMaps > {
925
943
use std:: io:: BufRead ;
926
944
let mut imports = FxHashMap :: default ( ) ;
927
945
let mut exports: FxHashMap < _ , Vec < _ > > = FxHashMap :: default ( ) ;
@@ -946,17 +964,17 @@ impl ThinLTOImports {
946
964
current_module = Some ( line. trim ( ) . to_string ( ) ) ;
947
965
}
948
966
}
949
- Ok ( ThinLTOImports { imports, exports } )
967
+ Ok ( ThinLTOImportMaps { imports, exports } )
950
968
}
951
969
952
970
/// Loads the ThinLTO import map from ThinLTOData.
953
- unsafe fn from_thin_lto_data ( data : * const llvm:: ThinLTOData ) -> ThinLTOImports {
971
+ unsafe fn from_thin_lto_data ( data : * const llvm:: ThinLTOData ) -> ThinLTOImportMaps {
954
972
unsafe extern "C" fn imported_module_callback (
955
973
payload : * mut libc:: c_void ,
956
974
importing_module_name : * const libc:: c_char ,
957
975
imported_module_name : * const libc:: c_char ,
958
976
) {
959
- let map = & mut * ( payload as * mut ThinLTOImports ) ;
977
+ let map = & mut * ( payload as * mut ThinLTOImportMaps ) ;
960
978
let importing_module_name = CStr :: from_ptr ( importing_module_name) ;
961
979
let importing_module_name = module_name_to_str ( & importing_module_name) ;
962
980
let imported_module_name = CStr :: from_ptr ( imported_module_name) ;
@@ -981,7 +999,7 @@ impl ThinLTOImports {
981
999
. push ( importing_module_name. to_owned ( ) ) ;
982
1000
}
983
1001
984
- let mut map = ThinLTOImports :: default ( ) ;
1002
+ let mut map = ThinLTOImportMaps :: default ( ) ;
985
1003
llvm:: LLVMRustGetThinLTOModuleImports (
986
1004
data,
987
1005
imported_module_callback,
0 commit comments