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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
//! Byte-neutral responsibility split of the former monolith; see the parent module.

use super::*;

/// Rewrite an INVALID single-DYNAMIC-index access chain into a single-member Block buffer that also
/// REINTERPRETS the element to a same-width scalar. The AIR lowered a `device float*` view of a
/// `device uint*` runtime-array buffer (or vice-versa) as
/// `OpInBoundsAccessChain %_ptr_StorageBuffer_float %buf %dyn` — ONE dynamic index applied directly to
/// the Block struct (illegal: spirv-val "the <id> passed to OpInBoundsAccessChain to index into a
/// structure must be an OpConstant") AND a `float` result over a `uint` runtime array. The byte-correct
/// lowering descends member-0 into the runtime array (`%buf %uint_0 %dyn` → a `uint` element pointer),
/// loads the `uint`, and `OpBitcast`s it to `float` — bit-EXACT because both are 32-bit, so it is
/// correct regardless of which type is the buffer's "real" layout (the bits are identical either way).
/// A store through the chain bitcasts the `float` object to `uint` first.
///
/// Byte-safe by construction — only chains that are CURRENTLY INVALID are touched, gated on:
/// (1) exactly ONE index, and it is NOT an `OpConstant` (a dynamic struct-member select — the illegal
///     form; a banked/valid module never matches, so the floor is provably untouched);
/// (2) the base pointee is a SINGLE-member struct whose member-0 is an array/runtime-array of a DIRECT
///     scalar element E (so member-0 insertion is unambiguous and `%dyn` is the array's element index);
/// (3) the result pointee P and E are BOTH direct int/float scalars of EQUAL bit width (so the
///     value reinterpret is a bit-exact `OpBitcast`, never a width change);
/// (4) EVERY use of the chain result is an `OpLoad`(P) or `OpStore`(_, P) — if any other use exists the
///     chain is skipped (we must not hand a retyped pointer to an unmodelled consumer).
/// Decides purely from IR structure (storage class + type walk + use kinds), never a shader name.
pub(in crate::passes) fn rewrite_dynamic_struct_index_reinterpret(
    ctx: &mut Ctx,
    entry_idx: usize,
) -> Result<(), String> {
    let mut ptr_info: HashMap<Word, (StorageClass, Word)> = HashMap::new();
    for inst in ctx
        .new_globals
        .iter()
        .chain(ctx.module.types_global_values.iter())
    {
        if inst.class.opcode == Op::TypePointer {
            if let (Some(id), Some(Operand::StorageClass(s)), Some(Operand::IdRef(p))) =
                (inst.result_id, inst.operands.first(), inst.operands.get(1))
            {
                ptr_info.insert(id, (*s, *p));
            }
        }
    }

    // The single member-0 array element of a single-member struct, if that member is an array/
    // runtime-array of a direct scalar.
    let struct_single_array_elem = |ctx: &Ctx, struct_ty: Word| -> Option<Word> {
        let def = type_def_of(ctx, struct_ty)?;
        if def.class.opcode != Op::TypeStruct || def.operands.len() != 1 {
            return None;
        }
        let Operand::IdRef(member0) = def.operands.first()? else {
            return None;
        };
        let mdef = type_def_of(ctx, *member0)?;
        if !matches!(mdef.class.opcode, Op::TypeArray | Op::TypeRuntimeArray) {
            return None;
        }
        let Operand::IdRef(elem) = mdef.operands.first()? else {
            return None;
        };
        direct_scalar_width(ctx, *elem).map(|_| *elem)
    };

    // Phase 1: collect candidate chains (read-only).
    struct Plan {
        bi: usize,
        ii: usize,
        ac_id: Word,
        base: Word,
        dyn_idx: Word,
        elem_ty: Word,
        result_pointee: Word,
        sc: StorageClass,
        reinterpret: bool,
    }
    let mut plans: Vec<Plan> = Vec::new();
    let blocks = &ctx.module.functions[entry_idx].blocks;
    for (bi, block) in blocks.iter().enumerate() {
        for (ii, inst) in block.instructions.iter().enumerate() {
            if !matches!(inst.class.opcode, Op::InBoundsAccessChain | Op::AccessChain) {
                continue;
            }
            // [base, idx0] — exactly one index.
            if inst.operands.len() != 2 {
                continue;
            }
            let (Some(ac_id), Some(result_type)) = (inst.result_id, inst.result_type) else {
                continue;
            };
            let Some(&(sc, result_pointee)) = ptr_info.get(&result_type) else {
                continue;
            };
            let Some(Operand::IdRef(base)) = inst.operands.first() else {
                continue;
            };
            let Some(Operand::IdRef(dyn_idx)) = inst.operands.get(1) else {
                continue;
            };
            // The single index must be DYNAMIC (a non-constant) — the illegal struct-member select.
            if const_u32(ctx, *dyn_idx).is_some() {
                continue;
            }
            let Some(base_ptr_ty) = value_result_type(ctx, *base) else {
                continue;
            };
            let Some(&(_, base_pointee)) = ptr_info.get(&base_ptr_ty) else {
                continue;
            };
            let Some(elem_ty) = struct_single_array_elem(ctx, base_pointee) else {
                continue;
            };
            // Same-width direct scalar reinterpret (or identity if the element already matches).
            let (Some(ew), Some(pw)) = (
                direct_scalar_width(ctx, elem_ty),
                direct_scalar_width(ctx, result_pointee),
            ) else {
                continue;
            };
            if ew != pw {
                continue;
            }
            plans.push(Plan {
                bi,
                ii,
                ac_id,
                base: *base,
                dyn_idx: *dyn_idx,
                elem_ty,
                result_pointee,
                sc,
                reinterpret: result_pointee != elem_ty,
            });
        }
    }
    if plans.is_empty() {
        return Ok(());
    }

    // Verify every use of each chain result is an OpLoad(result_pointee)/OpStore(_, result_pointee);
    // disqualify a chain otherwise. Returns the (bi, ii, is_load) of each accepted use.
    let mut use_sites: HashMap<Word, Vec<(usize, usize, bool)>> = HashMap::new();
    let mut disqualified: HashSet<Word> = HashSet::new();
    let plan_ids: HashSet<Word> = plans.iter().map(|p| p.ac_id).collect();
    let result_pointee_of: HashMap<Word, Word> =
        plans.iter().map(|p| (p.ac_id, p.result_pointee)).collect();
    for (bi, block) in ctx.module.functions[entry_idx].blocks.iter().enumerate() {
        for (ii, inst) in block.instructions.iter().enumerate() {
            // The defining AC instruction itself is not a "use".
            if inst
                .result_id
                .map(|r| plan_ids.contains(&r))
                .unwrap_or(false)
                && matches!(inst.class.opcode, Op::InBoundsAccessChain | Op::AccessChain)
            {
                continue;
            }
            match inst.class.opcode {
                Op::Load => {
                    if let Some(Operand::IdRef(ptr)) = inst.operands.first() {
                        if plan_ids.contains(ptr) {
                            if inst.result_type == result_pointee_of.get(ptr).copied() {
                                use_sites.entry(*ptr).or_default().push((bi, ii, true));
                            } else {
                                disqualified.insert(*ptr);
                            }
                        }
                    }
                }
                Op::Store => {
                    if let Some(Operand::IdRef(ptr)) = inst.operands.first() {
                        if plan_ids.contains(ptr) {
                            let obj = match inst.operands.get(1) {
                                Some(Operand::IdRef(o)) => value_result_type(ctx, *o),
                                _ => None,
                            };
                            if obj == result_pointee_of.get(ptr).copied() {
                                use_sites.entry(*ptr).or_default().push((bi, ii, false));
                            } else {
                                disqualified.insert(*ptr);
                            }
                        }
                    }
                }
                _ => {
                    // Any other operand reference to the chain disqualifies it.
                    for op in &inst.operands {
                        if let Operand::IdRef(id) = op {
                            if plan_ids.contains(id) {
                                disqualified.insert(*id);
                            }
                        }
                    }
                }
            }
        }
    }
    plans.retain(|p| !disqualified.contains(&p.ac_id) && use_sites.contains_key(&p.ac_id));
    if plans.is_empty() {
        return Ok(());
    }

    // Phase 2: allocate the member-0 constant + new element pointer types, then apply edits.
    let member0 = ctx.const_uint(0);
    let mut new_ptr_ty: HashMap<(StorageClass, Word), Word> = HashMap::new();
    for p in &plans {
        new_ptr_ty
            .entry((p.sc, p.elem_ty))
            .or_insert_with(|| ctx.ty_ptr(p.sc, p.elem_ty));
    }

    // Pre-allocate fresh ids for each load split (the new element-typed load id).
    let load_split_id: HashMap<(usize, usize), Word> = plans
        .iter()
        .flat_map(|p| use_sites.get(&p.ac_id).into_iter().flatten())
        .filter(|(_, _, is_load)| *is_load)
        .map(|&(bi, ii, _)| ((bi, ii), 0))
        .collect::<HashMap<_, _>>()
        .into_keys()
        .map(|k| (k, ctx.module.fresh_id()))
        .collect();
    let store_cast_id: HashMap<(usize, usize), Word> = plans
        .iter()
        .flat_map(|p| use_sites.get(&p.ac_id).into_iter().flatten())
        .filter(|(_, _, is_load)| !*is_load)
        .map(|&(bi, ii, _)| ((bi, ii), 0))
        .collect::<HashMap<_, _>>()
        .into_keys()
        .map(|k| (k, ctx.module.fresh_id()))
        .collect();

    // Rewrite the AC instructions in place (operand count goes 1 -> 2; result type to the element ptr).
    let ac_at: HashMap<(usize, usize), &Plan> = plans.iter().map(|p| ((p.bi, p.ii), p)).collect();
    let elem_ty_of_load: HashMap<(usize, usize), (Word, Word)> = plans
        .iter()
        .flat_map(|p| {
            use_sites
                .get(&p.ac_id)
                .into_iter()
                .flatten()
                .map(move |&(bi, ii, is_load)| ((bi, ii), (p.elem_ty, p.result_pointee, is_load)))
        })
        .map(|((bi, ii), (e, rp, _))| ((bi, ii), (e, rp)))
        .collect();

    let n_blocks = ctx.module.functions[entry_idx].blocks.len();
    for bi in 0..n_blocks {
        let old = std::mem::take(&mut ctx.module.functions[entry_idx].blocks[bi].instructions);
        let mut newv: Vec<Instruction> = Vec::with_capacity(old.len() + 4);
        for (ii, inst) in old.into_iter().enumerate() {
            if let Some(p) = ac_at.get(&(bi, ii)) {
                let new_ty = new_ptr_ty[&(p.sc, p.elem_ty)];
                newv.push(Instruction::new(
                    inst.class.opcode,
                    Some(new_ty),
                    Some(p.ac_id),
                    vec![
                        Operand::IdRef(p.base),
                        Operand::IdRef(member0),
                        Operand::IdRef(p.dyn_idx),
                    ],
                ));
                let _ = p.reinterpret;
                continue;
            }
            if let Some(&load_id) = load_split_id.get(&(bi, ii)) {
                let (elem_ty, result_pointee) = elem_ty_of_load[&(bi, ii)];
                let ptr = match inst.operands.first() {
                    Some(Operand::IdRef(p)) => *p,
                    _ => return Err("load split site lost its pointer operand".to_string()),
                };
                let res = inst.result_id.ok_or("load has a result id")?;
                // Preserve any trailing memory-access operand (e.g. Aligned) on the element load.
                let mut load_ops = vec![Operand::IdRef(ptr)];
                load_ops.extend(inst.operands.iter().skip(1).cloned());
                newv.push(Instruction::new(
                    Op::Load,
                    Some(elem_ty),
                    Some(load_id),
                    load_ops,
                ));
                if elem_ty == result_pointee {
                    // Identity (no reinterpret needed): rebind the original id to a copy of the load.
                    newv.push(Instruction::new(
                        Op::CopyObject,
                        Some(result_pointee),
                        Some(res),
                        vec![Operand::IdRef(load_id)],
                    ));
                } else {
                    newv.push(Instruction::new(
                        Op::Bitcast,
                        Some(result_pointee),
                        Some(res),
                        vec![Operand::IdRef(load_id)],
                    ));
                }
                continue;
            }
            if let Some(&cast_id) = store_cast_id.get(&(bi, ii)) {
                let (elem_ty, _result_pointee) = elem_ty_of_load[&(bi, ii)];
                let ptr = match inst.operands.first() {
                    Some(Operand::IdRef(p)) => *p,
                    _ => return Err("store cast site lost its pointer operand".to_string()),
                };
                let obj = match inst.operands.get(1) {
                    Some(Operand::IdRef(o)) => *o,
                    _ => return Err("store cast site lost its object operand".to_string()),
                };
                newv.push(Instruction::new(
                    Op::Bitcast,
                    Some(elem_ty),
                    Some(cast_id),
                    vec![Operand::IdRef(obj)],
                ));
                // Preserve any trailing memory-access operand (e.g. Aligned) on the store.
                let mut store_ops = vec![Operand::IdRef(ptr), Operand::IdRef(cast_id)];
                store_ops.extend(inst.operands.iter().skip(2).cloned());
                newv.push(Instruction::new(Op::Store, None, None, store_ops));
                continue;
            }
            newv.push(inst);
        }
        ctx.module.functions[entry_idx].blocks[bi].instructions = newv;
    }
    Ok(())
}

/// Rewrite an invalid dynamic vector view of a single-member runtime-array buffer into scalar lane
/// loads or stores.  This is the vector counterpart of
/// [`rewrite_dynamic_struct_index_reinterpret`]: LLVM's `getelementptr <N x T>, ptr %buf, i` can
/// arrive as an access chain with one dynamic index directly into the Block struct, even when the
/// actual backing is `{ runtime-array<E> }`.  A struct member index must be constant in SPIR-V, and
/// the vector's element stride must be made explicit.
///
/// The replacement addresses lane `j` as `buf[0][i * N + j]`, then reconstructs/extracts the vector.
/// It is byte-exact when `E` and the vector component have the same direct-scalar width: that is the
/// same contiguous `N * sizeof(E)` range LLVM's vector GEP names.  The pass accepts only direct
/// exact-typed loads/stores of the otherwise-invalid pointer, so pointer escapes, atomics, memory
/// operands, non-vector aggregates, and already-valid chains are left untouched.
pub(in crate::passes) fn rewrite_dynamic_struct_index_vector_reinterpret(
    ctx: &mut Ctx,
    entry_idx: usize,
) -> Result<(), String> {
    #[derive(Clone, Copy)]
    struct Plan {
        bi: usize,
        ii: usize,
        opcode: Op,
        ac_id: Word,
        base: Word,
        dyn_idx: Word,
        index_ty: Word,
        elem_ty: Word,
        component_ty: Word,
        result_pointee: Word,
        sc: StorageClass,
        lanes: u32,
    }

    let mut ptr_info: HashMap<Word, (StorageClass, Word)> = HashMap::new();
    for inst in ctx
        .new_globals
        .iter()
        .chain(ctx.module.types_global_values.iter())
    {
        if inst.class.opcode == Op::TypePointer {
            if let (Some(id), Some(Operand::StorageClass(sc)), Some(Operand::IdRef(pointee))) =
                (inst.result_id, inst.operands.first(), inst.operands.get(1))
            {
                ptr_info.insert(id, (*sc, *pointee));
            }
        }
    }

    let vector_shape = |ctx: &Ctx, ty: Word| -> Option<(Word, u32)> {
        let def = type_def_of(ctx, ty)?;
        if def.class.opcode != Op::TypeVector {
            return None;
        }
        let (Operand::IdRef(component), Operand::LiteralBit32(lanes)) =
            (def.operands.first()?, def.operands.get(1)?)
        else {
            return None;
        };
        (*lanes >= 2 && direct_scalar_width(ctx, *component).is_some())
            .then_some((*component, *lanes))
    };
    let integer_index = |ctx: &Ctx, ty: Word| {
        type_def_of(ctx, ty).is_some_and(|def| def.class.opcode == Op::TypeInt)
    };

    let mut plans = Vec::new();
    for (bi, block) in ctx.module.functions[entry_idx].blocks.iter().enumerate() {
        for (ii, inst) in block.instructions.iter().enumerate() {
            if !matches!(inst.class.opcode, Op::InBoundsAccessChain | Op::AccessChain)
                || inst.operands.len() != 2
            {
                continue;
            }
            let (Some(ac_id), Some(result_ptr_ty)) = (inst.result_id, inst.result_type) else {
                continue;
            };
            let Some(&(sc, result_pointee)) = ptr_info.get(&result_ptr_ty) else {
                continue;
            };
            let Some((component_ty, lanes)) = vector_shape(ctx, result_pointee) else {
                continue;
            };
            let (Some(Operand::IdRef(base)), Some(Operand::IdRef(dyn_idx))) =
                (inst.operands.first(), inst.operands.get(1))
            else {
                continue;
            };
            if const_u32(ctx, *dyn_idx).is_some() {
                continue;
            }
            let Some(index_ty) = value_result_type(ctx, *dyn_idx) else {
                continue;
            };
            if !integer_index(ctx, index_ty) {
                continue;
            }
            let Some(base_ptr_ty) = value_result_type(ctx, *base) else {
                continue;
            };
            let Some(&(_, base_pointee)) = ptr_info.get(&base_ptr_ty) else {
                continue;
            };
            let Some(elem_ty) = single_member_array_scalar_elem(ctx, base_pointee) else {
                continue;
            };
            if direct_scalar_width(ctx, elem_ty) != direct_scalar_width(ctx, component_ty) {
                continue;
            }
            // Preserve the invalid-only gate: a chain that already walks its base type is not ours
            // to alter, regardless of whether its result happens to be a vector.
            if walk_into_type(ctx, base_pointee, &inst.operands[1..]).is_some() {
                continue;
            }
            plans.push(Plan {
                bi,
                ii,
                opcode: inst.class.opcode,
                ac_id,
                base: *base,
                dyn_idx: *dyn_idx,
                index_ty,
                elem_ty,
                component_ty,
                result_pointee,
                sc,
                lanes,
            });
        }
    }
    if plans.is_empty() {
        return Ok(());
    }

    // A vector view has no legal pointer representation over the scalar runtime array.  Make sure
    // every use is a plain load/store before deleting that pointer and replaying its scalar lanes.
    let plan_ids: HashSet<Word> = plans.iter().map(|p| p.ac_id).collect();
    let result_pointee_of: HashMap<Word, Word> =
        plans.iter().map(|p| (p.ac_id, p.result_pointee)).collect();
    let mut use_sites: HashMap<Word, Vec<(usize, usize, bool)>> = HashMap::new();
    let mut disqualified = HashSet::new();
    for (bi, block) in ctx.module.functions[entry_idx].blocks.iter().enumerate() {
        for (ii, inst) in block.instructions.iter().enumerate() {
            if inst.result_id.is_some_and(|id| plan_ids.contains(&id))
                && matches!(inst.class.opcode, Op::InBoundsAccessChain | Op::AccessChain)
            {
                continue;
            }
            for chain_id in inst.operands.iter().filter_map(|operand| match operand {
                Operand::IdRef(id) if plan_ids.contains(id) => Some(*id),
                _ => None,
            }) {
                let exact_load = inst.class.opcode == Op::Load
                    && inst.operands.len() == 1
                    && inst.result_type == result_pointee_of.get(&chain_id).copied();
                let exact_store = inst.class.opcode == Op::Store
                    && inst.operands.len() == 2
                    && matches!(inst.operands.first(), Some(Operand::IdRef(id)) if *id == chain_id)
                    && inst.operands.get(1).and_then(|operand| match operand {
                        Operand::IdRef(value) => value_result_type(ctx, *value),
                        _ => None,
                    }) == result_pointee_of.get(&chain_id).copied();
                if exact_load {
                    use_sites.entry(chain_id).or_default().push((bi, ii, true));
                } else if exact_store {
                    use_sites.entry(chain_id).or_default().push((bi, ii, false));
                } else {
                    disqualified.insert(chain_id);
                }
            }
        }
    }
    plans.retain(|plan| !disqualified.contains(&plan.ac_id) && use_sites.contains_key(&plan.ac_id));
    if plans.is_empty() {
        return Ok(());
    }

    let member0 = ctx.const_uint(0);
    let mut elem_ptr_ty = HashMap::new();
    for plan in &plans {
        elem_ptr_ty
            .entry((plan.sc, plan.elem_ty))
            .or_insert_with(|| ctx.ty_ptr(plan.sc, plan.elem_ty));
    }
    let plan_by_id: HashMap<Word, Plan> = plans.iter().map(|plan| (plan.ac_id, *plan)).collect();
    let chain_at: HashMap<(usize, usize), Word> = plans
        .iter()
        .map(|plan| ((plan.bi, plan.ii), plan.ac_id))
        .collect();
    let use_at: HashMap<(usize, usize), Word> = plans
        .iter()
        .flat_map(|plan| {
            use_sites
                .get(&plan.ac_id)
                .into_iter()
                .flatten()
                .map(move |&(bi, ii, _)| ((bi, ii), plan.ac_id))
        })
        .collect();

    let n_blocks = ctx.module.functions[entry_idx].blocks.len();
    for bi in 0..n_blocks {
        let old = std::mem::take(&mut ctx.module.functions[entry_idx].blocks[bi].instructions);
        let mut rewritten = Vec::with_capacity(old.len() + 8);
        for (ii, inst) in old.into_iter().enumerate() {
            if chain_at.contains_key(&(bi, ii)) {
                // All verified uses are replayed below, so the illegal pointer itself disappears.
                continue;
            }
            let Some(chain_id) = use_at.get(&(bi, ii)).copied() else {
                rewritten.push(inst);
                continue;
            };
            let plan = plan_by_id[&chain_id];
            let lane_base = ctx.module.fresh_id();
            let lane_factor = ctx.const_int_of(plan.index_ty, i64::from(plan.lanes));
            rewritten.push(Instruction::new(
                Op::IMul,
                Some(plan.index_ty),
                Some(lane_base),
                vec![Operand::IdRef(plan.dyn_idx), Operand::IdRef(lane_factor)],
            ));

            let mut lane_ptrs = Vec::with_capacity(plan.lanes as usize);
            for lane in 0..plan.lanes {
                let lane_index = if lane == 0 {
                    lane_base
                } else {
                    let lane_offset = ctx.const_int_of(plan.index_ty, i64::from(lane));
                    let lane_index = ctx.module.fresh_id();
                    rewritten.push(Instruction::new(
                        Op::IAdd,
                        Some(plan.index_ty),
                        Some(lane_index),
                        vec![Operand::IdRef(lane_base), Operand::IdRef(lane_offset)],
                    ));
                    lane_index
                };
                let ptr = ctx.module.fresh_id();
                rewritten.push(Instruction::new(
                    plan.opcode,
                    Some(elem_ptr_ty[&(plan.sc, plan.elem_ty)]),
                    Some(ptr),
                    vec![
                        Operand::IdRef(plan.base),
                        Operand::IdRef(member0),
                        Operand::IdRef(lane_index),
                    ],
                ));
                lane_ptrs.push(ptr);
            }

            match inst.class.opcode {
                Op::Load => {
                    let result = inst.result_id.ok_or("vector load has a result id")?;
                    let mut components = Vec::with_capacity(plan.lanes as usize);
                    for ptr in lane_ptrs {
                        let loaded = ctx.module.fresh_id();
                        rewritten.push(Instruction::new(
                            Op::Load,
                            Some(plan.elem_ty),
                            Some(loaded),
                            vec![Operand::IdRef(ptr)],
                        ));
                        let component = if plan.elem_ty == plan.component_ty {
                            loaded
                        } else {
                            let cast = ctx.module.fresh_id();
                            rewritten.push(Instruction::new(
                                Op::Bitcast,
                                Some(plan.component_ty),
                                Some(cast),
                                vec![Operand::IdRef(loaded)],
                            ));
                            cast
                        };
                        components.push(Operand::IdRef(component));
                    }
                    rewritten.push(Instruction::new(
                        Op::CompositeConstruct,
                        Some(plan.result_pointee),
                        Some(result),
                        components,
                    ));
                }
                Op::Store => {
                    let object = match inst.operands.get(1) {
                        Some(Operand::IdRef(value)) => *value,
                        _ => return Err("vector store lost its object operand".to_string()),
                    };
                    for (lane, ptr) in lane_ptrs.into_iter().enumerate() {
                        let component = ctx.module.fresh_id();
                        rewritten.push(Instruction::new(
                            Op::CompositeExtract,
                            Some(plan.component_ty),
                            Some(component),
                            vec![Operand::IdRef(object), Operand::LiteralBit32(lane as u32)],
                        ));
                        let element = if plan.elem_ty == plan.component_ty {
                            component
                        } else {
                            let cast = ctx.module.fresh_id();
                            rewritten.push(Instruction::new(
                                Op::Bitcast,
                                Some(plan.elem_ty),
                                Some(cast),
                                vec![Operand::IdRef(component)],
                            ));
                            cast
                        };
                        rewritten.push(Instruction::new(
                            Op::Store,
                            None,
                            None,
                            vec![Operand::IdRef(ptr), Operand::IdRef(element)],
                        ));
                    }
                }
                _ => return Err("vector dynamic-chain use was not load/store".to_string()),
            }
        }
        ctx.module.functions[entry_idx].blocks[bi].instructions = rewritten;
    }
    Ok(())
}

/// The single member-0 element of a single-member struct `{ array/runtime-array<E> }`, when `E` is a
/// DIRECT scalar; `None` otherwise. (Shared shape test for the buffer-block reinterpret rewrites.)
pub(in crate::passes) fn single_member_array_scalar_elem(
    ctx: &Ctx,
    struct_ty: Word,
) -> Option<Word> {
    let def = type_def_of(ctx, struct_ty)?;
    if def.class.opcode != Op::TypeStruct || def.operands.len() != 1 {
        return None;
    }
    let Operand::IdRef(member0) = def.operands.first()? else {
        return None;
    };
    let mdef = type_def_of(ctx, *member0)?;
    if !matches!(mdef.class.opcode, Op::TypeArray | Op::TypeRuntimeArray) {
        return None;
    }
    let Operand::IdRef(elem) = mdef.operands.first()? else {
        return None;
    };
    direct_scalar_width(ctx, *elem).map(|_| *elem)
}

/// Rewrite an INVALID *nested-chained* element reinterpret over a buffer-block runtime array. The AIR
/// took a `device float*` element pointer into a `{ runtime-array<float> }` SSBO, REINTERPRET-cast it
/// to `device uint*`, and GEP'd it — which metal2vulkan lowers as a two-index chain over the float element:
///
/// ```text
///   %inner = OpInBoundsAccessChain %_ptr_StorageBuffer_float %buf  %uint_0 %dynF   ; element #dynF (VALID)
///   %out   = OpInBoundsAccessChain %_ptr_StorageBuffer_uint  %inner %uint_0 %dynU   ; reinterpret (INVALID)
/// ```
///
/// `%out`'s `[%uint_0, %dynU]` indexes into the scalar `float` `%inner` points at (spirv-val "reached
/// non-composite"). Because `float` and `uint` are both the array's element stride (4 bytes), the
/// byte-correct address is element `dynF + dynU` of the SAME float runtime array, so this re-roots
/// `%out` onto `%buf` at the summed index and loads the `float`, `OpBitcast`ing it to `uint`:
///
/// ```text
///   %sum   = OpIAdd %uint %dynF %dynU
///   %out   = OpInBoundsAccessChain %_ptr_StorageBuffer_float %buf %uint_0 %sum   ; float element ptr
///   ; at each load: %f = OpLoad %float %out ; %res = OpBitcast %uint %f
/// ```
///
/// Byte-EXACT: `dynF` strides whole floats and `dynU` strides whole uints, both 4 bytes, so
/// `(dynF + dynU) * 4` is the same byte offset the reinterpret-GEP addressed; the loaded bits are
/// identical under a same-width int/float `OpBitcast`. Stores bitcast the object back to the element
/// scalar first. `%inner` is left intact (it may have other, valid uses); only `%out` is re-rooted.
///
/// Byte-safe by construction — only chains that are CURRENTLY INVALID are touched, gated on:
/// (1) `%out` is a two-index chain `[%uint_0, %dynU]` whose result pointee P is a DIRECT scalar;
/// (2) its base `%inner` is itself a two-index chain `[%uint_0, %dynF]` into a SINGLE-member struct
///     `{ array/runtime-array<E> }` (so `%buf %uint_0 %sum` is a valid element pointer) whose element E
///     is a DIRECT scalar with `width(E) == width(P)` (so the value reinterpret is a bit-exact
///     `OpBitcast`, never a width change), and whose result pointee equals E;
/// (3) the base storage class permits the re-rooted descent (StorageBuffer/Workgroup/PSB);
/// (4) `%out` is CURRENTLY INVALID (the index walk over `%inner`'s pointee fails — always true for a
///     scalar pointee, so a banked/valid module never matches and the floor is provably untouched);
/// (5) EVERY use of `%out` is an `OpLoad`(P) or `OpStore`(_, P) — any other use skips the chain.
/// Decides purely from IR structure (storage class + type walk + use kinds), never a shader name.
pub(in crate::passes) fn rewrite_chained_element_reinterpret(
    ctx: &mut Ctx,
    entry_idx: usize,
) -> Result<(), String> {
    let mut ptr_info: HashMap<Word, (StorageClass, Word)> = HashMap::new();
    for inst in ctx
        .new_globals
        .iter()
        .chain(ctx.module.types_global_values.iter())
    {
        if inst.class.opcode == Op::TypePointer {
            if let (Some(id), Some(Operand::StorageClass(s)), Some(Operand::IdRef(p))) =
                (inst.result_id, inst.operands.first(), inst.operands.get(1))
            {
                ptr_info.insert(id, (*s, *p));
            }
        }
    }

    // The defining chain instruction of every access-chain result (for resolving `%inner`).
    let mut chain_def: HashMap<Word, Instruction> = HashMap::new();
    for block in &ctx.module.functions[entry_idx].blocks {
        for inst in &block.instructions {
            if matches!(inst.class.opcode, Op::InBoundsAccessChain | Op::AccessChain) {
                if let Some(rid) = inst.result_id {
                    chain_def.insert(rid, inst.clone());
                }
            }
        }
    }

    // A two-index chain `[base, %uint_0, %idx]` -> (base, idx) when its first index is constant 0.
    let two_index_zero_lead = |ctx: &Ctx, inst: &Instruction| -> Option<(Word, Word)> {
        if inst.operands.len() != 3 {
            return None;
        }
        let (Operand::IdRef(base), Operand::IdRef(i0), Operand::IdRef(i1)) =
            (&inst.operands[0], &inst.operands[1], &inst.operands[2])
        else {
            return None;
        };
        if const_u32(ctx, *i0) != Some(0) {
            return None;
        }
        Some((*base, *i1))
    };

    struct Plan {
        bi: usize,
        ii: usize,
        ac_id: Word,
        buf: Word,
        inner_idx: Word,
        out_idx: Word,
        elem_ty: Word,
        result_pointee: Word,
        sc: StorageClass,
    }
    let mut plans: Vec<Plan> = Vec::new();
    for (bi, block) in ctx.module.functions[entry_idx].blocks.iter().enumerate() {
        for (ii, inst) in block.instructions.iter().enumerate() {
            if !matches!(inst.class.opcode, Op::InBoundsAccessChain | Op::AccessChain) {
                continue;
            }
            let (Some(ac_id), Some(result_type)) = (inst.result_id, inst.result_type) else {
                continue;
            };
            let Some((inner, out_idx)) = two_index_zero_lead(ctx, inst) else {
                continue;
            };
            let Some(&(sc, result_pointee)) = ptr_info.get(&result_type) else {
                continue;
            };
            if !ptr_access_chain_allowed_storage(sc) {
                continue;
            }
            let Some(pw) = direct_scalar_width(ctx, result_pointee) else {
                continue;
            };
            // The base must itself be a `[base, %uint_0, %dynF]` chain into a single-member array struct.
            let Some(inner_inst) = chain_def.get(&inner) else {
                continue;
            };
            let Some((buf, inner_idx)) = two_index_zero_lead(ctx, inner_inst) else {
                continue;
            };
            let Some(inner_result_type) = inner_inst.result_type else {
                continue;
            };
            let Some(&(_, inner_pointee)) = ptr_info.get(&inner_result_type) else {
                continue;
            };
            let Some(buf_ptr_ty) = value_result_type(ctx, buf) else {
                continue;
            };
            let Some(&(_, buf_pointee)) = ptr_info.get(&buf_ptr_ty) else {
                continue;
            };
            let Some(elem_ty) = single_member_array_scalar_elem(ctx, buf_pointee) else {
                continue;
            };
            // `%inner` must genuinely point at the array's element scalar (so `dynF` is the element
            // index we sum with `dynU`), and that element width must match the reinterpret width.
            if inner_pointee != elem_ty {
                continue;
            }
            if direct_scalar_width(ctx, elem_ty) != Some(pw) {
                continue;
            }
            // `%out` must be CURRENTLY INVALID: walking `[%uint_0, %dynU]` into `%inner`'s scalar
            // pointee must fail (always does for a scalar — this is a belt-and-braces floor guard).
            if walk_into_type(ctx, inner_pointee, &inst.operands[1..]).is_some() {
                continue;
            }
            plans.push(Plan {
                bi,
                ii,
                ac_id,
                buf,
                inner_idx,
                out_idx,
                elem_ty,
                result_pointee,
                sc,
            });
        }
    }
    if plans.is_empty() {
        return Ok(());
    }

    // Every use of each `%out` must be an OpLoad(P)/OpStore(_, P); disqualify otherwise.
    let plan_ids: HashSet<Word> = plans.iter().map(|p| p.ac_id).collect();
    let result_pointee_of: HashMap<Word, Word> =
        plans.iter().map(|p| (p.ac_id, p.result_pointee)).collect();
    let mut use_sites: HashMap<Word, Vec<(usize, usize, bool)>> = HashMap::new();
    let mut disqualified: HashSet<Word> = HashSet::new();
    for (bi, block) in ctx.module.functions[entry_idx].blocks.iter().enumerate() {
        for (ii, inst) in block.instructions.iter().enumerate() {
            if inst
                .result_id
                .map(|r| plan_ids.contains(&r))
                .unwrap_or(false)
                && matches!(inst.class.opcode, Op::InBoundsAccessChain | Op::AccessChain)
            {
                continue;
            }
            match inst.class.opcode {
                Op::Load => {
                    if let Some(Operand::IdRef(ptr)) = inst.operands.first() {
                        if plan_ids.contains(ptr) {
                            if inst.result_type == result_pointee_of.get(ptr).copied() {
                                use_sites.entry(*ptr).or_default().push((bi, ii, true));
                            } else {
                                disqualified.insert(*ptr);
                            }
                        }
                    }
                }
                Op::Store => {
                    if let Some(Operand::IdRef(ptr)) = inst.operands.first() {
                        if plan_ids.contains(ptr) {
                            let obj = match inst.operands.get(1) {
                                Some(Operand::IdRef(o)) => value_result_type(ctx, *o),
                                _ => None,
                            };
                            if obj == result_pointee_of.get(ptr).copied() {
                                use_sites.entry(*ptr).or_default().push((bi, ii, false));
                            } else {
                                disqualified.insert(*ptr);
                            }
                        }
                    }
                }
                _ => {
                    for op in &inst.operands {
                        if let Operand::IdRef(id) = op {
                            if plan_ids.contains(id) {
                                disqualified.insert(*id);
                            }
                        }
                    }
                }
            }
        }
    }
    plans.retain(|p| !disqualified.contains(&p.ac_id) && use_sites.contains_key(&p.ac_id));
    if plans.is_empty() {
        return Ok(());
    }

    // Phase 2: allocate the member-0 const, the summed-index ids, the element pointer types, and the
    // per-use load/store split ids.
    let member0 = ctx.const_uint(0);
    let uint_ty = ctx.ty_uint();
    let mut new_ptr_ty: HashMap<(StorageClass, Word), Word> = HashMap::new();
    let mut sum_id: HashMap<Word, Word> = HashMap::new();
    for p in &plans {
        new_ptr_ty
            .entry((p.sc, p.elem_ty))
            .or_insert_with(|| ctx.ty_ptr(p.sc, p.elem_ty));
        sum_id.insert(p.ac_id, ctx.module.fresh_id());
    }
    let load_split_id: HashMap<(usize, usize), Word> = plans
        .iter()
        .flat_map(|p| use_sites.get(&p.ac_id).into_iter().flatten())
        .filter(|(_, _, is_load)| *is_load)
        .map(|&(bi, ii, _)| ((bi, ii), 0))
        .collect::<HashMap<_, _>>()
        .into_keys()
        .map(|k| (k, ctx.module.fresh_id()))
        .collect();
    let store_cast_id: HashMap<(usize, usize), Word> = plans
        .iter()
        .flat_map(|p| use_sites.get(&p.ac_id).into_iter().flatten())
        .filter(|(_, _, is_load)| !*is_load)
        .map(|&(bi, ii, _)| ((bi, ii), 0))
        .collect::<HashMap<_, _>>()
        .into_keys()
        .map(|k| (k, ctx.module.fresh_id()))
        .collect();

    let ac_at: HashMap<(usize, usize), &Plan> = plans.iter().map(|p| ((p.bi, p.ii), p)).collect();
    let elem_ty_of_use: HashMap<(usize, usize), (Word, Word)> = plans
        .iter()
        .flat_map(|p| {
            use_sites
                .get(&p.ac_id)
                .into_iter()
                .flatten()
                .map(move |&(bi, ii, _)| ((bi, ii), (p.elem_ty, p.result_pointee)))
        })
        .collect();

    let n_blocks = ctx.module.functions[entry_idx].blocks.len();
    for bi in 0..n_blocks {
        let old = std::mem::take(&mut ctx.module.functions[entry_idx].blocks[bi].instructions);
        let mut newv: Vec<Instruction> = Vec::with_capacity(old.len() + 4);
        for (ii, inst) in old.into_iter().enumerate() {
            if let Some(p) = ac_at.get(&(bi, ii)) {
                // `%sum = OpIAdd %uint %dynF %dynU` then re-rooted float-element chain over `%buf`.
                let sum = sum_id[&p.ac_id];
                newv.push(Instruction::new(
                    Op::IAdd,
                    Some(uint_ty),
                    Some(sum),
                    vec![Operand::IdRef(p.inner_idx), Operand::IdRef(p.out_idx)],
                ));
                let new_ty = new_ptr_ty[&(p.sc, p.elem_ty)];
                newv.push(Instruction::new(
                    inst.class.opcode,
                    Some(new_ty),
                    Some(p.ac_id),
                    vec![
                        Operand::IdRef(p.buf),
                        Operand::IdRef(member0),
                        Operand::IdRef(sum),
                    ],
                ));
                continue;
            }
            if let Some(&load_id) = load_split_id.get(&(bi, ii)) {
                let (elem_ty, result_pointee) = elem_ty_of_use[&(bi, ii)];
                let ptr = match inst.operands.first() {
                    Some(Operand::IdRef(p)) => *p,
                    _ => return Err("load split site lost its pointer operand".to_string()),
                };
                let res = inst.result_id.ok_or("load has a result id")?;
                let mut load_ops = vec![Operand::IdRef(ptr)];
                load_ops.extend(inst.operands.iter().skip(1).cloned());
                newv.push(Instruction::new(
                    Op::Load,
                    Some(elem_ty),
                    Some(load_id),
                    load_ops,
                ));
                newv.push(Instruction::new(
                    Op::Bitcast,
                    Some(result_pointee),
                    Some(res),
                    vec![Operand::IdRef(load_id)],
                ));
                continue;
            }
            if let Some(&cast_id) = store_cast_id.get(&(bi, ii)) {
                let (elem_ty, _result_pointee) = elem_ty_of_use[&(bi, ii)];
                let ptr = match inst.operands.first() {
                    Some(Operand::IdRef(p)) => *p,
                    _ => return Err("store cast site lost its pointer operand".to_string()),
                };
                let obj = match inst.operands.get(1) {
                    Some(Operand::IdRef(o)) => *o,
                    _ => return Err("store cast site lost its object operand".to_string()),
                };
                newv.push(Instruction::new(
                    Op::Bitcast,
                    Some(elem_ty),
                    Some(cast_id),
                    vec![Operand::IdRef(obj)],
                ));
                let mut store_ops = vec![Operand::IdRef(ptr), Operand::IdRef(cast_id)];
                store_ops.extend(inst.operands.iter().skip(2).cloned());
                newv.push(Instruction::new(Op::Store, None, None, store_ops));
                continue;
            }
            newv.push(inst);
        }
        ctx.module.functions[entry_idx].blocks[bi].instructions = newv;
    }
    Ok(())
}

/// Rewrite an INVALID *nested-chained* element reinterpret over a NARROW (byte/short) buffer-block array
/// where the reinterpret WIDENS — a `device uchar*`/`device ushort*` element pointer reinterpret-cast to
/// a WIDER scalar (`ushort`/`uint`/`ulong`) and GEP'd:
///
/// ```text
///   %inner = OpInBoundsAccessChain %_ptr_StorageBuffer_uchar  %buf   %uint_0 %byteIdx   ; byte #byteIdx (VALID)
///   %out   = OpInBoundsAccessChain %_ptr_StorageBuffer_ushort %inner %k                 ; reinterpret (INVALID)
/// ```
///
/// `%out`'s single index `%k` strides whole `ushort`s over the `uchar` `%inner` points at (spirv-val
/// "reached non-composite"). The byte-correct address is byte `byteIdx + k*R` of the SAME `uchar` runtime
/// array, where `R = width(ushort)/width(uchar) = 2` is how many narrow slots one wide element spans. So
/// the ushort is the `R` contiguous narrow slots starting at `sum = byteIdx + k*R`, little-endian:
///
/// ```text
///   %mul = OpIMul %uint %k %R ; %sum = OpIAdd %uint %byteIdx %mul
///   ; LOAD:  for j in 0..R: load uchar at slot sum+j, UConvert to ushort, shift left j*V, OR-assemble
///   ; STORE: for j in 0..R: shift object right j*V, UConvert (truncate) to uchar, store at slot sum+j
/// ```
///
/// Byte-EXACT on a little-endian target: `k` strides `R` narrow slots and `j` walks the `R` slots of one
/// wide element, so `sum+j` is the exact byte the reinterpret-GEP+load/store would have touched, and the
/// LE assemble/split reproduces the same bytes the native wide access does. `%inner` is left intact (it
/// may have other valid uses); only `%out` is re-rooted and its loads/stores expanded.
///
/// Byte-safe / floor-safe by construction — only chains CURRENTLY INVALID are touched, gated on:
/// (1) `%out` is a single-index chain `[%inner, %k]` whose result pointee P is a DIRECT INT scalar;
/// (2) its base `%inner` is a two-index chain `[%uint_0, %byteIdx]` into a SINGLE-member struct
///     `{ array/runtime-array<E> }` whose element E is a DIRECT INT scalar with `width(P) = R*width(E)`,
///     `R >= 2` (a genuine widen — the narrow direction is the existing same-width/narrow passes), and
///     whose result pointee equals E;
/// (3) the base storage class is StorageBuffer (member-access descent over a real SSBO);
/// (4) `%out` is CURRENTLY INVALID (the index walk over `%inner`'s scalar pointee fails — always true for
///     a scalar, so a banked/valid module never matches and the floor is provably untouched);
/// (5) EVERY use of `%out` is an `OpLoad`(P) or `OpStore`(_, P) — any other use skips the chain.
/// Decides purely from IR structure (storage class + type walk + use kinds), never a shader name.
pub(in crate::passes) fn rewrite_byte_buffer_chained_reinterpret(
    ctx: &mut Ctx,
    entry_idx: usize,
) -> Result<(), String> {
    let mut ptr_info: HashMap<Word, (StorageClass, Word)> = HashMap::new();
    for inst in ctx
        .new_globals
        .iter()
        .chain(ctx.module.types_global_values.iter())
    {
        if inst.class.opcode == Op::TypePointer {
            if let (Some(id), Some(Operand::StorageClass(s)), Some(Operand::IdRef(p))) =
                (inst.result_id, inst.operands.first(), inst.operands.get(1))
            {
                ptr_info.insert(id, (*s, *p));
            }
        }
    }

    let is_int = |ctx: &Ctx, ty: Word| -> bool {
        type_def_of(ctx, ty)
            .map(|d| d.class.opcode == Op::TypeInt)
            .unwrap_or(false)
    };

    // The defining chain instruction of every access-chain result (for resolving `%inner`).
    let mut chain_def: HashMap<Word, Instruction> = HashMap::new();
    for block in &ctx.module.functions[entry_idx].blocks {
        for inst in &block.instructions {
            if matches!(inst.class.opcode, Op::InBoundsAccessChain | Op::AccessChain) {
                if let Some(rid) = inst.result_id {
                    chain_def.insert(rid, inst.clone());
                }
            }
        }
    }

    struct Plan {
        bi: usize,
        ii: usize,
        ac_id: Word,
        buf: Word,
        inner_idx: Word,
        elem_ty: Word,
        result_pointee: Word,
        ratio: u32,
        slot_w: u32,
    }
    let mut plans: Vec<Plan> = Vec::new();
    for (bi, block) in ctx.module.functions[entry_idx].blocks.iter().enumerate() {
        for (ii, inst) in block.instructions.iter().enumerate() {
            if !matches!(inst.class.opcode, Op::InBoundsAccessChain | Op::AccessChain) {
                continue;
            }
            let (Some(ac_id), Some(result_type)) = (inst.result_id, inst.result_type) else {
                continue;
            };
            // `%out = AC %inner %k` — base + exactly one index.
            if inst.operands.len() != 2 {
                continue;
            }
            let (Operand::IdRef(inner), Operand::IdRef(_out_idx)) =
                (&inst.operands[0], &inst.operands[1])
            else {
                continue;
            };
            let Some(&(sc, result_pointee)) = ptr_info.get(&result_type) else {
                continue;
            };
            if sc != StorageClass::StorageBuffer {
                continue;
            }
            let Some(w) = direct_scalar_width(ctx, result_pointee) else {
                continue;
            };
            if !is_int(ctx, result_pointee) {
                continue;
            }
            // `%inner` must itself be a `[base, %uint_0, %byteIdx]` chain into a single-member byte array.
            let Some(inner_inst) = chain_def.get(inner) else {
                continue;
            };
            if inner_inst.operands.len() != 3 {
                continue;
            }
            let (Operand::IdRef(buf), Operand::IdRef(i0), Operand::IdRef(inner_idx)) = (
                &inner_inst.operands[0],
                &inner_inst.operands[1],
                &inner_inst.operands[2],
            ) else {
                continue;
            };
            if const_u32(ctx, *i0) != Some(0) {
                continue;
            }
            let Some(inner_result_type) = inner_inst.result_type else {
                continue;
            };
            let Some(&(_, inner_pointee)) = ptr_info.get(&inner_result_type) else {
                continue;
            };
            let Some(buf_ptr_ty) = value_result_type(ctx, *buf) else {
                continue;
            };
            let Some(&(_, buf_pointee)) = ptr_info.get(&buf_ptr_ty) else {
                continue;
            };
            let Some(elem_ty) = single_member_array_scalar_elem(ctx, buf_pointee) else {
                continue;
            };
            if inner_pointee != elem_ty || !is_int(ctx, elem_ty) {
                continue;
            }
            let Some(v) = direct_scalar_width(ctx, elem_ty) else {
                continue;
            };
            // Genuine WIDEN only: P is R whole narrow slots, R >= 2 (same-width/narrow are other passes).
            if v == 0 || w % v != 0 {
                continue;
            }
            let ratio = w / v;
            if ratio < 2 {
                continue;
            }
            // `%out` must be CURRENTLY INVALID: walking `[%k]` into `%inner`'s scalar pointee must fail.
            if walk_into_type(ctx, inner_pointee, &inst.operands[1..]).is_some() {
                continue;
            }
            plans.push(Plan {
                bi,
                ii,
                ac_id,
                buf: *buf,
                inner_idx: *inner_idx,
                elem_ty,
                result_pointee,
                ratio,
                slot_w: v,
            });
        }
    }
    if plans.is_empty() {
        return Ok(());
    }

    // Every use of each `%out` must be an OpLoad(P)/OpStore(_, P); disqualify otherwise.
    let plan_ids: HashSet<Word> = plans.iter().map(|p| p.ac_id).collect();
    let result_pointee_of: HashMap<Word, Word> =
        plans.iter().map(|p| (p.ac_id, p.result_pointee)).collect();
    let mut use_sites: HashMap<Word, Vec<(usize, usize, bool)>> = HashMap::new();
    let mut disqualified: HashSet<Word> = HashSet::new();
    for (bi, block) in ctx.module.functions[entry_idx].blocks.iter().enumerate() {
        for (ii, inst) in block.instructions.iter().enumerate() {
            if inst
                .result_id
                .map(|r| plan_ids.contains(&r))
                .unwrap_or(false)
                && matches!(inst.class.opcode, Op::InBoundsAccessChain | Op::AccessChain)
            {
                continue;
            }
            match inst.class.opcode {
                Op::Load => {
                    if let Some(Operand::IdRef(ptr)) = inst.operands.first() {
                        if plan_ids.contains(ptr) {
                            if inst.result_type == result_pointee_of.get(ptr).copied() {
                                use_sites.entry(*ptr).or_default().push((bi, ii, true));
                            } else {
                                disqualified.insert(*ptr);
                            }
                        }
                    }
                }
                Op::Store => {
                    if let Some(Operand::IdRef(ptr)) = inst.operands.first() {
                        if plan_ids.contains(ptr) {
                            let obj = match inst.operands.get(1) {
                                Some(Operand::IdRef(o)) => value_result_type(ctx, *o),
                                _ => None,
                            };
                            if obj == result_pointee_of.get(ptr).copied() {
                                use_sites.entry(*ptr).or_default().push((bi, ii, false));
                            } else {
                                disqualified.insert(*ptr);
                            }
                        }
                    }
                }
                _ => {
                    for op in &inst.operands {
                        if let Operand::IdRef(id) = op {
                            if plan_ids.contains(id) {
                                disqualified.insert(*id);
                            }
                        }
                    }
                }
            }
        }
    }
    plans.retain(|p| !disqualified.contains(&p.ac_id) && use_sites.contains_key(&p.ac_id));
    if plans.is_empty() {
        return Ok(());
    }

    // Phase 2: the byte-index `sum = inner_idx + out_idx*ratio` per plan, shared by every use.
    let member0 = ctx.const_uint(0);
    let uint_ty = ctx.ty_uint();
    let mut elem_ptr_ty: HashMap<Word, Word> = HashMap::new();
    let mut sum_id: HashMap<Word, Word> = HashMap::new();
    let mut ratio_const: HashMap<u32, Word> = HashMap::new();
    for p in &plans {
        elem_ptr_ty
            .entry(p.elem_ty)
            .or_insert_with(|| ctx.ty_ptr(StorageClass::StorageBuffer, p.elem_ty));
        sum_id.insert(p.ac_id, ctx.module.fresh_id());
        ratio_const
            .entry(p.ratio)
            .or_insert_with(|| ctx.const_uint(p.ratio));
    }

    let ac_at: HashMap<(usize, usize), Word> =
        plans.iter().map(|p| ((p.bi, p.ii), p.ac_id)).collect();
    let plan_by_id: HashMap<Word, (Word, Word, Word, Word, u32, u32)> = plans
        .iter()
        .map(|p| {
            (
                p.ac_id,
                (
                    p.buf,
                    p.elem_ty,
                    p.result_pointee,
                    p.inner_idx,
                    p.ratio,
                    p.slot_w,
                ),
            )
        })
        .collect();
    let use_at: HashMap<(usize, usize), Word> = plans
        .iter()
        .flat_map(|p| {
            use_sites
                .get(&p.ac_id)
                .into_iter()
                .flatten()
                .map(move |&(bi, ii, _)| ((bi, ii), p.ac_id))
        })
        .collect();

    let n_blocks = ctx.module.functions[entry_idx].blocks.len();
    for bi in 0..n_blocks {
        let old = std::mem::take(&mut ctx.module.functions[entry_idx].blocks[bi].instructions);
        let mut newv: Vec<Instruction> = Vec::with_capacity(old.len() + 8);
        for (ii, inst) in old.into_iter().enumerate() {
            // The original `%out` chain becomes the `sum = inner_idx + out_idx*ratio` computation.
            if let Some(&ac_id) = ac_at.get(&(bi, ii)) {
                let (_buf, _e, _p, inner_idx, ratio, _v) = plan_by_id[&ac_id];
                let sum = sum_id[&ac_id];
                let out_idx = match inst.operands.get(1) {
                    Some(Operand::IdRef(o)) => *o,
                    _ => {
                        return Err(
                            "byte-buffer reinterpret chain lost its index operand".to_string()
                        )
                    }
                };
                let mul = ctx.module.fresh_id();
                newv.push(Instruction::new(
                    Op::IMul,
                    Some(uint_ty),
                    Some(mul),
                    vec![Operand::IdRef(out_idx), Operand::IdRef(ratio_const[&ratio])],
                ));
                newv.push(Instruction::new(
                    Op::IAdd,
                    Some(uint_ty),
                    Some(sum),
                    vec![Operand::IdRef(inner_idx), Operand::IdRef(mul)],
                ));
                continue;
            }
            // A load/store through `%out` expands into `ratio` little-endian narrow slot accesses.
            if let Some(&ac_id) = use_at.get(&(bi, ii)) {
                let (buf, elem_ty, result_pointee, _ii, ratio, slot_w) = plan_by_id[&ac_id];
                let sum = sum_id[&ac_id];
                let eptr = elem_ptr_ty[&elem_ty];
                // Per-slot element pointers: `AC %buf %uint_0 (sum + j)`.
                let mut slot_ptr = Vec::with_capacity(ratio as usize);
                for j in 0..ratio {
                    let idx = if j == 0 {
                        sum
                    } else {
                        let off = ctx.const_uint(j);
                        let id = ctx.module.fresh_id();
                        newv.push(Instruction::new(
                            Op::IAdd,
                            Some(uint_ty),
                            Some(id),
                            vec![Operand::IdRef(sum), Operand::IdRef(off)],
                        ));
                        id
                    };
                    let pid = ctx.module.fresh_id();
                    newv.push(Instruction::new(
                        Op::InBoundsAccessChain,
                        Some(eptr),
                        Some(pid),
                        vec![
                            Operand::IdRef(buf),
                            Operand::IdRef(member0),
                            Operand::IdRef(idx),
                        ],
                    ));
                    slot_ptr.push(pid);
                }
                if inst.class.opcode == Op::Load {
                    // LE assemble: OR_j ( zext(load slot j) << (j*slot_w) ).
                    let res = inst.result_id.ok_or("load has a result id")?;
                    let mut acc: Option<Word> = None;
                    for (j, &pid) in slot_ptr.iter().enumerate() {
                        let raw = ctx.module.fresh_id();
                        newv.push(Instruction::new(
                            Op::Load,
                            Some(elem_ty),
                            Some(raw),
                            vec![Operand::IdRef(pid)],
                        ));
                        let wide = ctx.module.fresh_id();
                        newv.push(Instruction::new(
                            Op::UConvert,
                            Some(result_pointee),
                            Some(wide),
                            vec![Operand::IdRef(raw)],
                        ));
                        let shifted = if j == 0 {
                            wide
                        } else {
                            let sh = ctx.const_uint(j as u32 * slot_w);
                            let sid = ctx.module.fresh_id();
                            newv.push(Instruction::new(
                                Op::ShiftLeftLogical,
                                Some(result_pointee),
                                Some(sid),
                                vec![Operand::IdRef(wide), Operand::IdRef(sh)],
                            ));
                            sid
                        };
                        acc = Some(match acc {
                            None => shifted,
                            Some(prev) => {
                                let last = j as u32 == ratio - 1;
                                let oid = if last { res } else { ctx.module.fresh_id() };
                                newv.push(Instruction::new(
                                    Op::BitwiseOr,
                                    Some(result_pointee),
                                    Some(oid),
                                    vec![Operand::IdRef(prev), Operand::IdRef(shifted)],
                                ));
                                oid
                            }
                        });
                    }
                    // ratio >= 2, so the OR above always produced `res`; nothing else to emit.
                    let _ = acc;
                } else {
                    // LE split-store: store_j( trunc(obj >> (j*slot_w)) ).
                    let obj = match inst.operands.get(1) {
                        Some(Operand::IdRef(o)) => *o,
                        _ => {
                            return Err(
                                "store through reinterpret chain lost its object".to_string()
                            )
                        }
                    };
                    for (j, &pid) in slot_ptr.iter().enumerate() {
                        let shifted = if j == 0 {
                            obj
                        } else {
                            let sh = ctx.const_uint(j as u32 * slot_w);
                            let sid = ctx.module.fresh_id();
                            newv.push(Instruction::new(
                                Op::ShiftRightLogical,
                                Some(result_pointee),
                                Some(sid),
                                vec![Operand::IdRef(obj), Operand::IdRef(sh)],
                            ));
                            sid
                        };
                        let narrow = ctx.module.fresh_id();
                        newv.push(Instruction::new(
                            Op::UConvert,
                            Some(elem_ty),
                            Some(narrow),
                            vec![Operand::IdRef(shifted)],
                        ));
                        newv.push(Instruction::new(
                            Op::Store,
                            None,
                            None,
                            vec![Operand::IdRef(pid), Operand::IdRef(narrow)],
                        ));
                    }
                }
                continue;
            }
            newv.push(inst);
        }
        ctx.module.functions[entry_idx].blocks[bi].instructions = newv;
    }
    Ok(())
}