@@ -541,11 +541,23 @@ unsafe fn optimize(cgcx: &CodegenContext,
541
541
} ;
542
542
543
543
if config. verify_llvm_ir { assert ! ( addpass( "verify" ) ) ; }
544
+
545
+ // Some options cause LLVM bitcode to be emitted, which uses ThinLTOBuffers, so we need
546
+ // to make sure we run LLVM's NameAnonGlobals pass when emitting bitcode; otherwise
547
+ // we'll get errors in LLVM.
548
+ let using_thin_buffers = llvm:: LLVMRustThinLTOAvailable ( ) && ( config. emit_bc
549
+ || config. obj_is_bitcode || config. emit_bc_compressed || config. embed_bitcode ) ;
550
+ let mut have_name_anon_globals_pass = false ;
544
551
if !config. no_prepopulate_passes {
545
552
llvm:: LLVMRustAddAnalysisPasses ( tm, fpm, llmod) ;
546
553
llvm:: LLVMRustAddAnalysisPasses ( tm, mpm, llmod) ;
547
554
let opt_level = config. opt_level . unwrap_or ( llvm:: CodeGenOptLevel :: None ) ;
548
555
let prepare_for_thin_lto = cgcx. lto == Lto :: Thin || cgcx. lto == Lto :: ThinLocal ;
556
+ have_name_anon_globals_pass = have_name_anon_globals_pass || prepare_for_thin_lto;
557
+ if using_thin_buffers && !prepare_for_thin_lto {
558
+ assert ! ( addpass( "name-anon-globals" ) ) ;
559
+ have_name_anon_globals_pass = true ;
560
+ }
549
561
with_llvm_pmb ( llmod, & config, opt_level, prepare_for_thin_lto, & mut |b| {
550
562
llvm:: LLVMPassManagerBuilderPopulateFunctionPassManager ( b, fpm) ;
551
563
llvm:: LLVMPassManagerBuilderPopulateModulePassManager ( b, mpm) ;
@@ -557,6 +569,9 @@ unsafe fn optimize(cgcx: &CodegenContext,
557
569
diag_handler. warn ( & format ! ( "unknown pass `{}`, ignoring" ,
558
570
pass) ) ;
559
571
}
572
+ if pass == "name-anon-globals" {
573
+ have_name_anon_globals_pass = true ;
574
+ }
560
575
}
561
576
562
577
for pass in & cgcx. plugin_passes {
@@ -565,6 +580,22 @@ unsafe fn optimize(cgcx: &CodegenContext,
565
580
`{}` but LLVM does not \
566
581
recognize it", pass) ) ;
567
582
}
583
+ if pass == "name-anon-globals" {
584
+ have_name_anon_globals_pass = true ;
585
+ }
586
+ }
587
+
588
+ if using_thin_buffers && !have_name_anon_globals_pass {
589
+ // As described above, this will probably cause an error in LLVM
590
+ if config. no_prepopulate_passes {
591
+ diag_handler. err ( "The current compilation is going to use thin LTO buffers \
592
+ without running LLVM's NameAnonGlobals pass. \
593
+ This will likely cause errors in LLVM. Consider adding \
594
+ -C passes=name-anon-globals to the compiler command line.") ;
595
+ } else {
596
+ bug ! ( "We are using thin LTO buffers without running the NameAnonGlobals pass. \
597
+ This will likely cause errors in LLVM and shoud never happen.") ;
598
+ }
568
599
}
569
600
}
570
601
0 commit comments