Skip to content

Commit 87ef16c

Browse files
committed
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.
1 parent e1d1db7 commit 87ef16c

File tree

2 files changed

+64
-49
lines changed

2 files changed

+64
-49
lines changed

src/librustc_codegen_llvm/back/write.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use crate::ModuleLlvm;
1616
use log::debug;
1717
use rustc::bug;
1818
use rustc::ty::TyCtxt;
19-
use rustc_codegen_ssa::back::write::{run_assembler, CodegenContext, EmbedBitcode, ModuleConfig};
19+
use rustc_codegen_ssa::back::write::{
20+
run_assembler, BitcodeSection, CodegenContext, EmitObj, ModuleConfig,
21+
};
2022
use rustc_codegen_ssa::traits::*;
2123
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen, RLIB_BYTECODE_EXTENSION};
2224
use rustc_data_structures::small_c_str::SmallCStr;
@@ -651,7 +653,7 @@ pub(crate) unsafe fn codegen(
651653
let thin = ThinBuffer::new(llmod);
652654
let data = thin.data();
653655

654-
if config.emit_bc || config.obj_is_bitcode {
656+
if config.emit_bc || config.emit_obj == EmitObj::Bitcode {
655657
let _timer = cgcx.prof.generic_activity_with_arg(
656658
"LLVM_module_codegen_emit_bitcode",
657659
&module.name[..],
@@ -662,7 +664,7 @@ pub(crate) unsafe fn codegen(
662664
}
663665
}
664666

665-
if config.embed_bitcode == EmbedBitcode::Full {
667+
if config.emit_obj == EmitObj::ObjectCode(BitcodeSection::Full) {
666668
let _timer = cgcx.prof.generic_activity_with_arg(
667669
"LLVM_module_codegen_embed_bitcode",
668670
&module.name[..],
@@ -682,7 +684,7 @@ pub(crate) unsafe fn codegen(
682684
diag_handler.err(&msg);
683685
}
684686
}
685-
} else if config.embed_bitcode == EmbedBitcode::Marker {
687+
} else if config.emit_obj == EmitObj::ObjectCode(BitcodeSection::Marker) {
686688
embed_bitcode(cgcx, llcx, llmod, None);
687689
}
688690

@@ -732,9 +734,9 @@ pub(crate) unsafe fn codegen(
732734
})?;
733735
}
734736

735-
let config_emit_normal_obj = config.emit_obj && !config.obj_is_bitcode;
737+
let config_emit_object_code = matches!(config.emit_obj, EmitObj::ObjectCode(_));
736738

737-
if config.emit_asm || (config_emit_normal_obj && config.no_integrated_as) {
739+
if config.emit_asm || (config_emit_object_code && config.no_integrated_as) {
738740
let _timer = cgcx
739741
.prof
740742
.generic_activity_with_arg("LLVM_module_codegen_emit_asm", &module.name[..]);
@@ -743,13 +745,13 @@ pub(crate) unsafe fn codegen(
743745
// We can't use the same module for asm and binary output, because that triggers
744746
// various errors like invalid IR or broken binaries, so we might have to clone the
745747
// module to produce the asm output
746-
let llmod = if config.emit_obj { llvm::LLVMCloneModule(llmod) } else { llmod };
748+
let llmod = if config_emit_object_code { llvm::LLVMCloneModule(llmod) } else { llmod };
747749
with_codegen(tm, llmod, config.no_builtins, |cpm| {
748750
write_output_file(diag_handler, tm, cpm, llmod, &path, llvm::FileType::AssemblyFile)
749751
})?;
750752
}
751753

752-
if config_emit_normal_obj {
754+
if config_emit_object_code {
753755
if !config.no_integrated_as {
754756
let _timer = cgcx
755757
.prof
@@ -775,14 +777,10 @@ pub(crate) unsafe fn codegen(
775777
drop(fs::remove_file(&assembly));
776778
}
777779
}
778-
}
779-
780-
if config.obj_is_bitcode {
781-
if config.emit_obj {
782-
debug!("copying bitcode {:?} to obj {:?}", bc_out, obj_out);
783-
if let Err(e) = link_or_copy(&bc_out, &obj_out) {
784-
diag_handler.err(&format!("failed to copy bitcode to object file: {}", e));
785-
}
780+
} else if config.emit_obj == EmitObj::Bitcode {
781+
debug!("copying bitcode {:?} to obj {:?}", bc_out, obj_out);
782+
if let Err(e) = link_or_copy(&bc_out, &obj_out) {
783+
diag_handler.err(&format!("failed to copy bitcode to object file: {}", e));
786784
}
787785

788786
if !config.emit_bc {
@@ -796,7 +794,7 @@ pub(crate) unsafe fn codegen(
796794
drop(handlers);
797795
}
798796
Ok(module.into_compiled_module(
799-
config.emit_obj,
797+
config.emit_obj != EmitObj::None,
800798
config.emit_bc,
801799
config.emit_bc_compressed,
802800
&cgcx.output_filenames,

src/librustc_codegen_ssa/back/write.rs

+49-32
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,31 @@ use std::thread;
5151

5252
const PRE_LTO_BC_EXT: &str = "pre-lto.bc";
5353

54-
/// The kind of bitcode to embed in object files.
55-
#[derive(PartialEq)]
56-
pub enum EmbedBitcode {
54+
/// What kind of object file to emit.
55+
#[derive(Clone, Copy, PartialEq)]
56+
pub enum EmitObj {
57+
// No object file.
5758
None,
59+
60+
// Just uncompressed llvm bitcode. Provides easy compatibility with
61+
// emscripten's ecc compiler, when used as the linker.
62+
Bitcode,
63+
64+
// Object code, possibly augmented with a bitcode section.
65+
ObjectCode(BitcodeSection),
66+
}
67+
68+
/// What kind of llvm bitcode section to embed in an object file.
69+
#[derive(Clone, Copy, PartialEq)]
70+
pub enum BitcodeSection {
71+
// No bitcode section.
72+
None,
73+
74+
// An empty bitcode section (to placate tools such as the iOS linker that
75+
// require this section even if they don't use it).
5876
Marker,
77+
78+
// A full, uncompressed bitcode section.
5979
Full,
6080
}
6181

@@ -84,7 +104,7 @@ pub struct ModuleConfig {
84104
pub emit_bc_compressed: bool,
85105
pub emit_ir: bool,
86106
pub emit_asm: bool,
87-
pub emit_obj: bool,
107+
pub emit_obj: EmitObj,
88108
// Miscellaneous flags. These are mostly copied from command-line
89109
// options.
90110
pub verify_llvm_ir: bool,
@@ -96,12 +116,7 @@ pub struct ModuleConfig {
96116
pub merge_functions: bool,
97117
pub inline_threshold: Option<usize>,
98118
pub new_llvm_pass_manager: Option<bool>,
99-
// Instead of creating an object file by doing LLVM codegen, just
100-
// make the object file bitcode. Provides easy compatibility with
101-
// emscripten's ecc compiler, when used as the linker.
102-
pub obj_is_bitcode: bool,
103119
pub no_integrated_as: bool,
104-
pub embed_bitcode: EmbedBitcode,
105120
}
106121

107122
impl ModuleConfig {
@@ -124,9 +139,7 @@ impl ModuleConfig {
124139
emit_bc_compressed: false,
125140
emit_ir: false,
126141
emit_asm: false,
127-
emit_obj: false,
128-
obj_is_bitcode: false,
129-
embed_bitcode: EmbedBitcode::None,
142+
emit_obj: EmitObj::None,
130143
no_integrated_as: false,
131144

132145
verify_llvm_ir: false,
@@ -147,16 +160,6 @@ impl ModuleConfig {
147160
self.no_builtins = no_builtins || sess.target.target.options.no_builtins;
148161
self.inline_threshold = sess.opts.cg.inline_threshold;
149162
self.new_llvm_pass_manager = sess.opts.debugging_opts.new_llvm_pass_manager;
150-
self.obj_is_bitcode =
151-
sess.target.target.options.obj_is_bitcode || sess.opts.cg.linker_plugin_lto.enabled();
152-
self.embed_bitcode = if sess.opts.debugging_opts.embed_bitcode {
153-
match sess.opts.optimize {
154-
config::OptLevel::No | config::OptLevel::Less => EmbedBitcode::Marker,
155-
_ => EmbedBitcode::Full,
156-
}
157-
} else {
158-
EmbedBitcode::None
159-
};
160163

161164
// Copy what clang does by turning on loop vectorization at O2 and
162165
// slp vectorization at O3. Otherwise configure other optimization aspects
@@ -193,9 +196,9 @@ impl ModuleConfig {
193196

194197
pub fn bitcode_needed(&self) -> bool {
195198
self.emit_bc
196-
|| self.obj_is_bitcode
197199
|| self.emit_bc_compressed
198-
|| self.embed_bitcode == EmbedBitcode::Full
200+
|| self.emit_obj == EmitObj::Bitcode
201+
|| self.emit_obj == EmitObj::ObjectCode(BitcodeSection::Full)
199202
}
200203
}
201204

@@ -396,6 +399,20 @@ pub fn start_async_codegen<B: ExtraBackendMethods>(
396399
allocator_config.emit_bc_compressed = true;
397400
}
398401

402+
let emit_obj =
403+
if sess.target.target.options.obj_is_bitcode || sess.opts.cg.linker_plugin_lto.enabled() {
404+
EmitObj::Bitcode
405+
} else if sess.opts.debugging_opts.embed_bitcode {
406+
match sess.opts.optimize {
407+
config::OptLevel::No | config::OptLevel::Less => {
408+
EmitObj::ObjectCode(BitcodeSection::Marker)
409+
}
410+
_ => EmitObj::ObjectCode(BitcodeSection::Full),
411+
}
412+
} else {
413+
EmitObj::ObjectCode(BitcodeSection::None)
414+
};
415+
399416
modules_config.emit_pre_lto_bc = need_pre_lto_bitcode_for_incr_comp(sess);
400417

401418
modules_config.no_integrated_as =
@@ -415,20 +432,20 @@ pub fn start_async_codegen<B: ExtraBackendMethods>(
415432
// could be invoked specially with output_type_assembly, so
416433
// in this case we still want the metadata object file.
417434
if !sess.opts.output_types.contains_key(&OutputType::Assembly) {
418-
metadata_config.emit_obj = true;
419-
allocator_config.emit_obj = true;
435+
metadata_config.emit_obj = emit_obj;
436+
allocator_config.emit_obj = emit_obj;
420437
}
421438
}
422439
OutputType::Object => {
423-
modules_config.emit_obj = true;
440+
modules_config.emit_obj = emit_obj;
424441
}
425442
OutputType::Metadata => {
426-
metadata_config.emit_obj = true;
443+
metadata_config.emit_obj = emit_obj;
427444
}
428445
OutputType::Exe => {
429-
modules_config.emit_obj = true;
430-
metadata_config.emit_obj = true;
431-
allocator_config.emit_obj = true;
446+
modules_config.emit_obj = emit_obj;
447+
metadata_config.emit_obj = emit_obj;
448+
allocator_config.emit_obj = emit_obj;
432449
}
433450
OutputType::Mir => {}
434451
OutputType::DepInfo => {}
@@ -879,7 +896,7 @@ fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
879896
}
880897
}
881898

882-
assert_eq!(object.is_some(), module_config.emit_obj);
899+
assert_eq!(object.is_some(), module_config.emit_obj != EmitObj::None);
883900
assert_eq!(bytecode.is_some(), module_config.emit_bc);
884901
assert_eq!(bytecode_compressed.is_some(), module_config.emit_bc_compressed);
885902

0 commit comments

Comments
 (0)