@@ -85,10 +85,7 @@ mod type_of;
85
85
use std:: any:: Any ;
86
86
use std:: fmt:: Debug ;
87
87
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 } ;
92
89
use std:: sync:: { Arc , Mutex } ;
93
90
94
91
use back:: lto:: { ThinBuffer , ThinData } ;
@@ -182,6 +179,7 @@ impl LockedTargetInfo {
182
179
#[ derive( Clone ) ]
183
180
pub struct GccCodegenBackend {
184
181
target_info : LockedTargetInfo ,
182
+ lto_supported : Arc < AtomicBool > ,
185
183
}
186
184
187
185
impl CodegenBackend for GccCodegenBackend {
@@ -203,6 +201,29 @@ impl CodegenBackend for GccCodegenBackend {
203
201
* * self . target_info . info . lock ( ) . expect ( "lock" ) = context. get_target_info ( ) ;
204
202
}
205
203
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
+
206
227
#[ cfg( feature = "master" ) ]
207
228
gccjit:: set_global_personality_function_name ( b"rust_eh_personality\0 " ) ;
208
229
@@ -288,6 +309,7 @@ impl ExtraBackendMethods for GccCodegenBackend {
288
309
context : Arc :: new ( SyncContext :: new ( new_context ( tcx) ) ) ,
289
310
relocation_model : tcx. sess . relocation_model ( ) ,
290
311
lto_mode : LtoMode :: None ,
312
+ lto_supported : false ,
291
313
temp_dir : None ,
292
314
} ;
293
315
@@ -302,7 +324,12 @@ impl ExtraBackendMethods for GccCodegenBackend {
302
324
tcx : TyCtxt < ' _ > ,
303
325
cgu_name : Symbol ,
304
326
) -> ( 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
+ )
306
333
}
307
334
308
335
fn target_machine_factory (
@@ -329,6 +356,7 @@ pub struct GccContext {
329
356
/// LTO.
330
357
relocation_model : RelocModel ,
331
358
lto_mode : LtoMode ,
359
+ lto_supported : bool ,
332
360
// Temporary directory used by LTO. We keep it here so that it's not removed before linking.
333
361
temp_dir : Option < TempDir > ,
334
362
}
@@ -466,7 +494,10 @@ pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
466
494
supports_128bit_integers : AtomicBool :: new ( false ) ,
467
495
} ) ) ) ;
468
496
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
+ } )
470
501
}
471
502
472
503
fn to_gcc_opt_level ( optlevel : Option < OptLevel > ) -> OptimizationLevel {
0 commit comments