@@ -75,6 +75,10 @@ pub struct CodegenCx<'ll, 'tcx> {
7575 /// See <https://llvm.org/docs/LangRef.html#the-llvm-used-global-variable> for details
7676 pub used_statics : RefCell < Vec < & ' ll Value > > ,
7777
78+ /// Statics that will be placed in the llvm.compiler.used variable
79+ /// See <https://llvm.org/docs/LangRef.html#the-llvm-compiler-used-global-variable> for details
80+ pub compiler_used_statics : RefCell < Vec < & ' ll Value > > ,
81+
7882 /// Mapping of non-scalar types to llvm types and field remapping if needed.
7983 pub type_lowering : RefCell < FxHashMap < ( Ty < ' tcx > , Option < VariantIdx > ) , TypeLowering < ' ll > > > ,
8084
@@ -115,10 +119,6 @@ fn to_llvm_tls_model(tls_model: TlsModel) -> llvm::ThreadLocalMode {
115119 }
116120}
117121
118- fn strip_powerpc64_vectors ( data_layout : String ) -> String {
119- data_layout. replace ( "-v256:256:256-v512:512:512" , "" )
120- }
121-
122122pub unsafe fn create_module (
123123 tcx : TyCtxt < ' _ > ,
124124 llcx : & ' ll llvm:: Context ,
@@ -130,7 +130,18 @@ pub unsafe fn create_module(
130130
131131 let mut target_data_layout = sess. target . data_layout . clone ( ) ;
132132 if llvm_util:: get_version ( ) < ( 12 , 0 , 0 ) && sess. target . arch == "powerpc64" {
133- target_data_layout = strip_powerpc64_vectors ( target_data_layout) ;
133+ target_data_layout = target_data_layout. replace ( "-v256:256:256-v512:512:512" , "" ) ;
134+ }
135+ if llvm_util:: get_version ( ) < ( 13 , 0 , 0 ) {
136+ if sess. target . arch == "powerpc64" {
137+ target_data_layout = target_data_layout. replace ( "-S128" , "" ) ;
138+ }
139+ if sess. target . arch == "wasm32" {
140+ target_data_layout = "e-m:e-p:32:32-i64:64-n32:64-S128" . to_string ( ) ;
141+ }
142+ if sess. target . arch == "wasm64" {
143+ target_data_layout = "e-m:e-p:64:64-i64:64-n32:64-S128" . to_string ( ) ;
144+ }
134145 }
135146
136147 // Ensure the data-layout values hardcoded remain the defaults.
@@ -318,6 +329,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
318329 const_globals : Default :: default ( ) ,
319330 statics_to_rauw : RefCell :: new ( Vec :: new ( ) ) ,
320331 used_statics : RefCell :: new ( Vec :: new ( ) ) ,
332+ compiler_used_statics : RefCell :: new ( Vec :: new ( ) ) ,
321333 type_lowering : Default :: default ( ) ,
322334 scalar_lltypes : Default :: default ( ) ,
323335 pointee_infos : Default :: default ( ) ,
@@ -340,6 +352,18 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
340352 pub fn coverage_context ( & ' a self ) -> Option < & ' a coverageinfo:: CrateCoverageContext < ' ll , ' tcx > > {
341353 self . coverage_cx . as_ref ( )
342354 }
355+
356+ fn create_used_variable_impl ( & self , name : & ' static CStr , values : & [ & ' ll Value ] ) {
357+ let section = cstr ! ( "llvm.metadata" ) ;
358+ let array = self . const_array ( & self . type_ptr_to ( self . type_i8 ( ) ) , values) ;
359+
360+ unsafe {
361+ let g = llvm:: LLVMAddGlobal ( self . llmod , self . val_ty ( array) , name. as_ptr ( ) ) ;
362+ llvm:: LLVMSetInitializer ( g, array) ;
363+ llvm:: LLVMRustSetLinkage ( g, llvm:: Linkage :: AppendingLinkage ) ;
364+ llvm:: LLVMSetSection ( g, section. as_ptr ( ) ) ;
365+ }
366+ }
343367}
344368
345369impl MiscMethods < ' tcx > for CodegenCx < ' ll , ' tcx > {
@@ -430,6 +454,10 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
430454 & self . used_statics
431455 }
432456
457+ fn compiler_used_statics ( & self ) -> & RefCell < Vec < & ' ll Value > > {
458+ & self . compiler_used_statics
459+ }
460+
433461 fn set_frame_pointer_type ( & self , llfn : & ' ll Value ) {
434462 attributes:: set_frame_pointer_type ( self , llfn)
435463 }
@@ -440,17 +468,14 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
440468 }
441469
442470 fn create_used_variable ( & self ) {
443- let name = cstr ! ( "llvm.used" ) ;
444- let section = cstr ! ( "llvm.metadata" ) ;
445- let array =
446- self . const_array ( & self . type_ptr_to ( self . type_i8 ( ) ) , & * self . used_statics . borrow ( ) ) ;
471+ self . create_used_variable_impl ( cstr ! ( "llvm.used" ) , & * self . used_statics . borrow ( ) ) ;
472+ }
447473
448- unsafe {
449- let g = llvm:: LLVMAddGlobal ( self . llmod , self . val_ty ( array) , name. as_ptr ( ) ) ;
450- llvm:: LLVMSetInitializer ( g, array) ;
451- llvm:: LLVMRustSetLinkage ( g, llvm:: Linkage :: AppendingLinkage ) ;
452- llvm:: LLVMSetSection ( g, section. as_ptr ( ) ) ;
453- }
474+ fn create_compiler_used_variable ( & self ) {
475+ self . create_used_variable_impl (
476+ cstr ! ( "llvm.compiler.used" ) ,
477+ & * self . compiler_used_statics . borrow ( ) ,
478+ ) ;
454479 }
455480
456481 fn declare_c_main ( & self , fn_type : Self :: Type ) -> Option < Self :: Function > {
0 commit comments