Introduce EmitObj.

Currently, there are three fields in `ModuleConfig` that dictate
how object files are emitted: `emit_obj`, `obj_is_bitcode`, and
`embed_bitcode`.

Some of the combinations of these fields are nonsensical, in particular
having both `obj_is_bitcode` and `embed_bitcode` true at the same time.

Also, currently:
- we needlessly emit and then delete a bytecode file if `obj_is_bitcode`
  is true but `emit_obj` is false;
- we needlessly embed bitcode in the LLVM module if `embed_bitcode` is
  true and `emit_obj` is false.

This commit combines the three fields into one, with a new type
`EmitObj` (and the auxiliary `BitcodeSection`) which can encode five
different possibilities.

In the old code, `set_flags` would set `obj_is_bitcode` and
`embed_bitcode` on all three of the configs (`modules`, `allocator`,
`metadata`) if the relevant other conditions were met, even if no object
code needed to be emitted for one or more of them. Whereas
`start_async_codegen` would set `emit_obj`, but only for those configs
that need it.

In the new code, `start_async_codegen` does all the work of setting
`emit_obj`, and it only does that for the configs that need it.
`set_flags` no longer sets anything related to object file emission.
This commit is contained in:
Nicholas Nethercote 2020-03-24 12:24:52 +11:00
parent e1d1db790f
commit 87ef16c9dc
2 changed files with 64 additions and 49 deletions

View file

@ -16,7 +16,9 @@ use crate::ModuleLlvm;
use log::debug;
use rustc::bug;
use rustc::ty::TyCtxt;
use rustc_codegen_ssa::back::write::{run_assembler, CodegenContext, EmbedBitcode, ModuleConfig};
use rustc_codegen_ssa::back::write::{
run_assembler, BitcodeSection, CodegenContext, EmitObj, ModuleConfig,
};
use rustc_codegen_ssa::traits::*;
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen, RLIB_BYTECODE_EXTENSION};
use rustc_data_structures::small_c_str::SmallCStr;
@ -651,7 +653,7 @@ pub(crate) unsafe fn codegen(
let thin = ThinBuffer::new(llmod);
let data = thin.data();
if config.emit_bc || config.obj_is_bitcode {
if config.emit_bc || config.emit_obj == EmitObj::Bitcode {
let _timer = cgcx.prof.generic_activity_with_arg(
"LLVM_module_codegen_emit_bitcode",
&module.name[..],
@ -662,7 +664,7 @@ pub(crate) unsafe fn codegen(
}
}
if config.embed_bitcode == EmbedBitcode::Full {
if config.emit_obj == EmitObj::ObjectCode(BitcodeSection::Full) {
let _timer = cgcx.prof.generic_activity_with_arg(
"LLVM_module_codegen_embed_bitcode",
&module.name[..],
@ -682,7 +684,7 @@ pub(crate) unsafe fn codegen(
diag_handler.err(&msg);
}
}
} else if config.embed_bitcode == EmbedBitcode::Marker {
} else if config.emit_obj == EmitObj::ObjectCode(BitcodeSection::Marker) {
embed_bitcode(cgcx, llcx, llmod, None);
}
@ -732,9 +734,9 @@ pub(crate) unsafe fn codegen(
})?;
}
let config_emit_normal_obj = config.emit_obj && !config.obj_is_bitcode;
let config_emit_object_code = matches!(config.emit_obj, EmitObj::ObjectCode(_));
if config.emit_asm || (config_emit_normal_obj && config.no_integrated_as) {
if config.emit_asm || (config_emit_object_code && config.no_integrated_as) {
let _timer = cgcx
.prof
.generic_activity_with_arg("LLVM_module_codegen_emit_asm", &module.name[..]);
@ -743,13 +745,13 @@ pub(crate) unsafe fn codegen(
// We can't use the same module for asm and binary output, because that triggers
// various errors like invalid IR or broken binaries, so we might have to clone the
// module to produce the asm output
let llmod = if config.emit_obj { llvm::LLVMCloneModule(llmod) } else { llmod };
let llmod = if config_emit_object_code { llvm::LLVMCloneModule(llmod) } else { llmod };
with_codegen(tm, llmod, config.no_builtins, |cpm| {
write_output_file(diag_handler, tm, cpm, llmod, &path, llvm::FileType::AssemblyFile)
})?;
}
if config_emit_normal_obj {
if config_emit_object_code {
if !config.no_integrated_as {
let _timer = cgcx
.prof
@ -775,14 +777,10 @@ pub(crate) unsafe fn codegen(
drop(fs::remove_file(&assembly));
}
}
}
if config.obj_is_bitcode {
if config.emit_obj {
debug!("copying bitcode {:?} to obj {:?}", bc_out, obj_out);
if let Err(e) = link_or_copy(&bc_out, &obj_out) {
diag_handler.err(&format!("failed to copy bitcode to object file: {}", e));
}
} else if config.emit_obj == EmitObj::Bitcode {
debug!("copying bitcode {:?} to obj {:?}", bc_out, obj_out);
if let Err(e) = link_or_copy(&bc_out, &obj_out) {
diag_handler.err(&format!("failed to copy bitcode to object file: {}", e));
}
if !config.emit_bc {
@ -796,7 +794,7 @@ pub(crate) unsafe fn codegen(
drop(handlers);
}
Ok(module.into_compiled_module(
config.emit_obj,
config.emit_obj != EmitObj::None,
config.emit_bc,
config.emit_bc_compressed,
&cgcx.output_filenames,