Skip to content

Commit da50297

Browse files
committed
Save pre-link bitcode to ModuleCodegen
1 parent 9431427 commit da50297

File tree

7 files changed

+33
-22
lines changed

7 files changed

+33
-22
lines changed

Diff for: compiler/rustc_codegen_gcc/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl WriteBackendMethods for GccCodegenBackend {
393393
unsafe fn optimize(
394394
_cgcx: &CodegenContext<Self>,
395395
_dcx: DiagCtxtHandle<'_>,
396-
module: &ModuleCodegen<Self::Module>,
396+
module: &mut ModuleCodegen<Self::Module>,
397397
config: &ModuleConfig,
398398
) -> Result<(), FatalError> {
399399
module.module_llvm.context.set_optimization_level(to_gcc_opt_level(config.opt_level));

Diff for: compiler/rustc_codegen_llvm/src/back/lto.rs

+4
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,10 @@ pub(crate) unsafe fn optimize_thin_module(
776776
// that LLVM Context and Module.
777777
let module_llvm = ModuleLlvm::parse(cgcx, module_name, thin_module.data(), dcx)?;
778778
let mut module = ModuleCodegen::new_regular(thin_module.name(), module_llvm);
779+
// Given that the newly created module lacks a thinlto buffer for embedding, we need to re-add it here.
780+
if cgcx.config(ModuleKind::Regular).embed_bitcode() {
781+
module.thin_lto_buffer = Some(thin_module.data().to_vec());
782+
}
779783
{
780784
let target = &*module.module_llvm.tm;
781785
let llmod = module.module_llvm.llmod();

Diff for: compiler/rustc_codegen_llvm/src/back/write.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ pub(crate) unsafe fn llvm_optimize(
689689
pub(crate) unsafe fn optimize(
690690
cgcx: &CodegenContext<LlvmCodegenBackend>,
691691
dcx: DiagCtxtHandle<'_>,
692-
module: &ModuleCodegen<ModuleLlvm>,
692+
module: &mut ModuleCodegen<ModuleLlvm>,
693693
config: &ModuleConfig,
694694
) -> Result<(), FatalError> {
695695
let _timer = cgcx.prof.generic_activity_with_arg("LLVM_module_optimize", &*module.name);
@@ -745,10 +745,7 @@ pub(crate) unsafe fn optimize(
745745
}?;
746746
if let Some(thin_lto_buffer) = thin_lto_buffer {
747747
let thin_lto_buffer = unsafe { ThinBuffer::from_raw_ptr(thin_lto_buffer) };
748-
let thin_bc_out = cgcx.output_filenames.temp_path(OutputType::ThinBitcode, module_name);
749-
if let Err(err) = fs::write(&thin_bc_out, thin_lto_buffer.data()) {
750-
dcx.emit_err(WriteBytecode { path: &thin_bc_out, err });
751-
}
748+
module.thin_lto_buffer = Some(thin_lto_buffer.data().to_vec());
752749
let bc_summary_out =
753750
cgcx.output_filenames.temp_path(OutputType::ThinLinkBitcode, module_name);
754751
if config.emit_thin_lto_summary
@@ -848,20 +845,14 @@ pub(crate) unsafe fn codegen(
848845
}
849846
}
850847

851-
if config.emit_obj == EmitObj::ObjectCode(BitcodeSection::Full)
852-
&& module.kind == ModuleKind::Regular
853-
{
848+
if config.embed_bitcode() && module.kind == ModuleKind::Regular {
854849
let _timer = cgcx
855850
.prof
856851
.generic_activity_with_arg("LLVM_module_codegen_embed_bitcode", &*module.name);
857-
let thin_bc_out =
858-
cgcx.output_filenames.temp_path(OutputType::ThinBitcode, module_name);
859-
assert!(thin_bc_out.exists(), "cannot find {:?} as embedded bitcode", thin_bc_out);
860-
let data = fs::read(&thin_bc_out).unwrap();
861-
debug!("removing embed bitcode file {:?}", thin_bc_out);
862-
ensure_removed(dcx, &thin_bc_out);
852+
let thin_bc =
853+
module.thin_lto_buffer.as_deref().expect("cannot find embedded bitcode");
863854
unsafe {
864-
embed_bitcode(cgcx, llcx, llmod, &config.bc_cmdline, &data);
855+
embed_bitcode(cgcx, llcx, llmod, &config.bc_cmdline, &thin_bc);
865856
}
866857
}
867858
}

Diff for: compiler/rustc_codegen_llvm/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl WriteBackendMethods for LlvmCodegenBackend {
194194
unsafe fn optimize(
195195
cgcx: &CodegenContext<Self>,
196196
dcx: DiagCtxtHandle<'_>,
197-
module: &ModuleCodegen<Self::Module>,
197+
module: &mut ModuleCodegen<Self::Module>,
198198
config: &ModuleConfig,
199199
) -> Result<(), FatalError> {
200200
unsafe { back::write::optimize(cgcx, dcx, module, config) }

Diff for: compiler/rustc_codegen_ssa/src/back/write.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ impl ModuleConfig {
278278
|| self.emit_obj == EmitObj::Bitcode
279279
|| self.emit_obj == EmitObj::ObjectCode(BitcodeSection::Full)
280280
}
281+
282+
pub fn embed_bitcode(&self) -> bool {
283+
self.emit_obj == EmitObj::ObjectCode(BitcodeSection::Full)
284+
}
281285
}
282286

283287
/// Configuration passed to the function returned by the `target_machine_factory`.
@@ -880,14 +884,14 @@ pub(crate) fn compute_per_cgu_lto_type(
880884

881885
fn execute_optimize_work_item<B: ExtraBackendMethods>(
882886
cgcx: &CodegenContext<B>,
883-
module: ModuleCodegen<B::Module>,
887+
mut module: ModuleCodegen<B::Module>,
884888
module_config: &ModuleConfig,
885889
) -> Result<WorkItemResult<B>, FatalError> {
886890
let dcx = cgcx.create_dcx();
887891
let dcx = dcx.handle();
888892

889893
unsafe {
890-
B::optimize(cgcx, dcx, &module, module_config)?;
894+
B::optimize(cgcx, dcx, &mut module, module_config)?;
891895
}
892896

893897
// After we've done the initial round of optimizations we need to

Diff for: compiler/rustc_codegen_ssa/src/lib.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,27 @@ pub struct ModuleCodegen<M> {
7575
pub name: String,
7676
pub module_llvm: M,
7777
pub kind: ModuleKind,
78+
/// Saving the ThinLTO buffer for embedding in the object file.
79+
pub thin_lto_buffer: Option<Vec<u8>>,
7880
}
7981

8082
impl<M> ModuleCodegen<M> {
8183
pub fn new_regular(name: impl Into<String>, module: M) -> Self {
82-
Self { name: name.into(), module_llvm: module, kind: ModuleKind::Regular }
84+
Self {
85+
name: name.into(),
86+
module_llvm: module,
87+
kind: ModuleKind::Regular,
88+
thin_lto_buffer: None,
89+
}
8390
}
8491

8592
pub fn new_allocator(name: impl Into<String>, module: M) -> Self {
86-
Self { name: name.into(), module_llvm: module, kind: ModuleKind::Allocator }
93+
Self {
94+
name: name.into(),
95+
module_llvm: module,
96+
kind: ModuleKind::Allocator,
97+
thin_lto_buffer: None,
98+
}
8799
}
88100

89101
pub fn into_compiled_module(

Diff for: compiler/rustc_codegen_ssa/src/traits/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub trait WriteBackendMethods: 'static + Sized + Clone {
4040
unsafe fn optimize(
4141
cgcx: &CodegenContext<Self>,
4242
dcx: DiagCtxtHandle<'_>,
43-
module: &ModuleCodegen<Self::Module>,
43+
module: &mut ModuleCodegen<Self::Module>,
4444
config: &ModuleConfig,
4545
) -> Result<(), FatalError>;
4646
fn optimize_fat(

0 commit comments

Comments
 (0)