@@ -339,7 +339,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
339339 let exit_bb = self . append_sibling_block ( "memset_exit" ) ;
340340
341341 let count = self . udiv ( size_bytes, size_elem_const) ;
342- let index = self . alloca ( count. ty , zero_align) ;
342+ let index = self . alloca ( self . lookup_type ( count. ty ) . sizeof ( self ) . unwrap ( ) , zero_align) ;
343343 self . store ( zero, index, zero_align) ;
344344 self . br ( header_bb) ;
345345
@@ -919,6 +919,45 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
919919 s1
920920 }
921921 }
922+
923+ // HACK(eddyb) helper shared by `typed_alloca` and `alloca`.
924+ fn declare_func_local_var (
925+ & mut self ,
926+ ty : <Self as BackendTypes >:: Type ,
927+ _align : Align ,
928+ ) -> SpirvValue {
929+ let ptr_ty = self . type_ptr_to ( ty) ;
930+
931+ // "All OpVariable instructions in a function must be the first instructions in the first block."
932+ let mut builder = self . emit ( ) ;
933+ builder. select_block ( Some ( 0 ) ) . unwrap ( ) ;
934+ let index = {
935+ let block = & builder. module_ref ( ) . functions [ builder. selected_function ( ) . unwrap ( ) ]
936+ . blocks [ builder. selected_block ( ) . unwrap ( ) ] ;
937+ block
938+ . instructions
939+ . iter ( )
940+ . enumerate ( )
941+ . find_map ( |( index, inst) | {
942+ if inst. class . opcode != Op :: Variable {
943+ Some ( InsertPoint :: FromBegin ( index) )
944+ } else {
945+ None
946+ }
947+ } )
948+ . unwrap_or ( InsertPoint :: End )
949+ } ;
950+ // TODO: rspirv doesn't have insert_variable function
951+ let result_id = builder. id ( ) ;
952+ let inst = Instruction :: new (
953+ Op :: Variable ,
954+ Some ( ptr_ty) ,
955+ Some ( result_id) ,
956+ vec ! [ Operand :: StorageClass ( StorageClass :: Function ) ] ,
957+ ) ;
958+ builder. insert_into_block ( index, inst) . unwrap ( ) ;
959+ result_id. with_type ( ptr_ty)
960+ }
922961}
923962
924963impl < ' a , ' tcx > BuilderMethods < ' a , ' tcx > for Builder < ' a , ' tcx > {
@@ -1418,43 +1457,14 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
14181457 // HACK(eddyb) new method patched into `pqp_cg_ssa` (see `build.rs`).
14191458 #[ cfg( not( rustc_codegen_spirv_disable_pqp_cg_ssa) ) ]
14201459 fn typed_alloca ( & mut self , ty : Self :: Type , align : Align ) -> Self :: Value {
1421- self . alloca ( ty, align)
1460+ self . declare_func_local_var ( ty, align)
14221461 }
1423- fn alloca ( & mut self , ty : Self :: Type , _align : Align ) -> Self :: Value {
1424- let ptr_ty = self . type_ptr_to ( ty) ;
1425- // "All OpVariable instructions in a function must be the first instructions in the first block."
1426- let mut builder = self . emit ( ) ;
1427- builder. select_block ( Some ( 0 ) ) . unwrap ( ) ;
1428- let index = {
1429- let block = & builder. module_ref ( ) . functions [ builder. selected_function ( ) . unwrap ( ) ]
1430- . blocks [ builder. selected_block ( ) . unwrap ( ) ] ;
1431- block
1432- . instructions
1433- . iter ( )
1434- . enumerate ( )
1435- . find_map ( |( index, inst) | {
1436- if inst. class . opcode != Op :: Variable {
1437- Some ( InsertPoint :: FromBegin ( index) )
1438- } else {
1439- None
1440- }
1441- } )
1442- . unwrap_or ( InsertPoint :: End )
1443- } ;
1444- // TODO: rspirv doesn't have insert_variable function
1445- let result_id = builder. id ( ) ;
1446- let inst = Instruction :: new (
1447- Op :: Variable ,
1448- Some ( ptr_ty) ,
1449- Some ( result_id) ,
1450- vec ! [ Operand :: StorageClass ( StorageClass :: Function ) ] ,
1451- ) ;
1452- builder. insert_into_block ( index, inst) . unwrap ( ) ;
1453- result_id. with_type ( ptr_ty)
1462+ fn alloca ( & mut self , size : Size , align : Align ) -> Self :: Value {
1463+ self . declare_func_local_var ( self . type_array ( self . type_i8 ( ) , size. bytes ( ) ) , align)
14541464 }
14551465
1456- fn byte_array_alloca ( & mut self , _len : Self :: Value , _align : Align ) -> Self :: Value {
1457- self . fatal ( "array alloca not supported yet" )
1466+ fn dynamic_alloca ( & mut self , _len : Self :: Value , _align : Align ) -> Self :: Value {
1467+ self . fatal ( "dynamic alloca not supported yet" )
14581468 }
14591469
14601470 fn load ( & mut self , ty : Self :: Type , ptr : Self :: Value , _align : Align ) -> Self :: Value {
0 commit comments