@@ -150,9 +150,9 @@ pub(crate) fn provide(providers: &mut Providers) {
150
150
let TyAndLayout { ty, mut layout } =
151
151
( rustc_interface:: DEFAULT_QUERY_PROVIDERS . layout_of ) ( tcx, key) ?;
152
152
153
- // FIXME(eddyb) make use of this - at this point, it's just a placeholder.
154
- #[ allow( clippy:: match_single_binding) ]
153
+ #[ allow( clippy:: match_like_matches_macro) ]
155
154
let hide_niche = match ty. kind ( ) {
155
+ ty:: Bool => true ,
156
156
_ => false ,
157
157
} ;
158
158
@@ -284,13 +284,6 @@ enum PointeeDefState {
284
284
/// provides a uniform way of translating them.
285
285
pub trait ConvSpirvType < ' tcx > {
286
286
fn spirv_type ( & self , span : Span , cx : & CodegenCx < ' tcx > ) -> Word ;
287
- /// spirv (and llvm) do not allow storing booleans in memory, they are abstract unsized values.
288
- /// So, if we're dealing with a "memory type", convert bool to u8. The opposite is an
289
- /// "immediate type", which keeps bools as bools. See also the functions `from_immediate` and
290
- /// `to_immediate`, which convert between the two.
291
- fn spirv_type_immediate ( & self , span : Span , cx : & CodegenCx < ' tcx > ) -> Word {
292
- self . spirv_type ( span, cx)
293
- }
294
287
}
295
288
296
289
impl < ' tcx > ConvSpirvType < ' tcx > for PointeeTy < ' tcx > {
@@ -302,14 +295,6 @@ impl<'tcx> ConvSpirvType<'tcx> for PointeeTy<'tcx> {
302
295
. spirv_type ( span, cx) ,
303
296
}
304
297
}
305
- fn spirv_type_immediate ( & self , span : Span , cx : & CodegenCx < ' tcx > ) -> Word {
306
- match * self {
307
- PointeeTy :: Ty ( ty) => ty. spirv_type_immediate ( span, cx) ,
308
- PointeeTy :: Fn ( ty) => cx
309
- . fn_abi_of_fn_ptr ( ty, ty:: List :: empty ( ) )
310
- . spirv_type_immediate ( span, cx) ,
311
- }
312
- }
313
298
}
314
299
315
300
impl < ' tcx > ConvSpirvType < ' tcx > for FnAbi < ' tcx , Ty < ' tcx > > {
@@ -318,9 +303,7 @@ impl<'tcx> ConvSpirvType<'tcx> for FnAbi<'tcx, Ty<'tcx>> {
318
303
319
304
let return_type = match self . ret . mode {
320
305
PassMode :: Ignore => SpirvType :: Void . def ( span, cx) ,
321
- PassMode :: Direct ( _) | PassMode :: Pair ( ..) => {
322
- self . ret . layout . spirv_type_immediate ( span, cx)
323
- }
306
+ PassMode :: Direct ( _) | PassMode :: Pair ( ..) => self . ret . layout . spirv_type ( span, cx) ,
324
307
PassMode :: Cast ( _) | PassMode :: Indirect { .. } => span_bug ! (
325
308
span,
326
309
"query hooks should've made this `PassMode` impossible: {:#?}" ,
@@ -331,14 +314,10 @@ impl<'tcx> ConvSpirvType<'tcx> for FnAbi<'tcx, Ty<'tcx>> {
331
314
for arg in & self . args {
332
315
let arg_type = match arg. mode {
333
316
PassMode :: Ignore => continue ,
334
- PassMode :: Direct ( _) => arg. layout . spirv_type_immediate ( span, cx) ,
317
+ PassMode :: Direct ( _) => arg. layout . spirv_type ( span, cx) ,
335
318
PassMode :: Pair ( _, _) => {
336
- argument_types. push ( scalar_pair_element_backend_type (
337
- cx, span, arg. layout , 0 , true ,
338
- ) ) ;
339
- argument_types. push ( scalar_pair_element_backend_type (
340
- cx, span, arg. layout , 1 , true ,
341
- ) ) ;
319
+ argument_types. push ( scalar_pair_element_backend_type ( cx, span, arg. layout , 0 ) ) ;
320
+ argument_types. push ( scalar_pair_element_backend_type ( cx, span, arg. layout , 1 ) ) ;
342
321
continue ;
343
322
}
344
323
PassMode :: Cast ( _) | PassMode :: Indirect { .. } => span_bug ! (
@@ -359,77 +338,69 @@ impl<'tcx> ConvSpirvType<'tcx> for FnAbi<'tcx, Ty<'tcx>> {
359
338
}
360
339
361
340
impl < ' tcx > ConvSpirvType < ' tcx > for TyAndLayout < ' tcx > {
362
- fn spirv_type ( & self , span : Span , cx : & CodegenCx < ' tcx > ) -> Word {
363
- trans_type_impl ( cx, span, * self , false )
364
- }
365
- fn spirv_type_immediate ( & self , span : Span , cx : & CodegenCx < ' tcx > ) -> Word {
366
- trans_type_impl ( cx, span, * self , true )
367
- }
368
- }
369
-
370
- fn trans_type_impl < ' tcx > (
371
- cx : & CodegenCx < ' tcx > ,
372
- mut span : Span ,
373
- ty : TyAndLayout < ' tcx > ,
374
- is_immediate : bool ,
375
- ) -> Word {
376
- if let TyKind :: Adt ( adt, substs) = * ty. ty . kind ( ) {
377
- if span == DUMMY_SP {
378
- span = cx. tcx . def_span ( adt. did ) ;
379
- }
341
+ fn spirv_type ( & self , mut span : Span , cx : & CodegenCx < ' tcx > ) -> Word {
342
+ if let TyKind :: Adt ( adt, substs) = * self . ty . kind ( ) {
343
+ if span == DUMMY_SP {
344
+ span = cx. tcx . def_span ( adt. did ) ;
345
+ }
380
346
381
- let attrs = AggregatedSpirvAttributes :: parse ( cx, cx. tcx . get_attrs ( adt. did ) ) ;
347
+ let attrs = AggregatedSpirvAttributes :: parse ( cx, cx. tcx . get_attrs ( adt. did ) ) ;
382
348
383
- if let Some ( intrinsic_type_attr) = attrs. intrinsic_type . map ( |attr| attr. value ) {
384
- if let Ok ( spirv_type) = trans_intrinsic_type ( cx, span, ty, substs, intrinsic_type_attr)
385
- {
386
- return spirv_type;
349
+ if let Some ( intrinsic_type_attr) = attrs. intrinsic_type . map ( |attr| attr. value ) {
350
+ if let Ok ( spirv_type) =
351
+ trans_intrinsic_type ( cx, span, * self , substs, intrinsic_type_attr)
352
+ {
353
+ return spirv_type;
354
+ }
387
355
}
388
356
}
389
- }
390
357
391
- // Note: ty.layout is orthogonal to ty.ty, e.g. `ManuallyDrop<Result<isize, isize>>` has abi
392
- // `ScalarPair`.
393
- // There's a few layers that we go through here. First we inspect layout.abi, then if relevant, layout.fields, etc.
394
- match ty. abi {
395
- Abi :: Uninhabited => SpirvType :: Adt {
396
- def_id : def_id_for_spirv_type_adt ( ty) ,
397
- size : Some ( Size :: ZERO ) ,
398
- align : Align :: from_bytes ( 0 ) . unwrap ( ) ,
399
- field_types : Vec :: new ( ) ,
400
- field_offsets : Vec :: new ( ) ,
401
- field_names : None ,
402
- }
403
- . def_with_name ( cx, span, TyLayoutNameKey :: from ( ty) ) ,
404
- Abi :: Scalar ( ref scalar) => trans_scalar ( cx, span, ty, scalar, Size :: ZERO , is_immediate) ,
405
- Abi :: ScalarPair ( ref a, ref b) => {
406
- // Note: We can't use auto_struct_layout here because the spirv types here might be undefined due to
407
- // recursive pointer types.
408
- let a_offset = Size :: ZERO ;
409
- let b_offset = a. value . size ( cx) . align_to ( b. value . align ( cx) . abi ) ;
410
- // Note! Do not pass through is_immediate here - they're wrapped in a struct, hence, not immediate.
411
- let a = trans_scalar ( cx, span, ty, a, a_offset, false ) ;
412
- let b = trans_scalar ( cx, span, ty, b, b_offset, false ) ;
413
- let size = if ty. is_unsized ( ) { None } else { Some ( ty. size ) } ;
414
- SpirvType :: Adt {
415
- def_id : def_id_for_spirv_type_adt ( ty) ,
416
- size,
417
- align : ty. align . abi ,
418
- field_types : vec ! [ a, b] ,
419
- field_offsets : vec ! [ a_offset, b_offset] ,
358
+ // Note: ty.layout is orthogonal to ty.ty, e.g. `ManuallyDrop<Result<isize, isize>>` has abi
359
+ // `ScalarPair`.
360
+ // There's a few layers that we go through here. First we inspect layout.abi, then if relevant, layout.fields, etc.
361
+ match self . abi {
362
+ Abi :: Uninhabited => SpirvType :: Adt {
363
+ def_id : def_id_for_spirv_type_adt ( * self ) ,
364
+ size : Some ( Size :: ZERO ) ,
365
+ align : Align :: from_bytes ( 0 ) . unwrap ( ) ,
366
+ field_types : Vec :: new ( ) ,
367
+ field_offsets : Vec :: new ( ) ,
420
368
field_names : None ,
421
369
}
422
- . def_with_name ( cx, span, TyLayoutNameKey :: from ( ty) )
423
- }
424
- Abi :: Vector { ref element, count } => {
425
- let elem_spirv = trans_scalar ( cx, span, ty, element, Size :: ZERO , false ) ;
426
- SpirvType :: Vector {
427
- element : elem_spirv,
428
- count : count as u32 ,
370
+ . def_with_name ( cx, span, TyLayoutNameKey :: from ( * self ) ) ,
371
+ Abi :: Scalar ( ref scalar) => trans_scalar ( cx, span, * self , scalar, Size :: ZERO ) ,
372
+ Abi :: ScalarPair ( ref a, ref b) => {
373
+ // Note: We can't use auto_struct_layout here because the spirv types here might be undefined due to
374
+ // recursive pointer types.
375
+ let a_offset = Size :: ZERO ;
376
+ let b_offset = a. value . size ( cx) . align_to ( b. value . align ( cx) . abi ) ;
377
+ let a = trans_scalar ( cx, span, * self , a, a_offset) ;
378
+ let b = trans_scalar ( cx, span, * self , b, b_offset) ;
379
+ let size = if self . is_unsized ( ) {
380
+ None
381
+ } else {
382
+ Some ( self . size )
383
+ } ;
384
+ SpirvType :: Adt {
385
+ def_id : def_id_for_spirv_type_adt ( * self ) ,
386
+ size,
387
+ align : self . align . abi ,
388
+ field_types : vec ! [ a, b] ,
389
+ field_offsets : vec ! [ a_offset, b_offset] ,
390
+ field_names : None ,
391
+ }
392
+ . def_with_name ( cx, span, TyLayoutNameKey :: from ( * self ) )
393
+ }
394
+ Abi :: Vector { ref element, count } => {
395
+ let elem_spirv = trans_scalar ( cx, span, * self , element, Size :: ZERO ) ;
396
+ SpirvType :: Vector {
397
+ element : elem_spirv,
398
+ count : count as u32 ,
399
+ }
400
+ . def ( span, cx)
429
401
}
430
- . def ( span, cx )
402
+ Abi :: Aggregate { sized : _ } => trans_aggregate ( cx , span, * self ) ,
431
403
}
432
- Abi :: Aggregate { sized : _ } => trans_aggregate ( cx, span, ty) ,
433
404
}
434
405
}
435
406
@@ -440,7 +411,6 @@ pub fn scalar_pair_element_backend_type<'tcx>(
440
411
span : Span ,
441
412
ty : TyAndLayout < ' tcx > ,
442
413
index : usize ,
443
- is_immediate : bool ,
444
414
) -> Word {
445
415
let [ a, b] = match & ty. layout . abi {
446
416
Abi :: ScalarPair ( a, b) => [ a, b] ,
@@ -455,7 +425,7 @@ pub fn scalar_pair_element_backend_type<'tcx>(
455
425
1 => a. value . size ( cx) . align_to ( b. value . align ( cx) . abi ) ,
456
426
_ => unreachable ! ( ) ,
457
427
} ;
458
- trans_scalar ( cx, span, ty, [ a, b] [ index] , offset, is_immediate )
428
+ trans_scalar ( cx, span, ty, [ a, b] [ index] , offset)
459
429
}
460
430
461
431
/// A "scalar" is a basic building block: bools, ints, floats, pointers. (i.e. not something complex like a struct)
@@ -471,9 +441,8 @@ fn trans_scalar<'tcx>(
471
441
ty : TyAndLayout < ' tcx > ,
472
442
scalar : & Scalar ,
473
443
offset : Size ,
474
- is_immediate : bool ,
475
444
) -> Word {
476
- if is_immediate && scalar. is_bool ( ) {
445
+ if scalar. is_bool ( ) {
477
446
return SpirvType :: Bool . def ( span, cx) ;
478
447
}
479
448
@@ -608,7 +577,7 @@ fn trans_aggregate<'tcx>(cx: &CodegenCx<'tcx>, span: Span, ty: TyAndLayout<'tcx>
608
577
}
609
578
}
610
579
FieldsShape :: Array { stride, count } => {
611
- let element_type = trans_type_impl ( cx , span , ty. field ( cx, 0 ) , false ) ;
580
+ let element_type = ty. field ( cx, 0 ) . spirv_type ( span , cx ) ;
612
581
if ty. is_unsized ( ) {
613
582
// There's a potential for this array to be sized, but the element to be unsized, e.g. `[[u8]; 5]`.
614
583
// However, I think rust disallows all these cases, so assert this here.
@@ -676,7 +645,7 @@ fn trans_struct<'tcx>(cx: &CodegenCx<'tcx>, span: Span, ty: TyAndLayout<'tcx>) -
676
645
let mut field_names = Vec :: new ( ) ;
677
646
for i in ty. fields . index_by_increasing_offset ( ) {
678
647
let field_ty = ty. field ( cx, i) ;
679
- field_types. push ( trans_type_impl ( cx , span, field_ty , false ) ) ;
648
+ field_types. push ( field_ty . spirv_type ( span, cx ) ) ;
680
649
let offset = ty. fields . offset ( i) ;
681
650
field_offsets. push ( offset) ;
682
651
if let Variants :: Single { index } = ty. variants {
@@ -887,7 +856,7 @@ fn trans_intrinsic_type<'tcx>(
887
856
// The spirv type of it will be generated by querying the type of the first generic.
888
857
if let Some ( image_ty) = substs. types ( ) . next ( ) {
889
858
// TODO: enforce that the generic param is an image type?
890
- let image_type = trans_type_impl ( cx , span , cx . layout_of ( image_ty) , false ) ;
859
+ let image_type = cx . layout_of ( image_ty) . spirv_type ( span , cx ) ;
891
860
Ok ( SpirvType :: SampledImage { image_type } . def ( span, cx) )
892
861
} else {
893
862
cx. tcx
@@ -907,7 +876,7 @@ fn trans_intrinsic_type<'tcx>(
907
876
// We use a generic to indicate the underlying element type.
908
877
// The spirv type of it will be generated by querying the type of the first generic.
909
878
if let Some ( elem_ty) = substs. types ( ) . next ( ) {
910
- let element = trans_type_impl ( cx , span , cx . layout_of ( elem_ty) , false ) ;
879
+ let element = cx . layout_of ( elem_ty) . spirv_type ( span , cx ) ;
911
880
Ok ( SpirvType :: RuntimeArray { element } . def ( span, cx) )
912
881
} else {
913
882
cx. tcx
@@ -922,7 +891,7 @@ fn trans_intrinsic_type<'tcx>(
922
891
. expect ( "#[spirv(matrix)] must be added to a type which has DefId" ) ;
923
892
924
893
let field_types = ( 0 ..ty. fields . count ( ) )
925
- . map ( |i| trans_type_impl ( cx , span , ty. field ( cx, i) , false ) )
894
+ . map ( |i| ty. field ( cx, i) . spirv_type ( span , cx ) )
926
895
. collect :: < Vec < _ > > ( ) ;
927
896
if field_types. len ( ) < 2 {
928
897
cx. tcx
0 commit comments