@@ -24,7 +24,11 @@ use rustc_target::abi::{AddressSpace, Align, HasDataLayout, LayoutOf, Primitive,
2424
2525use std:: ffi:: CStr ;
2626
27- pub fn const_alloc_to_llvm ( cx : & CodegenCx < ' ll , ' _ > , alloc : & Allocation ) -> & ' ll Value {
27+ pub fn const_alloc_to_llvm (
28+ cx : & CodegenCx < ' ll , ' _ > ,
29+ alloc : & Allocation ,
30+ address_space : AddressSpace ,
31+ ) -> & ' ll Value {
2832 let mut llvals = Vec :: with_capacity ( alloc. relocations ( ) . len ( ) + 1 ) ;
2933 let dl = cx. data_layout ( ) ;
3034 let pointer_size = dl. pointer_size . bytes ( ) as usize ;
@@ -56,7 +60,7 @@ pub fn const_alloc_to_llvm(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) -> &'ll
5660 llvals. push ( cx. scalar_to_backend (
5761 Pointer :: new ( alloc_id, Size :: from_bytes ( ptr_offset) ) . into ( ) ,
5862 & Scalar { value : Primitive :: Pointer , valid_range : 0 ..=!0 } ,
59- cx. type_i8p ( AddressSpace :: default ( ) ) ,
63+ cx. type_i8p ( address_space ) ,
6064 ) ) ;
6165 next_offset = offset + pointer_size;
6266 }
@@ -79,11 +83,17 @@ pub fn codegen_static_initializer(
7983 cx : & CodegenCx < ' ll , ' tcx > ,
8084 def_id : DefId ,
8185) -> Result < ( & ' ll Value , & ' tcx Allocation ) , ErrorHandled > {
86+ let address_space = if cx. tcx . type_of ( def_id) . is_fn ( ) {
87+ cx. data_layout ( ) . instruction_address_space
88+ } else {
89+ AddressSpace :: default ( )
90+ } ;
91+
8292 let alloc = match cx. tcx . const_eval_poly ( def_id) ? {
8393 ConstValue :: ByRef { alloc, offset } if offset. bytes ( ) == 0 => alloc,
8494 val => bug ! ( "static const eval returned {:#?}" , val) ,
8595 } ;
86- Ok ( ( const_alloc_to_llvm ( cx, alloc) , alloc) )
96+ Ok ( ( const_alloc_to_llvm ( cx, alloc, address_space ) , alloc) )
8797}
8898
8999fn set_global_alignment ( cx : & CodegenCx < ' ll , ' _ > , gv : & ' ll Value , mut align : Align ) {
@@ -212,12 +222,15 @@ impl CodegenCx<'ll, 'tcx> {
212222 let g = if let Some ( def_id) = def_id. as_local ( ) {
213223 let id = self . tcx . hir ( ) . as_local_hir_id ( def_id) ;
214224 let llty = self . layout_of ( ty) . llvm_type ( self ) ;
225+
215226 // FIXME: refactor this to work without accessing the HIR
216227 let ( g, attrs) = match self . tcx . hir ( ) . get ( id) {
217228 Node :: Item ( & hir:: Item { attrs, span, kind : hir:: ItemKind :: Static ( ..) , .. } ) => {
218229 let sym_str = sym. as_str ( ) ;
219230 if let Some ( g) = self . get_declared_value ( & sym_str) {
220- if self . val_ty ( g) != self . type_ptr_to ( llty) {
231+ if self . val_ty ( g)
232+ != self . type_ptr_to ( llty, self . address_space_of_type ( llty) )
233+ {
221234 span_bug ! ( span, "Conflicting types for static" ) ;
222235 }
223236 }
@@ -496,7 +509,8 @@ impl StaticMethods for CodegenCx<'ll, 'tcx> {
496509
497510 if attrs. flags . contains ( CodegenFnAttrFlags :: USED ) {
498511 // This static will be stored in the llvm.used variable which is an array of i8*
499- let cast = llvm:: LLVMConstPointerCast ( g, self . type_i8p ( AddressSpace :: default ( ) ) ) ;
512+ let cast =
513+ llvm:: LLVMConstPointerCast ( g, self . type_i8p ( self . address_space_of_value ( g) ) ) ;
500514 self . used_statics . borrow_mut ( ) . push ( cast) ;
501515 }
502516 }
0 commit comments