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
5 changes: 3 additions & 2 deletions compiler/rustc_codegen_cranelift/src/driver/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ fn create_jit_module(
(jit_module, cx)
}

pub(crate) fn run_jit(tcx: TyCtxt<'_>, crate_info: &CrateInfo, jit_args: Vec<String>) -> ! {
pub(crate) fn run_jit(tcx: TyCtxt<'_>, target_cpu: String, jit_args: Vec<String>) -> ! {
if !tcx.crate_types().contains(&rustc_session::config::CrateType::Executable) {
tcx.dcx().fatal("can't jit non-executable crate");
}

let output_filenames = tcx.output_filenames(());
let crate_info = CrateInfo::new(tcx, target_cpu);
let should_write_ir = crate::pretty_clif::should_write_ir(tcx.sess);
let (mut jit_module, mut debug_context) = create_jit_module(tcx, crate_info);
let (mut jit_module, mut debug_context) = create_jit_module(tcx, &crate_info);
let mut cached_context = Context::new();

let cgus = tcx.collect_and_partition_mono_items(()).codegen_units;
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_codegen_cranelift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@ impl CodegenBackend for CraneliftCodegenBackend {
.to_owned()
}

fn codegen_crate(&self, tcx: TyCtxt<'_>, _crate_info: &CrateInfo) -> Box<dyn Any> {
fn codegen_crate(&self, tcx: TyCtxt<'_>) -> Box<dyn Any> {
info!("codegen crate {}", tcx.crate_name(LOCAL_CRATE));
let config = self.config.get().unwrap();
if config.jit_mode {
#[cfg(feature = "jit")]
driver::jit::run_jit(tcx, _crate_info, config.jit_args.clone());
driver::jit::run_jit(tcx, self.target_cpu(tcx.sess), config.jit_args.clone());

#[cfg(not(feature = "jit"))]
tcx.dcx().fatal("jit support was disabled when compiling rustc_codegen_cranelift");
Expand All @@ -228,6 +228,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
ongoing_codegen: Box<dyn Any>,
sess: &Session,
outputs: &OutputFilenames,
_crate_info: &CrateInfo,
) -> (CompiledModules, FxIndexMap<WorkProductId, WorkProduct>) {
ongoing_codegen.downcast::<driver::aot::OngoingCodegen>().unwrap().join(sess, outputs)
}
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_codegen_gcc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,20 +291,21 @@ impl CodegenBackend for GccCodegenBackend {
target_cpu(sess).to_owned()
}

fn codegen_crate(&self, tcx: TyCtxt<'_>, crate_info: &CrateInfo) -> Box<dyn Any> {
Box::new(codegen_crate(self.clone(), tcx, crate_info))
fn codegen_crate(&self, tcx: TyCtxt<'_>) -> Box<dyn Any> {
Box::new(codegen_crate(self.clone(), tcx))
}

fn join_codegen(
&self,
ongoing_codegen: Box<dyn Any>,
sess: &Session,
_outputs: &OutputFilenames,
crate_info: &CrateInfo,
) -> (CompiledModules, FxIndexMap<WorkProductId, WorkProduct>) {
ongoing_codegen
.downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<GccCodegenBackend>>()
.expect("Expected GccCodegenBackend's OngoingCodegen, found Box<Any>")
.join(sess)
.join(sess, crate_info)
}

fn target_config(&self, sess: &Session) -> TargetConfig {
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_codegen_llvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,20 +333,21 @@ impl CodegenBackend for LlvmCodegenBackend {
crate::llvm_util::target_cpu(sess).to_string()
}

fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>, crate_info: &CrateInfo) -> Box<dyn Any> {
Box::new(rustc_codegen_ssa::base::codegen_crate(LlvmCodegenBackend(()), tcx, crate_info))
fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Box<dyn Any> {
Box::new(rustc_codegen_ssa::base::codegen_crate(LlvmCodegenBackend(()), tcx))
}

fn join_codegen(
&self,
ongoing_codegen: Box<dyn Any>,
sess: &Session,
outputs: &OutputFilenames,
crate_info: &CrateInfo,
) -> (CompiledModules, FxIndexMap<WorkProductId, WorkProduct>) {
let (compiled_modules, work_products) = ongoing_codegen
.downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<LlvmCodegenBackend>>()
.expect("Expected LlvmCodegenBackend's OngoingCodegen, found Box<Any>")
.join(sess);
.join(sess, crate_info);

if sess.opts.unstable_opts.llvm_time_trace {
sess.time("llvm_dump_timing_file", || {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn crate_type_allows_lto(crate_type: CrateType) -> bool {
}
}

pub(super) fn exported_symbols_for_lto(
pub(crate) fn exported_symbols_for_lto(
tcx: TyCtxt<'_>,
each_linked_rlib_for_lto: &[CrateNum],
) -> Vec<String> {
Expand Down
86 changes: 22 additions & 64 deletions compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use rustc_span::{FileName, InnerSpan, Span, SpanData};
use rustc_target::spec::{MergeFunctions, SanitizerSet};
use tracing::debug;

use crate::back::link::{self, ensure_removed};
use crate::back::link::ensure_removed;
use crate::back::lto::{self, SerializedModule, check_lto_allowed};
use crate::errors::ErrorCreatingRemarkDir;
use crate::traits::*;
Expand Down Expand Up @@ -389,18 +389,8 @@ fn generate_thin_lto_work<B: WriteBackendMethods>(

enum MaybeLtoModules<B: WriteBackendMethods> {
NoLto(CompiledModules),
FatLto {
cgcx: CodegenContext,
exported_symbols_for_lto: Arc<Vec<String>>,
each_linked_rlib_file_for_lto: Vec<PathBuf>,
needs_fat_lto: Vec<FatLtoInput<B>>,
},
ThinLto {
cgcx: CodegenContext,
exported_symbols_for_lto: Arc<Vec<String>>,
each_linked_rlib_file_for_lto: Vec<PathBuf>,
needs_thin_lto: Vec<ThinLtoInput<B>>,
},
FatLto { cgcx: CodegenContext, needs_fat_lto: Vec<FatLtoInput<B>> },
ThinLto { cgcx: CodegenContext, needs_thin_lto: Vec<ThinLtoInput<B>> },
}

fn need_bitcode_in_object(tcx: TyCtxt<'_>) -> bool {
Expand All @@ -424,7 +414,6 @@ fn need_pre_lto_bitcode_for_incr_comp(sess: &Session) -> bool {
pub(crate) fn start_async_codegen<B: ExtraBackendMethods>(
backend: B,
tcx: TyCtxt<'_>,
crate_info: &CrateInfo,
allocator_module: Option<ModuleCodegen<B::Module>>,
) -> OngoingCodegen<B> {
let (coordinator_send, coordinator_receive) = channel();
Expand All @@ -440,7 +429,6 @@ pub(crate) fn start_async_codegen<B: ExtraBackendMethods>(
let coordinator_thread = start_executing_work(
backend.clone(),
tcx,
crate_info,
shared_emitter,
codegen_worker_send,
coordinator_receive,
Expand Down Expand Up @@ -992,8 +980,8 @@ fn do_thin_lto<B: WriteBackendMethods>(
prof: &SelfProfilerRef,
shared_emitter: SharedEmitter,
tm_factory: TargetMachineFactoryFn<B>,
exported_symbols_for_lto: Arc<Vec<String>>,
each_linked_rlib_for_lto: Vec<PathBuf>,
exported_symbols_for_lto: &[String],
each_linked_rlib_for_lto: &[PathBuf],
needs_thin_lto: Vec<ThinLtoInput<B>>,
) -> Vec<CompiledModule> {
let _timer = prof.verbose_generic_activity("LLVM_thinlto");
Expand Down Expand Up @@ -1231,7 +1219,6 @@ enum MainThreadState {
fn start_executing_work<B: ExtraBackendMethods>(
backend: B,
tcx: TyCtxt<'_>,
crate_info: &CrateInfo,
shared_emitter: SharedEmitter,
codegen_worker_send: Sender<CguMessage>,
coordinator_receive: Receiver<Message<B>>,
Expand All @@ -1243,22 +1230,9 @@ fn start_executing_work<B: ExtraBackendMethods>(
let sess = tcx.sess;
let prof = sess.prof.clone();

let mut each_linked_rlib_for_lto = Vec::new();
let mut each_linked_rlib_file_for_lto = Vec::new();
if sess.lto() != Lto::No && sess.lto() != Lto::ThinLocal {
drop(link::each_linked_rlib(crate_info, None, &mut |cnum, path| {
if link::ignored_for_lto(sess, crate_info, cnum) {
return;
}

each_linked_rlib_for_lto.push(cnum);
each_linked_rlib_file_for_lto.push(path.to_path_buf());
}));
}

// Compute the set of symbols we need to retain when doing LTO (if we need to)
// Compute the set of symbols we need to retain when doing thin local LTO (if we need to)
let exported_symbols_for_lto =
Arc::new(lto::exported_symbols_for_lto(tcx, &each_linked_rlib_for_lto));
if sess.lto() == Lto::ThinLocal { lto::exported_symbols_for_lto(tcx, &[]) } else { vec![] };

// First up, convert our jobserver into a helper thread so we can use normal
// mpsc channels to manage our messages and such.
Expand Down Expand Up @@ -1757,12 +1731,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
needs_fat_lto.push(FatLtoInput::Serialized { name: wp.cgu_name, bitcode_path })
}

return Ok(MaybeLtoModules::FatLto {
cgcx,
exported_symbols_for_lto,
each_linked_rlib_file_for_lto,
needs_fat_lto,
});
return Ok(MaybeLtoModules::FatLto { cgcx, needs_fat_lto });
} else if !needs_thin_lto.is_empty() || !lto_import_only_modules.is_empty() {
assert!(compiled_modules.is_empty());
assert!(needs_fat_lto.is_empty());
Expand All @@ -1777,8 +1746,8 @@ fn start_executing_work<B: ExtraBackendMethods>(
&prof,
shared_emitter.clone(),
tm_factory,
exported_symbols_for_lto,
each_linked_rlib_file_for_lto,
&exported_symbols_for_lto,
&[],
needs_thin_lto,
));
} else {
Expand All @@ -1790,12 +1759,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
});
}

return Ok(MaybeLtoModules::ThinLto {
cgcx,
exported_symbols_for_lto,
each_linked_rlib_file_for_lto,
needs_thin_lto,
});
return Ok(MaybeLtoModules::ThinLto { cgcx, needs_thin_lto });
}
}

Expand Down Expand Up @@ -2139,7 +2103,11 @@ pub struct OngoingCodegen<B: WriteBackendMethods> {
}

impl<B: WriteBackendMethods> OngoingCodegen<B> {
pub fn join(self, sess: &Session) -> (CompiledModules, FxIndexMap<WorkProductId, WorkProduct>) {
pub fn join(
self,
sess: &Session,
crate_info: &CrateInfo,
) -> (CompiledModules, FxIndexMap<WorkProductId, WorkProduct>) {
self.shared_emitter_main.check(sess, true);

let maybe_lto_modules = sess.time("join_worker_thread", || match self.coordinator.join() {
Expand All @@ -2163,12 +2131,7 @@ impl<B: WriteBackendMethods> OngoingCodegen<B> {
drop(shared_emitter);
compiled_modules
}
MaybeLtoModules::FatLto {
cgcx,
exported_symbols_for_lto,
each_linked_rlib_file_for_lto,
needs_fat_lto,
} => {
MaybeLtoModules::FatLto { cgcx, needs_fat_lto } => {
let tm_factory = self.backend.target_machine_factory(
sess,
cgcx.opt_level,
Expand All @@ -2181,19 +2144,14 @@ impl<B: WriteBackendMethods> OngoingCodegen<B> {
&cgcx,
shared_emitter,
tm_factory,
&exported_symbols_for_lto,
&each_linked_rlib_file_for_lto,
&crate_info.exported_symbols_for_lto,
&crate_info.each_linked_rlib_file_for_lto,
needs_fat_lto,
)],
allocator_module: None,
}
}
MaybeLtoModules::ThinLto {
cgcx,
exported_symbols_for_lto,
each_linked_rlib_file_for_lto,
needs_thin_lto,
} => {
MaybeLtoModules::ThinLto { cgcx, needs_thin_lto } => {
let tm_factory = self.backend.target_machine_factory(
sess,
cgcx.opt_level,
Expand All @@ -2206,8 +2164,8 @@ impl<B: WriteBackendMethods> OngoingCodegen<B> {
&sess.prof,
shared_emitter,
tm_factory,
exported_symbols_for_lto,
each_linked_rlib_file_for_lto,
&crate_info.exported_symbols_for_lto,
&crate_info.each_linked_rlib_file_for_lto,
needs_thin_lto,
),
allocator_module: None,
Expand Down
29 changes: 23 additions & 6 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,11 +686,7 @@ pub fn allocator_shim_contents(tcx: TyCtxt<'_>, kind: AllocatorKind) -> Vec<Allo
methods
}

pub fn codegen_crate<B: ExtraBackendMethods>(
backend: B,
tcx: TyCtxt<'_>,
crate_info: &CrateInfo,
) -> OngoingCodegen<B> {
pub fn codegen_crate<B: ExtraBackendMethods>(backend: B, tcx: TyCtxt<'_>) -> OngoingCodegen<B> {
if tcx.sess.target.need_explicit_cpu && tcx.sess.opts.cg.target_cpu.is_none() {
// The target has no default cpu, but none is set explicitly
tcx.dcx().emit_fatal(errors::CpuRequired);
Expand Down Expand Up @@ -734,7 +730,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
None
};

let ongoing_codegen = start_async_codegen(backend.clone(), tcx, crate_info, allocator_module);
let ongoing_codegen = start_async_codegen(backend.clone(), tcx, allocator_module);

// For better throughput during parallel processing by LLVM, we used to sort
// CGUs largest to smallest. This would lead to better thread utilization
Expand Down Expand Up @@ -959,6 +955,8 @@ impl CrateInfo {
natvis_debugger_visualizers: Default::default(),
lint_levels: CodegenLintLevels::from_tcx(tcx),
metadata_symbol: exported_symbols::metadata_symbol_name(tcx),
each_linked_rlib_file_for_lto: Default::default(),
exported_symbols_for_lto: Default::default(),
};

info.native_libraries.reserve(n_crates);
Expand Down Expand Up @@ -1044,6 +1042,25 @@ impl CrateInfo {
});
}

let mut each_linked_rlib_for_lto = Vec::new();
let mut each_linked_rlib_file_for_lto = Vec::new();
if tcx.sess.lto() != config::Lto::No && tcx.sess.lto() != config::Lto::ThinLocal {
drop(crate::back::link::each_linked_rlib(&info, None, &mut |cnum, path| {
if crate::back::link::ignored_for_lto(tcx.sess, &info, cnum) {
return;
}

each_linked_rlib_for_lto.push(cnum);
each_linked_rlib_file_for_lto.push(path.to_path_buf());
}));
}
info.each_linked_rlib_file_for_lto = each_linked_rlib_file_for_lto;

// FIXME move to -Zlink-only half such that each_linked_rlib_file_for_lto can be moved there too
// Compute the set of symbols we need to retain when doing LTO (if we need to)
info.exported_symbols_for_lto =
crate::back::lto::exported_symbols_for_lto(tcx, &each_linked_rlib_for_lto);

let embed_visualizers = tcx.crate_types().iter().any(|&crate_type| match crate_type {
CrateType::Executable | CrateType::Dylib | CrateType::Cdylib | CrateType::Sdylib => {
// These are crate types for which we invoke the linker and can embed
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_codegen_ssa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ pub struct CrateInfo {
pub natvis_debugger_visualizers: BTreeSet<DebuggerVisualizerFile>,
pub lint_levels: CodegenLintLevels,
pub metadata_symbol: String,
pub each_linked_rlib_file_for_lto: Vec<PathBuf>,
pub exported_symbols_for_lto: Vec<String>,
}

/// Target-specific options that get set in `cfg(...)`.
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_ssa/src/traits/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub trait CodegenBackend {

fn target_cpu(&self, sess: &Session) -> String;

fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>, crate_info: &CrateInfo) -> Box<dyn Any>;
fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Box<dyn Any>;

/// This is called on the returned `Box<dyn Any>` from [`codegen_crate`](Self::codegen_crate)
///
Expand All @@ -116,6 +116,7 @@ pub trait CodegenBackend {
ongoing_codegen: Box<dyn Any>,
sess: &Session,
outputs: &OutputFilenames,
crate_info: &CrateInfo,
) -> (CompiledModules, FxIndexMap<WorkProductId, WorkProduct>);

fn print_pass_timings(&self) {}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1305,8 +1305,6 @@ pub(crate) fn start_codegen<'tcx>(

let metadata = rustc_metadata::fs::encode_and_write_metadata(tcx);

let crate_info = CrateInfo::new(tcx, codegen_backend.target_cpu(tcx.sess));

let codegen = tcx.sess.time("codegen_crate", || {
if tcx.sess.opts.unstable_opts.no_codegen || !tcx.sess.opts.output_types.should_codegen() {
// Skip crate items and just output metadata in -Z no-codegen mode.
Expand All @@ -1315,7 +1313,7 @@ pub(crate) fn start_codegen<'tcx>(
// Linker::link will skip join_codegen in case of a CodegenResults Any value.
Box::new(CompiledModules { modules: vec![], allocator_module: None })
} else {
codegen_backend.codegen_crate(tcx, &crate_info)
codegen_backend.codegen_crate(tcx)
}
});

Expand All @@ -1327,6 +1325,8 @@ pub(crate) fn start_codegen<'tcx>(
tcx.sess.code_stats.print_type_sizes();
}

let crate_info = CrateInfo::new(tcx, codegen_backend.target_cpu(tcx.sess));

(codegen, crate_info, metadata)
}

Expand Down
Loading
Loading