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
//! Byte-neutral responsibility split of the former monolith impl; see the parent module.

use super::*;

impl Emitter {
    pub(in crate::native::emitter) fn emit_narrowing_vector_store(
        &mut self,
        object: &TypedValue,
        ptr: &TypedValue,
        pointee: &LlType,
        instructions: &mut Vec<Instruction>,
    ) -> Result<bool, String> {
        let object_ty = self.resolve_type(&object.ty)?;
        let Some((target_ty, access_indices)) = narrowing_vector_store_target(pointee, &object_ty)
        else {
            return Ok(false);
        };
        let LlType::Vector(_, target_lanes) = target_ty else {
            return Ok(false);
        };

        let object_id = self.value_id_in(&object.value, &object.ty, instructions)?;
        let target_type = self.type_id(&target_ty)?;
        let narrowed = self.fresh();
        let mut shuffle_ops = vec![Operand::IdRef(object_id), Operand::IdRef(object_id)];
        shuffle_ops.extend((0..target_lanes).map(Operand::LiteralBit32));
        instructions.push(Self::inst(
            Op::VectorShuffle,
            Some(target_type),
            Some(narrowed),
            shuffle_ops,
        ));

        let ptr_id = self.value_id(&ptr.value, &ptr.ty)?;
        let storage = match self.resolve_type(&ptr.ty)? {
            LlType::Ptr(addrspace) => self.pointer_storage_for(&ptr.value, addrspace)?,
            other => {
                return Err(format!(
                    "native emitter: store pointer is not a pointer: {other:?}"
                ))
            }
        };
        let store_ptr = if access_indices.is_empty() {
            ptr_id
        } else {
            let ptr_ty = self.ptr_type_id(storage, &target_ty)?;
            let result = self.fresh();
            let mut ops = vec![Operand::IdRef(ptr_id)];
            for idx in access_indices {
                ops.push(Operand::IdRef(self.const_uint(idx)?));
            }
            instructions.push(Self::inst(
                Op::InBoundsAccessChain,
                Some(ptr_ty),
                Some(result),
                ops,
            ));
            result
        };
        instructions.push(Self::inst(
            Op::Store,
            None,
            None,
            vec![Operand::IdRef(store_ptr), Operand::IdRef(narrowed)],
        ));
        Ok(true)
    }

    pub(in crate::native::emitter) fn emit_widening_vector_store(
        &mut self,
        object: &TypedValue,
        ptr: &TypedValue,
        pointee: &LlType,
        instructions: &mut Vec<Instruction>,
    ) -> Result<bool, String> {
        let object_ty = self.resolve_type(&object.ty)?;
        let (
            LlType::Vector(object_elem, object_lanes),
            LlType::Vector(pointee_elem, pointee_lanes),
        ) = (&object_ty, pointee)
        else {
            return Ok(false);
        };
        if object_lanes >= pointee_lanes || !types_compatible(object_elem, pointee_elem) {
            return Ok(false);
        }

        let object_id = self.value_id_in(&object.value, &object.ty, instructions)?;
        let elem_type = self.type_id(object_elem)?;
        let mut lanes = Vec::with_capacity(*pointee_lanes as usize);
        for lane in 0..*object_lanes {
            let extracted = self.fresh();
            instructions.push(Self::inst(
                Op::CompositeExtract,
                Some(elem_type),
                Some(extracted),
                vec![Operand::IdRef(object_id), Operand::LiteralBit32(lane)],
            ));
            lanes.push(Operand::IdRef(extracted));
        }
        for _ in *object_lanes..*pointee_lanes {
            lanes.push(Operand::IdRef(self.undef_id(object_elem)?));
        }

        let widened_ty = self.type_id(pointee)?;
        let widened = self.fresh();
        instructions.push(Self::inst(
            Op::CompositeConstruct,
            Some(widened_ty),
            Some(widened),
            lanes,
        ));

        let ptr_id = self.value_id(&ptr.value, &ptr.ty)?;
        instructions.push(Self::inst(
            Op::Store,
            None,
            None,
            vec![Operand::IdRef(ptr_id), Operand::IdRef(widened)],
        ));
        Ok(true)
    }

    pub(in crate::native::emitter) fn emit_vector_to_scalar_stores(
        &mut self,
        object: &TypedValue,
        ptr: &TypedValue,
        pointee: &LlType,
        instructions: &mut Vec<Instruction>,
    ) -> Result<bool, String> {
        let object_ty = self.resolve_type(&object.ty)?;
        let LlType::Vector(elem, lanes) = object_ty else {
            return Ok(false);
        };
        let elem = self.resolve_type(&elem)?;
        if lanes == 0 {
            return Ok(false);
        }
        let store_ty = if types_compatible(pointee, &elem) {
            elem.clone()
        } else {
            let (Some(elem_bits), Some(pointee_bits)) =
                (bitcast_width(&elem), bitcast_width(pointee))
            else {
                return Ok(false);
            };
            if elem_bits != pointee_bits {
                return Ok(false);
            }
            pointee.clone()
        };
        if lanes > 1 && !self.can_emit_gep_provenance_lane_ptrs(ptr, &store_ty)? {
            return Ok(false);
        }

        let object_id = self.value_id_in(&object.value, &object.ty, instructions)?;
        let ptr_id = self.value_id(&ptr.value, &ptr.ty)?;
        let elem_ty = self.type_id(&elem)?;
        let store_ty_id = self.type_id(&store_ty)?;
        for lane in 0..lanes {
            let lane_value = self.fresh();
            instructions.push(Self::inst(
                Op::CompositeExtract,
                Some(elem_ty),
                Some(lane_value),
                vec![Operand::IdRef(object_id), Operand::LiteralBit32(lane)],
            ));
            let store_value = if types_compatible(&elem, &store_ty) {
                lane_value
            } else {
                let converted = self.fresh();
                instructions.push(Self::inst(
                    Op::Bitcast,
                    Some(store_ty_id),
                    Some(converted),
                    vec![Operand::IdRef(lane_value)],
                ));
                converted
            };
            let lane_ptr = if lane == 0 {
                ptr_id
            } else {
                self.emit_gep_provenance_lane_ptr(ptr, &store_ty, lane, instructions)?
                    .ok_or_else(|| {
                        "native emitter: vector-to-scalar store lost pointer provenance".to_string()
                    })?
            };
            instructions.push(Self::inst(
                Op::Store,
                None,
                None,
                vec![Operand::IdRef(lane_ptr), Operand::IdRef(store_value)],
            ));
        }
        Ok(true)
    }

    pub(in crate::native::emitter) fn emit_workgroup_vector_chunk_stores(
        &mut self,
        object: &TypedValue,
        ptr: &TypedValue,
        object_ty: &LlType,
        pointee: &LlType,
        instructions: &mut Vec<Instruction>,
    ) -> Result<bool, String> {
        let LlType::Vector(object_elem, object_lanes) = object_ty else {
            return Ok(false);
        };
        let LlType::Vector(_, _) = pointee else {
            return Ok(false);
        };
        let storage = match self.resolve_type(&ptr.ty)? {
            LlType::Ptr(addrspace) => self.pointer_storage_for(&ptr.value, addrspace)?,
            other => {
                return Err(format!(
                    "native emitter: store pointer is not a pointer: {other:?}"
                ))
            }
        };
        if storage != StorageClass::Workgroup {
            return Ok(false);
        }
        let (Some(object_bits), Some(object_elem_bits), Some(pointee_bits)) = (
            bitcast_width(object_ty),
            bitcast_width(object_elem),
            bitcast_width(pointee),
        ) else {
            return Ok(false);
        };
        if pointee_bits == 0
            || object_elem_bits == 0
            || object_bits % pointee_bits != 0
            || pointee_bits % object_elem_bits != 0
        {
            return Ok(false);
        }
        let chunk_count = object_bits / pointee_bits;
        let lanes_per_chunk = pointee_bits / object_elem_bits;
        if chunk_count == 0
            || lanes_per_chunk == 0
            || *object_lanes != chunk_count * lanes_per_chunk
        {
            return Ok(false);
        }

        let chunk_ty = if lanes_per_chunk == 1 {
            object_elem.as_ref().clone()
        } else {
            LlType::Vector(object_elem.clone(), lanes_per_chunk)
        };
        if bitcast_width(&chunk_ty) != Some(pointee_bits) {
            return Ok(false);
        }

        let ptr_id = self.value_id(&ptr.value, &ptr.ty)?;
        let object_id = self.value_id_in(&object.value, &object.ty, instructions)?;
        let pointee_ty = self.type_id(pointee)?;
        let ptr_ty = self.ptr_type_id(StorageClass::Workgroup, pointee)?;
        let object_elem_ty = self.type_id(object_elem)?;
        let chunk_ty_id = self.type_id(&chunk_ty)?;
        for chunk in 0..chunk_count {
            let chunk_value = if chunk_count == 1 && lanes_per_chunk == *object_lanes {
                object_id
            } else if lanes_per_chunk == 1 {
                let extracted = self.fresh();
                instructions.push(Self::inst(
                    Op::CompositeExtract,
                    Some(object_elem_ty),
                    Some(extracted),
                    vec![
                        Operand::IdRef(object_id),
                        Operand::LiteralBit32(chunk * lanes_per_chunk),
                    ],
                ));
                extracted
            } else {
                let mut lanes = Vec::with_capacity(lanes_per_chunk as usize);
                for lane in 0..lanes_per_chunk {
                    let extracted = self.fresh();
                    instructions.push(Self::inst(
                        Op::CompositeExtract,
                        Some(object_elem_ty),
                        Some(extracted),
                        vec![
                            Operand::IdRef(object_id),
                            Operand::LiteralBit32(chunk * lanes_per_chunk + lane),
                        ],
                    ));
                    lanes.push(Operand::IdRef(extracted));
                }
                let chunk_value = self.fresh();
                instructions.push(Self::inst(
                    Op::CompositeConstruct,
                    Some(chunk_ty_id),
                    Some(chunk_value),
                    lanes,
                ));
                chunk_value
            };
            let store_value = if types_compatible(&chunk_ty, pointee) {
                chunk_value
            } else {
                let cast = self.fresh();
                instructions.push(Self::inst(
                    Op::Bitcast,
                    Some(pointee_ty),
                    Some(cast),
                    vec![Operand::IdRef(chunk_value)],
                ));
                cast
            };
            let store_ptr = if chunk == 0 {
                ptr_id
            } else {
                let index = self.const_uint(chunk)?;
                let chunk_ptr = self.fresh();
                instructions.push(Self::inst(
                    Op::PtrAccessChain,
                    Some(ptr_ty),
                    Some(chunk_ptr),
                    vec![Operand::IdRef(ptr_id), Operand::IdRef(index)],
                ));
                chunk_ptr
            };
            instructions.push(Self::inst(
                Op::Store,
                None,
                None,
                vec![Operand::IdRef(store_ptr), Operand::IdRef(store_value)],
            ));
        }
        Ok(true)
    }

    pub(in crate::native::emitter) fn emit_same_width_scalar_store(
        &mut self,
        object: &TypedValue,
        ptr: &TypedValue,
        object_ty: &LlType,
        pointee: &LlType,
        instructions: &mut Vec<Instruction>,
    ) -> Result<bool, String> {
        if matches!(object_ty, LlType::Vector(_, _)) {
            return Ok(false);
        }
        let (Some(object_bits), Some(pointee_bits)) =
            (bitcast_width(object_ty), bitcast_width(pointee))
        else {
            return Ok(false);
        };
        if object_bits != pointee_bits {
            return Ok(false);
        }

        let ptr_id = self.value_id(&ptr.value, &ptr.ty)?;
        let object_id = self.value_id_in(&object.value, &object.ty, instructions)?;
        let pointee_ty = self.type_id(pointee)?;
        let stored = self.fresh();
        instructions.push(Self::inst(
            Op::Bitcast,
            Some(pointee_ty),
            Some(stored),
            vec![Operand::IdRef(object_id)],
        ));
        instructions.push(Self::inst(
            Op::Store,
            None,
            None,
            vec![Operand::IdRef(ptr_id), Operand::IdRef(stored)],
        ));
        Ok(true)
    }

    /// Reinterpret a scalar `OpStore` whose object is a NARROWER scalar than the declared pointee (e.g.
    /// `store float` into an `Int(64)` slot — a union-like local scratch reused at a smaller width):
    /// read-modify-write the slot so only its low `object_bits` bytes change. Little-endian, so writing
    /// the low bits is exactly the byte effect of a `object_bits`-wide store at the slot's address; the
    /// high bytes are preserved (load → clear low bits via `>>`/`<<` → OR in the zero-extended object
    /// bits → store), the byte-faithful equivalent of a partial store. Restricted to THREAD-LOCAL
    /// (`Function`/`Private`) slots: on a shared buffer the read-modify-write could race another thread
    /// writing the high bytes, so a shared narrowing store falls through to the raw retry (a true
    /// partial byte store, no read-back). This is the store-side counterpart of
    /// `emit_scalar_narrowing_load`.
    ///
    /// Floor-safe / byte-safe by construction: only fires on a scalar→scalar width mismatch
    /// (`object_bits < pointee_bits`) into a thread-local slot a valid module never has — every prior
    /// store handler declined and a banked module's stores all match their pointee, so it never reaches
    /// here. Returns `Ok(false)` (fall through) for a vector operand, a non-bitcastable type, a
    /// widening/same-width store, or a shared-storage pointer.
    pub(in crate::native::emitter) fn emit_scalar_narrowing_store(
        &mut self,
        object: &TypedValue,
        ptr: &TypedValue,
        object_ty: &LlType,
        pointee: &LlType,
        instructions: &mut Vec<Instruction>,
    ) -> Result<bool, String> {
        if matches!(object_ty, LlType::Vector(..)) || matches!(pointee, LlType::Vector(..)) {
            return Ok(false);
        }
        let (Some(object_bits), Some(pointee_bits)) =
            (bitcast_width(object_ty), bitcast_width(pointee))
        else {
            return Ok(false);
        };
        // Strict narrowing only: a wider object would write past the slot (a sibling the access-chain
        // rewrite owns); same-width is `emit_same_width_scalar_store` above.
        if object_bits == 0 || pointee_bits == 0 || object_bits >= pointee_bits {
            return Ok(false);
        }
        // RMW is only byte-faithful on a THREAD-LOCAL slot: on a shared (device/workgroup) buffer
        // another thread could write the high bytes between our load and store, so the read-modify-
        // write would clobber them. Restrict to Function/Private; a shared narrowing store falls
        // through to the raw retry, which models a true partial byte store with no read-back.
        let storage = match self.resolve_type(&ptr.ty)? {
            LlType::Ptr(addrspace) => self.pointer_storage_for(&ptr.value, addrspace)?,
            _ => return Ok(false),
        };
        if !matches!(storage, StorageClass::Function | StorageClass::Private) {
            return Ok(false);
        }

        let ptr_id = self.value_id(&ptr.value, &ptr.ty)?;
        let object_id = self.value_id_in(&object.value, &object.ty, instructions)?;
        let pointee_uint = LlType::Int(pointee_bits);
        let pointee_uint_type = self.type_id(&pointee_uint)?;

        // Load the slot as its same-width unsigned int and clear the low `object_bits` (>> then <<),
        // preserving the high bytes.
        let pointee_type = self.type_id(pointee)?;
        let loaded = self.fresh();
        instructions.push(Self::inst(
            Op::Load,
            Some(pointee_type),
            Some(loaded),
            vec![Operand::IdRef(ptr_id)],
        ));
        let old_uint = if *pointee == pointee_uint {
            loaded
        } else {
            let id = self.fresh();
            instructions.push(Self::inst(
                Op::Bitcast,
                Some(pointee_uint_type),
                Some(id),
                vec![Operand::IdRef(loaded)],
            ));
            id
        };
        let shift = self.const_int(pointee_bits, object_bits as u64)?;
        let shifted_down = self.fresh();
        instructions.push(Self::inst(
            Op::ShiftRightLogical,
            Some(pointee_uint_type),
            Some(shifted_down),
            vec![Operand::IdRef(old_uint), Operand::IdRef(shift)],
        ));
        let high_kept = self.fresh();
        instructions.push(Self::inst(
            Op::ShiftLeftLogical,
            Some(pointee_uint_type),
            Some(high_kept),
            vec![Operand::IdRef(shifted_down), Operand::IdRef(shift)],
        ));

        // Reinterpret the object as its same-width unsigned int and zero-extend to the slot width.
        let object_uint = LlType::Int(object_bits);
        let object_as_uint = if *object_ty == object_uint {
            object_id
        } else {
            let object_uint_type = self.type_id(&object_uint)?;
            let id = self.fresh();
            instructions.push(Self::inst(
                Op::Bitcast,
                Some(object_uint_type),
                Some(id),
                vec![Operand::IdRef(object_id)],
            ));
            id
        };
        let object_wide = self.fresh();
        instructions.push(Self::inst(
            Op::UConvert,
            Some(pointee_uint_type),
            Some(object_wide),
            vec![Operand::IdRef(object_as_uint)],
        ));

        // Combine and store back in the slot's declared type.
        let combined = self.fresh();
        instructions.push(Self::inst(
            Op::BitwiseOr,
            Some(pointee_uint_type),
            Some(combined),
            vec![Operand::IdRef(high_kept), Operand::IdRef(object_wide)],
        ));
        let stored = if *pointee == pointee_uint {
            combined
        } else {
            let id = self.fresh();
            instructions.push(Self::inst(
                Op::Bitcast,
                Some(pointee_type),
                Some(id),
                vec![Operand::IdRef(combined)],
            ));
            id
        };
        instructions.push(Self::inst(
            Op::Store,
            None,
            None,
            vec![Operand::IdRef(ptr_id), Operand::IdRef(stored)],
        ));
        Ok(true)
    }

    pub(in crate::native::emitter) fn emit_zero_scalar_to_aggregate_store(
        &mut self,
        object: &TypedValue,
        ptr: &TypedValue,
        object_ty: &LlType,
        pointee: &LlType,
        instructions: &mut Vec<Instruction>,
    ) -> Result<bool, String> {
        if !typed_value_is_zero(object) || !matches!(object_ty, LlType::Int(_)) {
            return Ok(false);
        }
        if !matches!(pointee, LlType::Array(_, _) | LlType::Struct(_)) {
            return Ok(false);
        }

        let storage_pointee = function_storage_local_type(pointee);
        let storage_object = function_storage_local_type(object_ty);
        let (pointee_size, _) = self.raw_type_size_align(&storage_pointee)?;
        let (object_size, _) = self.raw_type_size_align(&storage_object)?;
        if pointee_size != object_size {
            return Ok(false);
        }

        let ptr_id = self.value_id(&ptr.value, &ptr.ty)?;
        let zero = self.const_null(&storage_pointee)?;
        instructions.push(Self::inst(
            Op::Store,
            None,
            None,
            vec![Operand::IdRef(ptr_id), Operand::IdRef(zero)],
        ));
        Ok(true)
    }

    pub(in crate::native::emitter) fn emit_first_vector_aggregate_reinterpret_store(
        &mut self,
        object: &TypedValue,
        ptr: &TypedValue,
        object_ty: &LlType,
        pointee: &LlType,
        instructions: &mut Vec<Instruction>,
    ) -> Result<bool, String> {
        if !matches!(pointee, LlType::Array(_, _) | LlType::Struct(_)) {
            return Ok(false);
        }
        let Some((vector_ty, access_path)) = leading_vector_store_target(pointee) else {
            return Ok(false);
        };
        // The object either matches the leading vector slot, or is a SAME-TOTAL-WIDTH vector of a
        // different type (e.g. a `<2 x float>` written into a `<4 x half>` slot — 8 bytes either way):
        // a byte-identical reinterpret store, lowered with an `OpBitcast` of the object to the slot
        // type. Only a genuine reinterpret (incompatible types) takes the bitcast, so a valid module
        // (matching store) is untouched.
        let needs_bitcast = if types_compatible(&vector_ty, object_ty) {
            false
        } else {
            let vw = self.vector_total_bits(&vector_ty);
            let ow = self.vector_total_bits(object_ty);
            match (vw, ow) {
                (Some(v), Some(o)) if v != 0 && v == o => true,
                _ => return Ok(false),
            }
        };

        let storage = match self.resolve_type(&ptr.ty)? {
            LlType::Ptr(addrspace) => self.pointer_storage_for(&ptr.value, addrspace)?,
            other => {
                return Err(format!(
                    "native emitter: store pointer is not a pointer: {other:?}"
                ))
            }
        };
        let raw_object_id = self.value_id_in(&object.value, &object.ty, instructions)?;
        let object_id = if needs_bitcast {
            let vector_ty_id = self.type_id(&vector_ty)?;
            let id = self.fresh();
            instructions.push(Self::inst(
                Op::Bitcast,
                Some(vector_ty_id),
                Some(id),
                vec![Operand::IdRef(raw_object_id)],
            ));
            id
        } else {
            raw_object_id
        };
        let ptr_id = self.value_id_in(&ptr.value, &ptr.ty, instructions)?;
        let vector_ptr_ty = self.ptr_type_id(storage, &vector_ty)?;
        let vector_ptr = self.fresh();
        let mut ops = vec![Operand::IdRef(ptr_id)];
        for idx in access_path {
            ops.push(Operand::IdRef(self.const_uint(idx)?));
        }
        instructions.push(Self::inst(
            Op::InBoundsAccessChain,
            Some(vector_ptr_ty),
            Some(vector_ptr),
            ops,
        ));
        instructions.push(Self::inst(
            Op::Store,
            None,
            None,
            vec![Operand::IdRef(vector_ptr), Operand::IdRef(object_id)],
        ));
        Ok(true)
    }

    /// Reinterpret a vector `OpStore` whose object is a SAME-TOTAL-WIDTH vector of a different type than
    /// the declared pointee (e.g. `store <2 x float>` through a `<4 x half>` pointer — 8 bytes either
    /// way): `OpBitcast` the object to the pointee vector and store. Byte-identical on any target (an
    /// equal-size vector bitcast is a pure bit reinterpret). Floor-safe by construction: only fires on a
    /// vector→vector store whose types are INCOMPATIBLE but equal in total width — a valid module's
    /// stores match their pointee, so it never reaches here.
    pub(in crate::native::emitter) fn emit_same_width_vector_reinterpret_store(
        &mut self,
        object: &TypedValue,
        ptr: &TypedValue,
        object_ty: &LlType,
        pointee: &LlType,
        instructions: &mut Vec<Instruction>,
    ) -> Result<bool, String> {
        if !matches!(object_ty, LlType::Vector(..)) || !matches!(pointee, LlType::Vector(..)) {
            return Ok(false);
        }
        let (Some(ow), Some(pw)) = (
            self.vector_total_bits(object_ty),
            self.vector_total_bits(pointee),
        ) else {
            return Ok(false);
        };
        if ow == 0 || ow != pw {
            return Ok(false);
        }
        let object_id = self.value_id_in(&object.value, &object.ty, instructions)?;
        let ptr_id = self.value_id_in(&ptr.value, &ptr.ty, instructions)?;
        let pointee_ty = self.type_id(pointee)?;
        let cast = self.fresh();
        instructions.push(Self::inst(
            Op::Bitcast,
            Some(pointee_ty),
            Some(cast),
            vec![Operand::IdRef(object_id)],
        ));
        instructions.push(Self::inst(
            Op::Store,
            None,
            None,
            vec![Operand::IdRef(ptr_id), Operand::IdRef(cast)],
        ));
        Ok(true)
    }

    /// Total bit width of a vector type (`elem_bits * lanes`), or `None` if `ty` is not a vector of a
    /// bitcastable scalar. Used to decide whether two distinct vector types are a byte-identical
    /// `OpBitcast` reinterpret (equal total width).
    pub(in crate::native::emitter) fn vector_total_bits(&self, ty: &LlType) -> Option<u32> {
        match self.resolve_type(ty).ok()? {
            LlType::Vector(elem, lanes) => {
                let elem = self.resolve_type(&elem).ok()?;
                Some(bitcast_width(&elem)? * lanes)
            }
            _ => None,
        }
    }

    pub(in crate::native::emitter) fn emit_first_scalar_aggregate_reinterpret_store(
        &mut self,
        object: &TypedValue,
        ptr: &TypedValue,
        object_ty: &LlType,
        pointee: &LlType,
        instructions: &mut Vec<Instruction>,
    ) -> Result<bool, String> {
        if !matches!(pointee, LlType::Array(_, _) | LlType::Struct(_)) {
            return Ok(false);
        }
        if matches!(object_ty, LlType::Vector(_, _)) {
            return Ok(false);
        }
        let storage = match self.resolve_type(&ptr.ty)? {
            LlType::Ptr(addrspace) => self.pointer_storage_for(&ptr.value, addrspace)?,
            other => {
                return Err(format!(
                    "native emitter: store pointer is not a pointer: {other:?}"
                ))
            }
        };
        if storage != StorageClass::Workgroup
            && !(storage == StorageClass::Private && self.is_imageblock_scratch_pointer(&ptr.value))
        {
            return Ok(false);
        }
        let Some((scalar_ty, access_path)) = first_scalar_access_path(pointee) else {
            return Ok(false);
        };
        let object_id = self.value_id_in(&object.value, &object.ty, instructions)?;
        let store_value = if types_compatible(object_ty, &scalar_ty) {
            object_id
        } else {
            let Some(object_bits) = bitcast_width(object_ty) else {
                return Ok(false);
            };
            let Some(scalar_bits) = bitcast_width(&scalar_ty) else {
                return Ok(false);
            };
            if object_bits != scalar_bits {
                return Ok(false);
            }
            let scalar_type = self.type_id(&scalar_ty)?;
            let cast = self.fresh();
            instructions.push(Self::inst(
                Op::Bitcast,
                Some(scalar_type),
                Some(cast),
                vec![Operand::IdRef(object_id)],
            ));
            cast
        };

        let ptr_id = self.value_id_in(&ptr.value, &ptr.ty, instructions)?;
        let scalar_ptr_ty = self.ptr_type_id(storage, &scalar_ty)?;
        let scalar_ptr = self.fresh();
        let mut ops = vec![Operand::IdRef(ptr_id)];
        for idx in access_path {
            ops.push(Operand::IdRef(self.const_uint(idx)?));
        }
        instructions.push(Self::inst(
            Op::InBoundsAccessChain,
            Some(scalar_ptr_ty),
            Some(scalar_ptr),
            ops,
        ));
        instructions.push(Self::inst(
            Op::Store,
            None,
            None,
            vec![Operand::IdRef(scalar_ptr), Operand::IdRef(store_value)],
        ));
        Ok(true)
    }

    pub(in crate::native::emitter) fn emit_first_pointer_aggregate_reinterpret_load(
        &mut self,
        result_name: &str,
        result: Word,
        pointee: &LlType,
        result_ty: &LlType,
        ptr_value: &TypedValue,
        ptr: Word,
        instructions: &mut Vec<Instruction>,
    ) -> Result<bool, String> {
        if !matches!(pointee, LlType::Array(_, _) | LlType::Struct(_)) {
            return Ok(false);
        }
        let Some((field_ty, access_path)) = first_pointer_access_path(pointee) else {
            return Ok(false);
        };
        if &field_ty != result_ty {
            return Ok(false);
        }

        let storage = match self.resolve_type(&ptr_value.ty)? {
            LlType::Ptr(addrspace) => self.pointer_storage_for(&ptr_value.value, addrspace)?,
            other => {
                return Err(format!(
                    "native emitter: load pointer is not a pointer: {other:?}"
                ))
            }
        };
        let key = LocalPointerField {
            root: ptr,
            indices: access_path.clone(),
        };
        if storage == StorageClass::Function
            || (storage == StorageClass::Private && type_contains_pointer(pointee))
            || self.local_pointer_fields.contains_key(&key)
        {
            self.emit_pointer_from_local_field_key(
                result_name,
                result,
                result_ty,
                &key,
                instructions,
            )?;
            return Ok(true);
        }
        let field_ptr_ty = self.ptr_type_id(storage, &field_ty)?;
        let field_ptr = self.fresh();
        let mut ops = vec![Operand::IdRef(ptr)];
        for idx in access_path {
            ops.push(Operand::IdRef(self.const_uint(idx)?));
        }
        instructions.push(Self::inst(
            Op::InBoundsAccessChain,
            Some(field_ptr_ty),
            Some(field_ptr),
            ops,
        ));

        let result_type = self.type_id(result_ty)?;
        instructions.push(Self::inst(
            Op::Load,
            Some(result_type),
            Some(result),
            vec![Operand::IdRef(field_ptr)],
        ));
        if let LlType::Ptr(addrspace) = result_ty {
            self.pointer_storage
                .insert(result_name.to_string(), llvm_pointer_storage(*addrspace)?);
        }
        Ok(true)
    }

    pub(in crate::native::emitter) fn emit_first_vector_aggregate_reinterpret_load(
        &mut self,
        result: Word,
        pointee: &LlType,
        result_ty: &LlType,
        ptr_value: &TypedValue,
        ptr: Word,
        instructions: &mut Vec<Instruction>,
    ) -> Result<bool, String> {
        if !matches!(pointee, LlType::Array(_, _) | LlType::Struct(_)) {
            return Ok(false);
        }
        let Some((vector_ty, access_path)) = leading_vector_store_target(pointee) else {
            return Ok(false);
        };
        // Direct case: the leading vector IS the requested type — load it straight.
        // Reinterpret case: the leading vector has the SAME lane count and total bit width as the
        // result vector but a different (same-width) element type — load it, then `OpBitcast` to the
        // result. This covers loading e.g. `<3 x i32>` from a `[4 x <3 x float>]` local (the leading
        // `<3 x float>` element reinterpreted lane-for-lane), a same-size numeric-vector bitcast that
        // is legal in Logical addressing (unlike a pointer bitcast).
        let direct = types_compatible(&vector_ty, result_ty);
        let bitcast = !direct
            && matches!(
                (&vector_ty, result_ty),
                (LlType::Vector(_, n), LlType::Vector(_, m))
                    if n == m
                        && bitcast_width(&vector_ty).is_some()
                        && bitcast_width(&vector_ty) == bitcast_width(result_ty)
            );
        if !direct && !bitcast {
            return Ok(false);
        }

        let storage = match self.resolve_type(&ptr_value.ty)? {
            LlType::Ptr(addrspace) => self.pointer_storage_for(&ptr_value.value, addrspace)?,
            other => {
                return Err(format!(
                    "native emitter: load pointer is not a pointer: {other:?}"
                ))
            }
        };
        let vector_ptr_ty = self.ptr_type_id(storage, &vector_ty)?;
        let vector_ptr = self.fresh();
        let mut ops = vec![Operand::IdRef(ptr)];
        for idx in access_path {
            ops.push(Operand::IdRef(self.const_uint(idx)?));
        }
        instructions.push(Self::inst(
            Op::InBoundsAccessChain,
            Some(vector_ptr_ty),
            Some(vector_ptr),
            ops,
        ));

        let result_type = self.type_id(result_ty)?;
        if direct {
            instructions.push(Self::inst(
                Op::Load,
                Some(result_type),
                Some(result),
                vec![Operand::IdRef(vector_ptr)],
            ));
        } else {
            // Load the leading vector in its own type, then bitcast lane-for-lane to the result.
            let vector_type = self.type_id(&vector_ty)?;
            let loaded = self.fresh();
            instructions.push(Self::inst(
                Op::Load,
                Some(vector_type),
                Some(loaded),
                vec![Operand::IdRef(vector_ptr)],
            ));
            instructions.push(Self::inst(
                Op::Bitcast,
                Some(result_type),
                Some(result),
                vec![Operand::IdRef(loaded)],
            ));
        }
        Ok(true)
    }

    pub(in crate::native::emitter) fn emit_first_scalar_aggregate_reinterpret_load(
        &mut self,
        result: Word,
        pointee: &LlType,
        result_ty: &LlType,
        ptr_value: &TypedValue,
        ptr: Word,
        instructions: &mut Vec<Instruction>,
    ) -> Result<bool, String> {
        if !matches!(pointee, LlType::Array(_, _) | LlType::Struct(_)) {
            return Ok(false);
        }
        let Some((scalar_ty, access_path)) = first_scalar_access_path(pointee) else {
            return Ok(false);
        };
        let Some(scalar_bits) = bitcast_width(&scalar_ty) else {
            return Ok(false);
        };
        let Some(result_bits) = bitcast_width(result_ty) else {
            return Ok(false);
        };
        let int_narrowing_load =
            matches!((&scalar_ty, result_ty), (LlType::Int(src), LlType::Int(dst)) if src > dst);
        if scalar_bits != result_bits && !int_narrowing_load {
            return Ok(false);
        }

        let storage = match self.resolve_type(&ptr_value.ty)? {
            LlType::Ptr(addrspace) => self.pointer_storage_for(&ptr_value.value, addrspace)?,
            other => {
                return Err(format!(
                    "native emitter: load pointer is not a pointer: {other:?}"
                ))
            }
        };
        let scalar_ptr_ty = self.ptr_type_id(storage, &scalar_ty)?;
        let scalar_ptr = self.fresh();
        let mut ops = vec![Operand::IdRef(ptr)];
        for idx in access_path {
            ops.push(Operand::IdRef(self.const_uint(idx)?));
        }
        instructions.push(Self::inst(
            Op::InBoundsAccessChain,
            Some(scalar_ptr_ty),
            Some(scalar_ptr),
            ops,
        ));

        let scalar_type = self.type_id(&scalar_ty)?;
        if types_compatible(&scalar_ty, result_ty) {
            instructions.push(Self::inst(
                Op::Load,
                Some(scalar_type),
                Some(result),
                vec![Operand::IdRef(scalar_ptr)],
            ));
            return Ok(true);
        }

        let loaded = self.fresh();
        instructions.push(Self::inst(
            Op::Load,
            Some(scalar_type),
            Some(loaded),
            vec![Operand::IdRef(scalar_ptr)],
        ));
        let result_type = self.type_id(result_ty)?;
        if int_narrowing_load {
            instructions.push(Self::inst(
                Op::UConvert,
                Some(result_type),
                Some(result),
                vec![Operand::IdRef(loaded)],
            ));
        } else {
            instructions.push(Self::inst(
                Op::Bitcast,
                Some(result_type),
                Some(result),
                vec![Operand::IdRef(loaded)],
            ));
        }
        Ok(true)
    }
}