Skip to content

Commit da6e4d4

Browse files
committed
Move CrateInfo computation after codegen_crate
CrateInfo is only necessary during linking and non-local LTO.
1 parent 4feb722 commit da6e4d4

15 files changed

Lines changed: 78 additions & 94 deletions

File tree

compiler/rustc_codegen_cranelift/src/driver/jit.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ fn create_jit_module(
3333
(jit_module, cx)
3434
}
3535

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

4141
let output_filenames = tcx.output_filenames(());
42+
let crate_info = CrateInfo::new(tcx, target_cpu);
4243
let should_write_ir = crate::pretty_clif::should_write_ir(tcx.sess);
43-
let (mut jit_module, mut debug_context) = create_jit_module(tcx, crate_info);
44+
let (mut jit_module, mut debug_context) = create_jit_module(tcx, &crate_info);
4445
let mut cached_context = Context::new();
4546

4647
let cgus = tcx.collect_and_partition_mono_items(()).codegen_units;

compiler/rustc_codegen_cranelift/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,12 @@ impl CodegenBackend for CraneliftCodegenBackend {
209209
.to_owned()
210210
}
211211

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

219219
#[cfg(not(feature = "jit"))]
220220
tcx.dcx().fatal("jit support was disabled when compiling rustc_codegen_cranelift");
@@ -228,6 +228,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
228228
ongoing_codegen: Box<dyn Any>,
229229
sess: &Session,
230230
outputs: &OutputFilenames,
231+
_crate_info: &CrateInfo,
231232
) -> (CompiledModules, FxIndexMap<WorkProductId, WorkProduct>) {
232233
ongoing_codegen.downcast::<driver::aot::OngoingCodegen>().unwrap().join(sess, outputs)
233234
}

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,20 +291,21 @@ impl CodegenBackend for GccCodegenBackend {
291291
target_cpu(sess).to_owned()
292292
}
293293

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

298298
fn join_codegen(
299299
&self,
300300
ongoing_codegen: Box<dyn Any>,
301301
sess: &Session,
302302
_outputs: &OutputFilenames,
303+
crate_info: &CrateInfo,
303304
) -> (CompiledModules, FxIndexMap<WorkProductId, WorkProduct>) {
304305
ongoing_codegen
305306
.downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<GccCodegenBackend>>()
306307
.expect("Expected GccCodegenBackend's OngoingCodegen, found Box<Any>")
307-
.join(sess)
308+
.join(sess, crate_info)
308309
}
309310

310311
fn target_config(&self, sess: &Session) -> TargetConfig {

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,20 +333,21 @@ impl CodegenBackend for LlvmCodegenBackend {
333333
crate::llvm_util::target_cpu(sess).to_string()
334334
}
335335

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

340340
fn join_codegen(
341341
&self,
342342
ongoing_codegen: Box<dyn Any>,
343343
sess: &Session,
344344
outputs: &OutputFilenames,
345+
crate_info: &CrateInfo,
345346
) -> (CompiledModules, FxIndexMap<WorkProductId, WorkProduct>) {
346347
let (compiled_modules, work_products) = ongoing_codegen
347348
.downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<LlvmCodegenBackend>>()
348349
.expect("Expected LlvmCodegenBackend's OngoingCodegen, found Box<Any>")
349-
.join(sess);
350+
.join(sess, crate_info);
350351

351352
if sess.opts.unstable_opts.llvm_time_trace {
352353
sess.time("llvm_dump_timing_file", || {

compiler/rustc_codegen_ssa/src/back/lto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn crate_type_allows_lto(crate_type: CrateType) -> bool {
8585
}
8686
}
8787

88-
pub(super) fn exported_symbols_for_lto(
88+
pub(crate) fn exported_symbols_for_lto(
8989
tcx: TyCtxt<'_>,
9090
each_linked_rlib_for_lto: &[CrateNum],
9191
) -> Vec<String> {

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 22 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use rustc_span::{FileName, InnerSpan, Span, SpanData};
3434
use rustc_target::spec::{MergeFunctions, SanitizerSet};
3535
use tracing::debug;
3636

37-
use crate::back::link::{self, ensure_removed};
37+
use crate::back::link::ensure_removed;
3838
use crate::back::lto::{self, SerializedModule, check_lto_allowed};
3939
use crate::errors::ErrorCreatingRemarkDir;
4040
use crate::traits::*;
@@ -389,18 +389,8 @@ fn generate_thin_lto_work<B: WriteBackendMethods>(
389389

390390
enum MaybeLtoModules<B: WriteBackendMethods> {
391391
NoLto(CompiledModules),
392-
FatLto {
393-
cgcx: CodegenContext,
394-
exported_symbols_for_lto: Arc<Vec<String>>,
395-
each_linked_rlib_file_for_lto: Vec<PathBuf>,
396-
needs_fat_lto: Vec<FatLtoInput<B>>,
397-
},
398-
ThinLto {
399-
cgcx: CodegenContext,
400-
exported_symbols_for_lto: Arc<Vec<String>>,
401-
each_linked_rlib_file_for_lto: Vec<PathBuf>,
402-
needs_thin_lto: Vec<ThinLtoInput<B>>,
403-
},
392+
FatLto { cgcx: CodegenContext, needs_fat_lto: Vec<FatLtoInput<B>> },
393+
ThinLto { cgcx: CodegenContext, needs_thin_lto: Vec<ThinLtoInput<B>> },
404394
}
405395

406396
fn need_bitcode_in_object(tcx: TyCtxt<'_>) -> bool {
@@ -424,7 +414,6 @@ fn need_pre_lto_bitcode_for_incr_comp(sess: &Session) -> bool {
424414
pub(crate) fn start_async_codegen<B: ExtraBackendMethods>(
425415
backend: B,
426416
tcx: TyCtxt<'_>,
427-
crate_info: &CrateInfo,
428417
allocator_module: Option<ModuleCodegen<B::Module>>,
429418
) -> OngoingCodegen<B> {
430419
let (coordinator_send, coordinator_receive) = channel();
@@ -440,7 +429,6 @@ pub(crate) fn start_async_codegen<B: ExtraBackendMethods>(
440429
let coordinator_thread = start_executing_work(
441430
backend.clone(),
442431
tcx,
443-
crate_info,
444432
shared_emitter,
445433
codegen_worker_send,
446434
coordinator_receive,
@@ -992,8 +980,8 @@ fn do_thin_lto<B: WriteBackendMethods>(
992980
prof: &SelfProfilerRef,
993981
shared_emitter: SharedEmitter,
994982
tm_factory: TargetMachineFactoryFn<B>,
995-
exported_symbols_for_lto: Arc<Vec<String>>,
996-
each_linked_rlib_for_lto: Vec<PathBuf>,
983+
exported_symbols_for_lto: &[String],
984+
each_linked_rlib_for_lto: &[PathBuf],
997985
needs_thin_lto: Vec<ThinLtoInput<B>>,
998986
) -> Vec<CompiledModule> {
999987
let _timer = prof.verbose_generic_activity("LLVM_thinlto");
@@ -1231,7 +1219,6 @@ enum MainThreadState {
12311219
fn start_executing_work<B: ExtraBackendMethods>(
12321220
backend: B,
12331221
tcx: TyCtxt<'_>,
1234-
crate_info: &CrateInfo,
12351222
shared_emitter: SharedEmitter,
12361223
codegen_worker_send: Sender<CguMessage>,
12371224
coordinator_receive: Receiver<Message<B>>,
@@ -1243,22 +1230,9 @@ fn start_executing_work<B: ExtraBackendMethods>(
12431230
let sess = tcx.sess;
12441231
let prof = sess.prof.clone();
12451232

1246-
let mut each_linked_rlib_for_lto = Vec::new();
1247-
let mut each_linked_rlib_file_for_lto = Vec::new();
1248-
if sess.lto() != Lto::No && sess.lto() != Lto::ThinLocal {
1249-
drop(link::each_linked_rlib(crate_info, None, &mut |cnum, path| {
1250-
if link::ignored_for_lto(sess, crate_info, cnum) {
1251-
return;
1252-
}
1253-
1254-
each_linked_rlib_for_lto.push(cnum);
1255-
each_linked_rlib_file_for_lto.push(path.to_path_buf());
1256-
}));
1257-
}
1258-
1259-
// Compute the set of symbols we need to retain when doing LTO (if we need to)
1233+
// Compute the set of symbols we need to retain when doing thin local LTO (if we need to)
12601234
let exported_symbols_for_lto =
1261-
Arc::new(lto::exported_symbols_for_lto(tcx, &each_linked_rlib_for_lto));
1235+
if sess.lto() == Lto::ThinLocal { lto::exported_symbols_for_lto(tcx, &[]) } else { vec![] };
12621236

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

1760-
return Ok(MaybeLtoModules::FatLto {
1761-
cgcx,
1762-
exported_symbols_for_lto,
1763-
each_linked_rlib_file_for_lto,
1764-
needs_fat_lto,
1765-
});
1734+
return Ok(MaybeLtoModules::FatLto { cgcx, needs_fat_lto });
17661735
} else if !needs_thin_lto.is_empty() || !lto_import_only_modules.is_empty() {
17671736
assert!(compiled_modules.is_empty());
17681737
assert!(needs_fat_lto.is_empty());
@@ -1777,8 +1746,8 @@ fn start_executing_work<B: ExtraBackendMethods>(
17771746
&prof,
17781747
shared_emitter.clone(),
17791748
tm_factory,
1780-
exported_symbols_for_lto,
1781-
each_linked_rlib_file_for_lto,
1749+
&exported_symbols_for_lto,
1750+
&[],
17821751
needs_thin_lto,
17831752
));
17841753
} else {
@@ -1790,12 +1759,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
17901759
});
17911760
}
17921761

1793-
return Ok(MaybeLtoModules::ThinLto {
1794-
cgcx,
1795-
exported_symbols_for_lto,
1796-
each_linked_rlib_file_for_lto,
1797-
needs_thin_lto,
1798-
});
1762+
return Ok(MaybeLtoModules::ThinLto { cgcx, needs_thin_lto });
17991763
}
18001764
}
18011765

@@ -2139,7 +2103,11 @@ pub struct OngoingCodegen<B: WriteBackendMethods> {
21392103
}
21402104

21412105
impl<B: WriteBackendMethods> OngoingCodegen<B> {
2142-
pub fn join(self, sess: &Session) -> (CompiledModules, FxIndexMap<WorkProductId, WorkProduct>) {
2106+
pub fn join(
2107+
self,
2108+
sess: &Session,
2109+
crate_info: &CrateInfo,
2110+
) -> (CompiledModules, FxIndexMap<WorkProductId, WorkProduct>) {
21432111
self.shared_emitter_main.check(sess, true);
21442112

21452113
let maybe_lto_modules = sess.time("join_worker_thread", || match self.coordinator.join() {
@@ -2163,12 +2131,7 @@ impl<B: WriteBackendMethods> OngoingCodegen<B> {
21632131
drop(shared_emitter);
21642132
compiled_modules
21652133
}
2166-
MaybeLtoModules::FatLto {
2167-
cgcx,
2168-
exported_symbols_for_lto,
2169-
each_linked_rlib_file_for_lto,
2170-
needs_fat_lto,
2171-
} => {
2134+
MaybeLtoModules::FatLto { cgcx, needs_fat_lto } => {
21722135
let tm_factory = self.backend.target_machine_factory(
21732136
sess,
21742137
cgcx.opt_level,
@@ -2181,19 +2144,14 @@ impl<B: WriteBackendMethods> OngoingCodegen<B> {
21812144
&cgcx,
21822145
shared_emitter,
21832146
tm_factory,
2184-
&exported_symbols_for_lto,
2185-
&each_linked_rlib_file_for_lto,
2147+
&crate_info.exported_symbols_for_lto,
2148+
&crate_info.each_linked_rlib_file_for_lto,
21862149
needs_fat_lto,
21872150
)],
21882151
allocator_module: None,
21892152
}
21902153
}
2191-
MaybeLtoModules::ThinLto {
2192-
cgcx,
2193-
exported_symbols_for_lto,
2194-
each_linked_rlib_file_for_lto,
2195-
needs_thin_lto,
2196-
} => {
2154+
MaybeLtoModules::ThinLto { cgcx, needs_thin_lto } => {
21972155
let tm_factory = self.backend.target_machine_factory(
21982156
sess,
21992157
cgcx.opt_level,
@@ -2206,8 +2164,8 @@ impl<B: WriteBackendMethods> OngoingCodegen<B> {
22062164
&sess.prof,
22072165
shared_emitter,
22082166
tm_factory,
2209-
exported_symbols_for_lto,
2210-
each_linked_rlib_file_for_lto,
2167+
&crate_info.exported_symbols_for_lto,
2168+
&crate_info.each_linked_rlib_file_for_lto,
22112169
needs_thin_lto,
22122170
),
22132171
allocator_module: None,

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -686,11 +686,7 @@ pub fn allocator_shim_contents(tcx: TyCtxt<'_>, kind: AllocatorKind) -> Vec<Allo
686686
methods
687687
}
688688

689-
pub fn codegen_crate<B: ExtraBackendMethods>(
690-
backend: B,
691-
tcx: TyCtxt<'_>,
692-
crate_info: &CrateInfo,
693-
) -> OngoingCodegen<B> {
689+
pub fn codegen_crate<B: ExtraBackendMethods>(backend: B, tcx: TyCtxt<'_>) -> OngoingCodegen<B> {
694690
if tcx.sess.target.need_explicit_cpu && tcx.sess.opts.cg.target_cpu.is_none() {
695691
// The target has no default cpu, but none is set explicitly
696692
tcx.dcx().emit_fatal(errors::CpuRequired);
@@ -734,7 +730,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
734730
None
735731
};
736732

737-
let ongoing_codegen = start_async_codegen(backend.clone(), tcx, crate_info, allocator_module);
733+
let ongoing_codegen = start_async_codegen(backend.clone(), tcx, allocator_module);
738734

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

964962
info.native_libraries.reserve(n_crates);
@@ -1044,6 +1042,25 @@ impl CrateInfo {
10441042
});
10451043
}
10461044

1045+
let mut each_linked_rlib_for_lto = Vec::new();
1046+
let mut each_linked_rlib_file_for_lto = Vec::new();
1047+
if tcx.sess.lto() != config::Lto::No && tcx.sess.lto() != config::Lto::ThinLocal {
1048+
drop(crate::back::link::each_linked_rlib(&info, None, &mut |cnum, path| {
1049+
if crate::back::link::ignored_for_lto(tcx.sess, &info, cnum) {
1050+
return;
1051+
}
1052+
1053+
each_linked_rlib_for_lto.push(cnum);
1054+
each_linked_rlib_file_for_lto.push(path.to_path_buf());
1055+
}));
1056+
}
1057+
info.each_linked_rlib_file_for_lto = each_linked_rlib_file_for_lto;
1058+
1059+
// FIXME move to -Zlink-only half such that each_linked_rlib_file_for_lto can be moved there too
1060+
// Compute the set of symbols we need to retain when doing LTO (if we need to)
1061+
info.exported_symbols_for_lto =
1062+
crate::back::lto::exported_symbols_for_lto(tcx, &each_linked_rlib_for_lto);
1063+
10471064
let embed_visualizers = tcx.crate_types().iter().any(|&crate_type| match crate_type {
10481065
CrateType::Executable | CrateType::Dylib | CrateType::Cdylib | CrateType::Sdylib => {
10491066
// These are crate types for which we invoke the linker and can embed

compiler/rustc_codegen_ssa/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ pub struct CrateInfo {
230230
pub natvis_debugger_visualizers: BTreeSet<DebuggerVisualizerFile>,
231231
pub lint_levels: CodegenLintLevels,
232232
pub metadata_symbol: String,
233+
pub each_linked_rlib_file_for_lto: Vec<PathBuf>,
234+
pub exported_symbols_for_lto: Vec<String>,
233235
}
234236

235237
/// Target-specific options that get set in `cfg(...)`.

compiler/rustc_codegen_ssa/src/traits/backend.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub trait CodegenBackend {
104104

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

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

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

121122
fn print_pass_timings(&self) {}

compiler/rustc_interface/src/passes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,8 +1305,6 @@ pub(crate) fn start_codegen<'tcx>(
13051305

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

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

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

1328+
let crate_info = CrateInfo::new(tcx, codegen_backend.target_cpu(tcx.sess));
1329+
13301330
(codegen, crate_info, metadata)
13311331
}
13321332

0 commit comments

Comments
 (0)