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
use super::parse::{
    is_ignored_intrinsic, parse_declaration, parse_function, parse_global, parse_type,
    strip_comment,
};
use crate::meta::{self, AirScalar, AirType, KernRole};
use std::collections::{HashMap, HashSet};

mod alloca;
mod metadata_pointees;
mod ordinary_inline;
mod parse;
mod pointer_pointees;
mod raw_buffer;
mod static_init;

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) enum LlType {
    Void,
    Bool,
    Float,
    Half,
    BFloat,
    Int(u32),
    Ptr(u32),
    Vector(Box<LlType>, u32),
    Array(Box<LlType>, u32),
    Struct(Vec<LlType>),
    Named(String),
}

/// SPIR-V capabilities requested as a side effect of materializing an LLVM type. Early helper
/// pruning must retain these declarations: the residual SPIR-V inliner used to materialize the
/// helper first, then remove its dead types without removing module capabilities.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub(super) enum LlTypeCapability {
    Float16,
    Int8,
    Int16,
    Int64,
}

#[derive(Clone, Debug)]
pub(super) struct TypedValue {
    pub(super) ty: LlType,
    pub(super) value: LlValue,
}

#[derive(Clone, Debug)]
pub(super) enum LlValue {
    Local(String),
    Global(String),
    Bool(bool),
    Int(u64),
    SignedInt(i64),
    Hex(u64),
    Float(f64),
    HalfBits(u16),
    BFloatBits(u16),
    Vector(Vec<TypedValue>),
    Array(Vec<TypedValue>),
    Struct(Vec<TypedValue>),
    Splat(Box<TypedValue>),
    Gep(Box<LlGep>),
    Zero,
    Undef,
}

#[derive(Clone, Debug)]
pub(super) struct LlGep {
    pub(super) source_ty: LlType,
    pub(super) base: TypedValue,
    pub(super) indices: Vec<TypedValue>,
}

#[derive(Clone, Debug)]
pub(super) struct LlFunction {
    pub(super) name: String,
    pub(super) ret: LlType,
    pub(super) params: Vec<(String, LlType)>,
    /// The function's basic blocks lowered to typed carriers — the parse-once typed IR and the SOLE
    /// body substrate. `parse_function` lexes the body lines transiently and hands them to
    /// `parse_inner`, which lowers them once (via `split_body_blocks`, where the module type table is
    /// complete) into these carriers and drops the text. There is no `Vec<String>` body field: after
    /// parse, LLVM-IR text is never re-read. Empty only on the unit-test `LlFunction` constructors
    /// (which build a params-only function for `implicit_entry_block_name`).
    pub(super) blocks: Vec<crate::native::cfg::BodyBlock>,
}

impl LlFunction {
    /// Iterate every instruction of the function's typed carriers, in body order (entry block first,
    /// then each labelled block; within a block, source order). The parse-once dual of walking the flat
    /// body line-by-line: terminators and labels are not instructions, so a reader that matched only
    /// ` = <opcode>` / `store ` / call lines (never a terminator or label) sees the identical
    /// instruction SEQUENCE. Every parse-time block lowers to `Some` for a well-formed function (a block
    /// with no terminator does not lower and is a fail-visible emit error), so on the emitting workload set the
    /// walk is complete; BC is the referee.
    pub(in crate::native) fn carrier_insts(
        &self,
    ) -> impl Iterator<Item = &crate::native::tir::TirInst> {
        self.blocks
            .iter()
            .filter_map(|b| b.typed.as_ref())
            .flat_map(|t| t.insts.iter())
    }
}

#[derive(Clone, Debug)]
pub(super) struct LlDeclaration {
    pub(super) name: String,
    pub(super) ret: LlType,
    pub(super) params: Vec<LlType>,
}

#[derive(Clone, Debug)]
pub(super) struct LlGlobal {
    pub(super) name: String,
    pub(super) addrspace: u32,
    pub(super) ty: LlType,
    pub(super) initializer: Option<TypedValue>,
}

#[derive(Clone, Debug)]
pub(super) struct LlModule {
    pub(super) types: HashMap<String, LlType>,
    pub(super) functions: Vec<LlFunction>,
    pub(super) declarations: Vec<LlDeclaration>,
    pub(super) globals: Vec<LlGlobal>,
    /// Stage entry selected by the caller's parsed AIR metadata. `None` preserves the downstream
    /// transform's historical fallback to the first bodied function.
    pub(super) entry_name: Option<String>,
    /// Static initializers whose simple single-block bodies were spliced into the typed entry. The
    /// post-emit injector skips these; the emitted-graph closure owns every other shape.
    pub(super) preinlined_static_initializers: HashSet<String>,
    /// Pointer-valued loads cloned across an ordinary-helper boundary. The old SPIR-V inliner kept
    /// cross-function local-pointer-field loads opaque until its sidecar recovery ran before
    /// interface binding. Early typed inlining preserves that boundary fact so emission takes the
    /// same marker/recovery path instead of resolving the load eagerly inside the combined function.
    pub(super) preinlined_helper_pointer_loads: HashSet<String>,
    /// Type capabilities requested while emitting functions removed by the typed helper inliner.
    /// Post-emit helper cleanup prunes those functions and their dead types but retains the capability
    /// declarations, so typed pruning replays the same declaration-only side effect.
    pub(super) preinlined_helper_type_capabilities: HashSet<LlTypeCapability>,
    pub(super) entry_functions: HashSet<String>,
    pub(super) ptr_pointees: HashMap<(String, String), LlType>,
    pub(super) local_alloca_pointees: HashMap<(String, String), LlType>,
    pub(super) imageblock_data_pointee: Option<LlType>,
    pub(super) imageblock_dimensions: Option<[u32; 2]>,
    /// True when one kernel addresses more than one structural imageblock coordinate. Those calls
    /// communicate through the tile and therefore require shared Workgroup cells rather than the
    /// per-invocation scratch used by single-coordinate slice staging.
    pub(super) imageblock_shared_cells: bool,
    /// AIR entry parameter carrying `[[threads_per_threadgroup]]`, used as the row stride for a
    /// shared imageblock whose dimensions are supplied by the dispatch rather than APV metadata.
    pub(super) imageblock_threads_per_threadgroup_param: Option<String>,
    metadata_pointee_params: HashSet<(String, String)>,
    /// For each metadata-seeded pointee param, the buffer element's declared `air.arg_type_size`
    /// (the authoritative byte extent, offset-aware over any union/bitfield tail). Used to let a
    /// same-size concrete LLVM aggregate GEP `source_ty` override the metadata struct, since the
    /// re-derived `LlType::Struct` size can exceed the declared size when overlapping members are
    /// flattened.
    metadata_pointee_sizes: HashMap<(String, String), u64>,
    metadata_byte_buffer_params: HashSet<(String, String)>,
    /// Entry params declared `air.buffer` in the kernel metadata (any element type): DATA pointers,
    /// as opposed to `air.texture`/sampler args that also arrive as `ptr addrspace(1)` params.
    pub(super) metadata_data_buffer_params: HashSet<(String, String)>,
    pub(super) raw_buffer_params: HashSet<(String, String)>,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(in crate::native) struct ParamCallEdge {
    caller_func: String,
    caller_param: String,
    callee_func: String,
    callee_param: String,
}

/// Trace a pointer-valued local back to one of `f`'s pointer parameters through aliases that preserve
/// the pointed-to address.  This deliberately accepts only a single, unambiguous parameter root: a
/// cross-buffer select/phi is not one buffer alias and must not make raw-buffer marking contagious.
fn pointer_param_alias_roots(f: &LlFunction) -> HashMap<String, String> {
    let mut roots: HashMap<String, String> = f
        .params
        .iter()
        .filter(|&(_name, ty)| matches!(ty, LlType::Ptr(_)))
        .map(|(name, _ty)| (name.clone(), name.clone()))
        .collect();
    let mut changed = true;
    while changed {
        changed = false;
        for inst in f.carrier_insts() {
            if let Some((res, base)) = &inst.identity_ptr_bitcast {
                if let Some(root) = roots.get(base).cloned() {
                    if roots.insert(res.clone(), root).is_none() {
                        changed = true;
                    }
                }
                continue;
            }

            let Some(res) = &inst.result else {
                continue;
            };
            if let Some(gep) = &inst.gep {
                let LlValue::Local(base) = &gep.base.value else {
                    continue;
                };
                if let Some(root) = roots.get(base).cloned() {
                    if roots.insert(res.clone(), root).is_none() {
                        changed = true;
                    }
                }
                continue;
            }

            let common_root = |values: Vec<&LlValue>| -> Option<String> {
                let mut values = values.into_iter();
                let LlValue::Local(first) = values.next()? else {
                    return None;
                };
                let root = roots.get(first)?.clone();
                for value in values {
                    let LlValue::Local(name) = value else {
                        return None;
                    };
                    if roots.get(name) != Some(&root) {
                        return None;
                    }
                }
                Some(root)
            };
            if let Some(incoming) = &inst.phi_incoming_values {
                if let Some(root) = common_root(incoming.iter().collect()) {
                    if roots.insert(res.clone(), root).is_none() {
                        changed = true;
                    }
                }
                continue;
            }
            if let Some((true_value, false_value)) = &inst.select_arms {
                if !matches!(true_value.ty, LlType::Ptr(_))
                    || !matches!(false_value.ty, LlType::Ptr(_))
                {
                    continue;
                }
                if let Some(root) = common_root(vec![&true_value.value, &false_value.value]) {
                    if roots.insert(res.clone(), root).is_none() {
                        changed = true;
                    }
                }
            }
        }
    }
    roots
}

/// Record the primitive GEP source types reached through a cross-buffer pointer phi, keyed by each
/// entry root. Normal single-root GEP inference — including its existing select-arm path — remains
/// authoritative for every other buffer. The fallback therefore needs BOTH provenance facts: a phi
/// joining distinct roots and a downstream GEP whose concrete source type agrees with the primitive
/// metadata contract.
fn cross_buffer_pointer_phi_gep_sources(f: &LlFunction) -> HashMap<String, HashSet<LlType>> {
    let mut roots: HashMap<String, HashSet<String>> = f
        .params
        .iter()
        .filter(|&(_name, ty)| matches!(ty, LlType::Ptr(_)))
        .map(|(name, _ty)| (name.clone(), HashSet::from([name.clone()])))
        .collect();
    let mut contains_phi: HashMap<String, bool> =
        roots.keys().cloned().map(|name| (name, false)).collect();
    let mut changed = true;
    while changed {
        changed = false;
        for inst in f.carrier_insts() {
            if let Some((res, base)) = &inst.identity_ptr_bitcast {
                let Some(base_roots) = roots.get(base).cloned() else {
                    continue;
                };
                let base_has_phi = contains_phi.get(base).copied().unwrap_or(false);
                let result_roots = roots.entry(res.clone()).or_default();
                let old_len = result_roots.len();
                result_roots.extend(base_roots);
                changed |= result_roots.len() != old_len;
                if base_has_phi && !contains_phi.get(res).copied().unwrap_or(false) {
                    contains_phi.insert(res.clone(), true);
                    changed = true;
                }
                continue;
            }

            let Some(res) = &inst.result else {
                continue;
            };
            if let Some(gep) = &inst.gep {
                let LlValue::Local(base) = &gep.base.value else {
                    continue;
                };
                let Some(base_roots) = roots.get(base).cloned() else {
                    continue;
                };
                let base_has_phi = contains_phi.get(base).copied().unwrap_or(false);
                let result = res.clone();
                let result_roots = roots.entry(result.clone()).or_default();
                let old_len = result_roots.len();
                result_roots.extend(base_roots);
                changed |= result_roots.len() != old_len;
                if base_has_phi && !contains_phi.get(&result).copied().unwrap_or(false) {
                    contains_phi.insert(result, true);
                    changed = true;
                }
                continue;
            }

            let (merge_roots, result_has_phi) = if let Some(incoming) = &inst.phi_incoming_values {
                let mut merged = HashSet::new();
                let mut complete = true;
                for value in incoming {
                    let LlValue::Local(name) = value else {
                        complete = false;
                        break;
                    };
                    let Some(value_roots) = roots.get(name) else {
                        complete = false;
                        break;
                    };
                    merged.extend(value_roots.iter().cloned());
                }
                (complete.then_some(merged), true)
            } else if let Some((true_value, false_value)) = &inst.select_arms {
                if !matches!(true_value.ty, LlType::Ptr(_))
                    || !matches!(false_value.ty, LlType::Ptr(_))
                {
                    continue;
                }
                let (LlValue::Local(true_name), LlValue::Local(false_name)) =
                    (&true_value.value, &false_value.value)
                else {
                    continue;
                };
                let (Some(true_roots), Some(false_roots)) =
                    (roots.get(true_name), roots.get(false_name))
                else {
                    continue;
                };
                let mut merged = true_roots.clone();
                merged.extend(false_roots.iter().cloned());
                (
                    Some(merged),
                    contains_phi.get(true_name).copied().unwrap_or(false)
                        || contains_phi.get(false_name).copied().unwrap_or(false),
                )
            } else {
                continue;
            };

            let Some(merge_roots) = merge_roots else {
                continue;
            };
            let result = res.clone();
            let result_roots = roots.entry(result.clone()).or_default();
            let old_len = result_roots.len();
            result_roots.extend(merge_roots);
            changed |= result_roots.len() != old_len;
            if result_has_phi && !contains_phi.get(&result).copied().unwrap_or(false) {
                contains_phi.insert(result, true);
                changed = true;
            }
        }
    }

    let mut sources: HashMap<String, HashSet<LlType>> = HashMap::new();
    for inst in f.carrier_insts() {
        let Some(gep) = &inst.gep else {
            continue;
        };
        let LlValue::Local(base) = &gep.base.value else {
            continue;
        };
        if !contains_phi.get(base).copied().unwrap_or(false) {
            continue;
        }
        let Some(base_roots) = roots.get(base) else {
            continue;
        };
        if base_roots.len() < 2 {
            continue;
        }
        for root in base_roots {
            sources
                .entry(root.clone())
                .or_default()
                .insert(gep.source_ty.clone());
        }
    }
    sources
}

fn infer_entry_functions(ll: &str) -> HashSet<String> {
    ["kernel", "vertex", "fragment"]
        .into_iter()
        .filter_map(|stage| meta::entry_name(ll, stage))
        .collect()
}

fn infer_metadata_byte_buffer_params(
    kern: Option<&meta::KernMeta>,
    entry_name: Option<&str>,
    functions: &[LlFunction],
) -> HashSet<(String, String)> {
    let mut out = HashSet::new();
    let Some(kern) = kern else {
        return out;
    };
    let Some(entry_name) = entry_name else {
        return out;
    };
    let Some(entry) = functions.iter().find(|f| f.name == entry_name) else {
        return out;
    };
    for (idx, (name, ty)) in entry.params.iter().enumerate() {
        let Some(arg_type) = kern.buffer_type_name(idx as u32) else {
            continue;
        };
        if arg_type != "char" && arg_type != "void" {
            continue;
        }
        if matches!(ty, LlType::Ptr(1 | 2)) {
            out.insert((entry.name.clone(), name.clone()));
        }
    }
    out
}

fn infer_metadata_data_buffer_params(
    kern: Option<&meta::KernMeta>,
    entry_name: Option<&str>,
    functions: &[LlFunction],
) -> HashSet<(String, String)> {
    let mut out = HashSet::new();
    let Some(kern) = kern else {
        return out;
    };
    let Some(entry_name) = entry_name else {
        return out;
    };
    let Some(entry) = functions.iter().find(|f| f.name == entry_name) else {
        return out;
    };
    for (idx, (name, ty)) in entry.params.iter().enumerate() {
        if kern.buffer_type_name(idx as u32).is_none() {
            continue;
        }
        if matches!(ty, LlType::Ptr(1 | 2)) {
            out.insert((entry.name.clone(), name.clone()));
        }
    }
    out
}

fn infer_cross_coordinate_imageblock(
    functions: &[LlFunction],
    entry_functions: &HashSet<String>,
) -> bool {
    for function in functions
        .iter()
        .filter(|function| entry_functions.contains(&function.name))
    {
        let mut coordinates = HashSet::new();
        for inst in function.carrier_insts() {
            let Some(call) = &inst.alias_call else {
                continue;
            };
            if call.callee != "air.imageblock_data" {
                continue;
            }
            let Some(coordinate) = call.args.first() else {
                continue;
            };
            let key = match &coordinate.value {
                LlValue::Local(name) => format!("local:{name}"),
                LlValue::Zero => "zero".to_string(),
                LlValue::Undef => "undef".to_string(),
                other => format!("{other:?}"),
            };
            coordinates.insert(key);
            if coordinates.len() > 1 {
                return true;
            }
        }
    }
    false
}

/// Whether an entry function byte-addresses a nonzero offset from an `air.imageblock_data` result.
///
/// AIR pointers are opaque, so the parser tracks only SSA pointer provenance here: seed every
/// imageblock-data result, propagate it through identity pointer bitcasts and GEPs, then inspect a
/// byte GEP whose base is in that provenance set. This is deliberately about the decoded pointer
/// graph and constant byte offset, never an AIR identifier or workload-specific shape.
fn infer_imageblock_nonzero_byte_field(
    functions: &[LlFunction],
    entry_functions: &HashSet<String>,
) -> bool {
    for function in functions
        .iter()
        .filter(|function| entry_functions.contains(&function.name))
    {
        let mut roots = HashSet::new();
        let mut changed = true;
        while changed {
            changed = false;
            for inst in function.carrier_insts() {
                let Some(result) = &inst.result else {
                    continue;
                };
                if !result.starts_with('%') {
                    continue;
                }

                // A value call whose rhs `strip_call_prefix` chain parses to `air.imageblock_data`.
                // `alias_call` on a result-bearing line comes only from the value-call rhs path (the
                // void fallback needs a `= `-less line), so this is the reader's `strip_call_prefix(rhs)`.
                if inst
                    .alias_call
                    .as_ref()
                    .is_some_and(|call| call.callee == "air.imageblock_data")
                {
                    changed |= roots.insert(result.clone());
                    continue;
                }

                if let Some((alias, base)) = &inst.identity_ptr_bitcast {
                    if roots.contains(base) {
                        changed |= roots.insert(alias.clone());
                    }
                    continue;
                }

                let Some(gep) = &inst.gep else {
                    continue;
                };
                let LlValue::Local(base) = &gep.base.value else {
                    continue;
                };
                if !roots.contains(base) {
                    continue;
                }
                changed |= roots.insert(result.clone());
                if gep.source_ty == LlType::Int(8)
                    && gep
                        .indices
                        .iter()
                        .filter_map(typed_value_u64)
                        .any(|offset| offset != 0)
                {
                    return true;
                }
            }
        }
    }
    false
}

fn infer_apv_imageblock_dimensions(ll: &str) -> Option<[u32; 2]> {
    let root = ll.lines().find_map(|line| {
        let rest = line
            .trim()
            .strip_prefix("!apv.imageblock_dimensions = !{!")?;
        rest.strip_suffix('}')?.parse::<u32>().ok()
    })?;
    let prefix = format!("!{root} = !{{");
    let body = ll.lines().find_map(|line| {
        line.trim()
            .strip_prefix(&prefix)
            .and_then(|rest| rest.strip_suffix('}'))
    })?;
    let mut fields = body.split(',').map(str::trim);
    let width = fields.next()?.strip_prefix("i32 ")?.parse().ok()?;
    let height = fields.next()?.strip_prefix("i32 ")?.parse().ok()?;
    (fields.next().is_none() && width != 0 && height != 0).then_some([width, height])
}

fn infer_imageblock_data_pointee(
    kern: Option<&meta::KernMeta>,
    complete_cell_layout: bool,
) -> Option<LlType> {
    let kern = kern?;
    let mut pointee = None;
    for layout in kern.imageblock_layouts.values() {
        let candidate = imageblock_data_pointee_from_air_type(layout, complete_cell_layout)?;
        match &pointee {
            Some(existing) if existing != &candidate => return None,
            Some(_) => {}
            None => pointee = Some(candidate),
        }
    }
    pointee
}

/// The imageblock-data pointee depends on its proven addressing model. Complete cells are necessary
/// for APV/shared tiles and whenever AIR follows an imageblock pointer with a nonzero byte field;
/// only a single-coordinate, first-member-only scratch may collapse a metadata struct to its first
/// member.
fn imageblock_data_pointee_from_air_type(
    ty: &AirType,
    complete_cell_layout: bool,
) -> Option<LlType> {
    match ty {
        AirType::Struct(members) if complete_cell_layout && members.len() != 1 => {
            Some(ll_type_from_air_type(ty))
        }
        AirType::Struct(members) => members
            .first()
            .map(|member| ll_type_from_air_type(&member.ty)),
        _ => Some(ll_type_from_air_type(ty)),
    }
}

pub(crate) fn ll_type_from_air_type(ty: &AirType) -> LlType {
    match ty {
        AirType::Scalar(scalar) => ll_type_from_air_scalar(*scalar),
        AirType::Vec { scalar, lanes } => {
            LlType::Vector(Box::new(ll_type_from_air_scalar(*scalar)), *lanes)
        }
        AirType::PackedVec { scalar, lanes } => {
            LlType::Array(Box::new(ll_type_from_air_scalar(*scalar)), *lanes)
        }
        AirType::Array { elem, len } => LlType::Array(Box::new(ll_type_from_air_type(elem)), *len),
        AirType::Matrix { scalar, cols, rows } => LlType::Struct(vec![LlType::Array(
            Box::new(LlType::Vector(
                Box::new(ll_type_from_air_scalar(*scalar)),
                *rows,
            )),
            *cols,
        )]),
        AirType::Struct(members) => LlType::Struct(
            members
                .iter()
                .map(|member| ll_type_from_air_type(&member.ty))
                .collect(),
        ),
    }
}

fn ll_type_from_air_scalar(scalar: AirScalar) -> LlType {
    match scalar {
        AirScalar::Float => LlType::Float,
        AirScalar::Half => LlType::Half,
        AirScalar::UInt | AirScalar::SInt => LlType::Int(32),
        AirScalar::ULong | AirScalar::SLong => LlType::Int(64),
        AirScalar::UShort | AirScalar::SShort => LlType::Int(16),
        AirScalar::UChar | AirScalar::Bool => LlType::Int(8),
    }
}

fn is_ignored_global(line: &str) -> bool {
    let t = line.trim_start();
    t.starts_with("@llvm.global_ctors")
        || t.starts_with("@llvm.global_dtors")
        || t.starts_with("@llvm.used")
        || t.starts_with("@llvm.compiler.used")
}

fn typed_value_u64(value: &TypedValue) -> Option<u64> {
    match value.value {
        LlValue::Int(value) | LlValue::Hex(value) => Some(value),
        LlValue::SignedInt(value) if value >= 0 => Some(value as u64),
        _ => None,
    }
}

pub(crate) fn round_up_u64(value: u64, align: u64) -> u64 {
    if align <= 1 {
        value
    } else {
        value.div_ceil(align) * align
    }
}

#[cfg(test)]
mod layout_abi_tests {
    //! Layout/ABI table + cross-calculator differential (refactor T3). The crate has several
    //! independent size/align calculators that agree on scalars but DIVERGE by design on vector
    //! padding; Phase 2/S4 will fold them into one `layout(ty, LayoutRule)` oracle, and these tests
    //! are the contract that fold must preserve. Each calculator here is one `LayoutRule` variant:
    //!
    //! - `type_storage_size_align`         → "Native"     (packed-style: vec3 = 12/4)
    //! - `native_memcpy_type_size_align`   → "Memcpy"     (vec3 padded to 4 lanes: 16/16)
    //! - `air_metadata_type_size_align`    → "AirMetadata" (packed/unpacked distinguished by type)
    //!
    //! The passes-layer std140 calculator (`stage_input::layout::ty_size_align`, a THIRD rule where
    //! vec3 = 12/16) is pinned in that module's own tests.
    use super::*;
    use crate::meta::{AirMember, AirScalar, AirType};

    fn module() -> LlModule {
        LlModule::parse("define void @k() {\nentry:\n  ret void\n}\n")
            .expect("minimal module parses")
    }

    fn vec(elem: LlType, lanes: u32) -> LlType {
        LlType::Vector(Box::new(elem), lanes)
    }

    #[test]
    fn scalar_sizes_match_metal_abi() {
        let m = module();
        for (ty, sz) in [
            (LlType::Bool, 1),
            (LlType::Int(8), 1),
            (LlType::Int(16), 2),
            (LlType::Half, 2),
            (LlType::BFloat, 2),
            (LlType::Int(32), 4),
            (LlType::Float, 4),
            (LlType::Int(64), 8),
            (LlType::Ptr(1), 8),
        ] {
            assert_eq!(
                m.scalar_storage_size(&ty),
                Some(sz),
                "scalar size of {ty:?}"
            );
        }
        // non-scalars have no scalar-storage size
        assert_eq!(m.scalar_storage_size(&vec(LlType::Float, 4)), None);
    }

    #[test]
    fn native_rule_is_packed_style() {
        // "Native" (`type_storage_size_align`): tight packing, align = element align (no vec growth).
        let m = module();
        assert_eq!(m.type_storage_size_align(&LlType::Float), Some((4, 4)));
        assert_eq!(m.type_storage_size_align(&LlType::Half), Some((2, 2)));
        assert_eq!(
            m.type_storage_size_align(&vec(LlType::Float, 2)),
            Some((8, 4))
        );
        assert_eq!(
            m.type_storage_size_align(&vec(LlType::Float, 3)),
            Some((12, 4))
        ); // packed_float3
        assert_eq!(
            m.type_storage_size_align(&vec(LlType::Float, 4)),
            Some((16, 4))
        );
        // odd-width int rounds up to whole bytes, align = next_pow2 capped at 8
        assert_eq!(m.type_storage_size_align(&LlType::Int(24)), Some((3, 4)));
        assert_eq!(m.type_storage_size_align(&LlType::Int(1)), Some((1, 1)));
        // array of scalars: tight
        assert_eq!(
            m.type_storage_size_align(&LlType::Array(Box::new(LlType::Float), 3)),
            Some((12, 4))
        );
        // struct { i8, float }: i8@0, float@4 (align 4), total 8
        assert_eq!(
            m.type_storage_size_align(&LlType::Struct(vec![LlType::Int(8), LlType::Float])),
            Some((8, 4))
        );
    }

    #[test]
    fn memcpy_rule_pads_vec3_to_four_lanes() {
        // "Memcpy" (`native_memcpy_type_size_align`): a vec3 occupies 4 lanes and self-aligns.
        let m = module();
        assert_eq!(
            m.native_memcpy_type_size_align(&LlType::Float),
            Some((4, 4))
        );
        assert_eq!(
            m.native_memcpy_type_size_align(&vec(LlType::Float, 2)),
            Some((8, 8))
        );
        assert_eq!(
            m.native_memcpy_type_size_align(&vec(LlType::Float, 3)),
            Some((16, 16))
        ); // float3
        assert_eq!(
            m.native_memcpy_type_size_align(&vec(LlType::Float, 4)),
            Some((16, 16))
        );
        // array of vec3 strides at the padded 16 and floors align at the element's 16
        assert_eq!(
            m.native_memcpy_type_size_align(&LlType::Array(Box::new(vec(LlType::Float, 3)), 2)),
            Some((32, 16))
        );
        // array of scalars floors align at 4
        assert_eq!(
            m.native_memcpy_type_size_align(&LlType::Array(Box::new(LlType::Float), 3)),
            Some((12, 4))
        );
    }

    #[test]
    fn air_metadata_rule_distinguishes_packed_from_unpacked() {
        // "AirMetadata" (`air_metadata_type_size_align`): the metadata type system carries the
        // packed-vs-unpacked distinction the LLVM `LlType` can't express.
        let m = module();
        assert_eq!(
            m.air_metadata_type_size_align(&AirType::Scalar(AirScalar::Float)),
            Some((4, 4))
        );
        assert_eq!(
            m.air_metadata_type_size_align(&AirType::Vec {
                scalar: AirScalar::Float,
                lanes: 3
            }),
            Some((16, 16)) // float3
        );
        assert_eq!(
            m.air_metadata_type_size_align(&AirType::PackedVec {
                scalar: AirScalar::Float,
                lanes: 3
            }),
            Some((12, 4)) // packed_float3
        );
        assert_eq!(
            m.air_metadata_type_size_align(&AirType::Struct(vec![
                AirMember {
                    offset: 0,
                    ty: AirType::Scalar(AirScalar::UChar)
                },
                AirMember {
                    offset: 4,
                    ty: AirType::Scalar(AirScalar::Float)
                },
            ])),
            Some((8, 4))
        );
    }

    #[test]
    fn differential_calculators_agree_on_scalars_and_scalar_aggregates() {
        // Where the layout rules MUST agree, S4's oracle unification must keep them equal.
        let m = module();
        let agree = [
            LlType::Bool,
            LlType::Int(8),
            LlType::Int(16),
            LlType::Int(32),
            LlType::Int(64),
            LlType::Half,
            LlType::Float,
            LlType::Ptr(1),
            LlType::Array(Box::new(LlType::Float), 3),
            LlType::Struct(vec![LlType::Int(8), LlType::Float]),
            LlType::Struct(vec![LlType::Float, LlType::Float, LlType::Int(32)]),
        ];
        for ty in agree {
            assert_eq!(
                m.type_storage_size_align(&ty),
                m.native_memcpy_type_size_align(&ty),
                "Native and Memcpy rules must agree on {ty:?}"
            );
        }
    }

    #[test]
    fn differential_calculators_diverge_on_vectors_by_design() {
        // The known, intentional divergence — captured so a future "why do these disagree?" reads
        // as documented behaviour, not a regression. vec3 is the canonical packed/unpacked split.
        let m = module();
        for lanes in [2u32, 3, 4] {
            let ty = vec(LlType::Float, lanes);
            let native = m.type_storage_size_align(&ty).unwrap();
            let memcpy = m.native_memcpy_type_size_align(&ty).unwrap();
            assert_ne!(
                native, memcpy,
                "Native vs Memcpy are expected to differ for float{lanes} (packed vs padded)"
            );
        }
        // spelled out for the canonical case
        assert_eq!(
            m.type_storage_size_align(&vec(LlType::Float, 3)),
            Some((12, 4))
        );
        assert_eq!(
            m.native_memcpy_type_size_align(&vec(LlType::Float, 3)),
            Some((16, 16))
        );
    }
}

#[cfg(test)]
mod resolve_known_type_tests {
    //! `resolve_known_type` canonicalizes an `LlType` against the module's named-type table — the
    //! primitive the M2 pointee-carrier census (`native::tir_pointee_check`) applies to BOTH sides
    //! before `operand_type_compatible`, so a carrier `Named("%struct._half8")` and an emitter
    //! `Struct([Array(Half,8)])` are recognized as the SAME type instead of being over-counted as a
    //! divergence (the reconciliation set an M2 consumer must settle). These pin that canonicalization.
    use super::*;

    fn module_with_types(air: &str) -> LlModule {
        LlModule::parse(air).expect("fixture parses")
    }

    #[test]
    fn named_struct_alias_resolves_to_structural_definition() {
        let m = module_with_types(concat!(
            "%struct._half8 = type { [8 x half] }\n",
            "define void @k() {\nentry:\n  ret void\n}\n",
        ));
        let named = LlType::Named("%struct._half8".to_string());
        let structural = LlType::Struct(vec![LlType::Array(Box::new(LlType::Half), 8)]);
        // The carrier holds the Named alias, the emitter the structural form: post-resolve they are
        // the same, so the census must classify this as agreement, not divergence.
        assert_eq!(m.resolve_known_type(&named), structural);
        assert_eq!(m.resolve_known_type(&structural), structural);
    }

    #[test]
    fn i1_and_single_lane_vector_canonicalize() {
        let m = module_with_types("define void @k() {\nentry:\n  ret void\n}\n");
        assert_eq!(m.resolve_known_type(&LlType::Int(1)), LlType::Bool);
        assert_eq!(
            m.resolve_known_type(&LlType::Vector(Box::new(LlType::Float), 1)),
            LlType::Float
        );
    }

    #[test]
    fn unknown_named_type_is_left_as_is() {
        let m = module_with_types("define void @k() {\nentry:\n  ret void\n}\n");
        let named = LlType::Named("%struct.absent".to_string());
        assert_eq!(m.resolve_known_type(&named), named);
    }
}

#[cfg(test)]
mod raw_buffer_inference_tests {
    //! Direct unit coverage for the `infer_raw_buffer_params` fixpoint (refactor T6 gap-list: one
    //! of the "highest-complexity zero-test" inference surfaces). `LlModule::parse` runs the whole
    //! inference chain, so each fixture parses AIR that exercises one raw-buffer signal and asserts
    //! the resulting `raw_buffer_params` set (keyed by `(function-name, param-name)`). A param is a
    //! "raw byte buffer" when the body reinterprets it away from its declared element type — the
    //! signal the passes layer needs to wrap it as `RuntimeArray<uchar>` rather than a typed buffer.
    use super::*;

    fn params(air: &str) -> HashSet<(String, String)> {
        LlModule::parse(air)
            .expect("fixture parses")
            .raw_buffer_params
    }

    #[test]
    fn byte_gep_then_wide_load_marks_param_raw() {
        // `%p = gep i8, %buf` makes %p a byte-alias of the param; a non-i8 load through %p is a
        // byte→wider reinterpret, so %buf is a raw byte buffer.
        let air = concat!(
            "define void @k(ptr addrspace(1) %buf) {\n",
            "entry:\n",
            "  %p = getelementptr i8, ptr addrspace(1) %buf, i64 0\n",
            "  %v = load i32, ptr addrspace(1) %p\n",
            "  ret void\n",
            "}\n",
        );
        assert!(params(air).contains(&("k".to_string(), "%buf".to_string())));
    }

    #[test]
    fn two_distinct_typed_loads_mark_param_raw() {
        // Loading the same param as two different (non-wrapper) types means no single typed view
        // fits it — it is accessed as raw bytes reinterpreted per-load.
        let air = concat!(
            "define void @k(ptr addrspace(1) %buf) {\n",
            "entry:\n",
            "  %a = load float, ptr addrspace(1) %buf\n",
            "  %b = load i32, ptr addrspace(1) %buf\n",
            "  ret void\n",
            "}\n",
        );
        assert!(params(air).contains(&("k".to_string(), "%buf".to_string())));
    }

    #[test]
    fn single_typed_load_leaves_param_typed() {
        // A param read only ever as one consistent scalar type has a valid typed view — NOT raw.
        let air = concat!(
            "define void @k(ptr addrspace(1) %buf) {\n",
            "entry:\n",
            "  %v = load float, ptr addrspace(1) %buf\n",
            "  ret void\n",
            "}\n",
        );
        assert!(!params(air).contains(&("k".to_string(), "%buf".to_string())));
    }

    #[test]
    fn non_pointer_param_is_never_raw() {
        // Only addrspace pointer params are candidates; a by-value scalar param cannot be a buffer.
        let air = concat!(
            "define void @k(i32 %n) {\n",
            "entry:\n",
            "  ret void\n",
            "}\n",
        );
        assert!(params(air).is_empty());
    }

    #[test]
    fn raw_buffer_mark_reaches_helper_through_byte_gep_alias() {
        // `%alias` is not a direct entry parameter: it is a byte-offset GEP followed by an identity
        // pointer bitcast.  The entry's byte→i32 read proves `%buf` is raw, so the same buffer object
        // must make the helper's `%p` raw before helper inlining exposes its float accesses.
        let air = concat!(
            "define void @entry(ptr addrspace(1) %buf) {\n",
            "entry:\n",
            "  %byte = getelementptr i8, ptr addrspace(1) %buf, i64 4\n",
            "  %word = load i32, ptr addrspace(1) %byte\n",
            "  %alias = bitcast ptr addrspace(1) %byte to ptr addrspace(1)\n",
            "  call void @helper(ptr addrspace(1) %alias)\n",
            "  ret void\n",
            "}\n",
            "define void @helper(ptr addrspace(1) %p) {\n",
            "entry:\n",
            "  %f = getelementptr float, ptr addrspace(1) %p, i64 0\n",
            "  %v = load float, ptr addrspace(1) %f\n",
            "  ret void\n",
            "}\n",
        );
        let raw = params(air);
        assert!(raw.contains(&("entry".to_string(), "%buf".to_string())));
        assert!(raw.contains(&("helper".to_string(), "%p".to_string())));
    }

    #[test]
    fn helper_raw_mark_reaches_entry_through_byte_gep_alias() {
        // The reverse direction matters too: helper-local byte reinterpretation must make the entry
        // buffer raw when the call receives a GEP alias rather than `%buf` directly. That preserves
        // the dynamic byte offset after helper inlining.
        let air = concat!(
            "define void @entry(ptr addrspace(1) %buf) {\n",
            "entry:\n",
            "  %alias = getelementptr i8, ptr addrspace(1) %buf, i64 4\n",
            "  call void @helper(ptr addrspace(1) %alias)\n",
            "  ret void\n",
            "}\n",
            "define void @helper(ptr addrspace(1) %p) {\n",
            "entry:\n",
            "  %byte = getelementptr i8, ptr addrspace(1) %p, i64 0\n",
            "  %word = load i32, ptr addrspace(1) %byte\n",
            "  ret void\n",
            "}\n",
        );
        let raw = params(air);
        assert!(raw.contains(&("entry".to_string(), "%buf".to_string())));
        assert!(raw.contains(&("helper".to_string(), "%p".to_string())));
    }

    #[test]
    fn raw_buffer_mark_does_not_cross_select_between_parameters() {
        // A select across two entry buffers is not an address-preserving alias of either one.  Even
        // though `%a` is raw, the helper parameter must remain unmarked: otherwise a byte view of one
        // resource would make an unrelated resource's typed accesses use the raw model.
        let air = concat!(
            "define void @entry(ptr addrspace(1) %a, ptr addrspace(1) %b, i1 %cond) {\n",
            "entry:\n",
            "  %byte = getelementptr i8, ptr addrspace(1) %a, i64 4\n",
            "  %word = load i32, ptr addrspace(1) %byte\n",
            "  %merged = select i1 %cond, ptr addrspace(1) %a, ptr addrspace(1) %b\n",
            "  call void @helper(ptr addrspace(1) %merged)\n",
            "  ret void\n",
            "}\n",
            "define void @helper(ptr addrspace(1) %p) {\n",
            "entry:\n",
            "  %f = getelementptr float, ptr addrspace(1) %p, i64 0\n",
            "  %v = load float, ptr addrspace(1) %f\n",
            "  ret void\n",
            "}\n",
        );
        let raw = params(air);
        assert!(raw.contains(&("entry".to_string(), "%a".to_string())));
        assert!(!raw.contains(&("entry".to_string(), "%b".to_string())));
        assert!(!raw.contains(&("helper".to_string(), "%p".to_string())));
    }
}

#[cfg(test)]
mod pointee_inference_tests {
    //! Direct unit coverage for the two remaining "highest-complexity zero-test" pointee-inference
    //! fixpoints (§6 gap-list): `infer_pointer_pointees` (what element type a pointer PARAM is
    //! indexed as) and `infer_local_alloca_pointees` (what same-size type a local ALLOCA is
    //! reinterpreted as). `LlModule::parse` runs both; each fixture parses AIR and asserts the
    //! resulting map keyed by `(function-name, value-name)`.
    use super::*;

    fn parsed(air: &str) -> LlModule {
        LlModule::parse(air).expect("fixture parses")
    }

    #[test]
    fn param_gep_source_becomes_pointer_pointee() {
        // A pointer param indexed as `float` records `float` as its pointee element type.
        let air = concat!(
            "define void @k(ptr addrspace(1) %buf) {\n",
            "entry:\n",
            "  %p = getelementptr float, ptr addrspace(1) %buf, i64 0\n",
            "  ret void\n",
            "}\n",
        );
        assert_eq!(
            parsed(air)
                .ptr_pointees
                .get(&("k".to_string(), "%buf".to_string())),
            Some(&LlType::Float)
        );
    }

    #[test]
    fn first_gep_source_wins_for_pointer_pointee() {
        // Distinct GEP element types off the same param: the first seen is retained (or_insert).
        let air = concat!(
            "define void @k(ptr addrspace(1) %buf) {\n",
            "entry:\n",
            "  %a = getelementptr float, ptr addrspace(1) %buf, i64 0\n",
            "  %b = getelementptr i32, ptr addrspace(1) %buf, i64 4\n",
            "  ret void\n",
            "}\n",
        );
        assert_eq!(
            parsed(air)
                .ptr_pointees
                .get(&("k".to_string(), "%buf".to_string())),
            Some(&LlType::Float)
        );
    }

    #[test]
    fn unindexed_param_has_no_pointee() {
        // A pointer param never used as a GEP base gets no inferred pointee.
        let air = concat!(
            "define void @k(ptr addrspace(1) %buf) {\n",
            "entry:\n",
            "  ret void\n",
            "}\n",
        );
        assert!(parsed(air).ptr_pointees.is_empty());
    }

    #[test]
    fn primitive_metadata_seeds_cross_buffer_phi_roots_without_direct_geps() {
        // The only float GEP is below a phi joining distinct buffers, so the single-root GEP pass
        // cannot attribute it to either input. `air.arg_type_name` + the declared four-byte extent
        // remain authoritative and seed both otherwise-untyped buffer roots.
        let air = r#"
define void @k(ptr addrspace(1) %a, ptr addrspace(1) %b, ptr addrspace(1) %select_a, ptr addrspace(1) %select_b, i1 %cond) {
entry:
  %select = select i1 %cond, ptr addrspace(1) %select_a, ptr addrspace(1) %select_b
  br i1 %cond, label %left, label %right
left:
  br label %join
right:
  br label %join
join:
  %merged = phi ptr addrspace(1) [ %a, %left ], [ %b, %right ]
  %p = getelementptr float, ptr addrspace(1) %merged, i64 0
  ret void
}
!air.kernel = !{!0}
!0 = !{ptr @k, !1, !2}
!1 = !{}
!2 = !{!3, !4, !5, !6}
!3 = !{i32 0, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"float", !"air.arg_name", !"a"}
!4 = !{i32 1, !"air.buffer", !"air.location_index", i32 1, i32 1, !"air.read", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"float", !"air.arg_name", !"b"}
!5 = !{i32 2, !"air.buffer", !"air.location_index", i32 2, i32 1, !"air.read", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"float", !"air.arg_name", !"select_a"}
!6 = !{i32 3, !"air.buffer", !"air.location_index", i32 3, i32 1, !"air.read", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"float", !"air.arg_name", !"select_b"}
"#;
        let unseeded = parsed(air);
        assert!(!unseeded
            .ptr_pointees
            .contains_key(&("k".to_string(), "%a".to_string())));
        assert!(!unseeded
            .ptr_pointees
            .contains_key(&("k".to_string(), "%b".to_string())));
        let module = LlModule::parse_with_primitive_phi_metadata(air).expect("fixture parses");
        assert_eq!(
            module
                .ptr_pointees
                .get(&("k".to_string(), "%a".to_string())),
            Some(&LlType::Float)
        );
        assert_eq!(
            module
                .ptr_pointees
                .get(&("k".to_string(), "%b".to_string())),
            Some(&LlType::Float)
        );
        assert!(!module
            .ptr_pointees
            .contains_key(&("k".to_string(), "%select_a".to_string())));
        assert!(!module
            .ptr_pointees
            .contains_key(&("k".to_string(), "%select_b".to_string())));
    }

    #[test]
    fn same_size_gep_reinterpret_records_alloca_pointee() {
        // An i32 alloca indexed as `float` (same 4-byte size, no pointers) is a reinterpret: the
        // alloca's pointee becomes `float`.
        let air = concat!(
            "define void @k() {\n",
            "entry:\n",
            "  %a = alloca i32\n",
            "  %p = getelementptr float, ptr %a, i64 0\n",
            "  ret void\n",
            "}\n",
        );
        assert_eq!(
            parsed(air)
                .local_alloca_pointees
                .get(&("k".to_string(), "%a".to_string())),
            Some(&LlType::Float)
        );
    }

    #[test]
    fn same_type_gep_leaves_alloca_unreinterpreted() {
        // Indexing an i32 alloca as i32 is not a reinterpret (candidate == original is filtered).
        let air = concat!(
            "define void @k() {\n",
            "entry:\n",
            "  %a = alloca i32\n",
            "  %p = getelementptr i32, ptr %a, i64 0\n",
            "  ret void\n",
            "}\n",
        );
        assert!(parsed(air).local_alloca_pointees.is_empty());
    }
}