Skip to content

Commit a03defc

Browse files
committed
Fix to not do LTO when LTO is not enabled in gcc
1 parent 7f02d4e commit a03defc

File tree

4 files changed

+55
-12
lines changed

4 files changed

+55
-12
lines changed

src/back/lto.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ fn fat_lto(
281281
let config = cgcx.config(module.kind);
282282
// NOTE: we need to set the optimization level here in order for LTO to do its job.
283283
context.set_optimization_level(to_gcc_opt_level(config.opt_level));
284+
println!("1. Using LTO");
284285
context.add_command_line_option("-flto=auto");
285286
context.add_command_line_option("-flto-partition=one");
286287
context.compile_to_file(OutputKind::ObjectFile, path);
@@ -641,6 +642,7 @@ pub fn optimize_thin_module(
641642
GccContext {
642643
context,
643644
lto_mode,
645+
lto_supported: false, // TODO(antoyo): check if this is correct to use this value.
644646
// TODO(antoyo): use the correct relocation model here.
645647
relocation_model: RelocModel::Pic,
646648
temp_dir: None,

src/back/write.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub(crate) fn codegen(
2525
let context = &module.module_llvm.context;
2626

2727
let lto_mode = module.module_llvm.lto_mode;
28+
let lto_supported = module.module_llvm.lto_supported;
2829

2930
let bc_out = cgcx.output_filenames.temp_path_for_cgu(
3031
OutputType::Bitcode,
@@ -51,14 +52,18 @@ pub(crate) fn codegen(
5152
);
5253
}*/
5354

55+
// TODO: only emit if libgccjit is compiled with LTO enabled?
5456
if config.emit_bc || config.emit_obj == EmitObj::Bitcode {
5557
let _timer = cgcx
5658
.prof
5759
.generic_activity_with_arg("GCC_module_codegen_emit_bitcode", &*module.name);
58-
context.add_command_line_option("-flto=auto");
59-
context.add_command_line_option("-flto-partition=one");
60-
// TODO(antoyo): remove since we don't want fat objects when it is for Bitcode only.
61-
context.add_command_line_option("-ffat-lto-objects");
60+
if lto_supported {
61+
println!("2. Using LTO: {}", module.name);
62+
context.add_command_line_option("-flto=auto");
63+
context.add_command_line_option("-flto-partition=one");
64+
// TODO(antoyo): remove since we don't want fat objects when it is for Bitcode only.
65+
context.add_command_line_option("-ffat-lto-objects");
66+
}
6267
context
6368
.compile_to_file(OutputKind::ObjectFile, bc_out.to_str().expect("path to str"));
6469
}
@@ -70,6 +75,7 @@ pub(crate) fn codegen(
7075
// TODO(antoyo): maybe we should call embed_bitcode to have the proper iOS fixes?
7176
//embed_bitcode(cgcx, llcx, llmod, &config.bc_cmdline, data);
7277

78+
println!("3. Using LTO");
7379
context.add_command_line_option("-flto=auto");
7480
context.add_command_line_option("-flto-partition=one");
7581
context.add_command_line_option("-ffat-lto-objects");
@@ -129,6 +135,7 @@ pub(crate) fn codegen(
129135
// We need to check if we're doing LTO since this code is also used for the
130136
// dummy ThinLTO implementation to combine the object files.
131137
if fat_lto {
138+
println!("4. Using LTO");
132139
context.add_command_line_option("-flto=auto");
133140
context.add_command_line_option("-flto-partition=one");
134141

@@ -170,6 +177,7 @@ pub(crate) fn codegen(
170177
// NOTE: these two options are needed to invoke LTO to produce an object file.
171178
// We need to initiate a second compilation because the arguments "-x lto"
172179
// needs to be at the very beginning.
180+
println!("Using LTO frontend");
173181
context.add_driver_option("-x");
174182
context.add_driver_option("lto");
175183
add_pic_option(&context, module.module_llvm.relocation_model);

src/base.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ pub fn compile_codegen_unit(
7474
tcx: TyCtxt<'_>,
7575
cgu_name: Symbol,
7676
target_info: LockedTargetInfo,
77+
lto_supported: bool,
7778
) -> (ModuleCodegen<GccContext>, u64) {
7879
let prof_timer = tcx.prof.generic_activity("codegen_module");
7980
let start_time = Instant::now();
@@ -82,7 +83,7 @@ pub fn compile_codegen_unit(
8283
let (module, _) = tcx.dep_graph.with_task(
8384
dep_node,
8485
tcx,
85-
(cgu_name, target_info),
86+
(cgu_name, target_info, lto_supported),
8687
module_codegen,
8788
Some(dep_graph::hash_result),
8889
);
@@ -95,7 +96,7 @@ pub fn compile_codegen_unit(
9596

9697
fn module_codegen(
9798
tcx: TyCtxt<'_>,
98-
(cgu_name, target_info): (Symbol, LockedTargetInfo),
99+
(cgu_name, target_info, lto_supported): (Symbol, LockedTargetInfo, bool),
99100
) -> ModuleCodegen<GccContext> {
100101
let cgu = tcx.codegen_unit(cgu_name);
101102
// Instantiate monomorphizations without filling out definitions yet...
@@ -242,6 +243,7 @@ pub fn compile_codegen_unit(
242243
GccContext {
243244
context: Arc::new(SyncContext::new(context)),
244245
relocation_model: tcx.sess.relocation_model(),
246+
lto_supported,
245247
lto_mode: LtoMode::None,
246248
temp_dir: None,
247249
},

src/lib.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,7 @@ mod type_of;
8484
use std::any::Any;
8585
use std::fmt::Debug;
8686
use std::ops::Deref;
87-
#[cfg(not(feature = "master"))]
88-
use std::sync::atomic::AtomicBool;
89-
#[cfg(not(feature = "master"))]
90-
use std::sync::atomic::Ordering;
87+
use std::sync::atomic::{AtomicBool, Ordering};
9188
use std::sync::{Arc, Mutex};
9289

9390
use back::lto::{ThinBuffer, ThinData};
@@ -181,6 +178,7 @@ impl LockedTargetInfo {
181178
#[derive(Clone)]
182179
pub struct GccCodegenBackend {
183180
target_info: LockedTargetInfo,
181+
lto_supported: Arc<AtomicBool>,
184182
}
185183

186184
impl CodegenBackend for GccCodegenBackend {
@@ -202,6 +200,29 @@ impl CodegenBackend for GccCodegenBackend {
202200
**self.target_info.info.lock().expect("lock") = context.get_target_info();
203201
}
204202

203+
// TODO: try the LTO frontend and check if it errors out. If so, do not embed the bitcode.
204+
{
205+
let temp_dir = TempDir::new().expect("cannot create temporary directory");
206+
let temp_file = temp_dir.into_path().join("result.asm");
207+
let context = Context::default();
208+
let object_file_path = temp_file.to_str().expect("path to str");
209+
context.compile_to_file(gccjit::OutputKind::ObjectFile, object_file_path);
210+
211+
//let temp_dir = TempDir::new().expect("cannot create temporary directory");
212+
//let temp_file = temp_dir.into_path().join("result.asm");
213+
let check_context = Context::default();
214+
check_context.add_driver_option("-x");
215+
check_context.add_driver_option("lto");
216+
check_context.add_driver_option(object_file_path);
217+
check_context.set_print_errors_to_stderr(false);
218+
//context.compile_to_file(gccjit::OutputKind::ObjectFile, temp_file.to_str().expect("path to str"));
219+
// FIXME: compile gives the error as expected, but compile_to_file doesn't.
220+
check_context.compile();
221+
let error = check_context.get_last_error();
222+
let lto_supported = error == Ok(None);
223+
self.lto_supported.store(lto_supported, Ordering::SeqCst);
224+
}
225+
205226
#[cfg(feature = "master")]
206227
gccjit::set_global_personality_function_name(b"rust_eh_personality\0");
207228

@@ -298,6 +319,7 @@ impl ExtraBackendMethods for GccCodegenBackend {
298319
context: Arc::new(SyncContext::new(new_context(tcx))),
299320
relocation_model: tcx.sess.relocation_model(),
300321
lto_mode: LtoMode::None,
322+
lto_supported: false,
301323
temp_dir: None,
302324
};
303325

@@ -312,7 +334,12 @@ impl ExtraBackendMethods for GccCodegenBackend {
312334
tcx: TyCtxt<'_>,
313335
cgu_name: Symbol,
314336
) -> (ModuleCodegen<Self::Module>, u64) {
315-
base::compile_codegen_unit(tcx, cgu_name, self.target_info.clone())
337+
base::compile_codegen_unit(
338+
tcx,
339+
cgu_name,
340+
self.target_info.clone(),
341+
self.lto_supported.load(Ordering::SeqCst),
342+
)
316343
}
317344

318345
fn target_machine_factory(
@@ -339,6 +366,7 @@ pub struct GccContext {
339366
/// LTO.
340367
relocation_model: RelocModel,
341368
lto_mode: LtoMode,
369+
lto_supported: bool,
342370
// Temporary directory used by LTO. We keep it here so that it's not removed before linking.
343371
temp_dir: Option<TempDir>,
344372
}
@@ -475,7 +503,10 @@ pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
475503
supports_128bit_integers: AtomicBool::new(false),
476504
})));
477505

478-
Box::new(GccCodegenBackend { target_info: LockedTargetInfo { info } })
506+
Box::new(GccCodegenBackend {
507+
lto_supported: Arc::new(AtomicBool::new(false)),
508+
target_info: LockedTargetInfo { info },
509+
})
479510
}
480511

481512
fn to_gcc_opt_level(optlevel: Option<OptLevel>) -> OptimizationLevel {

0 commit comments

Comments
 (0)