Skip to content

Commit 03f8fe8

Browse files
committed
Fix to not do LTO when LTO is not enabled in gcc
1 parent c808807 commit 03f8fe8

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

src/back/lto.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ pub fn optimize_thin_module(
642642
GccContext {
643643
context,
644644
lto_mode,
645+
lto_supported: false, // TODO(antoyo): check if this is correct to use this value.
645646
// TODO(antoyo): use the correct relocation model here.
646647
relocation_model: RelocModel::Pic,
647648
temp_dir: None,

src/back/write.rs

Lines changed: 15 additions & 9 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,17 @@ 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+
context.add_command_line_option("-flto=auto");
62+
context.add_command_line_option("-flto-partition=one");
63+
// TODO(antoyo): remove since we don't want fat objects when it is for Bitcode only.
64+
context.add_command_line_option("-ffat-lto-objects");
65+
}
6266
context
6367
.compile_to_file(OutputKind::ObjectFile, bc_out.to_str().expect("path to str"));
6468
}
@@ -67,12 +71,14 @@ pub(crate) fn codegen(
6771
let _timer = cgcx
6872
.prof
6973
.generic_activity_with_arg("GCC_module_codegen_embed_bitcode", &*module.name);
70-
// TODO(antoyo): maybe we should call embed_bitcode to have the proper iOS fixes?
71-
//embed_bitcode(cgcx, llcx, llmod, &config.bc_cmdline, data);
74+
if lto_supported {
75+
// TODO(antoyo): maybe we should call embed_bitcode to have the proper iOS fixes?
76+
//embed_bitcode(cgcx, llcx, llmod, &config.bc_cmdline, data);
7277

73-
context.add_command_line_option("-flto=auto");
74-
context.add_command_line_option("-flto-partition=one");
75-
context.add_command_line_option("-ffat-lto-objects");
78+
context.add_command_line_option("-flto=auto");
79+
context.add_command_line_option("-flto-partition=one");
80+
context.add_command_line_option("-ffat-lto-objects");
81+
}
7682
// TODO(antoyo): Send -plugin/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/liblto_plugin.so to linker (this should be done when specifying the appropriate rustc cli argument).
7783
context
7884
.compile_to_file(OutputKind::ObjectFile, bc_out.to_str().expect("path to str"));

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...
@@ -247,6 +248,7 @@ pub fn compile_codegen_unit(
247248
GccContext {
248249
context: Arc::new(SyncContext::new(context)),
249250
relocation_model: tcx.sess.relocation_model(),
251+
lto_supported,
250252
lto_mode: LtoMode::None,
251253
temp_dir: None,
252254
},

src/lib.rs

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

9491
use back::lto::{ThinBuffer, ThinData};
@@ -182,6 +179,7 @@ impl LockedTargetInfo {
182179
#[derive(Clone)]
183180
pub struct GccCodegenBackend {
184181
target_info: LockedTargetInfo,
182+
lto_supported: Arc<AtomicBool>,
185183
}
186184

187185
impl CodegenBackend for GccCodegenBackend {
@@ -203,6 +201,29 @@ impl CodegenBackend for GccCodegenBackend {
203201
**self.target_info.info.lock().expect("lock") = context.get_target_info();
204202
}
205203

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

@@ -288,6 +309,7 @@ impl ExtraBackendMethods for GccCodegenBackend {
288309
context: Arc::new(SyncContext::new(new_context(tcx))),
289310
relocation_model: tcx.sess.relocation_model(),
290311
lto_mode: LtoMode::None,
312+
lto_supported: false,
291313
temp_dir: None,
292314
};
293315

@@ -302,7 +324,12 @@ impl ExtraBackendMethods for GccCodegenBackend {
302324
tcx: TyCtxt<'_>,
303325
cgu_name: Symbol,
304326
) -> (ModuleCodegen<Self::Module>, u64) {
305-
base::compile_codegen_unit(tcx, cgu_name, self.target_info.clone())
327+
base::compile_codegen_unit(
328+
tcx,
329+
cgu_name,
330+
self.target_info.clone(),
331+
self.lto_supported.load(Ordering::SeqCst),
332+
)
306333
}
307334

308335
fn target_machine_factory(
@@ -329,6 +356,7 @@ pub struct GccContext {
329356
/// LTO.
330357
relocation_model: RelocModel,
331358
lto_mode: LtoMode,
359+
lto_supported: bool,
332360
// Temporary directory used by LTO. We keep it here so that it's not removed before linking.
333361
temp_dir: Option<TempDir>,
334362
}
@@ -466,7 +494,10 @@ pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
466494
supports_128bit_integers: AtomicBool::new(false),
467495
})));
468496

469-
Box::new(GccCodegenBackend { target_info: LockedTargetInfo { info } })
497+
Box::new(GccCodegenBackend {
498+
lto_supported: Arc::new(AtomicBool::new(false)),
499+
target_info: LockedTargetInfo { info },
500+
})
470501
}
471502

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

0 commit comments

Comments
 (0)