Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3790,6 +3790,7 @@ dependencies = [
"rustc_fs_util",
"rustc_hashes",
"rustc_hir",
"rustc_incremental",
"rustc_index",
"rustc_lint_defs",
"rustc_llvm",
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_codegen_cranelift/src/driver/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ use rustc_codegen_ssa::back::write::{
CodegenContext, FatLtoInput, ModuleConfig, SharedEmitter, TargetMachineFactoryFn, ThinLtoInput,
};
use rustc_codegen_ssa::traits::{ExtraBackendMethods, WriteBackendMethods};
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen, ModuleKind};
use rustc_codegen_ssa::{CachedModuleCodegen, CompiledModule, ModuleCodegen, ModuleKind};
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_errors::{DiagCtxt, DiagCtxtHandle};
use rustc_middle::dep_graph::{BorrowedIncrCompSession, WorkProduct};
use rustc_middle::dep_graph::IncrCompSession;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::mono::{MonoItem, MonoItemData, Visibility};
use rustc_session::Session;
Expand Down Expand Up @@ -337,13 +337,13 @@ impl WriteBackendMethods for AotDriver {

fn run_thin_lto(
_cgcx: &CodegenContext,
_prof: &SelfProfilerRef,
_incr_comp_session: Option<&BorrowedIncrCompSession>,
_sess: &Session,
_incr_comp_session: Option<&IncrCompSession>,
_dcx: rustc_errors::DiagCtxtHandle<'_>,
_exported_symbols_for_lto: &[String],
_each_linked_rlib_for_lto: &[PathBuf],
_modules: Vec<ThinLtoInput<Self>>,
) -> (Vec<ThinModule<Self>>, Vec<WorkProduct>) {
) -> (Vec<ThinModule<Self>>, Vec<CachedModuleCodegen>) {
unreachable!()
}

Expand Down
21 changes: 16 additions & 5 deletions compiler/rustc_codegen_cranelift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use rustc_codegen_ssa::{CompiledModules, CrateInfo, TargetConfig, back};
use rustc_data_structures::unord::UnordSet;
use rustc_log::tracing::info;
use rustc_middle::dep_graph::IncrCompSession;
use rustc_session::config::{NATIVE_CPU, OutputFilenames};
use rustc_session::config::NATIVE_CPU;
use rustc_session::{CodegenBackendInit, EarlySession, Session};
use rustc_span::{Symbol, sym};
use rustc_target::spec::{Arch, CfgAbi, Env, Os};
Expand Down Expand Up @@ -240,13 +240,24 @@ impl CodegenBackend for CraneliftCodegenBackend {
ongoing_codegen: Box<dyn Any>,
sess: &Session,
incr_comp_session: Option<&mut IncrCompSession>,
_outputs: &OutputFilenames,
crate_info: &CrateInfo,
) -> CompiledModules {
) -> Box<dyn Any> {
ongoing_codegen
.downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<driver::aot::AotDriver>>()
.unwrap()
.join(sess, incr_comp_session, crate_info)
.join(sess, incr_comp_session)
}

fn perform_lto(
&self,
pending_lto: Box<dyn Any>,
sess: &Session,
incr_comp_session: Option<&mut IncrCompSession>,
crate_info: &CrateInfo,
) -> CompiledModules {
pending_lto
.downcast::<rustc_codegen_ssa::back::write::PendingLto<driver::aot::AotDriver>>()
.unwrap()
.perform_lto(sess, incr_comp_session, crate_info)
}
}

Expand Down
33 changes: 23 additions & 10 deletions compiler/rustc_codegen_gcc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ use rustc_codegen_ssa::back::write::{
use rustc_codegen_ssa::base::codegen_crate;
use rustc_codegen_ssa::target_features::{self, internal_target_features};
use rustc_codegen_ssa::traits::{CodegenBackend, ExtraBackendMethods, WriteBackendMethods};
use rustc_codegen_ssa::{CompiledModule, CompiledModules, CrateInfo, ModuleCodegen, TargetConfig};
use rustc_codegen_ssa::{
CachedModuleCodegen, CompiledModule, CompiledModules, CrateInfo, ModuleCodegen, TargetConfig,
};
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::sync::IntoDynSyncSend;
use rustc_errors::{DiagCtxt, DiagCtxtHandle};
use rustc_middle::dep_graph::{BorrowedIncrCompSession, IncrCompSession, WorkProduct};
use rustc_middle::dep_graph::IncrCompSession;
use rustc_middle::ty::TyCtxt;
use rustc_session::config::{OptLevel, OutputFilenames};
use rustc_session::config::OptLevel;
use rustc_session::{CodegenBackendInit, EarlySession, Session};
use rustc_span::{Symbol, sym};
use rustc_target::spec::{RelocModel, TargetTuple};
Expand Down Expand Up @@ -322,13 +324,24 @@ impl CodegenBackend for GccCodegenBackend {
ongoing_codegen: Box<dyn Any>,
sess: &Session,
incr_comp_session: Option<&mut IncrCompSession>,
_outputs: &OutputFilenames,
crate_info: &CrateInfo,
) -> CompiledModules {
) -> Box<dyn Any> {
ongoing_codegen
.downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<GccCodegenBackend>>()
.expect("Expected GccCodegenBackend's OngoingCodegen, found Box<Any>")
.join(sess, incr_comp_session, crate_info)
.join(sess, incr_comp_session)
}

fn perform_lto(
&self,
pending_lto: Box<dyn Any>,
sess: &Session,
incr_comp_session: Option<&mut IncrCompSession>,
crate_info: &CrateInfo,
) -> CompiledModules {
pending_lto
.downcast::<rustc_codegen_ssa::back::write::PendingLto<GccCodegenBackend>>()
.expect("Expected GccCodegenBackend's PendingLto, found Box<Any>")
.perform_lto(sess, incr_comp_session, crate_info)
}
}

Expand Down Expand Up @@ -436,14 +449,14 @@ impl WriteBackendMethods for GccCodegenBackend {

fn run_thin_lto(
_cgcx: &CodegenContext,
_prof: &SelfProfilerRef,
_incr_comp_session: Option<&BorrowedIncrCompSession>,
_sess: &Session,
_incr_comp_session: Option<&IncrCompSession>,
_dcx: DiagCtxtHandle<'_>,
// FIXME(bjorn3): Limit LTO exports to these symbols
_exported_symbols_for_lto: &[String],
_each_linked_rlib_for_lto: &[PathBuf],
_modules: Vec<ThinLtoInput<Self>>,
) -> (Vec<ThinModule<Self>>, Vec<WorkProduct>) {
) -> (Vec<ThinModule<Self>>, Vec<CachedModuleCodegen>) {
unreachable!()
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_llvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ rustc_errors = { path = "../rustc_errors" }
rustc_fs_util = { path = "../rustc_fs_util" }
rustc_hashes = { path = "../rustc_hashes" }
rustc_hir = { path = "../rustc_hir" }
rustc_incremental = { path = "../rustc_incremental" }
rustc_index = { path = "../rustc_index" }
rustc_lint_defs = { path = "../rustc_lint_defs" }
rustc_llvm = { path = "../rustc_llvm" }
Expand Down
38 changes: 25 additions & 13 deletions compiler/rustc_codegen_llvm/src/back/llvm_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ use rustc_codegen_ssa::back::write::{
};
use rustc_codegen_ssa::traits::{CodegenBackend, ExtraBackendMethods, WriteBackendMethods};
use rustc_codegen_ssa::{
CompiledModule, CompiledModules, CrateInfo, ModuleCodegen, TargetConfig, target_features,
CachedModuleCodegen, CompiledModule, CompiledModules, CrateInfo, ModuleCodegen, TargetConfig,
target_features,
};
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_errors::{DiagCtxt, DiagCtxtHandle};
use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{BorrowedIncrCompSession, IncrCompSession, WorkProduct};
use rustc_middle::dep_graph::IncrCompSession;
use rustc_middle::ty::TyCtxt;
use rustc_session::config::{LtoCli, OptLevel, OutputFilenames, PrintKind, PrintRequest};
use rustc_session::{CodegenBackendInit, EarlySession, Session};
Expand Down Expand Up @@ -119,16 +120,16 @@ impl WriteBackendMethods for LlvmCodegenBackend {
}
fn run_thin_lto(
cgcx: &CodegenContext,
prof: &SelfProfilerRef,
incr_comp_session: Option<&BorrowedIncrCompSession>,
sess: &Session,
incr_comp_session: Option<&IncrCompSession>,
dcx: DiagCtxtHandle<'_>,
exported_symbols_for_lto: &[String],
each_linked_rlib_for_lto: &[PathBuf],
modules: Vec<ThinLtoInput<Self>>,
) -> (Vec<ThinModule<Self>>, Vec<WorkProduct>) {
) -> (Vec<ThinModule<Self>>, Vec<CachedModuleCodegen>) {
back::lto::run_thin(
cgcx,
prof,
sess,
incr_comp_session,
dcx,
exported_symbols_for_lto,
Expand Down Expand Up @@ -409,14 +410,27 @@ impl CodegenBackend for LlvmCodegenBackend {
ongoing_codegen: Box<dyn Any>,
sess: &Session,
incr_comp_session: Option<&mut IncrCompSession>,
outputs: &OutputFilenames,
crate_info: &CrateInfo,
) -> CompiledModules {
let compiled_modules = ongoing_codegen
) -> Box<dyn Any> {
ongoing_codegen
.downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<LlvmCodegenBackend>>()
.expect("Expected LlvmCodegenBackend's OngoingCodegen, found Box<Any>")
.join(sess, incr_comp_session, crate_info);
.join(sess, incr_comp_session)
}

fn perform_lto(
&self,
ongoing_codegen: Box<dyn Any>,
sess: &Session,
incr_comp_session: Option<&mut IncrCompSession>,
crate_info: &CrateInfo,
) -> CompiledModules {
ongoing_codegen
.downcast::<rustc_codegen_ssa::back::write::PendingLto<LlvmCodegenBackend>>()
.expect("Expected LlvmCodegenBackend's PendingLto, found Box<Any>")
.perform_lto(sess, incr_comp_session, crate_info)
}

fn post_codegen_metrics(&self, sess: &Session, outputs: &OutputFilenames) {
if sess.opts.unstable_opts.llvm_time_trace {
sess.time("llvm_dump_timing_file", || {
let file_name = outputs.with_extension("llvm_timings.json");
Expand Down Expand Up @@ -451,8 +465,6 @@ impl CodegenBackend for LlvmCodegenBackend {
));
}
}

compiled_modules
}

fn link(
Expand Down
52 changes: 33 additions & 19 deletions compiler/rustc_codegen_llvm/src/back/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ use rustc_codegen_ssa::back::write::{
CodegenContext, FatLtoInput, SharedEmitter, TargetMachineFactoryFn, ThinLtoInput,
};
use rustc_codegen_ssa::traits::*;
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen, ModuleKind};
use rustc_codegen_ssa::{CachedModuleCodegen, CompiledModule, ModuleCodegen, ModuleKind};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_errors::{DiagCtxt, DiagCtxtHandle};
use rustc_middle::dep_graph::{BorrowedIncrCompSession, WorkProduct};
use rustc_session::config;
use rustc_incremental::load_work_product_index;
use rustc_middle::dep_graph::{IncrCompSession, WorkProductId, WorkProductMap};
use rustc_session::{Session, config};
use rustc_span::bug;
use rustc_structures::SanitizerSet;
use tracing::{debug, info};
Expand Down Expand Up @@ -185,13 +186,13 @@ pub(crate) fn run_fat(
/// can simply be copied over from the incr. comp. cache.
pub(crate) fn run_thin(
cgcx: &CodegenContext,
prof: &SelfProfilerRef,
incr_comp_session: Option<&BorrowedIncrCompSession>,
sess: &Session,
incr_comp_session: Option<&IncrCompSession>,
dcx: DiagCtxtHandle<'_>,
exported_symbols_for_lto: &[String],
each_linked_rlib_for_lto: &[PathBuf],
modules: Vec<ThinLtoInput<LlvmCodegenBackend>>,
) -> (Vec<ThinModule<LlvmCodegenBackend>>, Vec<WorkProduct>) {
) -> (Vec<ThinModule<LlvmCodegenBackend>>, Vec<CachedModuleCodegen>) {
let (symbols_below_threshold, upstream_modules) =
prepare_lto(cgcx, exported_symbols_for_lto, each_linked_rlib_for_lto, dcx);
let symbols_below_threshold =
Expand All @@ -202,7 +203,7 @@ pub(crate) fn run_thin(
is deferred to the linker"
);
}
thin_lto(prof, incr_comp_session, dcx, modules, upstream_modules, &symbols_below_threshold)
thin_lto(sess, incr_comp_session, dcx, modules, upstream_modules, &symbols_below_threshold)
}

fn fat_lto(
Expand Down Expand Up @@ -369,22 +370,36 @@ fn fat_lto(
/// all of the `LtoModuleCodegen` units returned below and destroyed once
/// they all go out of scope.
fn thin_lto(
prof: &SelfProfilerRef,
incr_comp_session: Option<&BorrowedIncrCompSession>,
sess: &Session,
incr_comp_session: Option<&IncrCompSession>,
dcx: DiagCtxtHandle<'_>,
modules: Vec<ThinLtoInput<LlvmCodegenBackend>>,
serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>,
symbols_below_threshold: &[*const libc::c_char],
) -> (Vec<ThinModule<LlvmCodegenBackend>>, Vec<WorkProduct>) {
let _timer = prof.generic_activity("LLVM_thin_lto_global_analysis");
) -> (Vec<ThinModule<LlvmCodegenBackend>>, Vec<CachedModuleCodegen>) {
let _timer = sess.prof.generic_activity("LLVM_thin_lto_global_analysis");
unsafe {
info!("going for that thin, thin LTO");

// Try to load the previous session's dep graph and work products.
let prev_work_products = incr_comp_session.map(|incr_comp_session| {
load_work_product_index(sess, &incr_comp_session)
.unwrap_or_else(|()| WorkProductMap::default())
});
Comment thread
petrochenkov marked this conversation as resolved.
Resolved

let green_modules: FxHashMap<_, _> = modules
.iter()
.filter_map(|module| {
if let ThinLtoInput::Green { wp, .. } = module {
Some((wp.cgu_name.clone(), wp.clone()))
if let ThinLtoInput::Green { name, .. } = module {
Some((
name.clone(),
CachedModuleCodegen {
name: name.clone(),
source: prev_work_products.as_ref().unwrap()
[&WorkProductId::from_cgu_name(name)]
.clone(),
},
))
} else {
None
}
Expand All @@ -398,9 +413,9 @@ fn thin_lto(

for (i, module) in modules.into_iter().enumerate() {
let (name, buffer) = match module {
ThinLtoInput::Red { name, buffer } => (name, buffer),
ThinLtoInput::Green { wp, bitcode_path } => {
(wp.cgu_name, SerializedModule::from_file(&bitcode_path))
ThinLtoInput::Red { name, path: _, buffer } => (name, buffer),
ThinLtoInput::Green { name: cgu_name, bitcode_path } => {
(cgu_name, SerializedModule::from_file(&bitcode_path))
}
};
info!("local module: {} - {}", i, name);
Expand Down Expand Up @@ -461,7 +476,7 @@ fn thin_lto(

info!("thin LTO data created");

let new_key_map_path = incr_comp_session.as_ref().map(|incr_comp_session| {
let new_key_map_path = incr_comp_session.map(|incr_comp_session| {
incr_comp_session.new_session_directory.join(THIN_LTO_KEYS_INCR_COMP_FILE_NAME)
});

Expand Down Expand Up @@ -508,8 +523,7 @@ fn thin_lto(
// If a module exists in both the current and the previous session,
// and has the same LTO cache key in both sessions, then we can re-use it
if prev_key_map.keys.get(module_name) == curr_key_map.keys.get(module_name) {
let work_product = green_modules[module_name].clone();
copy_jobs.push(work_product);
copy_jobs.push(green_modules[module_name].clone());
info!(" - {}: re-used", module_name);
continue;
}
Expand Down
Loading
Loading