1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
// FIXME(eddyb) layouts are a bit tricky: this recomputes them from several passes.

use crate::qptr::shapes;
use crate::{
    spv, AddrSpace, Attr, Const, ConstCtor, Context, Diag, FxIndexMap, Type, TypeCtor, TypeCtorArg,
};
use itertools::Either;
use smallvec::SmallVec;
use std::cell::RefCell;
use std::cmp::Ordering;
use std::num::NonZeroU32;
use std::ops::Range;
use std::rc::Rc;

/// Various toggles for layout-related behavior that is not unambiguous from the
/// SPIR-V alone, or involves intermediary illegal SPIR-V (during legalization).
//
// FIXME(eddyb) use proper newtypes (and log2 for align!).
pub struct LayoutConfig {
    pub ignore_legacy_align: bool,
    pub min_aggregate_legacy_align: u32,

    /// Assumed size and alignment for `OpTypeBool`, even if unusable
    /// with externally-visible concrete memory (i.e. buffers).
    ///
    /// This is only useful for accurate handling of illegal SPIR-V relying on
    /// e.g. pointer casts, and as such defaults to `(1, 1)`, to merely ensure
    /// unique offsets and guarantee `qptr::lift` can tell fields apart.
    //
    // FIXME(eddyb) might be nice to default to an "offsets/sizes are abstract"
    // mode, which disallows reinterpretation on the basis that the precise
    // offsets/sizes may not match between types (but that's its own nightmare).
    pub abstract_bool_size_align: (u32, u32),

    /// Assumed size and alignment for logical `OpTypePointer`s, even if unusable
    /// with externally-visible concrete memory (i.e. buffers).
    ///
    /// This is only useful for accurate handling of illegal SPIR-V relying on
    /// e.g. pointer casts, and as such defaults to `(1, 1)`, to merely ensure
    /// unique offsets and guarantee `qptr::lift` can tell fields apart.
    //
    // FIXME(eddyb) might be nice to default to an "offsets/sizes are abstract"
    // mode, which disallows reinterpretation on the basis that the precise
    // offsets/sizes may not match between types (but that's its own nightmare).
    pub logical_ptr_size_align: (u32, u32),
}

impl LayoutConfig {
    pub const VULKAN_SCALAR_LAYOUT: Self = Self {
        ignore_legacy_align: true,
        min_aggregate_legacy_align: 1,

        abstract_bool_size_align: (1, 1),
        logical_ptr_size_align: (1, 1),
    };
    pub const VULKAN_STANDARD_LAYOUT: Self =
        Self { ignore_legacy_align: false, ..Self::VULKAN_SCALAR_LAYOUT };
    // FIXME(eddyb) is this even useful? (all the storage classes that have any
    // kind of alignment requirements, require explicit offsets)
    pub const VULKAN_EXTENDED_ALIGN_UBO_LAYOUT: Self =
        Self { min_aggregate_legacy_align: 16, ..Self::VULKAN_STANDARD_LAYOUT };
}

pub(super) struct LayoutError(pub(super) Diag);

#[derive(Clone)]
pub(super) enum TypeLayout {
    Handle(HandleLayout),
    HandleArray(HandleLayout, Option<NonZeroU32>),

    // FIXME(eddyb) unify terminology around "concrete"/"memory"/"untyped (data)".
    Concrete(Rc<MemTypeLayout>),
}

// NOTE(eddyb) `Handle` is parameterized over the `Buffer` layout.
pub(super) type HandleLayout = shapes::Handle<Rc<MemTypeLayout>>;

pub(super) struct MemTypeLayout {
    pub(super) original_type: Type,
    pub(super) mem_layout: shapes::MaybeDynMemLayout,
    pub(super) components: Components,
}

// FIXME(eddyb) use proper newtypes for byte sizes.
pub(super) enum Components {
    Scalar,

    /// Vector and array elements (all of them having the same `elem` layout).
    Elements {
        stride: NonZeroU32,
        elem: Rc<MemTypeLayout>,
        fixed_len: Option<NonZeroU32>,
    },

    Fields {
        // FIXME(eddyb) should these be fused? (but `u32` is smaller than `Rc`)
        offsets: SmallVec<[u32; 4]>,
        layouts: SmallVec<[Rc<MemTypeLayout>; 4]>,
    },
}

impl Components {
    /// Return all components (by index), which completely contain `offset_range`.
    ///
    /// If `offset_range` is zero-sized (`offset_range.start == offset_range.end`),
    /// this can return multiple components, with at most one ever being non-ZST.
    //
    // FIXME(eddyb) be more aggressive in pruning ZSTs so this can be simpler.
    pub(super) fn find_components_containing(
        &self,
        // FIXME(eddyb) consider renaming such offset ranges to "extent".
        offset_range: Range<u32>,
    ) -> impl Iterator<Item = usize> + '_ {
        match self {
            Components::Scalar => Either::Left(None.into_iter()),
            Components::Elements { stride, elem, fixed_len } => {
                Either::Left(
                    Some(offset_range.start / stride.get())
                        .and_then(|elem_idx| {
                            let elem_idx_vs_len = fixed_len
                                .map_or(Ordering::Less, |fixed_len| elem_idx.cmp(&fixed_len.get()));
                            let elem_size = match elem_idx_vs_len {
                                Ordering::Less => elem.mem_layout.fixed_base.size,

                                // HACK(eddyb) this allows one-past-the-end pointers.
                                Ordering::Equal => 0,

                                Ordering::Greater => return None,
                            };
                            let elem_start = elem_idx * stride.get();
                            Some((elem_idx, elem_start..elem_start.checked_add(elem_size)?))
                        })
                        .filter(|(_, elem_range)| offset_range.end <= elem_range.end)
                        .and_then(|(elem_idx, _)| usize::try_from(elem_idx).ok())
                        .into_iter(),
                )
            }
            // FIXME(eddyb) this is inefficient, we should be doing binary search
            // on offsets if they're ordered (with an optional `BTreeMap<offset, idx>`?)
            // - ideally this needs an abstraction tho, some kind of "binary-searchable array"?
            Components::Fields { offsets, layouts } => Either::Right(
                offsets
                    .iter()
                    .zip(layouts)
                    .map(|(&field_offset, field)| {
                        // HACK(eddyb) really need a maybe-open-ended range type.
                        if field.mem_layout.dyn_unit_stride.is_some() {
                            Err(field_offset..)
                        } else {
                            Ok(field_offset
                                ..field_offset
                                    .checked_add(field.mem_layout.fixed_base.size)
                                    .unwrap())
                        }
                    })
                    .enumerate()
                    .filter(move |(_, field_range)| match field_range {
                        Ok(field_range) => {
                            field_range.start <= offset_range.start
                                && offset_range.end <= field_range.end
                        }
                        Err(field_range) => field_range.start <= offset_range.start,
                    })
                    .map(|(field_idx, _)| field_idx),
            ),
        }
    }
}

/// Context for computing `TypeLayout`s from `Type`s (with caching).
pub(super) struct LayoutCache<'a> {
    cx: Rc<Context>,
    wk: &'static spv::spec::WellKnown,

    config: &'a LayoutConfig,

    cache: RefCell<FxIndexMap<Type, TypeLayout>>,
}

impl<'a> LayoutCache<'a> {
    pub(super) fn new(cx: Rc<Context>, config: &'a LayoutConfig) -> Self {
        Self { cx, wk: &spv::spec::Spec::get().well_known, config, cache: Default::default() }
    }

    // FIXME(eddyb) properly distinguish between zero-extension and sign-extension.
    fn const_as_u32(&self, ct: Const) -> Option<u32> {
        match &self.cx[ct].ctor {
            ConstCtor::SpvInst(spv_inst)
                if spv_inst.opcode == self.wk.OpConstant && spv_inst.imms.len() == 1 =>
            {
                match spv_inst.imms[..] {
                    [spv::Imm::Short(_, x)] => Some(x),
                    _ => unreachable!(),
                }
            }
            _ => None,
        }
    }

    /// Attempt to compute a `TypeLayout` for a given (SPIR-V) `Type`.
    pub(super) fn layout_of(&self, ty: Type) -> Result<TypeLayout, LayoutError> {
        if let Some(cached) = self.cache.borrow().get(&ty).cloned() {
            return Ok(cached);
        }

        let cx = &self.cx;
        let wk = self.wk;

        let ty_def = &cx[ty];
        let spv_inst = match &ty_def.ctor {
            // FIXME(eddyb) treat `QPtr`s as scalars.
            TypeCtor::QPtr => {
                return Err(LayoutError(Diag::bug(
                    ["`layout_of(qptr)` (already lowered?)".into()],
                )));
            }
            TypeCtor::SpvInst(spv_inst) => spv_inst,
            TypeCtor::SpvStringLiteralForExtInst => {
                return Err(LayoutError(Diag::bug([
                    "`layout_of(type_of(OpString<\"...\">))`".into()
                ])));
            }
        };

        let scalar_with_size_and_align = |(size, align)| {
            TypeLayout::Concrete(Rc::new(MemTypeLayout {
                original_type: ty,
                mem_layout: shapes::MaybeDynMemLayout {
                    fixed_base: shapes::MemLayout { align, legacy_align: align, size },
                    dyn_unit_stride: None,
                },
                components: Components::Scalar,
            }))
        };
        let scalar = |width: u32| {
            assert!(width.is_power_of_two());
            let size = width / 8;
            assert_eq!(size * 8, width);
            scalar_with_size_and_align((size, size))
        };
        let align_to = |size: u32, align: u32| {
            assert!(align.is_power_of_two() && align > 0);
            Ok(size.checked_add(align - 1).ok_or_else(|| {
                LayoutError(Diag::bug([
                    format!("`align_to({size}, {align})` overflowed `u32`").into()
                ]))
            })? & !(align - 1))
        };
        // HACK(eddyb) named arguments for the `array` closure.
        struct ArrayParams {
            fixed_len: Option<u32>,
            known_stride: Option<u32>,
            min_legacy_align: u32,
            legacy_align_multiplier: u32,
        }
        let array = |elem_type: Type,
                     ArrayParams {
                         fixed_len,
                         known_stride,
                         min_legacy_align,
                         legacy_align_multiplier,
                     }| {
            let fixed_len = fixed_len
                .map(|x| {
                    NonZeroU32::new(x).ok_or_else(|| {
                        LayoutError(Diag::err(["SPIR-V disallows arrays of `0` length".into()]))
                    })
                })
                .transpose()?;
            match self.layout_of(elem_type)? {
                TypeLayout::Handle(handle) => Ok(TypeLayout::HandleArray(handle, fixed_len)),
                TypeLayout::HandleArray(..) => Err(LayoutError(Diag::err([
                    "handle array `".into(),
                    elem_type.into(),
                    "` cannot be further wrapped in an array".into(),
                ]))),
                TypeLayout::Concrete(elem) => {
                    if elem.mem_layout.dyn_unit_stride.is_some() {
                        return Err(LayoutError(Diag::err([
                            "dynamically sized type `".into(),
                            elem_type.into(),
                            "` cannot be further wrapped in an array".into(),
                        ])));
                    }
                    let stride = match known_stride {
                        Some(stride) => stride,
                        None => {
                            let shapes::MemLayout { align, legacy_align, size } =
                                elem.mem_layout.fixed_base;
                            let (stride, legacy_stride) =
                                (align_to(size, align)?, align_to(size, legacy_align)?);

                            // FIXME(eddyb) this whole ambiguity mechanism is strange and
                            // maybe unnecessary? (all the storage classes that have any
                            // kind of alignment requirements, require explicit offsets)
                            if !self.config.ignore_legacy_align && stride != legacy_stride {
                                return Err(LayoutError(Diag::bug([format!(
                                    "ambiguous stride: \
                                    {stride} (scalar) vs {legacy_stride} (legacy), \
                                     due to alignment differences \
                                     ({align} scalar vs {legacy_align} legacy)",
                                )
                                .into()])));
                            }
                            stride
                        }
                    };
                    let stride = NonZeroU32::new(stride).ok_or_else(|| {
                        LayoutError(Diag::err(["SPIR-V disallows arrays of `0` stride".into()]))
                    })?;
                    Ok(TypeLayout::Concrete(Rc::new(MemTypeLayout {
                        original_type: ty,
                        mem_layout: shapes::MaybeDynMemLayout {
                            fixed_base: shapes::MemLayout {
                                align: elem.mem_layout.fixed_base.align,
                                legacy_align: elem
                                    .mem_layout
                                    .fixed_base
                                    .legacy_align
                                    .checked_mul(legacy_align_multiplier)
                                    .unwrap()
                                    .max(min_legacy_align),
                                size: fixed_len
                                    .map(|len| {
                                        stride.checked_mul(len).ok_or_else(|| {
                                            LayoutError(Diag::bug([format!(
                                                "`{stride} * {len}` overflowed `u32`"
                                            )
                                            .into()]))
                                        })
                                    })
                                    .transpose()?
                                    .map_or(0, |size| size.get()),
                            },
                            dyn_unit_stride: if fixed_len.is_none() { Some(stride) } else { None },
                        },
                        components: Components::Elements { stride, elem, fixed_len },
                    })))
                }
            }
        };
        let short_imm_at = |i| match spv_inst.imms[i] {
            spv::Imm::Short(_, x) => x,
            _ => unreachable!(),
        };

        // FIXME(eddyb) !!! what if... types had a min/max size and then...
        // that would allow surrounding offsets to limit their size... but... ugh...
        // ugh this doesn't make any sense. maybe if the front-end specifies
        // offsets with "abstract types", it must configure `qptr::layout`?
        let layout = if spv_inst.opcode == wk.OpTypeBool {
            // FIXME(eddyb) make this properly abstract instead of only configurable.
            scalar_with_size_and_align(self.config.abstract_bool_size_align)
        } else if spv_inst.opcode == wk.OpTypePointer {
            // FIXME(eddyb) make this properly abstract instead of only configurable.
            // FIXME(eddyb) categorize `OpTypePointer` by storage class and split on
            // logical vs physical here.
            scalar_with_size_and_align(self.config.logical_ptr_size_align)
        } else if [wk.OpTypeInt, wk.OpTypeFloat].contains(&spv_inst.opcode) {
            scalar(short_imm_at(0))
        } else if [wk.OpTypeVector, wk.OpTypeMatrix].contains(&spv_inst.opcode) {
            let len = short_imm_at(0);
            let (min_legacy_align, legacy_align_multiplier) = if spv_inst.opcode == wk.OpTypeVector
            {
                // NOTE(eddyb) this is specifically Vulkan "base alignment".
                (1, if len <= 2 { 2 } else { 4 })
            } else {
                (self.config.min_aggregate_legacy_align, 1)
            };
            // NOTE(eddyb) `RowMajor` is disallowed on `OpTypeStruct` members below.
            array(
                match ty_def.ctor_args[..] {
                    [TypeCtorArg::Type(elem_type)] => elem_type,
                    _ => unreachable!(),
                },
                ArrayParams {
                    fixed_len: Some(len),
                    known_stride: None,
                    min_legacy_align,
                    legacy_align_multiplier,
                },
            )?
        } else if [wk.OpTypeArray, wk.OpTypeRuntimeArray].contains(&spv_inst.opcode) {
            let len = ty_def
                .ctor_args
                .get(1)
                .map(|&len| {
                    let len = match len {
                        TypeCtorArg::Const(len) => len,
                        TypeCtorArg::Type(_) => unreachable!(),
                    };
                    self.const_as_u32(len).ok_or_else(|| {
                        LayoutError(Diag::bug(
                            ["specialization constants not supported yet".into()],
                        ))
                    })
                })
                .transpose()?;
            let mut stride_decoration = None;
            for attr in &cx[ty_def.attrs].attrs {
                match attr {
                    Attr::SpvAnnotation(attr_spv_inst)
                        if attr_spv_inst.opcode == wk.OpDecorate
                            && attr_spv_inst.imms[0]
                                == spv::Imm::Short(wk.Decoration, wk.ArrayStride) =>
                    {
                        stride_decoration = Some(match attr_spv_inst.imms[1] {
                            spv::Imm::Short(_, x) => x,
                            _ => unreachable!(),
                        });
                        break;
                    }
                    _ => {}
                }
            }
            array(
                match ty_def.ctor_args[0] {
                    TypeCtorArg::Type(elem_type) => elem_type,
                    TypeCtorArg::Const(_) => unreachable!(),
                },
                ArrayParams {
                    fixed_len: len,
                    known_stride: stride_decoration,
                    min_legacy_align: self.config.min_aggregate_legacy_align,
                    legacy_align_multiplier: 1,
                },
            )?
        } else if spv_inst.opcode == wk.OpTypeStruct {
            let field_layouts: SmallVec<[_; 4]> = ty_def
                .ctor_args
                .iter()
                .map(|&arg| match arg {
                    TypeCtorArg::Type(field_type) => field_type,
                    TypeCtorArg::Const(_) => unreachable!(),
                })
                .map(|field_type| match self.layout_of(field_type)? {
                    TypeLayout::Handle(_) | TypeLayout::HandleArray(..) => {
                        Err(LayoutError(Diag::bug([
                            "handles cannot be placed in a struct field".into()
                        ])))
                    }
                    TypeLayout::Concrete(field_layout) => Ok(field_layout),
                })
                .collect::<Result<_, _>>()?;

            let mut field_offsets: SmallVec<[_; 4]> = SmallVec::with_capacity(field_layouts.len());
            for attr in &cx[ty_def.attrs].attrs {
                match attr {
                    Attr::SpvAnnotation(attr_spv_inst)
                        if attr_spv_inst.opcode == wk.OpMemberDecorate
                            && attr_spv_inst.imms[1]
                                == spv::Imm::Short(wk.Decoration, wk.RowMajor) =>
                    {
                        return Err(LayoutError(Diag::bug([
                            "`RowMajor` matrix types unsupported".into(),
                        ])));
                    }
                    Attr::SpvAnnotation(attr_spv_inst)
                        if attr_spv_inst.opcode == wk.OpMemberDecorate
                            && attr_spv_inst.imms[1]
                                == spv::Imm::Short(wk.Decoration, wk.Offset) =>
                    {
                        let (field_idx, field_offset) = match attr_spv_inst.imms[..] {
                            [spv::Imm::Short(_, idx), _, spv::Imm::Short(_, offset)] => {
                                (idx, offset)
                            }
                            _ => unreachable!(),
                        };
                        let field_idx = usize::try_from(field_idx).unwrap();
                        match field_idx.cmp(&field_offsets.len()) {
                            Ordering::Less => {
                                return Err(LayoutError(Diag::bug([
                                    "a struct field cannot have more than one explicit offset"
                                        .into(),
                                ])));
                            }
                            Ordering::Greater => {
                                return Err(LayoutError(Diag::bug([
                                    "structs with explicit offsets must provide them for all fields"
                                        .into(),
                                ])));
                            }
                            Ordering::Equal => {
                                field_offsets.push(field_offset);
                            }
                        }
                    }
                    _ => {}
                }
            }
            let mut mem_layout = shapes::MaybeDynMemLayout {
                fixed_base: shapes::MemLayout {
                    align: 1,
                    legacy_align: self.config.min_aggregate_legacy_align,
                    size: 0,
                },
                dyn_unit_stride: None,
            };
            if !field_offsets.is_empty() {
                if field_offsets.len() != field_layouts.len() {
                    return Err(LayoutError(Diag::bug([
                        "structs with explicit offsets must provide them for all fields".into(),
                    ])));
                }

                // HACK(eddyb) this treats the struct more like an union, but
                // it shold nevertheless work (the other approach would be to
                // search for the "last field (in offset order)", and/or iterate
                // all fields in offset order, to validate the lack of overlap),
                // and also "last field (in offset order)" approaches would still
                // have to look at all the fields in order to compute alignment.
                for (&field_offset, field_layout) in field_offsets.iter().zip(&field_layouts) {
                    let field = field_layout.mem_layout;

                    mem_layout.fixed_base.align =
                        mem_layout.fixed_base.align.max(field.fixed_base.align);
                    mem_layout.fixed_base.legacy_align =
                        mem_layout.fixed_base.legacy_align.max(field.fixed_base.legacy_align);
                    mem_layout.fixed_base.size = mem_layout.fixed_base.size.max(
                        field_offset.checked_add(field.fixed_base.size).ok_or_else(|| {
                            LayoutError(Diag::bug([format!(
                                "`{} + {}` overflowed `u32`",
                                field_offset, field.fixed_base.size
                            )
                            .into()]))
                        })?,
                    );

                    // FIXME(eddyb) validate sized-vs-unsized fields, too.
                    if let Some(field_dyn_unit_stride) = field.dyn_unit_stride {
                        if mem_layout.dyn_unit_stride.is_some() {
                            return Err(LayoutError(Diag::bug([
                                "only one field of a struct can have a dynamically sized type"
                                    .into(),
                            ])));
                        }
                        mem_layout.dyn_unit_stride = Some(field_dyn_unit_stride);
                    }
                }
            } else {
                for field_layout in &field_layouts {
                    if mem_layout.dyn_unit_stride.is_some() {
                        return Err(LayoutError(Diag::bug([
                            "only the last field of a struct can have a dynamically sized type"
                                .into(),
                        ])));
                    }

                    let field = field_layout.mem_layout;

                    let (offset, legacy_offset) = (
                        align_to(mem_layout.fixed_base.size, field.fixed_base.align)?,
                        align_to(mem_layout.fixed_base.size, field.fixed_base.legacy_align)?,
                    );
                    // FIXME(eddyb) this whole ambiguity mechanism is strange and
                    // maybe unnecessary? (all the storage classes that have any
                    // kind of alignment requirements, require explicit offsets)
                    if !self.config.ignore_legacy_align && offset != legacy_offset {
                        return Err(LayoutError(Diag::bug([format!(
                            "ambiguous offset: {offset} (scalar) vs {legacy_offset} (legacy), \
                             due to alignment differences ({} scalar vs {} legacy)",
                            field.fixed_base.align, field.fixed_base.legacy_align
                        )
                        .into()])));
                    }

                    field_offsets.push(offset);

                    mem_layout.fixed_base.align =
                        mem_layout.fixed_base.align.max(field.fixed_base.align);
                    mem_layout.fixed_base.legacy_align =
                        mem_layout.fixed_base.legacy_align.max(field.fixed_base.legacy_align);
                    mem_layout.fixed_base.size =
                        offset.checked_add(field.fixed_base.size).ok_or_else(|| {
                            LayoutError(Diag::bug([format!(
                                "`{} + {}` overflowed `u32`",
                                offset, field.fixed_base.size
                            )
                            .into()]))
                        })?;

                    assert!(mem_layout.dyn_unit_stride.is_none());
                    mem_layout.dyn_unit_stride = field.dyn_unit_stride;
                }
            }
            // FIXME(eddyb) how should the fixed base be aligned in unsized structs?
            if mem_layout.dyn_unit_stride.is_none() {
                mem_layout.fixed_base.size =
                    align_to(mem_layout.fixed_base.size, mem_layout.fixed_base.align)?;
            }

            let concrete = Rc::new(MemTypeLayout {
                original_type: ty,
                mem_layout,
                components: Components::Fields { offsets: field_offsets, layouts: field_layouts },
            });
            let mut is_interface_block = false;
            for attr in &cx[ty_def.attrs].attrs {
                match attr {
                    Attr::SpvAnnotation(attr_spv_inst)
                        if attr_spv_inst.opcode == wk.OpDecorate
                            && attr_spv_inst.imms[0]
                                == spv::Imm::Short(wk.Decoration, wk.Block) =>
                    {
                        is_interface_block = true;
                        break;
                    }
                    _ => {}
                }
            }
            // FIXME(eddyb) not all "interface blocks" imply buffers, so this may
            // need to be ignored based on the SPIR-V storage class of a `GlobalVar`.
            //
            // FIXME(eddyb) but the lowering of operations on pointers depend on
            // whether the pointer is to a buffer or a data type - without the
            // way Rust-GPU uses `Generic`, it should at least be possible to
            // determine from the pointer type itself, at the op lowering time,
            // but with storage class inference this isn't knowable...
            //
            // OTOH, Rust-GPU doesn't really use `Block` outside of buffers, so
            // it's plausible there could be `qptr` customization options which
            // Rust-GPU uses to unambiguously communicate its (mis)use of SPIR-V
            // (long-term it should probably have different Rust types per
            // storage class, or even represent buffers as Rust pointers?)
            if is_interface_block {
                // HACK(eddyb) we need an `AddrSpace` but it's not known yet.
                TypeLayout::Handle(shapes::Handle::Buffer(AddrSpace::Handles, concrete))
            } else {
                TypeLayout::Concrete(concrete)
            }
        } else if [
            wk.OpTypeImage,
            wk.OpTypeSampler,
            wk.OpTypeSampledImage,
            wk.OpTypeAccelerationStructureKHR,
        ]
        .contains(&spv_inst.opcode)
        {
            TypeLayout::Handle(shapes::Handle::Opaque(ty))
        } else {
            return Err(LayoutError(Diag::bug([format!(
                "unknown/unsupported SPIR-V type `{}`",
                spv_inst.opcode.name()
            )
            .into()])));
        };
        self.cache.borrow_mut().insert(ty, layout.clone());
        Ok(layout)
    }
}