@@ -255,21 +255,38 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
255255 let ( prov, offset) = ptr. into_parts ( ) ;
256256 let ( base_addr, base_addr_space) = match self . tcx . global_alloc ( prov. alloc_id ( ) ) {
257257 GlobalAlloc :: Memory ( alloc) => {
258- let init = const_alloc_to_llvm ( self , alloc) ;
259- let alloc = alloc. inner ( ) ;
260- let value = match alloc. mutability {
261- Mutability :: Mut => self . static_addr_of_mut ( init, alloc. align , None ) ,
262- _ => self . static_addr_of ( init, alloc. align , None ) ,
263- } ;
264- if !self . sess ( ) . fewer_names ( ) && llvm:: get_value_name ( value) . is_empty ( ) {
265- let hash = self . tcx . with_stable_hashing_context ( |mut hcx| {
266- let mut hasher = StableHasher :: new ( ) ;
267- alloc. hash_stable ( & mut hcx, & mut hasher) ;
268- hasher. finish :: < Hash128 > ( )
269- } ) ;
270- llvm:: set_value_name ( value, format ! ( "alloc_{hash:032x}" ) . as_bytes ( ) ) ;
258+ // For ZSTs directly codegen an aligned pointer.
259+ // This avoids generating a zero-sized constant value and actually needing a
260+ // real address at runtime.
261+ if alloc. inner ( ) . len ( ) == 0 {
262+ assert_eq ! ( offset. bytes( ) , 0 ) ;
263+ let llval = self . const_usize ( alloc. inner ( ) . align . bytes ( ) ) ;
264+ return if matches ! ( layout. primitive( ) , Pointer ( _) ) {
265+ unsafe { llvm:: LLVMConstIntToPtr ( llval, llty) }
266+ } else {
267+ self . const_bitcast ( llval, llty)
268+ } ;
269+ } else {
270+ let init = const_alloc_to_llvm ( self , alloc, /*static*/ false ) ;
271+ let alloc = alloc. inner ( ) ;
272+ let value = match alloc. mutability {
273+ Mutability :: Mut => self . static_addr_of_mut ( init, alloc. align , None ) ,
274+ _ => self . static_addr_of ( init, alloc. align , None ) ,
275+ } ;
276+ if !self . sess ( ) . fewer_names ( ) && llvm:: get_value_name ( value) . is_empty ( )
277+ {
278+ let hash = self . tcx . with_stable_hashing_context ( |mut hcx| {
279+ let mut hasher = StableHasher :: new ( ) ;
280+ alloc. hash_stable ( & mut hcx, & mut hasher) ;
281+ hasher. finish :: < Hash128 > ( )
282+ } ) ;
283+ llvm:: set_value_name (
284+ value,
285+ format ! ( "alloc_{hash:032x}" ) . as_bytes ( ) ,
286+ ) ;
287+ }
288+ ( value, AddressSpace :: DATA )
271289 }
272- ( value, AddressSpace :: DATA )
273290 }
274291 GlobalAlloc :: Function ( fn_instance) => (
275292 self . get_fn_addr ( fn_instance. polymorphize ( self . tcx ) ) ,
@@ -280,7 +297,7 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
280297 . tcx
281298 . global_alloc ( self . tcx . vtable_allocation ( ( ty, trait_ref) ) )
282299 . unwrap_memory ( ) ;
283- let init = const_alloc_to_llvm ( self , alloc) ;
300+ let init = const_alloc_to_llvm ( self , alloc, /*static*/ false ) ;
284301 let value = self . static_addr_of ( init, alloc. inner ( ) . align , None ) ;
285302 ( value, AddressSpace :: DATA )
286303 }
@@ -308,7 +325,7 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
308325 }
309326
310327 fn const_data_from_alloc ( & self , alloc : ConstAllocation < ' tcx > ) -> Self :: Value {
311- const_alloc_to_llvm ( self , alloc)
328+ const_alloc_to_llvm ( self , alloc, /*static*/ false )
312329 }
313330
314331 fn const_bitcast ( & self , val : & ' ll Value , ty : & ' ll Type ) -> & ' ll Value {
0 commit comments