@@ -13,6 +13,7 @@ use crate::mir::place::PlaceRef;
13
13
use crate :: traits:: * ;
14
14
use crate :: { CachedModuleCodegen , CompiledModule , CrateInfo , MemFlags , ModuleCodegen , ModuleKind } ;
15
15
16
+ use rustc_ast:: expand:: allocator:: AllocatorKind ;
16
17
use rustc_attr as attr;
17
18
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
18
19
use rustc_data_structures:: profiling:: { get_resident_set_size, print_time_passes_entry} ;
@@ -545,6 +546,23 @@ pub fn collect_debugger_visualizers_transitive(
545
546
. collect :: < BTreeSet < _ > > ( )
546
547
}
547
548
549
+ /// Decide allocator kind to codegen. If `Some(_)` this will be the same as
550
+ /// `tcx.allocator_kind`, but it may be `None` in more cases (e.g. if using
551
+ /// allocator definitions from a dylib dependency).
552
+ pub fn allocator_kind_for_codegen ( tcx : TyCtxt < ' _ > ) -> Option < AllocatorKind > {
553
+ // If the crate doesn't have an `allocator_kind` set then there's definitely
554
+ // no shim to generate. Otherwise we also check our dependency graph for all
555
+ // our output crate types. If anything there looks like its a `Dynamic`
556
+ // linkage, then it's already got an allocator shim and we'll be using that
557
+ // one instead. If nothing exists then it's our job to generate the
558
+ // allocator!
559
+ let any_dynamic_crate = tcx. dependency_formats ( ( ) ) . iter ( ) . any ( |( _, list) | {
560
+ use rustc_middle:: middle:: dependency_format:: Linkage ;
561
+ list. iter ( ) . any ( |& linkage| linkage == Linkage :: Dynamic )
562
+ } ) ;
563
+ if any_dynamic_crate { None } else { tcx. allocator_kind ( ( ) ) }
564
+ }
565
+
548
566
pub fn codegen_crate < B : ExtraBackendMethods > (
549
567
backend : B ,
550
568
tcx : TyCtxt < ' _ > ,
@@ -615,20 +633,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
615
633
) ;
616
634
617
635
// Codegen an allocator shim, if necessary.
618
- //
619
- // If the crate doesn't have an `allocator_kind` set then there's definitely
620
- // no shim to generate. Otherwise we also check our dependency graph for all
621
- // our output crate types. If anything there looks like its a `Dynamic`
622
- // linkage, then it's already got an allocator shim and we'll be using that
623
- // one instead. If nothing exists then it's our job to generate the
624
- // allocator!
625
- let any_dynamic_crate = tcx. dependency_formats ( ( ) ) . iter ( ) . any ( |( _, list) | {
626
- use rustc_middle:: middle:: dependency_format:: Linkage ;
627
- list. iter ( ) . any ( |& linkage| linkage == Linkage :: Dynamic )
628
- } ) ;
629
- let allocator_module = if any_dynamic_crate {
630
- None
631
- } else if let Some ( kind) = tcx. allocator_kind ( ( ) ) {
636
+ if let Some ( kind) = allocator_kind_for_codegen ( tcx) {
632
637
let llmod_id =
633
638
cgu_name_builder. build_cgu_name ( LOCAL_CRATE , & [ "crate" ] , Some ( "allocator" ) ) . to_string ( ) ;
634
639
let module_llvm = tcx. sess . time ( "write_allocator_module" , || {
@@ -642,13 +647,10 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
642
647
)
643
648
} ) ;
644
649
645
- Some ( ModuleCodegen { name : llmod_id, module_llvm, kind : ModuleKind :: Allocator } )
646
- } else {
647
- None
648
- } ;
649
-
650
- if let Some ( allocator_module) = allocator_module {
651
- ongoing_codegen. submit_pre_codegened_module_to_llvm ( tcx, allocator_module) ;
650
+ ongoing_codegen. submit_pre_codegened_module_to_llvm (
651
+ tcx,
652
+ ModuleCodegen { name : llmod_id, module_llvm, kind : ModuleKind :: Allocator } ,
653
+ ) ;
652
654
}
653
655
654
656
// For better throughput during parallel processing by LLVM, we used to sort
0 commit comments