Skip to content

Commit 7f02d4e

Browse files
committed
Fix to do LTO when requested
1 parent 807d50a commit 7f02d4e

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
@@ -40,7 +40,7 @@ use tempfile::{TempDir, tempdir};
4040

4141
use crate::back::write::save_temp_bitcode;
4242
use crate::errors::{DynamicLinkingWithLTO, LtoBitcodeFromRlib, LtoDisallowed, LtoDylib};
43-
use crate::{GccCodegenBackend, GccContext, SyncContext, to_gcc_opt_level};
43+
use crate::{GccCodegenBackend, GccContext, LtoMode, SyncContext, to_gcc_opt_level};
4444

4545
pub fn crate_type_allows_lto(crate_type: CrateType) -> bool {
4646
match crate_type {
@@ -302,7 +302,7 @@ fn fat_lto(
302302
info!("linking {:?}", name);
303303
match bc_decoded {
304304
SerializedModule::Local(ref module_buffer) => {
305-
module.module_llvm.should_combine_object_files = true;
305+
module.module_llvm.lto_mode = LtoMode::Fat;
306306
module
307307
.module_llvm
308308
.context
@@ -610,7 +610,7 @@ pub fn optimize_thin_module(
610610
// that LLVM Context and Module.
611611
//let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names);
612612
//let llmod_raw = parse_module(llcx, module_name, thin_module.data(), &dcx)? as *const _;
613-
let mut should_combine_object_files = false;
613+
let mut lto_mode = LtoMode::None;
614614
let context = match thin_module.shared.thin_buffers.get(thin_module.idx) {
615615
Some(thin_buffer) => Arc::clone(&thin_buffer.context),
616616
None => {
@@ -621,7 +621,7 @@ pub fn optimize_thin_module(
621621
SerializedModule::Local(ref module_buffer) => {
622622
let path = module_buffer.0.to_str().expect("path");
623623
context.add_driver_option(path);
624-
should_combine_object_files = true;
624+
lto_mode = LtoMode::Thin;
625625
/*module.module_llvm.should_combine_object_files = true;
626626
module
627627
.module_llvm
@@ -640,7 +640,7 @@ pub fn optimize_thin_module(
640640
thin_module.name().to_string(),
641641
GccContext {
642642
context,
643-
should_combine_object_files,
643+
lto_mode,
644644
// TODO(antoyo): use the correct relocation model here.
645645
relocation_model: RelocModel::Pic,
646646
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
@@ -297,7 +297,7 @@ impl ExtraBackendMethods for GccCodegenBackend {
297297
let mut mods = GccContext {
298298
context: Arc::new(SyncContext::new(new_context(tcx))),
299299
relocation_model: tcx.sess.relocation_model(),
300-
should_combine_object_files: false,
300+
lto_mode: LtoMode::None,
301301
temp_dir: None,
302302
};
303303

@@ -326,12 +326,19 @@ impl ExtraBackendMethods for GccCodegenBackend {
326326
}
327327
}
328328

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

0 commit comments

Comments
 (0)