@@ -339,7 +339,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
339
339
let exit_bb = self . append_sibling_block ( "memset_exit" ) ;
340
340
341
341
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) ;
343
343
self . store ( zero, index, zero_align) ;
344
344
self . br ( header_bb) ;
345
345
@@ -919,6 +919,45 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
919
919
s1
920
920
}
921
921
}
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
+ }
922
961
}
923
962
924
963
impl < ' a , ' tcx > BuilderMethods < ' a , ' tcx > for Builder < ' a , ' tcx > {
@@ -1418,43 +1457,14 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
1418
1457
// HACK(eddyb) new method patched into `pqp_cg_ssa` (see `build.rs`).
1419
1458
#[ cfg( not( rustc_codegen_spirv_disable_pqp_cg_ssa) ) ]
1420
1459
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)
1422
1461
}
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)
1454
1464
}
1455
1465
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" )
1458
1468
}
1459
1469
1460
1470
fn load ( & mut self , ty : Self :: Type , ptr : Self :: Value , _align : Align ) -> Self :: Value {
0 commit comments