@@ -84,10 +84,7 @@ mod type_of;
84
84
use std:: any:: Any ;
85
85
use std:: fmt:: Debug ;
86
86
use std:: ops:: Deref ;
87
- #[ cfg( not( feature = "master" ) ) ]
88
- use std:: sync:: atomic:: AtomicBool ;
89
- #[ cfg( not( feature = "master" ) ) ]
90
- use std:: sync:: atomic:: Ordering ;
87
+ use std:: sync:: atomic:: { AtomicBool , Ordering } ;
91
88
use std:: sync:: { Arc , Mutex } ;
92
89
93
90
use back:: lto:: { ThinBuffer , ThinData } ;
@@ -181,6 +178,7 @@ impl LockedTargetInfo {
181
178
#[ derive( Clone ) ]
182
179
pub struct GccCodegenBackend {
183
180
target_info : LockedTargetInfo ,
181
+ lto_supported : Arc < AtomicBool > ,
184
182
}
185
183
186
184
impl CodegenBackend for GccCodegenBackend {
@@ -202,6 +200,29 @@ impl CodegenBackend for GccCodegenBackend {
202
200
* * self . target_info . info . lock ( ) . expect ( "lock" ) = context. get_target_info ( ) ;
203
201
}
204
202
203
+ // TODO: try the LTO frontend and check if it errors out. If so, do not embed the bitcode.
204
+ {
205
+ let temp_dir = TempDir :: new ( ) . expect ( "cannot create temporary directory" ) ;
206
+ let temp_file = temp_dir. into_path ( ) . join ( "result.asm" ) ;
207
+ let context = Context :: default ( ) ;
208
+ let object_file_path = temp_file. to_str ( ) . expect ( "path to str" ) ;
209
+ context. compile_to_file ( gccjit:: OutputKind :: ObjectFile , object_file_path) ;
210
+
211
+ //let temp_dir = TempDir::new().expect("cannot create temporary directory");
212
+ //let temp_file = temp_dir.into_path().join("result.asm");
213
+ let check_context = Context :: default ( ) ;
214
+ check_context. add_driver_option ( "-x" ) ;
215
+ check_context. add_driver_option ( "lto" ) ;
216
+ check_context. add_driver_option ( object_file_path) ;
217
+ check_context. set_print_errors_to_stderr ( false ) ;
218
+ //context.compile_to_file(gccjit::OutputKind::ObjectFile, temp_file.to_str().expect("path to str"));
219
+ // FIXME: compile gives the error as expected, but compile_to_file doesn't.
220
+ check_context. compile ( ) ;
221
+ let error = check_context. get_last_error ( ) ;
222
+ let lto_supported = error == Ok ( None ) ;
223
+ self . lto_supported . store ( lto_supported, Ordering :: SeqCst ) ;
224
+ }
225
+
205
226
#[ cfg( feature = "master" ) ]
206
227
gccjit:: set_global_personality_function_name ( b"rust_eh_personality\0 " ) ;
207
228
@@ -298,6 +319,7 @@ impl ExtraBackendMethods for GccCodegenBackend {
298
319
context : Arc :: new ( SyncContext :: new ( new_context ( tcx) ) ) ,
299
320
relocation_model : tcx. sess . relocation_model ( ) ,
300
321
lto_mode : LtoMode :: None ,
322
+ lto_supported : false ,
301
323
temp_dir : None ,
302
324
} ;
303
325
@@ -312,7 +334,12 @@ impl ExtraBackendMethods for GccCodegenBackend {
312
334
tcx : TyCtxt < ' _ > ,
313
335
cgu_name : Symbol ,
314
336
) -> ( ModuleCodegen < Self :: Module > , u64 ) {
315
- base:: compile_codegen_unit ( tcx, cgu_name, self . target_info . clone ( ) )
337
+ base:: compile_codegen_unit (
338
+ tcx,
339
+ cgu_name,
340
+ self . target_info . clone ( ) ,
341
+ self . lto_supported . load ( Ordering :: SeqCst ) ,
342
+ )
316
343
}
317
344
318
345
fn target_machine_factory (
@@ -339,6 +366,7 @@ pub struct GccContext {
339
366
/// LTO.
340
367
relocation_model : RelocModel ,
341
368
lto_mode : LtoMode ,
369
+ lto_supported : bool ,
342
370
// Temporary directory used by LTO. We keep it here so that it's not removed before linking.
343
371
temp_dir : Option < TempDir > ,
344
372
}
@@ -475,7 +503,10 @@ pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
475
503
supports_128bit_integers : AtomicBool :: new ( false ) ,
476
504
} ) ) ) ;
477
505
478
- Box :: new ( GccCodegenBackend { target_info : LockedTargetInfo { info } } )
506
+ Box :: new ( GccCodegenBackend {
507
+ lto_supported : Arc :: new ( AtomicBool :: new ( false ) ) ,
508
+ target_info : LockedTargetInfo { info } ,
509
+ } )
479
510
}
480
511
481
512
fn to_gcc_opt_level ( optlevel : Option < OptLevel > ) -> OptimizationLevel {
0 commit comments