Skip to content

Commit d84c074

Browse files
committed
WIP: Working state for having the test program to compile with LTO
This requires having the following line commented in libgccjit: ADD_ARG ("-fno-use-linker-plugin"); And this also requires having the sysroot compiled with fat LTO.
1 parent d01e7d1 commit d84c074

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

src/back/lto.rs

+23-5
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ fn fat_lto(
280280
context.set_optimization_level(to_gcc_opt_level(config.opt_level));
281281
context.add_command_line_option("-flto=auto");
282282
context.add_command_line_option("-flto-partition=one");
283+
//context.add_command_line_option("-fno-use-linker-plugin");
283284
context.compile_to_file(OutputKind::ObjectFile, path);
284285
let buffer = ModuleBuffer::new(PathBuf::from(path));
285286
let llmod_id = CString::new(&module.name[..]).unwrap();
@@ -314,6 +315,20 @@ fn fat_lto(
314315
save_temp_bitcode(cgcx, &module, "lto.input");
315316

316317
let int_type = module.module_llvm.context.new_type::<i32>();
318+
/*
319+
* TODO: Preserve the symbols via a linker script instead?
320+
* TODO TODO: use the symbols in inline asm to keep them?
321+
* ===> This seems to work and is easy.
322+
*
323+
* FIXME: main is not genarated in GIMPLE IR; it is generated as asm.
324+
* ===> seems now fixed since I commented this line in libgccjit:
325+
* ADD_ARG ("-fno-use-linker-plugin");
326+
* FIXME: it seems "make install" doesn't copy liblto_plugin.so.
327+
* ===> seems now fixed after a rebuild.
328+
*
329+
* TODO TODO TODO: add -fno-use-linker-plugin because:
330+
* This option is enabled by default when LTO support in GCC is enabled and GCC was configured for use with a linker supporting plugins (GNU ld 2.21 or newer or gold).
331+
*/
317332
for symbol in symbols_below_threshold {
318333
println!("*** Keeping symbol: {}", symbol);
319334
module.module_llvm.context.new_global(None, GlobalKind::Imported, int_type, symbol);
@@ -322,17 +337,20 @@ fn fat_lto(
322337
}
323338
let void_type = module.module_llvm.context.new_type::<()>();
324339
let main_func = module.module_llvm.context.new_function(None, FunctionType::Extern, void_type, &[], "main", false);
325-
main_func.add_attribute(FnAttribute::Used);
340+
//main_func.add_attribute(FnAttribute::Used);
326341

327342
// NOTE: look at the code from 64b30d344ce54f8ee496cb3590b4c7cf3cb30447 to see previous
328343
// attemps.
329344
let func = module.module_llvm.context.new_function(None, FunctionType::Exported, void_type, &[], "__my_call_main", false);
330-
func.add_attribute(FnAttribute::AlwaysInline);
331-
func.add_attribute(FnAttribute::Inline);
345+
//func.add_attribute(FnAttribute::AlwaysInline);
346+
//func.add_attribute(FnAttribute::Inline);
332347
func.add_attribute(FnAttribute::Used);
333348
let block = func.new_block("start");
334-
let call = module.module_llvm.context.new_call(None, main_func, &[]);
335-
block.add_eval(None, call);
349+
let main_func_address = main_func.get_address(None);
350+
/*let asm = block.add_extended_asm(None, "");
351+
asm.add_input_operand(None, "X", main_func_address);*/
352+
//let call = module.module_llvm.context.new_call(None, main_func, &[]);
353+
//block.add_eval(None, call);
336354
block.end_with_void_return(None);
337355

338356
// Internalize everything below threshold to help strip out more modules and such.

src/back/write.rs

+18
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,17 @@ pub(crate) unsafe fn codegen(
5757
);
5858
}*/
5959

60+
// TODO: make this work to make it easier to debug GCC-related issue within
61+
// rustc_codegen_gcc:
62+
//context.add_driver_option("-wrapper gdb,--args");
6063
if config.emit_bc || config.emit_obj == EmitObj::Bitcode {
6164
let _timer = cgcx.prof.generic_activity_with_arg(
6265
"GCC_module_codegen_emit_bitcode",
6366
&*module.name,
6467
);
6568
context.add_command_line_option("-flto=auto");
6669
context.add_command_line_option("-flto-partition=one");
70+
//context.add_command_line_option("-fno-use-linker-plugin");
6771
// TODO(antoyo): remove since we don't want fat objects when it is for Bitcode only.
6872
context.add_command_line_option("-ffat-lto-objects");
6973
context.compile_to_file(
@@ -82,6 +86,7 @@ pub(crate) unsafe fn codegen(
8286

8387
context.add_command_line_option("-flto=auto");
8488
context.add_command_line_option("-flto-partition=one");
89+
//context.add_command_line_option("-fno-use-linker-plugin");
8590
context.add_command_line_option("-ffat-lto-objects");
8691
// 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).
8792
context.compile_to_file(
@@ -169,6 +174,14 @@ pub(crate) unsafe fn codegen(
169174
if fat_lto {
170175
context.add_command_line_option("-flto=auto");
171176
context.add_command_line_option("-flto-partition=one");
177+
//context.add_command_line_option("-ffat-lto-objects");
178+
//context.add_command_line_option("-fno-use-linker-plugin");
179+
180+
// FIXME FIXME FIXME:
181+
// /usr/bin/ld: warning: incremental linking of LTO and non-LTO objects; using -flinker-output=nolto-rel which will bypass whole program optimization
182+
// ====> So I'm probably missing -flto somewhere.
183+
184+
println!("**** Adding -flto to {:?}", obj_out.to_str().expect("path to str"));
172185

173186
// FIXME: the problem is probably that the code is only in GIMPLE IR while
174187
// we would want to get the optimized asm done from LTO.
@@ -180,6 +193,7 @@ pub(crate) unsafe fn codegen(
180193
// NOTE: we need -nostdlib, otherwise, we get the following error:
181194
// /usr/bin/ld: cannot find -lgcc_s: No such file or directory
182195
context.add_driver_option("-nostdlib");
196+
//context.add_driver_option("-v");
183197

184198
let path = obj_out.to_str().expect("path to str");
185199

@@ -193,9 +207,12 @@ pub(crate) unsafe fn codegen(
193207
//
194208
// This option is to mute it to make the UI tests pass with LTO enabled.
195209
context.add_driver_option("-Wno-lto-type-mismatch");
210+
//context.add_driver_option("-v");
196211
// NOTE: this doesn't actually generate an executable. With the above
197212
// flags, it combines the .o files together in another .o.
213+
println!("Lto path: {:?}", lto_path);
198214
context.compile_to_file(OutputKind::Executable, &lto_path);
215+
println!("****************************************************************************************************");
199216

200217
let context = Context::default();
201218
if cgcx.target_arch == "x86" || cgcx.target_arch == "x86_64" {
@@ -209,6 +226,7 @@ pub(crate) unsafe fn codegen(
209226
// needs to be at the very beginning.
210227
context.add_driver_option("-x");
211228
context.add_driver_option("lto");
229+
//context.add_driver_option("-v");
212230
add_pic_option(&context, module.module_llvm.relocation_model);
213231
context.add_driver_option(lto_path);
214232
// TODO TODO: inspect the resulting file to see if it contains the GIMPLE IR or

src/declare.rs

+4
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ fn declare_raw_fn<'gcc>(
189189
.collect();
190190
#[cfg(not(feature = "master"))]
191191
let name = &mangle_name(name);
192+
if name == "main" {
193+
//cx.context.add_driver_option("-v");
194+
println!("**** Main function in {}", cx.codegen_unit.name());
195+
}
192196
let func =
193197
cx.context.new_function(None, cx.linkage.get(), return_type, &params, name, variadic);
194198
#[cfg(feature = "master")]

0 commit comments

Comments
 (0)