Skip to content

Commit 34ed154

Browse files
committed
Fix LTO
1 parent 3f23706 commit 34ed154

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

src/back/write.rs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{env, fs};
22

3-
use gccjit::OutputKind;
3+
use gccjit::{Context, OutputKind};
44
use rustc_codegen_ssa::back::link::ensure_removed;
55
use rustc_codegen_ssa::back::write::{BitcodeSection, CodegenContext, EmitObj, ModuleConfig};
66
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen};
@@ -176,10 +176,58 @@ pub(crate) unsafe fn codegen(
176176
context.add_driver_option("-nostdlib");
177177

178178
// NOTE: this doesn't actually generate an executable. With the above flags, it combines the .o files together in another .o.
179-
context.compile_to_file(
179+
// FIXME FIXME: this produces an object file with GIMPLE IR, but it should
180+
// produce an object file with machine code.
181+
//println!("LTO-ed object file: {:?}", obj_out);
182+
/*context.compile_to_file(
180183
OutputKind::Executable,
181184
obj_out.to_str().expect("path to str"),
182-
);
185+
);*/
186+
187+
let path = obj_out.to_str().expect("path to str");
188+
189+
if fat_lto {
190+
let lto_path = format!("{}.lto", path);
191+
// FIXME(antoyo): The LTO frontend generates the following warning:
192+
// ../build_sysroot/sysroot_src/library/core/src/num/dec2flt/lemire.rs:150:15: warning: type of ‘_ZN4core3num7dec2flt5table17POWER_OF_FIVE_12817ha449a68fb31379e4E’ does not match original declaration [-Wlto-type-mismatch]
193+
// 150 | let (lo5, hi5) = POWER_OF_FIVE_128[index];
194+
// | ^
195+
// lto1: note: ‘_ZN4core3num7dec2flt5table17POWER_OF_FIVE_12817ha449a68fb31379e4E’ was previously declared here
196+
//
197+
// This option is to mute it to make the UI tests pass with LTO enabled.
198+
context.add_driver_option("-Wno-lto-type-mismatch");
199+
// NOTE: this doesn't actually generate an executable. With the above
200+
// flags, it combines the .o files together in another .o.
201+
context.compile_to_file(OutputKind::Executable, &lto_path);
202+
203+
let context = Context::default(); // TODO: might need to set some other flags from new_context.
204+
//context.add_driver_option("-v");
205+
//println!("*** Arch: {}", cgcx.target_arch);
206+
if cgcx.target_arch == "x86" || cgcx.target_arch == "x86_64" {
207+
//println!("**** Added -masm=intel");
208+
//context.add_command_line_option("-masm=intel");
209+
// NOTE: it seems we need to use add_driver_option instead of
210+
// add_command_line_option here because we use the LTO frontend via gcc.
211+
context.add_driver_option("-masm=intel");
212+
}
213+
214+
// NOTE: these two options are needed to invoke LTO to produce an object file.
215+
// We need to initiate a second compilation because the arguments "-x lto"
216+
// needs to be at the very beginning.
217+
// TODO TODO: check that LTO still does the optimizations across different
218+
// object files with this change.
219+
// TODO: this should probably be in a condition `if fat_lto`.
220+
context.add_driver_option("-x");
221+
context.add_driver_option("lto");
222+
context.add_driver_option("-fPIC"); // TODO TODO: only add this flag when necessary.
223+
context.add_driver_option(lto_path);
224+
225+
context.compile_to_file(OutputKind::ObjectFile, path);
226+
} else {
227+
// NOTE: this doesn't actually generate an executable. With the above
228+
// flags, it combines the .o files together in another .o.
229+
context.compile_to_file(OutputKind::Executable, path);
230+
}
183231
} else {
184232
context.compile_to_file(
185233
OutputKind::ObjectFile,

0 commit comments

Comments
 (0)