Skip to content

Commit badabab

Browse files
committed
Only borrow EncodedMetadata in codegen_crate
And move passing it to the linker to the driver code.
1 parent c68032f commit badabab

File tree

12 files changed

+90
-91
lines changed

12 files changed

+90
-91
lines changed

compiler/rustc_codegen_cranelift/src/driver/aot.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ pub(crate) struct OngoingCodegen {
6262
modules: Vec<OngoingModuleCodegen>,
6363
allocator_module: Option<CompiledModule>,
6464
metadata_module: Option<CompiledModule>,
65-
metadata: EncodedMetadata,
6665
crate_info: CrateInfo,
6766
concurrency_limiter: ConcurrencyLimiter,
6867
}
@@ -135,7 +134,6 @@ impl OngoingCodegen {
135134
modules,
136135
allocator_module: self.allocator_module,
137136
metadata_module: self.metadata_module,
138-
metadata: self.metadata,
139137
crate_info: self.crate_info,
140138
};
141139

@@ -706,11 +704,7 @@ fn emit_allocator_module(tcx: TyCtxt<'_>) -> Option<CompiledModule> {
706704
}
707705
}
708706

709-
pub(crate) fn run_aot(
710-
tcx: TyCtxt<'_>,
711-
metadata: EncodedMetadata,
712-
need_metadata_module: bool,
713-
) -> Box<OngoingCodegen> {
707+
pub(crate) fn run_aot(tcx: TyCtxt<'_>, metadata: Option<&EncodedMetadata>) -> Box<OngoingCodegen> {
714708
// FIXME handle `-Ctarget-cpu=native`
715709
let target_cpu = match tcx.sess.opts.cg.target_cpu {
716710
Some(ref name) => name,
@@ -727,7 +721,6 @@ pub(crate) fn run_aot(
727721
modules: vec![],
728722
allocator_module: None,
729723
metadata_module: None,
730-
metadata,
731724
crate_info: CrateInfo::new(tcx, target_cpu),
732725
concurrency_limiter: ConcurrencyLimiter::new(0),
733726
});
@@ -787,14 +780,12 @@ pub(crate) fn run_aot(
787780

788781
let allocator_module = emit_allocator_module(tcx);
789782

790-
let metadata_module =
791-
if need_metadata_module { Some(emit_metadata_module(tcx, &metadata)) } else { None };
783+
let metadata_module = metadata.map(|metadata| emit_metadata_module(tcx, metadata));
792784

793785
Box::new(OngoingCodegen {
794786
modules,
795787
allocator_module,
796788
metadata_module,
797-
metadata,
798789
crate_info: CrateInfo::new(tcx, target_cpu),
799790
concurrency_limiter: concurrency_limiter.0,
800791
})

compiler/rustc_codegen_cranelift/src/lib.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
238238
println!("Cranelift version: {}", cranelift_codegen::VERSION);
239239
}
240240

241-
fn codegen_crate(
242-
&self,
243-
tcx: TyCtxt<'_>,
244-
metadata: EncodedMetadata,
245-
need_metadata_module: bool,
246-
) -> Box<dyn Any> {
241+
fn codegen_crate(&self, tcx: TyCtxt<'_>, metadata: Option<&EncodedMetadata>) -> Box<dyn Any> {
247242
info!("codegen crate {}", tcx.crate_name(LOCAL_CRATE));
248243
let config = self.config.clone().unwrap_or_else(|| {
249244
BackendConfig::from_opts(&tcx.sess.opts.cg.llvm_args)
@@ -256,7 +251,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
256251
#[cfg(not(feature = "jit"))]
257252
tcx.dcx().fatal("jit support was disabled when compiling rustc_codegen_cranelift");
258253
} else {
259-
driver::aot::run_aot(tcx, metadata, need_metadata_module)
254+
driver::aot::run_aot(tcx, metadata)
260255
}
261256
}
262257

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -230,20 +230,9 @@ impl CodegenBackend for GccCodegenBackend {
230230
providers.global_backend_features = |tcx, ()| gcc_util::global_gcc_features(tcx.sess, true)
231231
}
232232

233-
fn codegen_crate(
234-
&self,
235-
tcx: TyCtxt<'_>,
236-
metadata: EncodedMetadata,
237-
need_metadata_module: bool,
238-
) -> Box<dyn Any> {
233+
fn codegen_crate(&self, tcx: TyCtxt<'_>, metadata: Option<&EncodedMetadata>) -> Box<dyn Any> {
239234
let target_cpu = target_cpu(tcx.sess);
240-
let res = codegen_crate(
241-
self.clone(),
242-
tcx,
243-
target_cpu.to_string(),
244-
metadata,
245-
need_metadata_module,
246-
);
235+
let res = codegen_crate(self.clone(), tcx, target_cpu.to_string(), metadata);
247236

248237
Box::new(res)
249238
}

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,15 +344,13 @@ impl CodegenBackend for LlvmCodegenBackend {
344344
fn codegen_crate<'tcx>(
345345
&self,
346346
tcx: TyCtxt<'tcx>,
347-
metadata: EncodedMetadata,
348-
need_metadata_module: bool,
347+
metadata: Option<&EncodedMetadata>,
349348
) -> Box<dyn Any> {
350349
Box::new(rustc_codegen_ssa::base::codegen_crate(
351350
LlvmCodegenBackend(()),
352351
tcx,
353352
crate::llvm_util::target_cpu(tcx.sess).to_string(),
354353
metadata,
355-
need_metadata_module,
356354
))
357355
}
358356

@@ -377,14 +375,20 @@ impl CodegenBackend for LlvmCodegenBackend {
377375
(codegen_results, work_products)
378376
}
379377

380-
fn link(&self, sess: &Session, codegen_results: CodegenResults, outputs: &OutputFilenames) {
378+
fn link(
379+
&self,
380+
sess: &Session,
381+
codegen_results: CodegenResults,
382+
metadata: EncodedMetadata,
383+
outputs: &OutputFilenames,
384+
) {
381385
use rustc_codegen_ssa::back::link::link_binary;
382386

383387
use crate::back::archive::LlvmArchiveBuilderBuilder;
384388

385389
// Run the linker on any artifacts that resulted from the LLVM run.
386390
// This should produce either a finished executable or library.
387-
link_binary(sess, &LlvmArchiveBuilderBuilder, codegen_results, outputs);
391+
link_binary(sess, &LlvmArchiveBuilderBuilder, codegen_results, metadata, outputs);
388392
}
389393
}
390394

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
2323
use rustc_macros::LintDiagnostic;
2424
use rustc_metadata::fs::{METADATA_FILENAME, copy_to_stdout, emit_wrapper_file};
2525
use rustc_metadata::{
26-
NativeLibSearchFallback, find_native_static_library, walk_native_lib_search_dirs,
26+
EncodedMetadata, NativeLibSearchFallback, find_native_static_library,
27+
walk_native_lib_search_dirs,
2728
};
2829
use rustc_middle::bug;
2930
use rustc_middle::lint::lint_level;
@@ -91,6 +92,7 @@ pub fn link_binary(
9192
sess: &Session,
9293
archive_builder_builder: &dyn ArchiveBuilderBuilder,
9394
codegen_results: CodegenResults,
95+
metadata: EncodedMetadata,
9496
outputs: &OutputFilenames,
9597
) {
9698
let _timer = sess.timer("link_binary");
@@ -142,6 +144,7 @@ pub fn link_binary(
142144
sess,
143145
archive_builder_builder,
144146
&codegen_results,
147+
&metadata,
145148
RlibFlavor::Normal,
146149
&path,
147150
)
@@ -152,6 +155,7 @@ pub fn link_binary(
152155
sess,
153156
archive_builder_builder,
154157
&codegen_results,
158+
&metadata,
155159
&out_filename,
156160
&path,
157161
);
@@ -312,18 +316,16 @@ fn link_rlib<'a>(
312316
sess: &'a Session,
313317
archive_builder_builder: &dyn ArchiveBuilderBuilder,
314318
codegen_results: &CodegenResults,
319+
metadata: &EncodedMetadata,
315320
flavor: RlibFlavor,
316321
tmpdir: &MaybeTempDir,
317322
) -> Box<dyn ArchiveBuilder + 'a> {
318323
let mut ab = archive_builder_builder.new_archive_builder(sess);
319324

320325
let trailing_metadata = match flavor {
321326
RlibFlavor::Normal => {
322-
let (metadata, metadata_position) = create_wrapper_file(
323-
sess,
324-
".rmeta".to_string(),
325-
codegen_results.metadata.stub_or_full(),
326-
);
327+
let (metadata, metadata_position) =
328+
create_wrapper_file(sess, ".rmeta".to_string(), metadata.stub_or_full());
327329
let metadata = emit_wrapper_file(sess, &metadata, tmpdir, METADATA_FILENAME);
328330
match metadata_position {
329331
MetadataPosition::First => {
@@ -473,6 +475,7 @@ fn link_staticlib(
473475
sess: &Session,
474476
archive_builder_builder: &dyn ArchiveBuilderBuilder,
475477
codegen_results: &CodegenResults,
478+
metadata: &EncodedMetadata,
476479
out_filename: &Path,
477480
tempdir: &MaybeTempDir,
478481
) {
@@ -481,6 +484,7 @@ fn link_staticlib(
481484
sess,
482485
archive_builder_builder,
483486
codegen_results,
487+
metadata,
484488
RlibFlavor::StaticlibBase,
485489
tempdir,
486490
);

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
2424
use rustc_incremental::{
2525
copy_cgu_workproduct_to_incr_comp_cache_dir, in_incr_comp_dir, in_incr_comp_dir_sess,
2626
};
27-
use rustc_metadata::EncodedMetadata;
2827
use rustc_metadata::fs::copy_to_stdout;
2928
use rustc_middle::bug;
3029
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
@@ -474,7 +473,6 @@ pub(crate) fn start_async_codegen<B: ExtraBackendMethods>(
474473
backend: B,
475474
tcx: TyCtxt<'_>,
476475
target_cpu: String,
477-
metadata: EncodedMetadata,
478476
metadata_module: Option<CompiledModule>,
479477
) -> OngoingCodegen<B> {
480478
let (coordinator_send, coordinator_receive) = channel();
@@ -506,7 +504,6 @@ pub(crate) fn start_async_codegen<B: ExtraBackendMethods>(
506504

507505
OngoingCodegen {
508506
backend,
509-
metadata,
510507
metadata_module,
511508
crate_info,
512509

@@ -2055,7 +2052,6 @@ impl<B: ExtraBackendMethods> Drop for Coordinator<B> {
20552052

20562053
pub struct OngoingCodegen<B: ExtraBackendMethods> {
20572054
pub backend: B,
2058-
pub metadata: EncodedMetadata,
20592055
pub metadata_module: Option<CompiledModule>,
20602056
pub crate_info: CrateInfo,
20612057
pub codegen_worker_receive: Receiver<CguMessage>,
@@ -2096,7 +2092,6 @@ impl<B: ExtraBackendMethods> OngoingCodegen<B> {
20962092

20972093
(
20982094
CodegenResults {
2099-
metadata: self.metadata,
21002095
crate_info: self.crate_info,
21012096

21022097
modules: compiled_modules.modules,

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -669,12 +669,11 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
669669
backend: B,
670670
tcx: TyCtxt<'_>,
671671
target_cpu: String,
672-
metadata: EncodedMetadata,
673-
need_metadata_module: bool,
672+
metadata: Option<&EncodedMetadata>,
674673
) -> OngoingCodegen<B> {
675674
// Skip crate items and just output metadata in -Z no-codegen mode.
676675
if tcx.sess.opts.unstable_opts.no_codegen || !tcx.sess.opts.output_types.should_codegen() {
677-
let ongoing_codegen = start_async_codegen(backend, tcx, target_cpu, metadata, None);
676+
let ongoing_codegen = start_async_codegen(backend, tcx, target_cpu, None);
678677

679678
ongoing_codegen.codegen_finished(tcx);
680679

@@ -707,7 +706,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
707706
}
708707
}
709708

710-
let metadata_module = need_metadata_module.then(|| {
709+
let metadata_module = if let Some(metadata) = metadata {
711710
// Emit compressed metadata object.
712711
let metadata_cgu_name =
713712
cgu_name_builder.build_cgu_name(LOCAL_CRATE, &["crate"], Some("metadata")).to_string();
@@ -719,13 +718,13 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
719718
);
720719
let data = create_compressed_metadata_file(
721720
tcx.sess,
722-
&metadata,
721+
metadata,
723722
&exported_symbols::metadata_symbol_name(tcx),
724723
);
725724
if let Err(error) = std::fs::write(&file_name, data) {
726725
tcx.dcx().emit_fatal(errors::MetadataObjectFileWrite { error });
727726
}
728-
CompiledModule {
727+
Some(CompiledModule {
729728
name: metadata_cgu_name,
730729
kind: ModuleKind::Metadata,
731730
object: Some(file_name),
@@ -734,12 +733,13 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
734733
assembly: None,
735734
llvm_ir: None,
736735
links_from_incr_cache: Vec::new(),
737-
}
736+
})
738737
})
739-
});
738+
} else {
739+
None
740+
};
740741

741-
let ongoing_codegen =
742-
start_async_codegen(backend.clone(), tcx, target_cpu, metadata, metadata_module);
742+
let ongoing_codegen = start_async_codegen(backend.clone(), tcx, target_cpu, metadata_module);
743743

744744
// Codegen an allocator shim, if necessary.
745745
if let Some(kind) = allocator_kind_for_codegen(tcx) {

compiler/rustc_codegen_ssa/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use rustc_data_structures::unord::UnordMap;
3131
use rustc_hir::CRATE_HIR_ID;
3232
use rustc_hir::def_id::CrateNum;
3333
use rustc_macros::{Decodable, Encodable, HashStable};
34+
use rustc_metadata::EncodedMetadata;
3435
use rustc_middle::dep_graph::WorkProduct;
3536
use rustc_middle::lint::LevelAndSource;
3637
use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
@@ -258,7 +259,6 @@ pub struct CodegenResults {
258259
pub modules: Vec<CompiledModule>,
259260
pub allocator_module: Option<CompiledModule>,
260261
pub metadata_module: Option<CompiledModule>,
261-
pub metadata: rustc_metadata::EncodedMetadata,
262262
pub crate_info: CrateInfo,
263263
}
264264

@@ -303,6 +303,7 @@ impl CodegenResults {
303303
sess: &Session,
304304
rlink_file: &Path,
305305
codegen_results: &CodegenResults,
306+
metadata: &EncodedMetadata,
306307
outputs: &OutputFilenames,
307308
) -> Result<usize, io::Error> {
308309
let mut encoder = FileEncoder::new(rlink_file)?;
@@ -312,14 +313,15 @@ impl CodegenResults {
312313
encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes());
313314
encoder.emit_str(sess.cfg_version);
314315
Encodable::encode(codegen_results, &mut encoder);
316+
Encodable::encode(metadata, &mut encoder);
315317
Encodable::encode(outputs, &mut encoder);
316318
encoder.finish().map_err(|(_path, err)| err)
317319
}
318320

319321
pub fn deserialize_rlink(
320322
sess: &Session,
321323
data: Vec<u8>,
322-
) -> Result<(Self, OutputFilenames), CodegenErrors> {
324+
) -> Result<(Self, EncodedMetadata, OutputFilenames), CodegenErrors> {
323325
// The Decodable machinery is not used here because it panics if the input data is invalid
324326
// and because its internal representation may change.
325327
if !data.starts_with(RLINK_MAGIC) {
@@ -350,8 +352,9 @@ impl CodegenResults {
350352
}
351353

352354
let codegen_results = CodegenResults::decode(&mut decoder);
355+
let metadata = EncodedMetadata::decode(&mut decoder);
353356
let outputs = OutputFilenames::decode(&mut decoder);
354-
Ok((codegen_results, outputs))
357+
Ok((codegen_results, metadata, outputs))
355358
}
356359
}
357360

compiler/rustc_codegen_ssa/src/traits/backend.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ pub trait CodegenBackend {
7777
fn codegen_crate<'tcx>(
7878
&self,
7979
tcx: TyCtxt<'tcx>,
80-
metadata: EncodedMetadata,
81-
need_metadata_module: bool,
80+
metadata: Option<&EncodedMetadata>,
8281
) -> Box<dyn Any>;
8382

8483
/// This is called on the returned `Box<dyn Any>` from [`codegen_crate`](Self::codegen_crate)
@@ -94,8 +93,14 @@ pub trait CodegenBackend {
9493
) -> (CodegenResults, FxIndexMap<WorkProductId, WorkProduct>);
9594

9695
/// This is called on the returned [`CodegenResults`] from [`join_codegen`](Self::join_codegen).
97-
fn link(&self, sess: &Session, codegen_results: CodegenResults, outputs: &OutputFilenames) {
98-
link_binary(sess, &ArArchiveBuilderBuilder, codegen_results, outputs);
96+
fn link(
97+
&self,
98+
sess: &Session,
99+
codegen_results: CodegenResults,
100+
metadata: EncodedMetadata,
101+
outputs: &OutputFilenames,
102+
) {
103+
link_binary(sess, &ArArchiveBuilderBuilder, codegen_results, metadata, outputs);
99104
}
100105
}
101106

0 commit comments

Comments
 (0)