@@ -541,11 +541,23 @@ unsafe fn optimize(cgcx: &CodegenContext,
541541 } ;
542542
543543 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 ;
544551 if !config. no_prepopulate_passes {
545552 llvm:: LLVMRustAddAnalysisPasses ( tm, fpm, llmod) ;
546553 llvm:: LLVMRustAddAnalysisPasses ( tm, mpm, llmod) ;
547554 let opt_level = config. opt_level . unwrap_or ( llvm:: CodeGenOptLevel :: None ) ;
548555 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+ }
549561 with_llvm_pmb ( llmod, & config, opt_level, prepare_for_thin_lto, & mut |b| {
550562 llvm:: LLVMPassManagerBuilderPopulateFunctionPassManager ( b, fpm) ;
551563 llvm:: LLVMPassManagerBuilderPopulateModulePassManager ( b, mpm) ;
@@ -557,6 +569,9 @@ unsafe fn optimize(cgcx: &CodegenContext,
557569 diag_handler. warn ( & format ! ( "unknown pass `{}`, ignoring" ,
558570 pass) ) ;
559571 }
572+ if pass == "name-anon-globals" {
573+ have_name_anon_globals_pass = true ;
574+ }
560575 }
561576
562577 for pass in & cgcx. plugin_passes {
@@ -565,6 +580,22 @@ unsafe fn optimize(cgcx: &CodegenContext,
565580 `{}` but LLVM does not \
566581 recognize it", pass) ) ;
567582 }
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+ }
568599 }
569600 }
570601
0 commit comments