@@ -24,7 +24,11 @@ use rustc_target::abi::{AddressSpace, Align, HasDataLayout, LayoutOf, Primitive,
24
24
25
25
use std:: ffi:: CStr ;
26
26
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 {
28
32
let mut llvals = Vec :: with_capacity ( alloc. relocations ( ) . len ( ) + 1 ) ;
29
33
let dl = cx. data_layout ( ) ;
30
34
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
56
60
llvals. push ( cx. scalar_to_backend (
57
61
Pointer :: new ( alloc_id, Size :: from_bytes ( ptr_offset) ) . into ( ) ,
58
62
& Scalar { value : Primitive :: Pointer , valid_range : 0 ..=!0 } ,
59
- cx. type_i8p ( AddressSpace :: default ( ) ) ,
63
+ cx. type_i8p ( address_space ) ,
60
64
) ) ;
61
65
next_offset = offset + pointer_size;
62
66
}
@@ -79,11 +83,17 @@ pub fn codegen_static_initializer(
79
83
cx : & CodegenCx < ' ll , ' tcx > ,
80
84
def_id : DefId ,
81
85
) -> 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
+
82
92
let alloc = match cx. tcx . const_eval_poly ( def_id) ? {
83
93
ConstValue :: ByRef { alloc, offset } if offset. bytes ( ) == 0 => alloc,
84
94
val => bug ! ( "static const eval returned {:#?}" , val) ,
85
95
} ;
86
- Ok ( ( const_alloc_to_llvm ( cx, alloc) , alloc) )
96
+ Ok ( ( const_alloc_to_llvm ( cx, alloc, address_space ) , alloc) )
87
97
}
88
98
89
99
fn set_global_alignment ( cx : & CodegenCx < ' ll , ' _ > , gv : & ' ll Value , mut align : Align ) {
@@ -212,12 +222,15 @@ impl CodegenCx<'ll, 'tcx> {
212
222
let g = if let Some ( def_id) = def_id. as_local ( ) {
213
223
let id = self . tcx . hir ( ) . as_local_hir_id ( def_id) ;
214
224
let llty = self . layout_of ( ty) . llvm_type ( self ) ;
225
+
215
226
// FIXME: refactor this to work without accessing the HIR
216
227
let ( g, attrs) = match self . tcx . hir ( ) . get ( id) {
217
228
Node :: Item ( & hir:: Item { attrs, span, kind : hir:: ItemKind :: Static ( ..) , .. } ) => {
218
229
let sym_str = sym. as_str ( ) ;
219
230
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
+ {
221
234
span_bug ! ( span, "Conflicting types for static" ) ;
222
235
}
223
236
}
@@ -496,7 +509,8 @@ impl StaticMethods for CodegenCx<'ll, 'tcx> {
496
509
497
510
if attrs. flags . contains ( CodegenFnAttrFlags :: USED ) {
498
511
// 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) ) ) ;
500
514
self . used_statics . borrow_mut ( ) . push ( cast) ;
501
515
}
502
516
}
0 commit comments