@@ -8,9 +8,7 @@ use crate::spirv_type::SpirvType;
88use itertools:: Itertools as _;
99use rspirv:: spirv:: Word ;
1010use rustc_abi:: { self as abi, AddressSpace , Float , HasDataLayout , Integer , Primitive , Size } ;
11- use rustc_codegen_ssa:: traits:: {
12- BaseTypeCodegenMethods , ConstCodegenMethods , MiscCodegenMethods , StaticCodegenMethods ,
13- } ;
11+ use rustc_codegen_ssa:: traits:: { ConstCodegenMethods , MiscCodegenMethods , StaticCodegenMethods } ;
1412use rustc_middle:: mir:: interpret:: { AllocError , ConstAllocation , GlobalAlloc , Scalar , alloc_range} ;
1513use rustc_middle:: ty:: layout:: LayoutOf ;
1614use rustc_span:: { DUMMY_SP , Span } ;
@@ -170,14 +168,13 @@ impl ConstCodegenMethods for CodegenCx<'_> {
170168 let str_ty = self
171169 . layout_of ( self . tcx . types . str_ )
172170 . spirv_type ( DUMMY_SP , self ) ;
173- let bytes_ty = self . type_array ( self . type_i8 ( ) , len as u64 ) ;
174171 (
175172 self . def_constant (
176173 self . type_ptr_to ( str_ty) ,
177174 SpirvConst :: PtrTo {
178175 pointee : self
179176 . constant_composite (
180- bytes_ty ,
177+ str_ty ,
181178 s. bytes ( ) . map ( |b| self . const_u8 ( b) . def_cx ( self ) ) ,
182179 )
183180 . def_cx ( self ) ,
@@ -562,7 +559,8 @@ impl<'tcx> CodegenCx<'tcx> {
562559 }
563560 SpirvType :: Vector { element, .. }
564561 | SpirvType :: Matrix { element, .. }
565- | SpirvType :: Array { element, .. } => {
562+ | SpirvType :: Array { element, .. }
563+ | SpirvType :: RuntimeArray { element } => {
566564 let stride = self . lookup_type ( element) . sizeof ( self ) . unwrap ( ) ;
567565
568566 let count = match ty_def {
@@ -572,6 +570,9 @@ impl<'tcx> CodegenCx<'tcx> {
572570 SpirvType :: Array { count, .. } => {
573571 u64:: try_from ( self . builder . lookup_const_scalar ( count) . unwrap ( ) ) . unwrap ( )
574572 }
573+ SpirvType :: RuntimeArray { .. } => {
574+ ( alloc. inner ( ) . size ( ) - offset) . bytes ( ) / stride. bytes ( )
575+ }
575576 _ => unreachable ! ( ) ,
576577 } ;
577578
@@ -594,50 +595,18 @@ impl<'tcx> CodegenCx<'tcx> {
594595 assert_eq ! ( read_size, ty_size) ;
595596 }
596597
597- ( result, read_size)
598- }
599- SpirvType :: RuntimeArray { element } => {
600- let stride = self . lookup_type ( element) . sizeof ( self ) . unwrap ( ) ;
601- if stride. bytes ( ) == 0 {
602- let result = self . undef ( ty) ;
603- self . zombie_no_span (
604- result. def_cx ( self ) ,
605- & format ! (
606- "unsupported unsized `{}` constant with zero-sized elements" ,
607- self . debug_type( ty)
608- ) ,
609- ) ;
610- return ( result, Size :: ZERO ) ;
611- }
612-
613- let read_size = alloc. inner ( ) . size ( ) - offset;
614- let rem = read_size. bytes ( ) % stride. bytes ( ) ;
615- if rem != 0 {
616- let result = self . undef ( ty) ;
598+ if let SpirvType :: RuntimeArray { .. } = ty_def {
599+ // FIXME(eddyb) values of this type should never be created,
600+ // the only reasonable encoding of e.g. `&str` consts should
601+ // be `&[u8; N]` consts, with the `static_addr_of` pointer
602+ // (*not* the value it points to) cast to `&str`, afterwards.
617603 self . zombie_no_span (
618604 result. def_cx ( self ) ,
619- & format ! (
620- "unsupported unsized `{}` constant with {rem} trailing bytes" ,
621- self . debug_type( ty)
622- ) ,
605+ & format ! ( "unsupported unsized `{}` constant" , self . debug_type( ty) ) ,
623606 ) ;
624- return ( result, read_size) ;
625607 }
626608
627- let count = read_size. bytes ( ) / stride. bytes ( ) ;
628- let sized_ty = self . type_array ( element, count) ;
629-
630- let result = self . constant_composite (
631- sized_ty,
632- ( 0 ..count) . map ( |i| {
633- let ( e, e_size) =
634- self . read_from_const_alloc_at ( alloc, element, offset + i * stride) ;
635- assert_eq ! ( e_size, stride) ;
636- e. def_cx ( self )
637- } ) ,
638- ) ;
639-
640- ( result, count * stride)
609+ ( result, read_size)
641610 }
642611
643612 SpirvType :: Void
0 commit comments