Skip to content

Commit 9795908

Browse files
authored
Rollup merge of #141769 - bjorn3:codegen_metadata_module_rework, r=workingjubilee,saethlin
Move metadata object generation for dylibs to the linker code This deduplicates some code between codegen backends and may in the future allow adding extra metadata that is only known at link time. Prerequisite of #96708.
2 parents 8b24077 + 2974c99 commit 9795908

File tree

2 files changed

+3
-62
lines changed

2 files changed

+3
-62
lines changed

src/driver/aot.rs

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ use std::thread::JoinHandle;
1111
use cranelift_object::{ObjectBuilder, ObjectModule};
1212
use rustc_codegen_ssa::assert_module_sources::CguReuse;
1313
use rustc_codegen_ssa::back::link::ensure_removed;
14-
use rustc_codegen_ssa::back::metadata::create_compressed_metadata_file;
1514
use rustc_codegen_ssa::base::determine_cgu_reuse;
1615
use rustc_codegen_ssa::{
1716
CodegenResults, CompiledModule, CrateInfo, ModuleKind, errors as ssa_errors,
1817
};
1918
use rustc_data_structures::profiling::SelfProfilerRef;
2019
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2120
use rustc_data_structures::sync::{IntoDynSyncSend, par_map};
22-
use rustc_metadata::EncodedMetadata;
2321
use rustc_metadata::fs::copy_to_stdout;
2422
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
2523
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
@@ -61,8 +59,6 @@ impl<HCX> HashStable<HCX> for OngoingModuleCodegen {
6159
pub(crate) struct OngoingCodegen {
6260
modules: Vec<OngoingModuleCodegen>,
6361
allocator_module: Option<CompiledModule>,
64-
metadata_module: Option<CompiledModule>,
65-
metadata: EncodedMetadata,
6662
crate_info: CrateInfo,
6763
concurrency_limiter: ConcurrencyLimiter,
6864
}
@@ -134,8 +130,6 @@ impl OngoingCodegen {
134130
let codegen_results = CodegenResults {
135131
modules,
136132
allocator_module: self.allocator_module,
137-
metadata_module: self.metadata_module,
138-
metadata: self.metadata,
139133
crate_info: self.crate_info,
140134
};
141135

@@ -646,42 +640,6 @@ fn module_codegen(
646640
}))
647641
}
648642

649-
fn emit_metadata_module(tcx: TyCtxt<'_>, metadata: &EncodedMetadata) -> CompiledModule {
650-
use rustc_middle::mir::mono::CodegenUnitNameBuilder;
651-
652-
let _timer = tcx.sess.timer("write compressed metadata");
653-
654-
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
655-
let metadata_cgu_name = cgu_name_builder
656-
.build_cgu_name(LOCAL_CRATE, ["crate"], Some("metadata"))
657-
.as_str()
658-
.to_string();
659-
660-
let tmp_file = tcx.output_filenames(()).temp_path_for_cgu(
661-
OutputType::Metadata,
662-
&metadata_cgu_name,
663-
tcx.sess.invocation_temp.as_deref(),
664-
);
665-
666-
let symbol_name = rustc_middle::middle::exported_symbols::metadata_symbol_name(tcx);
667-
let obj = create_compressed_metadata_file(tcx.sess, metadata, &symbol_name);
668-
669-
if let Err(err) = std::fs::write(&tmp_file, obj) {
670-
tcx.dcx().fatal(format!("error writing metadata object file: {}", err));
671-
}
672-
673-
CompiledModule {
674-
name: metadata_cgu_name,
675-
kind: ModuleKind::Metadata,
676-
object: Some(tmp_file),
677-
dwarf_object: None,
678-
bytecode: None,
679-
assembly: None,
680-
llvm_ir: None,
681-
links_from_incr_cache: Vec::new(),
682-
}
683-
}
684-
685643
fn emit_allocator_module(tcx: TyCtxt<'_>) -> Option<CompiledModule> {
686644
let mut allocator_module = make_module(tcx.sess, "allocator_shim".to_string());
687645
let created_alloc_shim = crate::allocator::codegen(tcx, &mut allocator_module);
@@ -706,11 +664,7 @@ fn emit_allocator_module(tcx: TyCtxt<'_>) -> Option<CompiledModule> {
706664
}
707665
}
708666

709-
pub(crate) fn run_aot(
710-
tcx: TyCtxt<'_>,
711-
metadata: EncodedMetadata,
712-
need_metadata_module: bool,
713-
) -> Box<OngoingCodegen> {
667+
pub(crate) fn run_aot(tcx: TyCtxt<'_>) -> Box<OngoingCodegen> {
714668
// FIXME handle `-Ctarget-cpu=native`
715669
let target_cpu = match tcx.sess.opts.cg.target_cpu {
716670
Some(ref name) => name,
@@ -726,8 +680,6 @@ pub(crate) fn run_aot(
726680
return Box::new(OngoingCodegen {
727681
modules: vec![],
728682
allocator_module: None,
729-
metadata_module: None,
730-
metadata,
731683
crate_info: CrateInfo::new(tcx, target_cpu),
732684
concurrency_limiter: ConcurrencyLimiter::new(0),
733685
});
@@ -787,14 +739,9 @@ pub(crate) fn run_aot(
787739

788740
let allocator_module = emit_allocator_module(tcx);
789741

790-
let metadata_module =
791-
if need_metadata_module { Some(emit_metadata_module(tcx, &metadata)) } else { None };
792-
793742
Box::new(OngoingCodegen {
794743
modules,
795744
allocator_module,
796-
metadata_module,
797-
metadata,
798745
crate_info: CrateInfo::new(tcx, target_cpu),
799746
concurrency_limiter: concurrency_limiter.0,
800747
})

src/lib.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ use cranelift_codegen::isa::TargetIsa;
4646
use cranelift_codegen::settings::{self, Configurable};
4747
use rustc_codegen_ssa::traits::CodegenBackend;
4848
use rustc_codegen_ssa::{CodegenResults, TargetConfig};
49-
use rustc_metadata::EncodedMetadata;
5049
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
5150
use rustc_session::Session;
5251
use rustc_session::config::OutputFilenames;
@@ -238,12 +237,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
238237
println!("Cranelift version: {}", cranelift_codegen::VERSION);
239238
}
240239

241-
fn codegen_crate(
242-
&self,
243-
tcx: TyCtxt<'_>,
244-
metadata: EncodedMetadata,
245-
need_metadata_module: bool,
246-
) -> Box<dyn Any> {
240+
fn codegen_crate(&self, tcx: TyCtxt<'_>) -> Box<dyn Any> {
247241
info!("codegen crate {}", tcx.crate_name(LOCAL_CRATE));
248242
let config = self.config.clone().unwrap_or_else(|| {
249243
BackendConfig::from_opts(&tcx.sess.opts.cg.llvm_args)
@@ -256,7 +250,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
256250
#[cfg(not(feature = "jit"))]
257251
tcx.dcx().fatal("jit support was disabled when compiling rustc_codegen_cranelift");
258252
} else {
259-
driver::aot::run_aot(tcx, metadata, need_metadata_module)
253+
driver::aot::run_aot(tcx)
260254
}
261255
}
262256

0 commit comments

Comments
 (0)