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