Skip to content

Commit 37bbe26

Browse files
committed
Fix to do LTO when requested
1 parent 8f048e9 commit 37bbe26

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

src/back/lto.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use tempfile::{TempDir, tempdir};
4141

4242
use crate::back::write::save_temp_bitcode;
4343
use crate::errors::{DynamicLinkingWithLTO, LtoBitcodeFromRlib, LtoDisallowed, LtoDylib};
44-
use crate::{GccCodegenBackend, GccContext, SyncContext, to_gcc_opt_level};
44+
use crate::{GccCodegenBackend, GccContext, LtoMode, SyncContext, to_gcc_opt_level};
4545

4646
pub fn crate_type_allows_lto(crate_type: CrateType) -> bool {
4747
match crate_type {
@@ -303,7 +303,7 @@ fn fat_lto(
303303
info!("linking {:?}", name);
304304
match bc_decoded {
305305
SerializedModule::Local(ref module_buffer) => {
306-
module.module_llvm.should_combine_object_files = true;
306+
module.module_llvm.lto_mode = LtoMode::Fat;
307307
module
308308
.module_llvm
309309
.context
@@ -611,7 +611,7 @@ pub fn optimize_thin_module(
611611
// that LLVM Context and Module.
612612
//let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names);
613613
//let llmod_raw = parse_module(llcx, module_name, thin_module.data(), &dcx)? as *const _;
614-
let mut should_combine_object_files = false;
614+
let mut lto_mode = LtoMode::None;
615615
let context = match thin_module.shared.thin_buffers.get(thin_module.idx) {
616616
Some(thin_buffer) => Arc::clone(&thin_buffer.context),
617617
None => {
@@ -622,7 +622,7 @@ pub fn optimize_thin_module(
622622
SerializedModule::Local(ref module_buffer) => {
623623
let path = module_buffer.0.to_str().expect("path");
624624
context.add_driver_option(path);
625-
should_combine_object_files = true;
625+
lto_mode = LtoMode::Thin;
626626
/*module.module_llvm.should_combine_object_files = true;
627627
module
628628
.module_llvm
@@ -641,7 +641,7 @@ pub fn optimize_thin_module(
641641
thin_module.name().to_string(),
642642
GccContext {
643643
context,
644-
should_combine_object_files,
644+
lto_mode,
645645
// TODO(antoyo): use the correct relocation model here.
646646
relocation_model: RelocModel::Pic,
647647
temp_dir: None,

src/back/write.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_target::spec::SplitDebuginfo;
1212

1313
use crate::base::add_pic_option;
1414
use crate::errors::CopyBitcode;
15-
use crate::{GccCodegenBackend, GccContext};
15+
use crate::{GccCodegenBackend, GccContext, LtoMode};
1616

1717
pub(crate) fn codegen(
1818
cgcx: &CodegenContext<GccCodegenBackend>,
@@ -24,7 +24,7 @@ pub(crate) fn codegen(
2424
{
2525
let context = &module.module_llvm.context;
2626

27-
let should_combine_object_files = module.module_llvm.should_combine_object_files;
27+
let lto_mode = module.module_llvm.lto_mode;
2828

2929
let bc_out = cgcx.output_filenames.temp_path_for_cgu(
3030
OutputType::Bitcode,
@@ -124,8 +124,8 @@ pub(crate) fn codegen(
124124
context.set_debug_info(true);
125125
context.dump_to_file(path, true);
126126
}
127-
if should_combine_object_files {
128-
let fat_lto = config.emit_obj == EmitObj::ObjectCode(BitcodeSection::Full);
127+
if lto_mode != LtoMode::None {
128+
let fat_lto = lto_mode == LtoMode::Fat;
129129
// We need to check if we're doing LTO since this code is also used for the
130130
// dummy ThinLTO implementation to combine the object files.
131131
if fat_lto {

src/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_target::spec::{PanicStrategy, RelocModel};
2121

2222
use crate::builder::Builder;
2323
use crate::context::CodegenCx;
24-
use crate::{GccContext, LockedTargetInfo, SyncContext, gcc_util, new_context};
24+
use crate::{GccContext, LockedTargetInfo, LtoMode, SyncContext, gcc_util, new_context};
2525

2626
#[cfg(feature = "master")]
2727
pub fn visibility_to_gcc(visibility: Visibility) -> gccjit::Visibility {
@@ -242,7 +242,7 @@ pub fn compile_codegen_unit(
242242
GccContext {
243243
context: Arc::new(SyncContext::new(context)),
244244
relocation_model: tcx.sess.relocation_model(),
245-
should_combine_object_files: false,
245+
lto_mode: LtoMode::None,
246246
temp_dir: None,
247247
},
248248
)

src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ impl ExtraBackendMethods for GccCodegenBackend {
299299
let mut mods = GccContext {
300300
context: Arc::new(SyncContext::new(new_context(tcx))),
301301
relocation_model: tcx.sess.relocation_model(),
302-
should_combine_object_files: false,
302+
lto_mode: LtoMode::None,
303303
temp_dir: None,
304304
};
305305

@@ -328,12 +328,19 @@ impl ExtraBackendMethods for GccCodegenBackend {
328328
}
329329
}
330330

331+
#[derive(Clone, Copy, PartialEq)]
332+
pub enum LtoMode {
333+
None,
334+
Thin,
335+
Fat,
336+
}
337+
331338
pub struct GccContext {
332339
context: Arc<SyncContext>,
333340
/// This field is needed in order to be able to set the flag -fPIC when necessary when doing
334341
/// LTO.
335342
relocation_model: RelocModel,
336-
should_combine_object_files: bool,
343+
lto_mode: LtoMode,
337344
// Temporary directory used by LTO. We keep it here so that it's not removed before linking.
338345
temp_dir: Option<TempDir>,
339346
}

0 commit comments

Comments
 (0)