Skip to content

Commit d05ae3a

Browse files
committed
Incorporated review feedback:
Renamed the struct to make it a little clearer that it doesn't just hold one imports map. (I couldn't bring myself to write it as `ThinLTOImportsExports` though, mainly since the exports map is literally derived from the imports map data.) Added some doc to the struct too. Revised comments to add link to the newer issue that discusses why the exports are relevant. Renamed a few of the methods so that the two character difference is more apparent (because 1. the method name is shorter and, perhaps more importantly, the changed characters now lie at the beginning of the method name.)
1 parent 12207f6 commit d05ae3a

File tree

1 file changed

+40
-22
lines changed
  • src/librustc_codegen_llvm/back

1 file changed

+40
-22
lines changed

Diff for: src/librustc_codegen_llvm/back/lto.rs

+40-22
Original file line numberDiff line numberDiff line change
@@ -463,15 +463,18 @@ fn thin_lto(
463463
// If previous imports have been deleted, or we get an IO error
464464
// reading the file storing them, then we'll just use `None` as the
465465
// 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);
469472
(Some(path), prev, curr)
470473
} else {
471474
// If we don't compile incrementally, we don't need to load the
472475
// import data from LLVM.
473476
assert!(green_modules.is_empty());
474-
let curr = ThinLTOImports::default();
477+
let curr = ThinLTOImportMaps::default();
475478
(None, None, curr)
476479
};
477480
info!("thin LTO import map loaded");
@@ -497,10 +500,14 @@ fn thin_lto(
497500
let module_name = module_name_to_str(module_name);
498501

499502
// 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.)
504511
//
505512
// This strategy means we can always save the computed imports as
506513
// canon: when we reuse the post-ThinLTO version, condition (3.)
@@ -509,16 +516,18 @@ fn thin_lto(
509516
// version, the current import set *is* the correct one, since we
510517
// are doing the ThinLTO in this current compilation cycle.)
511518
//
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).
513522
if let (Some(prev_import_map), true) =
514523
(prev_import_map.as_ref(), green_modules.contains_key(module_name))
515524
{
516525
assert!(cgcx.incr_comp_session_dir.is_some());
517526

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);
522531
let imports_all_green = curr_imports
523532
.iter()
524533
.all(|imported_module| green_modules.contains_key(imported_module));
@@ -890,20 +899,29 @@ pub unsafe fn optimize_thin_module(
890899
Ok(module)
891900
}
892901

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.
893909
#[derive(Debug, Default)]
894-
pub struct ThinLTOImports {
910+
pub struct ThinLTOImportMaps {
895911
// key = llvm name of importing module, value = list of modules it imports from
896912
imports: FxHashMap<String, Vec<String>>,
897913
// key = llvm name of exporting module, value = list of modules it exports to
898914
exports: FxHashMap<String, Vec<String>>,
899915
}
900916

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] {
903920
self.imports.get(llvm_module_name).map(|v| &v[..]).unwrap_or(&[])
904921
}
905922

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] {
907925
self.exports.get(llvm_module_name).map(|v| &v[..]).unwrap_or(&[])
908926
}
909927

@@ -921,7 +939,7 @@ impl ThinLTOImports {
921939
Ok(())
922940
}
923941

924-
fn load_from_file(path: &Path) -> io::Result<ThinLTOImports> {
942+
fn load_from_file(path: &Path) -> io::Result<ThinLTOImportMaps> {
925943
use std::io::BufRead;
926944
let mut imports = FxHashMap::default();
927945
let mut exports: FxHashMap<_, Vec<_>> = FxHashMap::default();
@@ -946,17 +964,17 @@ impl ThinLTOImports {
946964
current_module = Some(line.trim().to_string());
947965
}
948966
}
949-
Ok(ThinLTOImports { imports, exports })
967+
Ok(ThinLTOImportMaps { imports, exports })
950968
}
951969

952970
/// 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 {
954972
unsafe extern "C" fn imported_module_callback(
955973
payload: *mut libc::c_void,
956974
importing_module_name: *const libc::c_char,
957975
imported_module_name: *const libc::c_char,
958976
) {
959-
let map = &mut *(payload as *mut ThinLTOImports);
977+
let map = &mut *(payload as *mut ThinLTOImportMaps);
960978
let importing_module_name = CStr::from_ptr(importing_module_name);
961979
let importing_module_name = module_name_to_str(&importing_module_name);
962980
let imported_module_name = CStr::from_ptr(imported_module_name);
@@ -981,7 +999,7 @@ impl ThinLTOImports {
981999
.push(importing_module_name.to_owned());
9821000
}
9831001

984-
let mut map = ThinLTOImports::default();
1002+
let mut map = ThinLTOImportMaps::default();
9851003
llvm::LLVMRustGetThinLTOModuleImports(
9861004
data,
9871005
imported_module_callback,

0 commit comments

Comments
 (0)