metal2vulkan 0.1.0

Translate Metal AIR / LLVM IR to Vulkan SPIR-V with a native Rust emitter
Documentation
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
//! Workgroup-rooted and interface-rooted pointer materialization.

use super::*;
use crate::passes::resources::rewrites::*;

/// Threadgroup memory params are spliced to fixed-size Workgroup arrays. Access chains rooted at the
/// param are already valid after `rewrite_pointer_storage`, but a direct AIR load/store or pointer
/// merge arm means element zero and cannot legally use the array variable itself as the pointer
/// operand. Re-root those direct uses through the array path to their expected leaf type.
pub(in crate::passes) fn rewrite_workgroup_root_access(
    ctx: &mut Ctx,
    entry_idx: usize,
    var: Word,
    defs: &HashMap<Word, Instruction>,
) {
    let types = combined_type_defs(ctx, defs);
    let value_types = combined_value_types(ctx, entry_idx);
    let Some(var_ptr_ty) = value_types.get(&var).copied() else {
        return;
    };
    let Some(array_ty) = ptr_pointee(&types, var_ptr_ty) else {
        return;
    };

    let mut want: Vec<Word> = vec![];
    for blk in &ctx.module.functions[entry_idx].blocks {
        for inst in &blk.instructions {
            match inst.class.opcode {
                Op::Load if inst.operands.first() == Some(&Operand::IdRef(var)) => {
                    if let Some(t) = inst.result_type {
                        if !want.contains(&t) {
                            want.push(t);
                        }
                    }
                }
                Op::Store if inst.operands.first() == Some(&Operand::IdRef(var)) => {
                    let Some(Operand::IdRef(object)) = inst.operands.get(1) else {
                        continue;
                    };
                    let Some(t) = value_types.get(object).copied() else {
                        continue;
                    };
                    if !want.contains(&t) {
                        want.push(t);
                    }
                }
                Op::Select | Op::Phi => {
                    let uses_var = if inst.class.opcode == Op::Select {
                        inst.operands
                            .get(1..)
                            .is_some_and(|arms| arms.contains(&Operand::IdRef(var)))
                    } else {
                        inst.operands
                            .iter()
                            .step_by(2)
                            .any(|operand| operand == &Operand::IdRef(var))
                    };
                    if !uses_var {
                        continue;
                    }
                    let Some(target) = inst.result_type.and_then(|ty| ptr_pointee(&types, ty))
                    else {
                        continue;
                    };
                    if !want.contains(&target) {
                        want.push(target);
                    }
                }
                _ => {}
            }
        }
    }
    if want.is_empty() {
        return;
    }

    let u0 = ctx.const_uint(0);
    let mut leaf_chain: HashMap<Word, Word> = HashMap::new();
    let mut injected: Vec<Instruction> = vec![];
    for target in want {
        let Some(path) = path_to_leaf(&types, array_ty, target) else {
            continue;
        };
        let ptr_ty = ctx.ty_ptr(StorageClass::Workgroup, target);
        let id = ctx.module.fresh_id();
        let mut ops = vec![Operand::IdRef(var)];
        for _ in &path {
            ops.push(Operand::IdRef(u0));
        }
        injected.push(Instruction::new(
            Op::InBoundsAccessChain,
            Some(ptr_ty),
            Some(id),
            ops,
        ));
        leaf_chain.insert(target, id);
    }
    if leaf_chain.is_empty() {
        return;
    }

    for blk in &mut ctx.module.functions[entry_idx].blocks {
        for inst in &mut blk.instructions {
            match inst.class.opcode {
                Op::Load if inst.operands.first() == Some(&Operand::IdRef(var)) => {
                    if let Some(&chain) = inst.result_type.as_ref().and_then(|t| leaf_chain.get(t))
                    {
                        inst.operands[0] = Operand::IdRef(chain);
                    }
                }
                Op::Store if inst.operands.first() == Some(&Operand::IdRef(var)) => {
                    let Some(Operand::IdRef(object)) = inst.operands.get(1) else {
                        continue;
                    };
                    if let Some(&chain) = value_types.get(object).and_then(|t| leaf_chain.get(t)) {
                        inst.operands[0] = Operand::IdRef(chain);
                    }
                }
                Op::Select | Op::Phi => {
                    let Some(chain) = inst
                        .result_type
                        .and_then(|ty| ptr_pointee(&types, ty))
                        .and_then(|target| leaf_chain.get(&target))
                        .copied()
                    else {
                        continue;
                    };
                    if inst.class.opcode == Op::Select {
                        for operand in inst.operands.iter_mut().skip(1) {
                            if *operand == Operand::IdRef(var) {
                                *operand = Operand::IdRef(chain);
                            }
                        }
                    } else {
                        for operand in inst.operands.iter_mut().step_by(2) {
                            if *operand == Operand::IdRef(var) {
                                *operand = Operand::IdRef(chain);
                            }
                        }
                    }
                }
                _ => {}
            }
        }
    }

    if let Some(first) = ctx.module.functions[entry_idx].blocks.first_mut() {
        let at = first
            .instructions
            .iter()
            .position(|i| i.class.opcode != Op::Variable)
            .unwrap_or(first.instructions.len());
        for (k, chain) in injected.into_iter().enumerate() {
            first.instructions.insert(at + k, chain);
        }
    }
}

/// Rewrite the uses of a buffer param the backend collapsed into a bare element pointer. `block_ty`
/// is the StorageBuffer Block we point the var at (a `{ RuntimeArray<T> }` for a genuine array, or the
/// reconstructed struct). Each access chain rooted at the param is re-rooted at the var (with a
/// member-0 index prepended iff `prepend_member0`, for the runtime-array case); each direct OpLoad of
/// the param — the offset-0 leaf — is routed through an access chain to that leaf.
pub(in crate::passes) fn rewrite_collapsed_buffer(
    ctx: &mut Ctx,
    entry_idx: usize,
    pid: Word,
    var: Word,
    block_ty: Word,
    prepend_member0: bool,
    defs: &HashMap<Word, Instruction>,
) {
    let u0 = ctx.const_uint(0);

    // A combined type map (original defs + the types we synthesized) for leaf-path descent.
    let mut types = defs.clone();
    for g in &ctx.new_globals {
        if let Some(id) = g.result_id {
            types.entry(id).or_insert_with(|| g.clone());
        }
    }

    let value_types = combined_value_types(ctx, entry_idx);
    let desired_pointees = pointer_leaf_use_types(ctx, entry_idx, &value_types);

    // 1) Access chains based directly at the param: re-root at the buffer var; prepend member-0 only
    //    for the runtime-array wrapping (the original first index then indexes the runtime array).
    //    Some AIR record-0 member paths arrive as a single padded LLVM struct index even though the
    //    chosen wrapper is `{ RuntimeArray<compact-metadata-struct> }`; when the chain's user proves the
    //    intended compact member, route it through record 0 before falling back to plain array indexing.
    let n_blocks = ctx.module.functions[entry_idx].blocks.len();
    for bi in 0..n_blocks {
        let n_inst = ctx.module.functions[entry_idx].blocks[bi]
            .instructions
            .len();
        for ii in 0..n_inst {
            let (old_indices, result_type, result_id) = {
                let inst = &ctx.module.functions[entry_idx].blocks[bi].instructions[ii];
                if matches!(
                    inst.class.opcode,
                    Op::AccessChain | Op::InBoundsAccessChain | Op::PtrAccessChain
                ) && inst.operands.first() == Some(&Operand::IdRef(pid))
                {
                    (
                        inst.operands[1..].to_vec(),
                        inst.result_type,
                        inst.result_id,
                    )
                } else {
                    (vec![], None, None)
                }
            };
            if old_indices.is_empty() {
                continue;
            }

            let direct_root = if prepend_member0 {
                runtime_array_block_element_type(&types, block_ty)
            } else {
                Some(block_ty)
            };
            let remapped = direct_root.and_then(|root_ty| {
                remap_collapsed_direct_air_struct_access(
                    ctx,
                    &types,
                    root_ty,
                    &old_indices,
                    result_type,
                )
                .or_else(|| {
                    result_id
                        .and_then(|rid| desired_pointees.get(&rid).and_then(|ty| *ty))
                        .and_then(|desired_pointee| {
                            remap_direct_air_struct_access_to_pointee(
                                ctx,
                                &types,
                                root_ty,
                                &old_indices,
                                desired_pointee,
                            )
                        })
                })
            });

            if let Some((indices, pointee)) = remapped {
                let mut operands = vec![Operand::IdRef(var)];
                if prepend_member0 {
                    operands.push(Operand::IdRef(u0));
                    operands.push(Operand::IdRef(u0));
                }
                operands.extend(indices);
                let ptr_ty = ctx.ty_ptr(StorageClass::StorageBuffer, pointee);
                let inst = &mut ctx.module.functions[entry_idx].blocks[bi].instructions[ii];
                inst.operands = operands;
                inst.result_type = Some(ptr_ty);
                continue;
            }

            let inst = &mut ctx.module.functions[entry_idx].blocks[bi].instructions[ii];
            inst.operands[0] = Operand::IdRef(var);
            if prepend_member0 {
                inst.operands.insert(1, Operand::IdRef(u0));
            }
        }
    }

    // 2) Direct loads/stores of the param (offset-0 object): route each through an access chain to
    //    the exact aggregate or leaf type used by the instruction, one chain per distinct type.
    let mut want: Vec<Word> = vec![]; // distinct direct object types needing a chain
    for blk in &ctx.module.functions[entry_idx].blocks {
        for inst in &blk.instructions {
            match inst.class.opcode {
                Op::Load if inst.operands.first() == Some(&Operand::IdRef(pid)) => {
                    if let Some(t) = inst.result_type {
                        if !want.contains(&t) {
                            want.push(t);
                        }
                    }
                }
                Op::Store if inst.operands.first() == Some(&Operand::IdRef(pid)) => {
                    let Some(Operand::IdRef(object)) = inst.operands.get(1) else {
                        continue;
                    };
                    if let Some(t) = value_types.get(object).copied() {
                        if !want.contains(&t) {
                            want.push(t);
                        }
                    }
                }
                _ => {}
            }
        }
    }
    let mut leaf_chain: HashMap<Word, Word> = HashMap::new();
    let mut injected: Vec<Instruction> = vec![];
    for t in want {
        let Some(path) = path_to_leaf(&types, block_ty, t) else {
            continue;
        };
        let ptr_ty = ctx.ty_ptr(StorageClass::StorageBuffer, t);
        let id = ctx.module.fresh_id();
        let mut ops = vec![Operand::IdRef(var)];
        for _ in &path {
            ops.push(Operand::IdRef(u0));
        }
        injected.push(Instruction::new(
            Op::AccessChain,
            Some(ptr_ty),
            Some(id),
            ops,
        ));
        leaf_chain.insert(t, id);
    }
    // Point each direct load/store at its exact-type chain.
    for blk in &mut ctx.module.functions[entry_idx].blocks {
        for inst in &mut blk.instructions {
            match inst.class.opcode {
                Op::Load if inst.operands.first() == Some(&Operand::IdRef(pid)) => {
                    if let Some(&c) = inst.result_type.as_ref().and_then(|t| leaf_chain.get(t)) {
                        inst.operands[0] = Operand::IdRef(c);
                    }
                }
                Op::Store if inst.operands.first() == Some(&Operand::IdRef(pid)) => {
                    let Some(Operand::IdRef(object)) = inst.operands.get(1) else {
                        continue;
                    };
                    if let Some(&c) = value_types.get(object).and_then(|t| leaf_chain.get(t)) {
                        inst.operands[0] = Operand::IdRef(c);
                    }
                }
                _ => {}
            }
        }
    }
    if let Some(first) = ctx.module.functions[entry_idx].blocks.first_mut() {
        for (k, ld) in injected.into_iter().enumerate() {
            first.instructions.insert(k, ld);
        }
    }

    // 3) Defensive: any remaining direct use of the param (pass-through) routes to the first
    //    scalar/vector leaf at offset 0.
    let still_used = ctx.module.functions[entry_idx].blocks.iter().any(|b| {
        b.instructions.iter().any(|i| {
            i.operands
                .iter()
                .any(|o| matches!(o, Operand::IdRef(r) if *r == pid))
        })
    });
    if still_used {
        // Descend to the first non-aggregate leaf.
        let mut cur = block_ty;
        let mut path = vec![];
        while let Some(next) = types.get(&cur).and_then(member0_type) {
            path.push(0u32);
            cur = next;
        }
        let ptr_ty = ctx.ty_ptr(StorageClass::StorageBuffer, cur);
        let id = ctx.module.fresh_id();
        let mut ops = vec![Operand::IdRef(var)];
        for _ in &path {
            ops.push(Operand::IdRef(u0));
        }
        if let Some(first) = ctx.module.functions[entry_idx].blocks.first_mut() {
            first.instructions.insert(
                0,
                Instruction::new(Op::AccessChain, Some(ptr_ty), Some(id), ops),
            );
        }
        replace_id_in_function(&mut ctx.module.functions[entry_idx], pid, id);
    }
}

/// After a pointer param has been spliced to a real variable (a StorageBuffer buffer block, or a
/// Private zero var for an unmodeled pointer), every access-chain off it still produces a
/// `UniformConstant` element pointer (the type the backend chose). Vulkan requires the element
/// pointer's storage class to match the root variable's. Walk the entry block, find access chains
/// and pointer aliases transitively rooted at one of `root_vars`, and rewrite their result-type
/// pointer to `target_sc` (creating the pointer type if needed). OpLoad/OpStore through those
/// pointers are unaffected by class. Used both for buffers (-> StorageBuffer) and unmodeled pointer
/// params (-> Private).
pub(in crate::passes) fn rewrite_pointer_storage(
    ctx: &mut Ctx,
    entry_idx: usize,
    root_vars: &[Word],
    target_sc: StorageClass,
    defs: &HashMap<Word, Instruction>,
) -> Result<(), String> {
    let mut roots: HashSet<Word> = root_vars.iter().copied().collect();
    let direct_roots: HashSet<Word> = root_vars.iter().copied().collect();
    // iterate to a fixpoint over access chains so chained derefs are caught.
    let mut changed = true;
    let mut new_ptr_types: HashMap<(Word, Word), Word> = HashMap::new(); // (old ptr ty, pointee) -> new ptr ty
    let mut value_types = combined_value_types(ctx, entry_idx);
    let mut value_defs = combined_value_defs(ctx, entry_idx);
    let types = combined_type_defs(ctx, defs);
    let mut scaled_vector_strides = HashSet::new();
    let mut vector_stride_plans = vec![];

    while changed {
        changed = false;
        let n_blocks = ctx.module.functions[entry_idx].blocks.len();
        for bi in 0..n_blocks {
            let n_inst = ctx.module.functions[entry_idx].blocks[bi]
                .instructions
                .len();
            for ii in 0..n_inst {
                let (op, rty, rid, base, operands) = {
                    let inst = &ctx.module.functions[entry_idx].blocks[bi].instructions[ii];
                    let base = inst.operands.first().and_then(|o| match o {
                        Operand::IdRef(b) => Some(*b),
                        _ => None,
                    });
                    (
                        inst.class.opcode,
                        inst.result_type,
                        inst.result_id,
                        base,
                        inst.operands.clone(),
                    )
                };
                let rooted = if matches!(
                    op,
                    Op::AccessChain | Op::InBoundsAccessChain | Op::PtrAccessChain
                ) {
                    base.map(|id| roots.contains(&id)).unwrap_or(false)
                } else if matches!(op, Op::CopyObject)
                    || (op == Op::Bitcast
                        && rty
                            .and_then(|ty| pointer_pointee_including_new(ctx, &types, ty))
                            .is_some())
                {
                    base.map(|id| roots.contains(&id)).unwrap_or(false)
                } else if matches!(op, Op::Phi) {
                    ctx.module.functions[entry_idx].blocks[bi].instructions[ii]
                        .operands
                        .iter()
                        .step_by(2)
                        .any(|operand| matches!(operand, Operand::IdRef(id) if roots.contains(id)))
                } else if matches!(op, Op::Select) {
                    ctx.module.functions[entry_idx].blocks[bi].instructions[ii]
                        .operands
                        .iter()
                        .skip(1)
                        .any(|operand| matches!(operand, Operand::IdRef(id) if roots.contains(id)))
                } else {
                    false
                };
                if !rooted {
                    continue;
                }
                // This pointer result is buffer-derived; mark it as a root and rewrite its type.
                if let Some(rid) = rid {
                    if roots.insert(rid) {
                        changed = true;
                    }
                }
                if let Some(old_ty) = rty {
                    let pointee = if let Some(pointee) = rewritten_rooted_pointer_pointee(
                        ctx,
                        &types,
                        &value_types,
                        &roots,
                        op,
                        base,
                        &operands,
                    ) {
                        pointee
                    } else {
                        let old_pointee = pointer_pointee_including_new(ctx, defs, old_ty)
                            .ok_or_else(|| {
                                format!("buffer access-chain result type {old_ty} not a pointer")
                            })?;
                        canonical_rooted_access_pointee(
                            ctx,
                            &types,
                            &value_types,
                            &roots,
                            op,
                            base,
                            old_pointee,
                        )
                    };
                    if target_sc == StorageClass::StorageBuffer {
                        if let (Some(rid), Some((index, index_ty, lanes))) = (
                            rid,
                            rooted_vector_stride_plan(
                                ctx,
                                &types,
                                &value_types,
                                op,
                                old_ty,
                                pointee,
                                &operands,
                            ),
                        ) {
                            if scaled_vector_strides.insert(rid) {
                                vector_stride_plans.push((rid, index, index_ty, lanes));
                            }
                        }
                    }
                    let new_ty = if let Some(&t) = new_ptr_types.get(&(old_ty, pointee)) {
                        t
                    } else {
                        let t = ctx.ty_ptr(target_sc, pointee);
                        new_ptr_types.insert((old_ty, pointee), t);
                        t
                    };
                    if let Some(rid) = rid {
                        value_types.insert(rid, new_ty);
                    }
                    if matches!(op, Op::Phi | Op::Select) {
                        let replacements = rewritten_pointer_constant_operands(
                            ctx,
                            &mut value_types,
                            &mut value_defs,
                            op,
                            &operands,
                            new_ty,
                        );
                        if !replacements.is_empty() {
                            let inst =
                                &mut ctx.module.functions[entry_idx].blocks[bi].instructions[ii];
                            for (operand_idx, replacement) in replacements {
                                inst.operands[operand_idx] = Operand::IdRef(replacement);
                            }
                        }
                    }
                    ctx.module.functions[entry_idx].blocks[bi].instructions[ii].result_type =
                        Some(new_ty);
                }
                if target_sc == StorageClass::Workgroup
                    && op == Op::PtrAccessChain
                    && base.is_some_and(|id| direct_roots.contains(&id))
                {
                    let inst = ctx.module.functions[entry_idx].blocks[bi].instructions[ii].clone();
                    ctx.module.functions[entry_idx].blocks[bi].instructions[ii] = Instruction::new(
                        Op::InBoundsAccessChain,
                        inst.result_type,
                        inst.result_id,
                        inst.operands,
                    );
                }
            }
        }
    }

    // A vector-source GEP can be rooted at a scalar lane after interface buffer reconstruction:
    // `gep <N x E>, ptr %lane, %record` then needs `%record * N` in E units. The old result pointer
    // type is the proof that the PtrAccessChain element operand carried a vector stride; after the
    // rooted rewrite changes its pointee to E, preserve that byte offset explicitly. The later
    // subword-store pass decomposes the mismatched vector store into N scalar stores from this point.
    for (result, index, index_ty, lanes) in vector_stride_plans {
        let factor = ctx.const_int_of(index_ty, i64::from(lanes));
        let scaled = ctx.module.fresh_id();
        let scale = Instruction::new(
            Op::IMul,
            Some(index_ty),
            Some(scaled),
            vec![Operand::IdRef(index), Operand::IdRef(factor)],
        );
        for block in &mut ctx.module.functions[entry_idx].blocks {
            let Some(pos) = block
                .instructions
                .iter()
                .position(|inst| inst.result_id == Some(result))
            else {
                continue;
            };
            block.instructions[pos].operands[1] = Operand::IdRef(scaled);
            block.instructions.insert(pos, scale);
            break;
        }
    }
    Ok(())
}

pub(in crate::passes) fn rooted_vector_stride_plan(
    ctx: &Ctx,
    types: &HashMap<Word, Instruction>,
    value_types: &HashMap<Word, Word>,
    op: Op,
    old_ptr_ty: Word,
    new_pointee: Word,
    operands: &[Operand],
) -> Option<(Word, Word, u32)> {
    if op != Op::PtrAccessChain || operands.len() != 2 {
        return None;
    }
    let old_pointee = pointer_pointee_including_new(ctx, types, old_ptr_ty)?;
    let vector = types.get(&old_pointee)?;
    if vector.class.opcode != Op::TypeVector {
        return None;
    }
    let (Some(Operand::IdRef(element)), Some(Operand::LiteralBit32(lanes))) =
        (vector.operands.first(), vector.operands.get(1))
    else {
        return None;
    };
    if *lanes <= 1
        || (*element != new_pointee && !types_structurally_match(ctx, types, *element, new_pointee))
    {
        return None;
    }
    let Operand::IdRef(index) = operands[1] else {
        return None;
    };
    let index_ty = *value_types.get(&index)?;
    (types.get(&index_ty)?.class.opcode == Op::TypeInt).then_some((index, index_ty, *lanes))
}

pub(in crate::passes) fn rewritten_rooted_pointer_pointee(
    ctx: &Ctx,
    types: &HashMap<Word, Instruction>,
    value_types: &HashMap<Word, Word>,
    roots: &HashSet<Word>,
    op: Op,
    base: Option<Word>,
    operands: &[Operand],
) -> Option<Word> {
    if matches!(
        op,
        Op::AccessChain | Op::InBoundsAccessChain | Op::PtrAccessChain
    ) {
        let base_ptr_ty = value_types.get(&base?)?;
        let base_pointee = pointer_pointee_including_new(ctx, types, *base_ptr_ty)?;
        let access_operands = if op == Op::PtrAccessChain {
            operands.get(2..)?
        } else {
            operands.get(1..)?
        };
        return type_after_spirv_access_operands(types, base_pointee, access_operands);
    }
    if op == Op::CopyObject {
        let source_ty = value_types.get(&base?)?;
        return pointer_pointee_including_new(ctx, types, *source_ty);
    }
    if op == Op::Phi {
        return rooted_operand_pointer_pointee(ctx, types, value_types, roots, operands, 0, 2);
    }
    if op == Op::Select {
        return rooted_operand_pointer_pointee(ctx, types, value_types, roots, operands, 1, 1);
    }
    None
}

pub(in crate::passes) fn rooted_operand_pointer_pointee(
    ctx: &Ctx,
    types: &HashMap<Word, Instruction>,
    value_types: &HashMap<Word, Word>,
    roots: &HashSet<Word>,
    operands: &[Operand],
    skip: usize,
    step: usize,
) -> Option<Word> {
    let mut fallback = None;
    for operand in operands.iter().skip(skip).step_by(step) {
        let Operand::IdRef(id) = operand else {
            continue;
        };
        let Some(ptr_ty) = value_types.get(id) else {
            continue;
        };
        let Some(pointee) = pointer_pointee_including_new(ctx, types, *ptr_ty) else {
            continue;
        };
        if roots.contains(id) {
            return Some(pointee);
        }
        fallback.get_or_insert(pointee);
    }
    fallback
}