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
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
//! The residual SPIR-V passes — the stage-*interface* half of this crate. The structurizer is NOT
//! here: the native emitter (`native/`, with the structured-control-flow logic in `native/cfg/` and
//! `native/emitter/control.rs`) is what turns the AIR CFG into a structured `OpCapability Shader` /
//! `Logical GLSL450` module, emitting OpSelectionMerge/OpLoopMerge and running the budgeted
//! post-hoc repair. By the time a module reaches these passes it is already structured; what remains
//! is purely the stage interface: the emitter lowers each entry parameter to an opaque
//! pointer/value param and each `air.*` to an imported function call. The native emitter never emits
//! the legacy LLVM backend's Linkage capability/decorations. We:
//!   1. turn entry parameters into Vulkan interface variables by their AIR role
//!      (varying -> Input@Location, texture -> UniformConstant image, sampler -> UniformConstant
//!      sampler, buffer -> Uniform Block@set/binding);
//!   2. turn the entry's return value into Output variable(s) @Location (MRT = struct split);
//!   3. lower the residual `air.*` OpFunctionCalls (sample -> OpImageSample*, math -> GLSL.std.450,
//!      dfdx/dfdy -> OpDPdx/OpDPdy, discard -> OpKill, ...);
//!   4. synthesize OpEntryPoint + OpExecutionMode and drop the now-unused air.* function decls.
//!
//! Everything operates on the crate-owned module representation (`crate::spirv_module::Module`),
//! whose instructions, operands, functions, and blocks are crate-owned nodes.

use crate::meta::{
    AirScalar, AirType, FragMeta, FragRole, KernMeta, KernRole, VertMeta, VertOutRole, VertRole,
};
use crate::spirv_module::{is_block_terminator, Block, Function, Instruction, Module, Operand};
use spirv::{
    BuiltIn, Decoration, Dim, FunctionControl, ImageFormat, MemorySemantics, Op, Scope,
    StorageClass, Word,
};
use std::collections::{HashMap, HashSet};

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Stage {
    Vertex,
    Fragment,
    /// Metal compute kernel (`!air.kernel`). GLCompute entry, LocalSize (64,1,1) default, compute
    /// thread/grid builtins -> Vulkan builtins or local-size constants, `air.buffer` -> SSBO.
    Kernel,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TransformOptions {
    pub kernel_local_size: [u32; 3],
    /// Exact Metal `[[threads_per_grid]]` value when it is not derivable as
    /// Vulkan NumWorkgroups * LocalSize (non-uniform `dispatchThreads` tail regions).
    pub kernel_threads_per_grid: Option<[u32; 3]>,
    /// M-D2: lower `air.simd_{sum,min,max,and,or,xor}` REDUCE as a `GroupNonUniform ClusteredReduce`
    /// over a 32-lane cluster (Metal's simdgroup width) instead of a whole-subgroup Reduce, so a
    /// driver whose subgroup is WIDER than 32 still reduces over exactly the 32 lanes Apple's `simd_*`
    /// intrinsics define. Default false → emitted bytes are identical to the whole-subgroup form; the
    /// caller opts in only for a wider-subgroup driver (see kb conformance M-D2, pending G7).
    pub simd_cluster32: bool,
}

impl Default for TransformOptions {
    fn default() -> Self {
        Self {
            kernel_local_size: [64, 1, 1],
            kernel_threads_per_grid: None,
            simd_cluster32: false,
        }
    }
}

/// The sampled component type of a texture (the `OpTypeImage` "Sampled Type"). Metal integer
/// textures (`air.read_texture_2d.u.v4i32` / `.s.v4i32`) must produce an int-typed image so the
/// OpImageFetch result is `v4uint`/`v4int` and matches the AIR result struct member — a float image
/// (the common case) would emit `v4float` and fail spirv-val on the struct construct.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ImageComp {
    Float,
    Uint,
    Sint,
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
struct AirStaticSamplerState {
    word: u64,
}

impl AirStaticSamplerState {
    fn from_word(word: u64) -> Self {
        Self { word }
    }

    fn uses_pixel_nearest(self) -> bool {
        self.uses_pixel_coordinates() && !self.uses_linear_filter()
    }

    fn uses_linear_filter(self) -> bool {
        self.word & 0x0a00 == 0x0a00
    }

    fn uses_pixel_coordinates(self) -> bool {
        self.word & 0x8000 != 0
    }

    fn address_code(self, dimension: usize) -> u64 {
        (self.word >> (dimension * 3)) & 0x7
    }

    fn spatial_clamps_to_zero(self, dimension: usize) -> bool {
        self.address_code(dimension) == 0
    }
}

/// Typed cache key for the singleton types the finalize pass synthesizes (cleanup-plan S4). Each has
/// at most one instance per module and carries its own scan predicate in `finalize.rs`, so keying the
/// memo by this enum replaces the former ad-hoc `"fnvoid"`/`"uchar"`/`"ushort"` string keys.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub(in crate::passes) enum SingletonType {
    /// `OpTypeFunction` returning void with no parameters (`void ()`).
    FnVoid,
    /// `OpTypeInt 8 0` (unsigned char).
    Int8,
    /// `OpTypeInt 16 0` (unsigned short).
    Int16,
}

/// Typed cache key for the synthesized array type + numeric constants the lowering passes memoize
/// (cleanup-plan C3): replaces the former prefix-namespaced string keys (`arr_…`, `ci_…`, `cf_…`,
/// `ch_…`) with a structural enum. Each builder keeps its exact scan / no-scan behavior — this only
/// changes the key representation, not when an instance is minted vs reused — so it is byte-neutral.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub(in crate::passes) enum SynthCacheKey {
    /// `OpTypeArray <elem> <len-const>` (keyed by element type + the length `OpConstant` id).
    Array { elem: Word, len_const: Word },
    /// `OpConstant <int_ty> <value>` synthesized by `const_int_of` (reuses an existing match).
    ConstInt { int_ty: Word, value: i64 },
    /// `OpConstant <float> <bits>` synthesized by `const_float`, keyed by the f32 bit pattern.
    ConstFloat { bits: u32 },
    /// `OpConstant <half> <bits>` synthesized by `const_half`, keyed by the binary16 bit pattern.
    ConstHalf { bits: u16 },
    /// `OpTypeInt <bits> <signed>` synthesized by `conversions::integer_type` (reuses an existing
    /// match).
    IntType { bits: u32, signed: bool },
    /// `OpTypeVector <elem> <lanes>` synthesized by `conversions::vector_type`.
    VecType { elem: Word, lanes: u32 },
    /// `OpConstantComposite <ty> <value×lanes>` synthesized by `access::const_composite_splat`.
    CompositeSplat { ty: Word, value: Word, lanes: u32 },
    /// The singleton `LocalInvocationIndex` builtin Input variable (`matrix_shuffle`).
    LocalInvocationIndexInputVar,
}

/// All state needed while rewriting; the module owns result-id allocation.
struct Ctx {
    module: Module,
    emit_sidecar: crate::emit_sidecar::EmitSidecar,
    /// The AIR stage currently being lowered. Sampling rules differ by execution model: GLCompute
    /// cannot use implicit-LOD sampling unless derivative-group execution modes are present.
    stage: Stage,
    /// id of the GLSL.std.450 ext-inst import, created lazily.
    glsl_ext: Option<Word>,
    /// interface variable ids to list on OpEntryPoint (Input/Output for <=1.3; everything for 1.4+).
    interface: Vec<Word>,
    /// new type/constant/variable instructions to append to types_global_values.
    new_globals: Vec<Instruction>,
    /// memoized synthesized array-type + numeric-constant ids, keyed structurally by
    /// [`SynthCacheKey`]. Used by the bespoke synthesizers that carry their own scan predicate or
    /// mint-fresh scheme (`ty_array`, `const_int_of`, `const_float`, `const_half`); each keeps its
    /// exact scan / no-scan behavior, this map only changes the key representation.
    synth_cache: HashMap<SynthCacheKey, Word>,
    /// Singleton finalize-synthesized types, memoized by their typed identity rather than a string key
    /// (cleanup-plan S4): the `void ()` function type, and the `i8`/`i16` scalar int types. Each carries
    /// its own module scan predicate (a `void ()` OpTypeFunction; an OpTypeInt of the given width) so it
    /// reuses an existing matching type before minting one; at most one of each exists per module.
    singleton_types: HashMap<SingletonType, Word>,
    /// Memoized `get_or_create` results keyed structurally by the exact `(op, result_type, operands)`
    /// tuple, replacing the hand-written textual keys the ~23 `ty_*`/`const_*` builders used to pass
    /// (refactor S11). Memoization-only: `get_or_create` still linear-scans `types_global_values` +
    /// `new_globals` on a cache miss, so the returned id (and thus output bytes) is unchanged; the
    /// structural key merely unifies builders that previously used distinct strings for the same type.
    struct_cache: HashMap<(Op, Option<Word>, Vec<Operand>), Word>,
    /// loaded-image id -> (Dim, arrayed), so the sample lowering builds the matching sampled-image
    /// type + coordinate shape.
    image_dims: HashMap<Word, (Dim, bool)>,
    /// loaded-image id -> sampled component type (default Float), so integer textures fetch/sample
    /// into a matching int vector.
    image_comp: HashMap<Word, ImageComp>,
    /// loaded-image ids that are STORAGE images (`OpTypeImage Sampled=2` with an explicit ImageFormat),
    /// the write-texture binding. `air.write_texture_*` lowers to `OpImageWrite` only on these.
    image_storage: HashSet<Word>,
    /// texture-array descriptor variable id -> (element `OpTypeImage` id, (Dim, arrayed), comp). Set
    /// by the interface `ParamBinding::ImageArray` binding for a runtime-indexed `array_ref<texture>`
    /// argument. `materialize_texture_array_loads` reads it to turn each `OpLoad` of a handle from a
    /// dynamically-indexed array element into `OpAccessChain %arrayvar %idx` + `OpLoad %image`.
    image_array_vars: HashMap<Word, (Word, (Dim, bool), ImageComp)>,
    /// loaded-image ids synthesized by `air.get_null_texture_*()`.
    null_image_values: HashSet<Word>,
    /// composite type ids already given explicit Offset/ArrayStride layout (dedup; a type decorated
    /// twice is a validation error).
    laid_out: HashSet<Word>,
    /// Struct type ids reconstructed from `air.struct_type_info`, with their exact AIR member offsets.
    air_struct_offsets: HashMap<Word, Vec<u32>>,
    /// GLCompute LocalSize and the value exposed to AIR `[[threads_per_threadgroup]]`.
    kernel_local_size: [u32; 3],
    /// Optional exact value exposed to AIR `[[threads_per_grid]]`.
    kernel_threads_per_grid: Option<[u32; 3]>,
    /// M-D2 simd-reduce clustering opt-in (see [`TransformOptions::simd_cluster32`]).
    simd_cluster32: bool,
    /// lazily-created default sampler variable id, for `air.get_read_sampler()` (a sampler-less
    /// `texture.read` still passes a sampler operand AIR-side; we synthesize one valid sampler).
    default_sampler_var: Option<Word>,
    /// Loaded sampler value ids that came from AIR-embedded `__air_sampler_state` globals.
    sampler_states: HashMap<Word, AirStaticSamplerState>,
    /// lazily-created default (null) float image variables for `air.get_null_texture_*()`, keyed by
    /// (Dim, arrayed) so a 2D and a 3D null texture get distinct bindings/types.
    default_null_image_vars: HashMap<(Dim, bool), Word>,
    /// Fragment output rewrite created a `BuiltIn FragDepth` Output variable.
    writes_frag_depth: bool,
}

impl Ctx {
    #[cfg(test)]
    fn new(module: Module) -> Self {
        Self::with_options(module, Stage::Kernel, TransformOptions::default())
    }

    #[cfg(test)]
    fn with_options(module: Module, stage: Stage, options: TransformOptions) -> Self {
        Self::with_options_and_sidecar(
            module,
            crate::emit_sidecar::EmitSidecar::default(),
            stage,
            options,
        )
    }

    fn with_options_and_sidecar(
        module: Module,
        emit_sidecar: crate::emit_sidecar::EmitSidecar,
        stage: Stage,
        options: TransformOptions,
    ) -> Self {
        let mut module = module;
        module.sync_id_bound_from_header();
        let air_struct_offsets = emit_sidecar.air_struct_offsets.clone();
        Ctx {
            module,
            emit_sidecar,
            stage,
            glsl_ext: None,
            interface: vec![],
            new_globals: vec![],
            synth_cache: HashMap::new(),
            singleton_types: HashMap::new(),
            struct_cache: HashMap::new(),
            image_dims: HashMap::new(),
            image_comp: HashMap::new(),
            image_storage: HashSet::new(),
            image_array_vars: HashMap::new(),
            null_image_values: HashSet::new(),
            laid_out: HashSet::new(),
            air_struct_offsets,
            kernel_local_size: options.kernel_local_size,
            kernel_threads_per_grid: options.kernel_threads_per_grid,
            simd_cluster32: options.simd_cluster32,
            default_sampler_var: None,
            sampler_states: HashMap::new(),
            default_null_image_vars: HashMap::new(),
            writes_frag_depth: false,
        }
    }
}

/// A small helper to build a type/constant instruction and register it in the cache + new_globals.
fn type_inst(op: Op, result: Word, operands: Vec<Operand>) -> Instruction {
    Instruction::new(op, None, Some(result), operands)
}

/// Convert an f32 to its IEEE-754 binary16 (half) bit pattern. Round-to-nearest-even; handles the
/// small set of exact values we synthesize (0.0, 1.0) plus general finite values for robustness.
pub(crate) fn f32_to_f16_bits(v: f32) -> u16 {
    let bits = v.to_bits();
    let sign = ((bits >> 16) & 0x8000) as u16;
    let exp = ((bits >> 23) & 0xff) as i32 - 127 + 15;
    let mant = bits & 0x007f_ffff;
    if ((bits >> 23) & 0xff) == 0xff {
        // inf / nan
        return sign | 0x7c00 | if mant != 0 { 0x0200 } else { 0 };
    }
    if exp >= 0x1f {
        return sign | 0x7c00; // overflow -> inf
    }
    if exp <= 0 {
        // subnormal / underflow to zero (sufficient for 0.0; our synthesized consts are 0/1).
        if exp < -10 {
            return sign;
        }
        let mant = (mant | 0x0080_0000) >> (1 - exp);
        let rounded = (mant + 0x0000_1000) >> 13;
        return sign | rounded as u16;
    }
    let half_mant = (mant + 0x0000_1000) >> 13; // round to nearest even (approx)
    sign | ((exp as u16) << 10) | half_mant as u16
}

/// Find the entry function by name (the Vulkan backend does NOT inline helpers, so a module has many
/// functions with bodies; only one is the AIR stage entry). Falls back to the first bodied function.
fn find_entry_index(module: &Module, entry_name: Option<&str>) -> Option<usize> {
    if let Some(name) = entry_name {
        // match via OpName <id> "<name>" -> the function whose def result_id is that id.
        let mut id_for_name = None;
        for inst in &module.debug_names {
            if inst.class.opcode == Op::Name {
                if let (Some(Operand::IdRef(id)), Some(Operand::LiteralString(s))) =
                    (inst.operands.first(), inst.operands.get(1))
                {
                    if s == name {
                        id_for_name = Some(*id);
                    }
                }
            }
        }
        if let Some(id) = id_for_name {
            if let Some(p) = module.functions.iter().position(|f| {
                !f.blocks.is_empty() && f.def.as_ref().and_then(|d| d.result_id) == Some(id)
            }) {
                return Some(p);
            }
        }
    }
    module.functions.iter().position(|f| !f.blocks.is_empty())
}

/// Map every existing type id -> its defining instruction (clone), for type inspection.
fn type_defs(module: &Module) -> HashMap<Word, Instruction> {
    let mut m = HashMap::new();
    for inst in &module.types_global_values {
        if let Some(rid) = inst.result_id {
            m.insert(rid, inst.clone());
        }
    }
    m
}

/// The element type of a pointer type id, if it is OpTypePointer.
fn ptr_pointee(defs: &HashMap<Word, Instruction>, ptr: Word) -> Option<Word> {
    let inst = defs.get(&ptr)?;
    if inst.class.opcode == Op::TypePointer {
        if let Operand::IdRef(p) = inst.operands[1] {
            return Some(p);
        }
    }
    None
}

/// The storage class of a pointer type id.
fn ptr_storage(defs: &HashMap<Word, Instruction>, ptr: Word) -> Option<StorageClass> {
    let inst = defs.get(&ptr)?;
    if inst.class.opcode == Op::TypePointer {
        if let Operand::StorageClass(sc) = inst.operands[0] {
            return Some(sc);
        }
    }
    None
}

impl Ctx {
    /// Get-or-create skeleton shared by the `ty_*`/`const_*` synthesizers (refactor S6/S11): return
    /// the cached id for the structural `(op, result_type, operands)` key; else the id of the first
    /// existing `types_global_values`/`new_globals` instruction with exactly that shape; else allocate
    /// a fresh id, append the instruction, and cache it. `result_type` is `None` for type declarations
    /// (matching `type_inst`) and `Some(ty)` for constants. Because every foldable opcode here is
    /// fixed-arity, full-operand equality reproduces each caller's original per-operand scan predicate
    /// exactly. S11 replaced the caller-supplied textual key with this structural key: it is
    /// memoization-only (the linear scan still decides the returned id), so it is byte-identical while
    /// removing the ~23 hand-maintained key strings and collapsing builders that shared a shape.
    fn get_or_create(&mut self, op: Op, result_type: Option<Word>, operands: Vec<Operand>) -> Word {
        let key = (op, result_type, operands.clone());
        if let Some(&id) = self.struct_cache.get(&key) {
            return id;
        }
        for inst in self
            .module
            .types_global_values
            .iter()
            .chain(self.new_globals.iter())
        {
            if inst.class.opcode == op
                && inst.result_type == result_type
                && inst.operands == operands
            {
                if let Some(rid) = inst.result_id {
                    self.struct_cache.insert(key, rid);
                    return rid;
                }
            }
        }
        let id = self.module.fresh_id();
        self.new_globals
            .push(Instruction::new(op, result_type, Some(id), operands));
        self.struct_cache.insert(key, id);
        id
    }

    /// Get or create OpTypeFloat 32. (Width matters: a half shader has an `OpTypeFloat 16` that must
    /// not be mistaken for `float`.)
    fn ty_float(&mut self) -> Word {
        self.get_or_create(Op::TypeFloat, None, vec![Operand::LiteralBit32(32)])
    }

    /// Get or create OpTypeBool.
    fn ty_bool(&mut self) -> Word {
        self.get_or_create(Op::TypeBool, None, vec![])
    }

    /// Get or create OpTypeVector bool N.
    fn ty_vec_bool(&mut self, n: u32) -> Word {
        let b = self.ty_bool();
        self.get_or_create(
            Op::TypeVector,
            None,
            vec![Operand::IdRef(b), Operand::LiteralBit32(n)],
        )
    }

    /// Get or create OpTypeVector float N.
    fn ty_vecf(&mut self, n: u32) -> Word {
        let f = self.ty_float();
        self.get_or_create(
            Op::TypeVector,
            None,
            vec![Operand::IdRef(f), Operand::LiteralBit32(n)],
        )
    }

    /// Get or create OpTypeVector half N.
    fn ty_vech(&mut self, n: u32) -> Word {
        let h = self.ty_half();
        self.get_or_create(
            Op::TypeVector,
            None,
            vec![Operand::IdRef(h), Operand::LiteralBit32(n)],
        )
    }

    /// Get or create OpTypeVector unsigned 16-bit int N.
    fn ty_vec_u16(&mut self, n: u32) -> Word {
        let u16_ty = self.ty_int16();
        self.get_or_create(
            Op::TypeVector,
            None,
            vec![Operand::IdRef(u16_ty), Operand::LiteralBit32(n)],
        )
    }

    /// Get or create OpTypePointer <sc> <pointee>.
    fn ty_ptr(&mut self, sc: StorageClass, pointee: Word) -> Word {
        self.get_or_create(
            Op::TypePointer,
            None,
            vec![Operand::StorageClass(sc), Operand::IdRef(pointee)],
        )
    }

    /// Get or create OpTypeArray <elem> <len-const>.
    fn ty_array(&mut self, elem: Word, len: u32) -> Word {
        let len_c = self.const_uint(len);
        let key = SynthCacheKey::Array {
            elem,
            len_const: len_c,
        };
        if let Some(&id) = self.synth_cache.get(&key) {
            return id;
        }
        let id = self.module.fresh_id();
        self.new_globals.push(type_inst(
            Op::TypeArray,
            id,
            vec![Operand::IdRef(elem), Operand::IdRef(len_c)],
        ));
        self.synth_cache.insert(key, id);
        id
    }

    /// Build the SPIR-V type for a reconstructed AIR aggregate (`air.struct_type_info`). Mirrors the
    /// original Metal layout so the backend's collapsed access-chain indices land correctly: a matrix
    /// `floatCxR` becomes `{ [cols x vec(rows)] }` (the `metal::matrix` wrapper struct), a struct
    /// becomes an `OpTypeStruct` of its members in order.
    fn build_air_type(&mut self, t: &AirType) -> Word {
        match t {
            AirType::Scalar(scalar) => self.ty_air_scalar(*scalar),
            AirType::Vec { scalar, lanes } => self.ty_air_vec(*scalar, *lanes),
            AirType::PackedVec { scalar, lanes } => {
                let elem = self.ty_air_scalar(*scalar);
                self.ty_array(elem, *lanes)
            }
            AirType::Array { elem, len } => {
                let elem_ty = self.build_air_type(elem);
                self.ty_array(elem_ty, *len)
            }
            AirType::Matrix { scalar, cols, rows } => {
                let col = self.ty_air_vec(*scalar, *rows);
                let arr = self.ty_array(col, *cols);
                let st = self.module.fresh_id();
                self.new_globals
                    .push(type_inst(Op::TypeStruct, st, vec![Operand::IdRef(arr)]));
                st
            }
            AirType::Struct(members) => {
                let mtys: Vec<Word> = members.iter().map(|m| self.build_air_type(&m.ty)).collect();
                let st = self.module.fresh_id();
                let offsets: Vec<u32> = members.iter().map(|m| m.offset).collect();
                if offsets.windows(2).all(|w| w[1] > w[0]) {
                    self.air_struct_offsets.insert(st, offsets);
                }
                self.new_globals.push(type_inst(
                    Op::TypeStruct,
                    st,
                    mtys.into_iter().map(Operand::IdRef).collect(),
                ));
                st
            }
        }
    }

    fn ty_air_scalar(&mut self, scalar: AirScalar) -> Word {
        match scalar {
            AirScalar::Float => self.ty_float(),
            AirScalar::Half => self.ty_half(),
            AirScalar::UInt => self.ty_uint(),
            AirScalar::ULong | AirScalar::SLong => self.ty_ulong(),
            AirScalar::UShort | AirScalar::SShort => self.ty_int16(),
            // LLVM integer values are signless. Keep buffer storage leaves as unsigned words so
            // reconstructed `int`/`packed_intN` layouts type-check against the backend's loads/stores;
            // signed interpretation is carried by the signed SPIR-V opcodes.
            AirScalar::SInt => self.ty_uint(),
            AirScalar::UChar | AirScalar::Bool => self.ty_int8(),
        }
    }

    fn ty_air_vec(&mut self, scalar: AirScalar, lanes: u32) -> Word {
        match scalar {
            AirScalar::Float => self.ty_vecf(lanes),
            AirScalar::Half => self.ty_vech(lanes),
            AirScalar::UInt => self.ty_vec_uint(lanes),
            AirScalar::ULong | AirScalar::SLong => self.ty_vec_ulong(lanes),
            AirScalar::UShort | AirScalar::SShort => self.ty_vec_u16(lanes),
            AirScalar::SInt => self.ty_vec_uint(lanes),
            AirScalar::UChar | AirScalar::Bool => {
                let elem = self.ty_int8();
                self.ty_array(elem, lanes)
            }
        }
    }

    /// Get or create OpTypeRuntimeArray <elem>. Used to wrap a `device T*` (or a homogeneous struct
    /// the backend collapsed into a bare element pointer) as a StorageBuffer Block `{ RuntimeArray<T> }`
    /// so Logical SPIR-V can express `buf[i]`.
    fn ty_runtime_array(&mut self, elem: Word) -> Word {
        self.get_or_create(Op::TypeRuntimeArray, None, vec![Operand::IdRef(elem)])
    }

    /// Get or create OpTypeImage with the given sampled component type, 2D/1D, sampled.
    fn ty_image(&mut self, dim: Dim, arrayed: bool, comp: ImageComp) -> Word {
        let f = match comp {
            ImageComp::Float => self.ty_float(),
            ImageComp::Uint => self.ty_uint(),
            ImageComp::Sint => self.ty_sint(),
        };
        self.get_or_create(
            Op::TypeImage,
            None,
            vec![
                Operand::IdRef(f),
                Operand::Dim(dim),
                Operand::LiteralBit32(0), // Depth: not a depth image
                Operand::LiteralBit32(arrayed as u32), // Arrayed
                Operand::LiteralBit32(0), // MS
                Operand::LiteralBit32(1), // Sampled: 1 = used with sampler
                Operand::ImageFormat(ImageFormat::Unknown),
            ],
        )
    }

    /// Get or create an input-attachment image type (`subpassInput`/`SubpassData`).
    fn ty_input_attachment(&mut self, sampled: Word) -> Word {
        self.get_or_create(
            Op::TypeImage,
            None,
            vec![
                Operand::IdRef(sampled),
                Operand::Dim(Dim::DimSubpassData),
                Operand::LiteralBit32(0), // Depth
                Operand::LiteralBit32(0), // Arrayed
                Operand::LiteralBit32(0), // MS
                Operand::LiteralBit32(2), // Sampled: 2 = read without sampler
                Operand::ImageFormat(ImageFormat::Unknown),
            ],
        )
    }

    /// Get or create a STORAGE `OpTypeImage` (Sampled=2) with an explicit `ImageFormat`, the
    /// write-texture binding. The sampled component type must match the storage image format so
    /// `OpImageWrite` accepts float, uint, or sint texels.
    fn ty_storage_image(
        &mut self,
        dim: Dim,
        arrayed: bool,
        fmt: ImageFormat,
        comp: ImageComp,
    ) -> Word {
        let sampled = match comp {
            ImageComp::Float => self.ty_float(),
            ImageComp::Uint => self.ty_uint(),
            ImageComp::Sint => self.ty_sint(),
        };
        self.get_or_create(
            Op::TypeImage,
            None,
            vec![
                Operand::IdRef(sampled),
                Operand::Dim(dim),
                Operand::LiteralBit32(0),              // Depth
                Operand::LiteralBit32(arrayed as u32), // Arrayed
                Operand::LiteralBit32(0),              // MS
                Operand::LiteralBit32(2),              // Sampled: 2 = read/write (storage)
                Operand::ImageFormat(fmt),
            ],
        )
    }

    fn ty_sampler(&mut self) -> Word {
        self.get_or_create(Op::TypeSampler, None, vec![])
    }

    fn ty_sampled_image(&mut self, image: Word) -> Word {
        self.get_or_create(Op::TypeSampledImage, None, vec![Operand::IdRef(image)])
    }

    fn ty_void(&mut self) -> Word {
        self.get_or_create(Op::TypeVoid, None, vec![])
    }

    /// OpTypeInt 32 unsigned.
    fn ty_uint(&mut self) -> Word {
        self.get_or_create(
            Op::TypeInt,
            None,
            vec![Operand::LiteralBit32(32), Operand::LiteralBit32(0)],
        )
    }

    /// OpTypeInt 64 unsigned.
    fn ty_ulong(&mut self) -> Word {
        self.get_or_create(
            Op::TypeInt,
            None,
            vec![Operand::LiteralBit32(64), Operand::LiteralBit32(0)],
        )
    }

    /// OpTypeInt 32 signed.
    fn ty_sint(&mut self) -> Word {
        self.get_or_create(
            Op::TypeInt,
            None,
            vec![Operand::LiteralBit32(32), Operand::LiteralBit32(1)],
        )
    }

    /// Get or create OpTypeVector sint N.
    fn ty_vec_sint(&mut self, n: u32) -> Word {
        let s = self.ty_sint();
        self.get_or_create(
            Op::TypeVector,
            None,
            vec![Operand::IdRef(s), Operand::LiteralBit32(n)],
        )
    }

    /// Get or create OpTypeVector uint N (e.g. v3uint for the GlobalInvocationId builtin).
    fn ty_vec_uint(&mut self, n: u32) -> Word {
        let u = self.ty_uint();
        self.get_or_create(
            Op::TypeVector,
            None,
            vec![Operand::IdRef(u), Operand::LiteralBit32(n)],
        )
    }

    /// Get or create OpTypeVector ulong N.
    fn ty_vec_ulong(&mut self, n: u32) -> Word {
        let u = self.ty_ulong();
        self.get_or_create(
            Op::TypeVector,
            None,
            vec![Operand::IdRef(u), Operand::LiteralBit32(n)],
        )
    }

    /// OpConstant uint <v>.
    fn const_uint(&mut self, v: u32) -> Word {
        let uint = self.ty_uint();
        self.get_or_create(Op::Constant, Some(uint), vec![Operand::LiteralBit32(v)])
    }

    /// OpConstantTrue/OpConstantFalse of an existing bool type.
    fn const_bool_of(&mut self, bool_ty: Word, value: bool) -> Word {
        let op = if value {
            Op::ConstantTrue
        } else {
            Op::ConstantFalse
        };
        self.get_or_create(op, Some(bool_ty), vec![])
    }

    /// Constant integer `v` of the given integer type `int_ty` (any width/signedness). Reuses an
    /// existing matching `OpConstant`. Used to synthesize `0`/`1` of a value's own int type for
    /// bool<->int convert lowerings.
    fn const_int_of(&mut self, int_ty: Word, v: i64) -> Word {
        let key = SynthCacheKey::ConstInt { int_ty, value: v };
        if let Some(&id) = self.synth_cache.get(&key) {
            return id;
        }
        let bits = self
            .module
            .types_global_values
            .iter()
            .chain(self.new_globals.iter())
            .find_map(|inst| {
                if inst.class.opcode == Op::TypeInt && inst.result_id == Some(int_ty) {
                    match inst.operands.first() {
                        Some(Operand::LiteralBit32(bits)) => Some(*bits),
                        _ => None,
                    }
                } else {
                    None
                }
            })
            .unwrap_or(32);
        let lit = if bits == 64 {
            Operand::LiteralBit64(v as u64)
        } else {
            Operand::LiteralBit32(v as u32)
        };
        for inst in self
            .module
            .types_global_values
            .iter()
            .chain(self.new_globals.iter())
        {
            if inst.class.opcode == Op::Constant
                && inst.result_type == Some(int_ty)
                && inst.operands.first() == Some(&lit)
            {
                if let Some(rid) = inst.result_id {
                    self.synth_cache.insert(key, rid);
                    return rid;
                }
            }
        }
        let id = self.module.fresh_id();
        self.new_globals.push(Instruction::new(
            Op::Constant,
            Some(int_ty),
            Some(id),
            vec![lit],
        ));
        self.synth_cache.insert(key, id);
        id
    }

    /// Constant float 0.0/1.0 etc.
    fn const_float(&mut self, bits: f32) -> Word {
        let key = SynthCacheKey::ConstFloat {
            bits: bits.to_bits(),
        };
        if let Some(&id) = self.synth_cache.get(&key) {
            return id;
        }
        let f = self.ty_float();
        let id = self.module.fresh_id();
        self.new_globals.push(type_inst(
            Op::Constant,
            id,
            vec![Operand::LiteralBit32(bits.to_bits())],
        ));
        // fix result_type (Constant needs it)
        let last = self.new_globals.last_mut().unwrap();
        last.result_type = Some(f);
        self.synth_cache.insert(key, id);
        id
    }

    /// Get or create OpTypeFloat 16 (`half`). Reuses an existing one (llc emits it for any half
    /// shader). Adds the `Float16` capability requirement implicitly (asserted in finalize).
    fn ty_half(&mut self) -> Word {
        self.get_or_create(Op::TypeFloat, None, vec![Operand::LiteralBit32(16)])
    }

    /// Constant `half` of value `v` (0.0/1.0 etc.), encoded as the IEEE-754 binary16 bit pattern.
    fn const_half(&mut self, v: f32) -> Word {
        let bits = f32_to_f16_bits(v);
        let key = SynthCacheKey::ConstHalf { bits };
        if let Some(&id) = self.synth_cache.get(&key) {
            return id;
        }
        let h = self.ty_half();
        let id = self.module.fresh_id();
        self.new_globals.push(Instruction::new(
            Op::Constant,
            Some(h),
            Some(id),
            vec![Operand::LiteralBit32(bits as u32)],
        ));
        self.synth_cache.insert(key, id);
        id
    }

    fn glsl(&mut self) -> Word {
        if let Some(id) = self.glsl_ext {
            return id;
        }
        // reuse if present
        for inst in &self.module.ext_inst_imports {
            if let Some(Operand::LiteralString(s)) = inst.operands.first() {
                if s == "GLSL.std.450" {
                    let id = inst.result_id.unwrap();
                    self.glsl_ext = Some(id);
                    return id;
                }
            }
        }
        let id = self.module.fresh_id();
        self.module.ext_inst_imports.push(Instruction::new(
            Op::ExtInstImport,
            None,
            Some(id),
            vec![Operand::LiteralString("GLSL.std.450".into())],
        ));
        self.glsl_ext = Some(id);
        id
    }
}

mod access;
mod air_calls;
mod cfg_repair;
mod emitted_inline;
mod finalize;
#[cfg(test)]
mod lowering_regression_tests;
mod module_cleanup;
mod prune;
mod resources;
mod spirv_cfg;
mod stage_input;
mod stage_output;
mod type_singletons;
mod value_queries;
mod workgroup;

use access::{
    compose_derived_access_chains, decorate_ptr_access_chain_base_strides,
    drop_dead_invalid_access_chains, drop_overindexed_zero_tail,
    drop_writeonly_dead_local_array_stores, fix_noop_width_converts,
    guard_integer_division_by_zero, hoist_function_variables, lower_cross_member_subword_load,
    lower_cross_member_subword_store, lower_private_byte_aggregate_reinterpret,
    lower_private_memory_atomics, lower_subword_scalar_store, narrow_access_chain_indices,
    neutralize_null_access_chains, neutralize_private_placeholder_access_chains,
    normalize_int_arith_operand_widths, normalize_scalar_store_types,
    recover_inlined_local_pointer_fields, remap_dynamic_word_index_to_array_member,
    remap_dynamic_word_index_to_array_struct_field, remap_overflow_word_index_to_outer_member,
    remap_word_index_to_struct_member, remodel_workgroup_flatword_aggregate,
    remodel_workgroup_floatarray_atomic_as_uint, repair_scalar_load_through_vector_ptr,
    repair_vector_load_through_scalar_stride, reroot_demoted_array_element_overindex,
    retype_demoted_copymemory_placeholder, rewrite_byte_buffer_chained_reinterpret,
    rewrite_chained_element_reinterpret, rewrite_dynamic_struct_index_reinterpret,
    rewrite_dynamic_struct_index_vector_reinterpret, rewrite_raw_byte_pointer_wide_loads,
    rewrite_reinterpret_scalar_loads, rewrite_scalar_pointer_arithmetic_access_chains,
    rewrite_scalar_slot_array_overindex, rewrite_strided_descent_access_chains,
};
use air_calls::lower_air_calls;
use emitted_inline::{
    compose_chained_access_chains, inline_selected_helpers, prune_unreferenced_functions,
};
use finalize::finalize;
use prune::prune_unreachable_blocks;
use resources::*;
use stage_input::build_stage_input;
use stage_output::rewrite_return;
use value_queries::*;
use workgroup::*;

/// OpName id -> string for every residual intrinsic function. Shared by the inline pass (to skip
/// calls that must be lowered, not inlined) and the lowering pass. Lives in the parent so both
/// submodules reach it without a sibling dependency.
fn air_names(module: &Module) -> HashMap<Word, String> {
    let mut m = HashMap::new();
    for inst in &module.debug_names {
        if inst.class.opcode == Op::Name {
            if let (Some(Operand::IdRef(id)), Some(Operand::LiteralString(s))) =
                (inst.operands.first(), inst.operands.get(1))
            {
                if s.starts_with("air.")
                    || s.starts_with("llvm.fabs.")
                    || s.starts_with("llvm.fmuladd.")
                    || s == "llvm.agx3.edgecheck"
                    || matches!(
                        s.as_str(),
                        "llvm.agx3.load.with.emask.global.v4i8"
                            | "llvm.agx3.load.with.emask.global.v4i16"
                            | "llvm.agx3.load.with.emask.global.v4i32"
                            | "llvm.agx3.store.with.emask.global.v4i8"
                            | "llvm.agx3.store.with.emask.global.v4i16"
                            | "llvm.agx3.store.with.emask.global.v4i32"
                    )
                    || s.starts_with("llvm.bswap.")
                    || s.starts_with("llvm.maxnum.")
                    || s.starts_with("llvm.minnum.")
                    || s == "llvm.assume"
                {
                    m.insert(*id, s.clone());
                }
            }
        }
    }
    m
}

/// Apply every bodied helper splice to the native emitter's completed typed SPIR-V graph before it
/// is handed to the final passes.
///
/// This producer-side invocation does not prune functions or run module-wide post-inline recovery.
/// Those cleanup operations retain their downstream phase in the final passes.
pub(crate) fn inline_all_emitted_helpers(
    mut module: Module,
    emit_sidecar: crate::emit_sidecar::EmitSidecar,
    entry_name: Option<&str>,
) -> Result<(Module, crate::emit_sidecar::EmitSidecar), String> {
    // Some emitter CFG paths retain later `OpLabel`s inside the first SPIR-V block until the first
    // serialize/load boundary. Reproduce the loader's block partition in memory so this invocation
    // and the former residual invocation saw the same graph.
    if !partition_embedded_blocks(&mut module) {
        return Ok((module, emit_sidecar));
    }
    // `stage` and transform options are not consulted by the inliner. Supplying the ordinary kernel
    // defaults constructs the same allocation/type caches the later residual Ctx starts with.
    let mut ctx = Ctx::with_options_and_sidecar(
        module,
        emit_sidecar,
        Stage::Kernel,
        TransformOptions::default(),
    );
    let entry_idx = find_entry_index(&ctx.module, entry_name)
        .ok_or_else(|| "no entry function with a body found before emitted inlining".to_string())?;
    let entry_id = ctx.module.functions[entry_idx]
        .def
        .as_ref()
        .and_then(|instruction| instruction.result_id);
    let selected_ids = ctx
        .module
        .functions
        .iter()
        .filter(|function| !function.blocks.is_empty())
        .filter_map(|function| {
            let id = function
                .def
                .as_ref()
                .and_then(|instruction| instruction.result_id)?;
            (Some(id) != entry_id).then_some(id)
        })
        .collect::<HashSet<_>>();
    if selected_ids.is_empty() {
        return Ok((ctx.module, ctx.emit_sidecar));
    }
    inline_selected_helpers(&mut ctx, entry_idx, &selected_ids)?;
    if let Some((function, block, terminator, instructions)) =
        first_detached_instruction(&ctx.module)
    {
        return Err(format!(
            "emitted inliner produced instructions after a terminator \
             (reason=emitted_inline_detached_instruction, function={function}, block={block}, \
             first_terminator={terminator}, instructions={instructions})"
        ));
    }
    ctx.module.types_global_values.append(&mut ctx.new_globals);
    Ok((ctx.module, ctx.emit_sidecar))
}

/// Reproduce SPIR-V loader block partitioning without crossing the words boundary.
///
/// Returns `false` without mutation when the instruction stream would not parse: every embedded
/// label must immediately follow a block terminator, and every resulting block must end in one.
fn partition_embedded_blocks(module: &mut Module) -> bool {
    let is_terminator = |instruction: &Instruction| is_block_terminator(instruction.class.opcode);
    let can_partition = module.functions.iter().all(|function| {
        function.blocks.iter().all(|block| {
            let mut segment_has_terminator = false;
            for instruction in &block.instructions {
                if instruction.class.opcode == Op::Label {
                    if !segment_has_terminator {
                        return false;
                    }
                    segment_has_terminator = false;
                } else if segment_has_terminator {
                    return false;
                } else if is_terminator(instruction) {
                    segment_has_terminator = true;
                }
            }
            segment_has_terminator
        })
    });
    if !can_partition {
        return false;
    }

    for function in &mut module.functions {
        let mut blocks = Vec::new();
        for block in std::mem::take(&mut function.blocks) {
            let mut current = Block {
                label: block.label,
                instructions: Vec::new(),
            };
            for instruction in block.instructions {
                if instruction.class.opcode == Op::Label {
                    blocks.push(current);
                    current = Block {
                        label: Some(instruction),
                        instructions: Vec::new(),
                    };
                } else {
                    current.instructions.push(instruction);
                }
            }
            blocks.push(current);
        }
        function.blocks = blocks;
    }
    true
}

fn first_detached_instruction(module: &Module) -> Option<(usize, usize, usize, usize)> {
    for (function_index, function) in module.functions.iter().enumerate() {
        for (block_index, block) in function.blocks.iter().enumerate() {
            let terminator = block.instructions.iter().position(|instruction| {
                matches!(
                    instruction.class.opcode,
                    Op::Branch
                        | Op::BranchConditional
                        | Op::Switch
                        | Op::Return
                        | Op::ReturnValue
                        | Op::Kill
                        | Op::Unreachable
                )
            });
            if let Some(terminator) =
                terminator.filter(|index| index + 1 != block.instructions.len())
            {
                return Some((
                    function_index,
                    block_index,
                    terminator,
                    block.instructions.len(),
                ));
            }
        }
    }
    None
}

/// Replace every use of id `from` with `to` across a function body (operands only). Shared by the
/// inline + interface passes.
fn replace_id_in_function(func: &mut Function, from: Word, to: Word) {
    for blk in &mut func.blocks {
        for inst in &mut blk.instructions {
            for op in &mut inst.operands {
                if let Operand::IdRef(r) = op {
                    if *r == from {
                        *r = to;
                    }
                }
            }
        }
    }
}

/// Top-level driver: take the llc Vulkan SPIR-V module and produce a Vulkan entry-pointed module.
/// `entry_name` is the AIR stage entry function name (from `!air.<stage>` metadata) used to pick the
/// right function among the many the Vulkan backend leaves un-inlined.
/// Renumber every result id into a deterministic canonical form: walk all instructions in serialized
/// order and assign new sequential ids on first appearance, then remap every id reference (result id,
/// result type, and the three id-bearing operand kinds). The serialized order is fully Vec-ordered
/// (deterministic), so this makes the assembled bytes reproducible regardless of the HashMap-order-
/// sensitive id allocation during emission. Pure renumbering — semantically identical SPIR-V.
pub(crate) fn canonicalize_ids(module: &mut Module) {
    canonicalize_ids_and_remap(module, &mut []);
}

/// Canonicalize module ids while keeping a small caller-owned set of typed sidecar ids aligned with
/// the same remap. The tracked ids are not SPIR-V roots by themselves; callers use them to preserve
/// semantic sidecar facts through later in-memory rewrites after debug-marker removal.
pub(crate) fn canonicalize_ids_and_remap(module: &mut Module, tracked_ids: &mut [Word]) {
    let mut remap: HashMap<Word, Word> = HashMap::new();
    let mut next: Word = 1;
    for inst in module.all_inst_iter() {
        if let Some(result_id) = inst.result_id {
            remap.entry(result_id).or_insert_with(|| {
                let id = next;
                next += 1;
                id
            });
        }
    }
    let map = |w: Word| remap.get(&w).copied().unwrap_or(w);
    for inst in module.all_inst_iter_mut() {
        if let Some(result_type) = inst.result_type.as_mut() {
            *result_type = map(*result_type);
        }
        if let Some(result_id) = inst.result_id.as_mut() {
            *result_id = map(*result_id);
        }
        for operand in &mut inst.operands {
            match operand {
                Operand::IdRef(w) | Operand::IdMemorySemantics(w) | Operand::IdScope(w) => {
                    *w = map(*w);
                }
                _ => {}
            }
        }
    }
    module.set_id_bound(next);
    for id in tracked_ids {
        *id = map(*id);
    }
}

#[cfg(test)]
mod canonicalize_tests {
    use super::*;
    use crate::spirv_module::ModuleHeader;

    #[test]
    fn canonicalize_remaps_typed_sidecar_ids_with_the_module() {
        let mut module = Module::new();
        module.header = Some(ModuleHeader::new(81));
        module.types_global_values = vec![
            Instruction::new(
                Op::TypeInt,
                None,
                Some(50),
                vec![Operand::LiteralBit32(64), Operand::LiteralBit32(0)],
            ),
            Instruction::new(Op::ConstantNull, Some(50), Some(80), vec![]),
        ];
        let mut tracked = [80];

        canonicalize_ids_and_remap(&mut module, &mut tracked);

        assert_eq!(tracked, [2]);
        assert_eq!(module.types_global_values[1].result_id, Some(2));
        assert_eq!(module.header.as_ref().map(|header| header.bound), Some(3));
    }
}

#[cfg(test)]
mod emitted_inline_tests {
    use super::*;
    use crate::spirv_module::ModuleHeader;
    use spirv::FunctionControl;

    fn function(id: Word, label: Word, instructions: Vec<Instruction>) -> Function {
        Function {
            def: Some(Instruction::new(
                Op::Function,
                Some(2),
                Some(id),
                vec![
                    Operand::FunctionControl(FunctionControl::NONE),
                    Operand::IdRef(3),
                ],
            )),
            end: Some(Instruction::new(Op::FunctionEnd, None, None, vec![])),
            parameters: Vec::new(),
            blocks: vec![Block {
                label: Some(Instruction::new(Op::Label, None, Some(label), vec![])),
                instructions,
            }],
        }
    }

    #[test]
    fn emitted_selection_skips_a_prepass_intermediate_the_old_inliner_cannot_observe() {
        let entry_instructions = vec![
            Instruction::new(Op::FunctionCall, None, None, vec![Operand::IdRef(20)]),
            Instruction::new(Op::Branch, None, None, vec![Operand::IdRef(11)]),
            Instruction::new(Op::UConvert, Some(5), Some(12), vec![Operand::IdRef(6)]),
            Instruction::new(Op::Return, None, None, vec![]),
        ];
        let mut module = Module::new();
        module.header = Some(ModuleHeader::new(100));
        module.debug_names = vec![
            Instruction::new(
                Op::Name,
                None,
                None,
                vec![
                    Operand::IdRef(10),
                    Operand::LiteralString("main".to_string()),
                ],
            ),
            Instruction::new(
                Op::Name,
                None,
                None,
                vec![
                    Operand::IdRef(20),
                    Operand::LiteralString("helper".to_string()),
                ],
            ),
        ];
        module.functions = vec![
            function(10, 11, entry_instructions.clone()),
            function(
                20,
                21,
                vec![Instruction::new(Op::Return, None, None, vec![])],
            ),
        ];

        let (module, _) = inline_all_emitted_helpers(
            module,
            crate::emit_sidecar::EmitSidecar::default(),
            Some("main"),
        )
        .expect("invalid intermediate follows the historical retry route");

        assert_eq!(
            module.functions[0].blocks[0].instructions,
            entry_instructions
        );
        assert_eq!(
            module.functions.len(),
            2,
            "the selected helper remains untouched"
        );
    }

    #[test]
    fn emitted_closure_partitions_embedded_labels_like_the_loader() {
        let mut module = Module::new();
        module.header = Some(ModuleHeader::new(100));
        module.debug_names = vec![Instruction::new(
            Op::Name,
            None,
            None,
            vec![
                Operand::IdRef(10),
                Operand::LiteralString("main".to_string()),
            ],
        )];
        module.functions = vec![
            function(
                10,
                11,
                vec![
                    Instruction::new(Op::FunctionCall, None, None, vec![Operand::IdRef(20)]),
                    Instruction::new(Op::Branch, None, None, vec![Operand::IdRef(12)]),
                    Instruction::new(Op::Label, None, Some(12), vec![]),
                    Instruction::new(Op::Return, None, None, vec![]),
                ],
            ),
            function(
                20,
                21,
                vec![Instruction::new(Op::Return, None, None, vec![])],
            ),
        ];

        let (module, _) = inline_all_emitted_helpers(
            module,
            crate::emit_sidecar::EmitSidecar::default(),
            Some("main"),
        )
        .expect("complete emitted closure");

        assert_eq!(module.functions[0].blocks.len(), 2);
        assert_eq!(
            module.functions[0].blocks[1]
                .label
                .as_ref()
                .and_then(|label| label.result_id),
            Some(12)
        );
        assert!(
            module.functions[0]
                .blocks
                .iter()
                .flat_map(|block| &block.instructions)
                .all(|instruction| instruction.class.opcode != Op::FunctionCall),
            "the helper is spliced after reproducing the serialized block partition"
        );
    }

    #[test]
    fn emitted_closure_selects_bodied_function_ids_without_debug_names() {
        let mut module = Module::new();
        module.header = Some(ModuleHeader::new(100));
        module.debug_names = vec![Instruction::new(
            Op::Name,
            None,
            None,
            vec![
                Operand::IdRef(10),
                Operand::LiteralString("main".to_string()),
            ],
        )];
        module.functions = vec![
            function(
                10,
                11,
                vec![
                    Instruction::new(Op::FunctionCall, None, None, vec![Operand::IdRef(20)]),
                    Instruction::new(Op::Return, None, None, vec![]),
                ],
            ),
            function(
                20,
                21,
                vec![Instruction::new(Op::Return, None, None, vec![])],
            ),
        ];

        let (module, _) = inline_all_emitted_helpers(
            module,
            crate::emit_sidecar::EmitSidecar::default(),
            Some("main"),
        )
        .expect("complete emitted closure");

        assert!(
            module.functions[0].blocks[0]
                .instructions
                .iter()
                .all(|instruction| instruction.class.opcode != Op::FunctionCall),
            "all bodied function ids are selected independently of OpName"
        );
    }
}

#[cfg(test)]
pub(crate) fn transform(
    module: Module,
    stage: Stage,
    frag: Option<&FragMeta>,
    vert: Option<&VertMeta>,
    kern: Option<&KernMeta>,
    entry_name: Option<&str>,
) -> Result<Module, String> {
    transform_with_options(
        module,
        stage,
        frag,
        vert,
        kern,
        entry_name,
        TransformOptions::default(),
    )
}

#[cfg(test)]
pub(crate) fn transform_with_options(
    module: Module,
    stage: Stage,
    frag: Option<&FragMeta>,
    vert: Option<&VertMeta>,
    kern: Option<&KernMeta>,
    entry_name: Option<&str>,
    options: TransformOptions,
) -> Result<Module, String> {
    transform_with_options_and_sidecar(
        module,
        crate::emit_sidecar::EmitSidecar::default(),
        stage,
        frag,
        vert,
        kern,
        entry_name,
        options,
    )
    .map(|(module, _)| module)
}

pub(crate) fn transform_with_options_and_sidecar(
    module: Module,
    emit_sidecar: crate::emit_sidecar::EmitSidecar,
    stage: Stage,
    frag: Option<&FragMeta>,
    vert: Option<&VertMeta>,
    kern: Option<&KernMeta>,
    entry_name: Option<&str>,
    options: TransformOptions,
) -> Result<(Module, crate::emit_sidecar::EmitSidecar), String> {
    if matches!(stage, Stage::Kernel) && options.kernel_local_size.contains(&0) {
        return Err("kernel LocalSize dimensions must be non-zero".to_string());
    }
    let mut ctx = Ctx::with_options_and_sidecar(module, emit_sidecar, stage, options);

    let mut entry_idx = find_entry_index(&ctx.module, entry_name)
        .ok_or_else(|| "no entry function with a body found".to_string())?;

    // Producer-side closure already made the entry self-contained. Preserve the former residual
    // inliner's two post-splice cleanup operations at this phase.
    compose_chained_access_chains(&mut ctx, entry_idx);
    prune_unreferenced_functions(&mut ctx, entry_idx);
    entry_idx = find_entry_index(&ctx.module, entry_name)
        .ok_or_else(|| "entry vanished after helper cleanup".to_string())?;
    recover_inlined_local_pointer_fields(&mut ctx, entry_idx);
    compose_derived_access_chains(&mut ctx, entry_idx);
    neutralize_private_placeholder_access_chains(&mut ctx, entry_idx)?;
    neutralize_null_access_chains(&mut ctx, entry_idx);

    // 1a) decoded params -> stage-input/resource vars and entry-body replacements. Preserve the
    //     original type snapshot for the immediately-following return rewrite.
    let input_defs = build_stage_input(&mut ctx, entry_idx, &stage, frag, vert, kern)?;
    // 1b) return -> output vars.
    rewrite_return(&mut ctx, entry_idx, &stage, frag, vert, &input_defs)?;
    // Turn each runtime-indexed `array_ref<texture>` element handle load into an OpAccessChain+OpLoad
    // into the descriptor array declared by the interface `ImageArray` binding, so the sample/query
    // lowering below sees an ordinary loaded image (no-op unless an ImageArray binding exists). Runs
    // BEFORE `recover_inlined_local_pointer_fields`, which otherwise rewrites the per-element handle
    // load into a local-pointer-field marker and severs the access chain this pass keys on.
    resources::materialize_texture_array_loads(&mut ctx, entry_idx);
    hoist_function_variables(&mut ctx, entry_idx);
    // Entry texture parameters become loaded image ids only after interface binding. Replay any
    // helper-field markers that still reference those parameters before image calls lower.
    recover_inlined_local_pointer_fields(&mut ctx, entry_idx);

    // 2) lower residual air.* calls inside the entry function.
    lower_air_calls(&mut ctx, entry_idx)?;
    lower_private_memory_atomics(&mut ctx, entry_idx);

    // 2b) repair width-preserving int/float converts (illegal in SPIR-V) that arise from binding a
    //     narrower AIR param (`ushort [[vertex_id]]`) to the 32-bit Vulkan builtin -> a same-width
    //     `OpUConvert` the body's original widening cast compiled to. A no-op `OpCopyObject` is correct.
    fix_noop_width_converts(&mut ctx, entry_idx);

    // 2c) define otherwise-undefined integer divide-by-zero side arms before drivers can materialize
    //     divergent values from guarded select expressions.
    guard_integer_division_by_zero(&mut ctx, entry_idx);

    // 2d) repair structured-CFG merge placement (an OpSelectionMerge/OpLoopMerge that llc's
    //     structurizer left mid-block, separated from its branch by a hoisted value computation).
    cfg_repair::fix_merge_placement(&mut ctx, entry_idx);
    cfg_repair::repair_loop_continue_pass_through_targets(&mut ctx, entry_idx);
    cfg_repair::repair_continue_selection_merge_targets(&mut ctx, entry_idx);
    // The external-predecessor and phi-edge repairs feed each other: fixing a loop-continue's
    // external predecessor can leave a stale phi incoming, and rewriting a phi edge can expose a new
    // external predecessor. This was hand-unrolled as exactly two rounds; run it as an explicit
    // bounded fixpoint instead, asserting it settles — a case that never converges is a repair bug we
    // want surfaced, not silently truncated. Byte-identical while two rounds sufficed (the extra
    // convergence-confirming round makes no change); a case needing a third changing round is exactly
    // what the assert/gates now catch. `Block` is not `PartialEq`, so compare its content directly.
    const CONTINUE_PHI_REPAIR_CAP: usize = 8;
    let mut converged = false;
    for _ in 0..CONTINUE_PHI_REPAIR_CAP {
        let before = ctx.module.functions[entry_idx].blocks.clone();
        cfg_repair::repair_loop_continue_external_predecessors(&mut ctx, entry_idx);
        cfg_repair::repair_phi_predecessor_edges(&mut ctx, entry_idx);
        let after = &ctx.module.functions[entry_idx].blocks;
        if before.len() == after.len()
            && before
                .iter()
                .zip(after)
                .all(|(a, b)| a.label == b.label && a.instructions == b.instructions)
        {
            converged = true;
            break;
        }
    }
    debug_assert!(
        converged,
        "loop-continue/phi-edge repair did not converge within {CONTINUE_PHI_REPAIR_CAP} rounds"
    );

    // 2e) narrow 64-bit access-chain INDEX operands to 32-bit. NVIDIA's SPIR-V->NVVM compiler crashes
    //     when an access-chain index is a 64-bit (`%ulong`) value (a driver-fragility class), so we
    //     value-preservingly rewrite each i64 index to a 32-bit equivalent. After this the i64 index
    //     constants/types are dead and `finalize` can drop the now-unused `OpCapability Int64`.
    narrow_access_chain_indices(&mut ctx, entry_idx);
    compose_derived_access_chains(&mut ctx, entry_idx);
    rewrite_scalar_pointer_arithmetic_access_chains(&mut ctx, entry_idx);
    drop_overindexed_zero_tail(&mut ctx, entry_idx);
    reroot_demoted_array_element_overindex(&mut ctx, entry_idx);
    remap_word_index_to_struct_member(&mut ctx, entry_idx);
    remap_overflow_word_index_to_outer_member(&mut ctx, entry_idx);
    remap_dynamic_word_index_to_array_member(&mut ctx, entry_idx);
    remap_dynamic_word_index_to_array_struct_field(&mut ctx, entry_idx);
    repair_vector_load_through_scalar_stride(&mut ctx, entry_idx);
    repair_scalar_load_through_vector_ptr(&mut ctx, entry_idx);
    drop_writeonly_dead_local_array_stores(&mut ctx, entry_idx);
    lower_cross_member_subword_load(&mut ctx, entry_idx)?;
    lower_cross_member_subword_store(&mut ctx, entry_idx);
    normalize_scalar_store_types(&mut ctx, entry_idx);
    lower_subword_scalar_store(&mut ctx, entry_idx);
    drop_dead_invalid_access_chains(&mut ctx, entry_idx);
    rewrite_strided_descent_access_chains(&mut ctx, entry_idx);
    rewrite_dynamic_struct_index_reinterpret(&mut ctx, entry_idx)?;
    rewrite_dynamic_struct_index_vector_reinterpret(&mut ctx, entry_idx)?;
    rewrite_chained_element_reinterpret(&mut ctx, entry_idx)?;
    rewrite_byte_buffer_chained_reinterpret(&mut ctx, entry_idx)?;
    // The raw-byte replay introduces PtrAccessChain byte pointers.  Decorate their common uchar
    // base type immediately below, together with every pre-existing PtrAccessChain base.
    rewrite_raw_byte_pointer_wide_loads(&mut ctx, entry_idx);
    decorate_ptr_access_chain_base_strides(&mut ctx);
    // Runs AFTER the stride decoration so the byte-buffer (PtrAccessChain) widen can read the base
    // pointer's ArrayStride to prove slot contiguity.
    rewrite_reinterpret_scalar_loads(&mut ctx, entry_idx);
    rewrite_scalar_slot_array_overindex(&mut ctx, entry_idx)?;
    remodel_workgroup_flatword_aggregate(&mut ctx, entry_idx);
    remodel_workgroup_floatarray_atomic_as_uint(&mut ctx, entry_idx)?;
    lower_private_byte_aggregate_reinterpret(&mut ctx, entry_idx)?;
    retype_demoted_copymemory_placeholder(&mut ctx, entry_idx);

    // 2h) width-normalize integer arithmetic operands. A value widened to `ulong` for a StorageBuffer
    //     access-chain index that is then reused in a 32-bit offset multiply leaves an `OpIMul %uint`
    //     with a `ulong` operand, which spirv-val rejects. This inserts a truncating `OpUConvert` for
    //     any arithmetic/bitwise operand wider than its result type — byte-neutral when widths already
    //     match, purely structural (bit width only, no name matching). Runs module-wide, last, so it
    //     repairs the mismatch regardless of which earlier lowering pass introduced it.
    normalize_int_arith_operand_widths(&mut ctx);

    // 2g) deterministic threadgroup memory: zero-fill every Workgroup variable at kernel entry
    //     (stores of OpConstantNull + one control barrier), the candidate half of the harness's
    //     defined refinement of Metal's undefined threadgroup contents. Kernel-only: Workgroup
    //     variables exist only in compute, and the barrier is only legal there.
    if matches!(stage, Stage::Kernel) {
        workgroup::zero_initialize_workgroup_memory(&mut ctx, entry_idx);
    }

    // 2f) drop blocks unreachable from the entry (orphaned constructs the structured-CFG repair's
    //     clone-then-rewire steps can leave behind). spirv-val tolerates them; SPIRV-Cross (MoltenVK)
    //     throws on them at pipeline creation. See `prune.rs`.
    prune_unreachable_blocks(&mut ctx.module);

    // 3) finalize: append synthesized globals, drop dead air.* decls, add entry point + exec modes,
    //    bump the bound.
    finalize(&mut ctx, entry_idx, &stage)?;

    Ok((ctx.module, ctx.emit_sidecar))
}