Skip to content

Commit 9d177d3

Browse files
committed
Fix to combine into an object file when needed
1 parent 40ee677 commit 9d177d3

File tree

4 files changed

+12
-21
lines changed

4 files changed

+12
-21
lines changed

src/back/lto.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use std::ffi::CString;
1111
use std::fs::{self, File};
1212
use std::path::{Path, PathBuf};
13+
use std::sync::atomic::Ordering;
1314

1415
use gccjit::OutputKind;
1516
use object::read::archive::ArchiveFile;
@@ -28,7 +29,7 @@ use tempfile::{TempDir, tempdir};
2829

2930
use crate::back::write::save_temp_bitcode;
3031
use crate::errors::{
31-
DynamicLinkingWithLTO, LoadBitcode, LtoBitcodeFromRlib, LtoDisallowed, LtoDylib,
32+
DynamicLinkingWithLTO, LtoBitcodeFromRlib, LtoDisallowed, LtoDylib,
3233
};
3334
use crate::{GccCodegenBackend, GccContext};
3435

@@ -250,8 +251,6 @@ fn fat_lto(cgcx: &CodegenContext<GccCodegenBackend>, diag_handler: &Handler, mod
250251
};
251252
let mut serialized_bitcode = Vec::new();
252253
{
253-
let context = &module.module_llvm.context;
254-
println!("Base module: {}", module.name);
255254
info!("using {:?} as a base module", module.name);
256255

257256
// For all other modules we codegened we'll need to link them into our own
@@ -284,6 +283,7 @@ fn fat_lto(cgcx: &CodegenContext<GccCodegenBackend>, diag_handler: &Handler, mod
284283
info!("linking {:?}", name);
285284
match bc_decoded {
286285
SerializedModule::Local(ref module_buffer) => {
286+
cgcx.backend.should_combine_object_files.store(true, Ordering::SeqCst);
287287
module.module_llvm.context.add_driver_option(module_buffer.0.to_str().expect("path"));
288288
},
289289
SerializedModule::FromRlib(_) => unimplemented!("from rlib"),

src/back/write.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::sync::atomic::Ordering;
12
use std::{env, fs};
23

34
use gccjit::OutputKind;
@@ -20,8 +21,7 @@ pub(crate) unsafe fn codegen(cgcx: &CodegenContext<GccCodegenBackend>, diag_hand
2021

2122
let module_name = module.name.clone();
2223

23-
// FIXME: by adding a variable in CodegenContext.
24-
let should_combine_object_files = module_name == "test_rust.8bd2b20a-cgu.0";
24+
let should_combine_object_files = cgcx.backend.should_combine_object_files.load(Ordering::SeqCst);
2525

2626
let module_name = Some(&module_name[..]);
2727

@@ -50,16 +50,7 @@ pub(crate) unsafe fn codegen(cgcx: &CodegenContext<GccCodegenBackend>, diag_hand
5050
context.add_driver_option("-flto");
5151
context.add_command_line_option("-flto-partition=one");
5252
context.add_driver_option("-flto-partition=one");
53-
if should_combine_object_files {
54-
unimplemented!(); // TODO: remove this line.
55-
context.add_driver_option("-Wl,-r");
56-
context.add_driver_option("-nostdlib");
57-
// NOTE: this doesn't actually generate an executable. With the above flags, it combines the .o files together in another .o.
58-
context.compile_to_file(OutputKind::Executable, obj_out.to_str().expect("path to str"));
59-
}
60-
else {
61-
context.compile_to_file(OutputKind::ObjectFile, bc_out.to_str().expect("path to str"));
62-
}
53+
context.compile_to_file(OutputKind::ObjectFile, bc_out.to_str().expect("path to str"));
6354
}
6455

6556
if config.emit_obj == EmitObj::ObjectCode(BitcodeSection::Full) {
@@ -68,9 +59,6 @@ pub(crate) unsafe fn codegen(cgcx: &CodegenContext<GccCodegenBackend>, diag_hand
6859
.generic_activity_with_arg("GCC_module_codegen_embed_bitcode", &*module.name);
6960
// TODO(antoyo): maybe we should call embed_bitcode to have the proper iOS fixes?
7061
//embed_bitcode(cgcx, llcx, llmod, &config.bc_cmdline, data);
71-
if should_combine_object_files {
72-
unimplemented!();
73-
}
7462

7563
context.add_command_line_option("-flto");
7664
context.add_driver_option("-flto");

src/declare.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use gccjit::{Function, FunctionType, GlobalKind, LValue, RValue, Type, FnAttribute};
1+
use gccjit::{Function, FunctionType, GlobalKind, LValue, RValue, Type};
22
use rustc_codegen_ssa::traits::BaseTypeMethods;
33
use rustc_middle::ty::Ty;
44
use rustc_span::Symbol;

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ mod type_;
6666
mod type_of;
6767

6868
use std::any::Any;
69+
use std::sync::atomic::AtomicBool;
6970
use std::sync::{Arc, Mutex};
7071

7172
use gccjit::{Context, OptimizationLevel, CType};
@@ -105,6 +106,7 @@ impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
105106

106107
#[derive(Clone)]
107108
pub struct GccCodegenBackend {
109+
should_combine_object_files: Arc<AtomicBool>,
108110
supports_128bit_integers: Arc<Mutex<bool>>,
109111
}
110112

@@ -113,7 +115,7 @@ impl CodegenBackend for GccCodegenBackend {
113115
crate::DEFAULT_LOCALE_RESOURCE
114116
}
115117

116-
fn init(&self, sess: &Session) {
118+
fn init(&self, _sess: &Session) {
117119
#[cfg(feature="master")]
118120
gccjit::set_global_personality_function_name(b"rust_eh_personality\0");
119121

@@ -212,7 +214,7 @@ impl WriteBackendMethods for GccCodegenBackend {
212214
type ThinData = ();
213215
type ThinBuffer = ThinBuffer;
214216

215-
fn run_fat_lto(cgcx: &CodegenContext<Self>, mut modules: Vec<FatLTOInput<Self>>, cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>) -> Result<LtoModuleCodegen<Self>, FatalError> {
217+
fn run_fat_lto(cgcx: &CodegenContext<Self>, modules: Vec<FatLTOInput<Self>>, cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>) -> Result<LtoModuleCodegen<Self>, FatalError> {
216218
back::lto::run_fat(cgcx, modules, cached_modules)
217219
}
218220

@@ -259,6 +261,7 @@ impl WriteBackendMethods for GccCodegenBackend {
259261
#[no_mangle]
260262
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
261263
Box::new(GccCodegenBackend {
264+
should_combine_object_files: Arc::new(AtomicBool::new(false)),
262265
supports_128bit_integers: Arc::new(Mutex::new(false)),
263266
})
264267
}

0 commit comments

Comments
 (0)