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

use super::*;

impl Emitter {
    pub(in crate::native::emitter) fn emit_void_air_call(
        &mut self,
        call: &LlCall,
        instructions: &mut Vec<Instruction>,
    ) -> Result<bool, String> {
        match call.callee.as_str() {
            "air.simdgroup.barrier" => {
                if call.args.len() != 2 {
                    return Err(
                        "native emitter: air.simdgroup.barrier expects 2 operands".to_string()
                    );
                }
                let scope = self.const_uint(Scope::Subgroup as u32)?;
                let semantics = self.const_uint(
                    (MemorySemantics::ACQUIRE_RELEASE | MemorySemantics::WORKGROUP_MEMORY).bits(),
                )?;
                instructions.push(Self::inst(
                    Op::ControlBarrier,
                    None,
                    None,
                    vec![
                        Operand::IdScope(scope),
                        Operand::IdScope(scope),
                        Operand::IdMemorySemantics(semantics),
                    ],
                ));
                Ok(true)
            }
            "air.wg.barrier" => {
                if call.args.len() != 2 {
                    return Err("native emitter: air.wg.barrier expects 2 operands".to_string());
                }
                let scope = self.const_uint(Scope::Workgroup as u32)?;
                let semantics = self.const_uint(
                    (MemorySemantics::ACQUIRE_RELEASE | MemorySemantics::WORKGROUP_MEMORY).bits(),
                )?;
                instructions.push(Self::inst(
                    Op::ControlBarrier,
                    None,
                    None,
                    vec![
                        Operand::IdScope(scope),
                        Operand::IdScope(scope),
                        Operand::IdMemorySemantics(semantics),
                    ],
                ));
                Ok(true)
            }
            "air.atomic.fence" => {
                if call.args.len() != 3 {
                    return Err("native emitter: air.atomic.fence expects 3 operands".to_string());
                }
                let scope_kind = match air_i32_literal(&call.args[2].value) {
                    Some(1) => Scope::Workgroup,
                    _ => Scope::Device,
                };
                let semantics_kind = if scope_kind == Scope::Workgroup {
                    MemorySemantics::ACQUIRE_RELEASE | MemorySemantics::WORKGROUP_MEMORY
                } else {
                    MemorySemantics::ACQUIRE_RELEASE
                        | MemorySemantics::UNIFORM_MEMORY
                        | MemorySemantics::CROSS_WORKGROUP_MEMORY
                };
                let scope = self.const_uint(scope_kind as u32)?;
                let semantics = self.const_uint(semantics_kind.bits())?;
                instructions.push(Self::inst(
                    Op::MemoryBarrier,
                    None,
                    None,
                    vec![
                        Operand::IdScope(scope),
                        Operand::IdMemorySemantics(semantics),
                    ],
                ));
                Ok(true)
            }
            callee if callee.starts_with("air.fence_texture") => {
                if call.args.len() != 1 {
                    return Err(format!("native emitter: {} expects 1 operand", call.callee));
                }
                let scope = self.const_uint(Scope::Device as u32)?;
                let semantics = self.const_uint(
                    (MemorySemantics::ACQUIRE_RELEASE | MemorySemantics::IMAGE_MEMORY).bits(),
                )?;
                instructions.push(Self::inst(
                    Op::MemoryBarrier,
                    None,
                    None,
                    vec![
                        Operand::IdScope(scope),
                        Operand::IdMemorySemantics(semantics),
                    ],
                ));
                Ok(true)
            }
            callee if is_coherent_air_store(callee) => {
                if call.args.len() != 2 {
                    return Err(format!(
                        "native emitter: {} expects 2 operands",
                        call.callee
                    ));
                }
                let value =
                    self.value_id_in(&call.args[0].value, &call.args[0].ty, instructions)?;
                let ptr_arg = &call.args[1];
                if let LlValue::Local(name) = &ptr_arg.value {
                    if let Some(raw) = self.raw_offsets.get(name).cloned() {
                        self.emit_raw_store(&call.args[0].ty, value, &raw, None, instructions)?;
                        return Ok(true);
                    }
                    if self.unmodeled_pointers.contains(name) {
                        return Ok(true);
                    }
                }
                let ptr = self.value_id_in(&ptr_arg.value, &ptr_arg.ty, instructions)?;
                instructions.push(Self::inst(
                    Op::Store,
                    None,
                    None,
                    vec![Operand::IdRef(ptr), Operand::IdRef(value)],
                ));
                Ok(true)
            }
            "air.atomic.local.store.i32" | "air.atomic.global.store.i32" => {
                if call.args.len() != 5 {
                    return Err(format!(
                        "native emitter: {} expects 5 operands",
                        call.callee
                    ));
                }
                let ptr = self.atomic_i32_pointer_id(&call.args[0], instructions)?;
                let value =
                    self.value_id_in(&call.args[1].value, &call.args[1].ty, instructions)?;
                let scope_kind = if call.callee == "air.atomic.global.store.i32" {
                    Scope::Device
                } else {
                    Scope::Workgroup
                };
                let scope = self.const_uint(scope_kind as u32)?;
                let semantics = self.const_uint(MemorySemantics::RELAXED.bits())?;
                instructions.push(Self::inst(
                    Op::AtomicStore,
                    None,
                    None,
                    vec![
                        Operand::IdRef(ptr),
                        Operand::IdScope(scope),
                        Operand::IdMemorySemantics(semantics),
                        Operand::IdRef(value),
                    ],
                ));
                Ok(true)
            }
            _ => Ok(false),
        }
    }

    pub(in crate::native::emitter) fn emit_value_air_call(
        &mut self,
        call: &LlCall,
        name: &str,
        instructions: &mut Vec<Instruction>,
    ) -> Result<bool, String> {
        match call.callee.as_str() {
            // `air.get_null_texture_<dim>()` yields a NULL texture handle. In the MPS NDArray
            // kernels (TopK/pooling/FFT-chirp) the result is stored into a vestigial `texture2d`
            // field of an `_MPSKernelInOut`/`MPSNDArrays` wrapper struct and forwarded to an
            // inlined helper, but NEVER sampled/read (these are pure buffer-compute kernels). Model
            // it as an unmodeled null pointer so the downstream store/forward validates; byte-safe
            // by construction because the handle is never dereferenced. Dispatches on a stable
            // `air.*` ABI symbol, not on a shader name.
            callee if callee.starts_with("air.get_null_texture") => {
                let result_ty = self.resolve_type(&call.ret)?;
                let LlType::Ptr(addrspace) = result_ty else {
                    return Err(format!(
                        "native emitter: {} returned non-pointer {result_ty:?}",
                        call.callee
                    ));
                };
                self.define_unmodeled_byte_pointer_value(name, addrspace)?;
                self.null_texture_values.insert(name.to_string());
                Ok(true)
            }
            // `air.is_null_texture_<dim>(%tex)` asks whether a texture handle is the null texture.
            // We consume it HERE only when `%tex` is a value we ourselves synthesized from
            // `air.get_null_texture_*` (tracked in `null_texture_values`): that value never crosses
            // the emitter->reparse seam as a recognizable image, so the passes-layer null tracking
            // (`ctx.null_image_values`) can't see it and would answer FALSE. A real bound texture is
            // NOT in the set, so it falls through to the default emission and the passes layer lowers
            // it exactly as before — keeping every regression case byte-identical. Dispatches on a stable
            // `air.*` ABI symbol plus our own data-flow set, never a shader name.
            callee if callee.starts_with("air.is_null_texture") => {
                if call.args.len() == 1 {
                    if let LlValue::Local(arg_name) = &call.args[0].value {
                        if self.null_texture_values.contains(arg_name) {
                            let result_ty = self.resolve_type(&call.ret)?;
                            let result_type = self.type_id(&result_ty)?;
                            let result = self.result_id(name, &result_ty)?;
                            let c = self.const_bool(true)?;
                            instructions.push(Self::inst(
                                Op::CopyObject,
                                Some(result_type),
                                Some(result),
                                vec![Operand::IdRef(c)],
                            ));
                            return Ok(true);
                        }
                    }
                }
                Ok(false)
            }
            "air.get_instance_count_instance_acceleration_structure" => {
                if call.args.len() != 1 {
                    return Err(format!("native emitter: {} expects 1 operand", call.callee));
                }
                let LlValue::Local(shadow_name) = &call.args[0].value else {
                    return Err(format!(
                        "native emitter: {} shadow operand is not SSA",
                        call.callee
                    ));
                };
                let Some(mut raw) = self.raw_offsets.get(shadow_name).cloned() else {
                    return Ok(false);
                };
                raw.const_off += crate::as_shadow::INSTANCE_COUNT_BYTE_OFFSET as i64;
                let result_ty = self.resolve_type(&call.ret)?;
                if result_ty != LlType::Int(32) {
                    return Err(format!(
                        "native emitter: {} returned {result_ty:?}, expected i32",
                        call.callee
                    ));
                }
                let result = self.result_id(name, &result_ty)?;
                self.emit_raw_load(result, &result_ty, &raw, Some(4), instructions)?;
                Ok(true)
            }
            "air.get_primitive_acceleration_structure_instance_acceleration_structure" => {
                if call.args.len() != 2 {
                    return Err(format!(
                        "native emitter: {} expects 2 operands",
                        call.callee
                    ));
                }
                let LlValue::Local(shadow_name) = &call.args[0].value else {
                    return Err(format!(
                        "native emitter: {} shadow operand is not SSA",
                        call.callee
                    ));
                };
                let Some(mut raw) = self.raw_offsets.get(shadow_name).cloned() else {
                    return Ok(false);
                };
                let result_ty = self.resolve_type(&call.ret)?;
                let LlType::Ptr(addrspace) = result_ty else {
                    return Err(format!(
                        "native emitter: {} returned {result_ty:?}, expected pointer",
                        call.callee
                    ));
                };
                raw.const_off += crate::as_shadow::CHILD_REFERENCES_BYTE_OFFSET as i64;
                raw.dyn_terms.push((
                    call.args[1].clone(),
                    crate::as_shadow::CHILD_REFERENCE_BYTE_STRIDE as i64,
                ));
                self.define_unmodeled_byte_pointer_value(name, addrspace)?;
                let (payload, is_null) =
                    self.emit_raw_pointer_payload(&raw, 0, Some(8), instructions)?;
                self.pointer_payload_words.insert(name.to_string(), payload);
                self.record_pointer_nullness(name.to_string(), is_null);
                Ok(true)
            }
            // `air.get_data_pointer_instance_acceleration_structure(%p)` returns the data pointer of an
            // instance acceleration structure. In the paravirt AS ABI (a design decision this stack
            // co-designs — see the AS-pointer-passthrough note) the instance AS's data pointer IS its
            // device address, so the intrinsic is an IDENTITY passthrough of `%p`. The argument is a
            // device pointer loaded from the instances buffer (BDA-eligible); when BDA mode has rooted
            // it at a device address, alias the result to that same device-address offset. A later
            // store of the result then copies the address verbatim and a field-offset GEP/deref reads
            // through it — exactly the plain-BDA path the 12 `store ptr addrspace(1)` cases use, with no
            // intervening tag-bit arithmetic. Byte-correct GIVEN the host rail lays out the instance AS
            // at this device address (the contract). Dispatches on a stable `air.*` ABI symbol, never a
            // shader name; outside BDA mode it returns `Ok(false)` so the default emit is untouched.
            "air.get_data_pointer_instance_acceleration_structure" => {
                if self.bda_device_pointers && call.args.len() == 1 {
                    if let LlValue::Local(arg_name) = &call.args[0].value {
                        if let Some(raw) = self.raw_offsets.get(arg_name).cloned() {
                            if raw.device_addr_base.is_some() {
                                self.used_device_address = true;
                                self.raw_offsets.insert(name.to_string(), raw);
                                self.pointer_storage
                                    .insert(name.to_string(), StorageClass::PhysicalStorageBuffer);
                                return Ok(true);
                            }
                        }
                    }
                }
                Ok(false)
            }
            callee if is_coherent_air_load(callee) => {
                if call.args.len() != 1 {
                    return Err(format!("native emitter: {} expects 1 operand", call.callee));
                }
                let result_ty = self.resolve_type(&call.ret)?;
                let result_type = self.type_id(&result_ty)?;
                let result = self.result_id(name, &result_ty)?;
                let ptr_arg = &call.args[0];
                if let LlValue::Local(ptr_name) = &ptr_arg.value {
                    if let Some(raw) = self.raw_offsets.get(ptr_name).cloned() {
                        self.emit_raw_load(result, &result_ty, &raw, None, instructions)?;
                        return Ok(true);
                    }
                    if self.unmodeled_pointers.contains(ptr_name) {
                        let zero = self.const_null(&result_ty)?;
                        instructions.push(Self::inst(
                            Op::CopyObject,
                            Some(result_type),
                            Some(result),
                            vec![Operand::IdRef(zero)],
                        ));
                        return Ok(true);
                    }
                }
                let ptr = self.value_id_in(&ptr_arg.value, &ptr_arg.ty, instructions)?;
                instructions.push(Self::inst(
                    Op::Load,
                    Some(result_type),
                    Some(result),
                    vec![Operand::IdRef(ptr)],
                ));
                Ok(true)
            }
            "air.simd_any" | "air.simd_all" => {
                if call.args.len() != 1 {
                    return Err(format!("native emitter: {} expects 1 operand", call.callee));
                }
                let result_ty = self.resolve_type(&call.ret)?;
                if !is_bool_type(&result_ty) {
                    return Err(format!(
                        "native emitter: {} returned {result_ty:?}",
                        call.callee
                    ));
                }
                let value_ty = self.resolve_type(&call.args[0].ty)?;
                if !is_bool_type(&value_ty) {
                    return Err(format!(
                        "native emitter: {} value is {value_ty:?}",
                        call.callee
                    ));
                }
                let result_type = self.type_id(&result_ty)?;
                let result = self.result_id(name, &result_ty)?;
                let value =
                    self.value_id_in(&call.args[0].value, &call.args[0].ty, instructions)?;
                let scope = self.const_uint(Scope::Subgroup as u32)?;
                let op = if call.callee == "air.simd_any" {
                    Op::GroupNonUniformAny
                } else {
                    Op::GroupNonUniformAll
                };
                instructions.push(Self::inst(
                    op,
                    Some(result_type),
                    Some(result),
                    vec![Operand::IdScope(scope), Operand::IdRef(value)],
                ));
                Ok(true)
            }
            "air.simd_ballot.i64" => {
                if call.args.len() != 1 {
                    return Err("native emitter: air.simd_ballot.i64 expects 1 operand".to_string());
                }
                let result_ty = self.resolve_type(&call.ret)?;
                if result_ty != LlType::Int(64) {
                    return Err(format!(
                        "native emitter: air.simd_ballot.i64 returned {result_ty:?}"
                    ));
                }
                let predicate_ty = self.resolve_type(&call.args[0].ty)?;
                if !is_bool_type(&predicate_ty) {
                    return Err(format!(
                        "native emitter: air.simd_ballot.i64 predicate is {predicate_ty:?}"
                    ));
                }

                let uint = self.type_id(&LlType::Int(32))?;
                let ulong = self.type_id(&LlType::Int(64))?;
                let ballot_ty = LlType::Vector(Box::new(LlType::Int(32)), 4);
                let ballot_type = self.type_id(&ballot_ty)?;
                let result = self.result_id(name, &result_ty)?;
                let predicate =
                    self.value_id_in(&call.args[0].value, &call.args[0].ty, instructions)?;
                let scope = self.const_uint(Scope::Subgroup as u32)?;

                let ballot = self.fresh();
                instructions.push(Self::inst(
                    Op::GroupNonUniformBallot,
                    Some(ballot_type),
                    Some(ballot),
                    vec![Operand::IdScope(scope), Operand::IdRef(predicate)],
                ));

                let lo32 = self.fresh();
                instructions.push(Self::inst(
                    Op::CompositeExtract,
                    Some(uint),
                    Some(lo32),
                    vec![Operand::IdRef(ballot), Operand::LiteralBit32(0)],
                ));
                let hi32 = self.fresh();
                instructions.push(Self::inst(
                    Op::CompositeExtract,
                    Some(uint),
                    Some(hi32),
                    vec![Operand::IdRef(ballot), Operand::LiteralBit32(1)],
                ));

                let lo64 = self.fresh();
                instructions.push(Self::inst(
                    Op::UConvert,
                    Some(ulong),
                    Some(lo64),
                    vec![Operand::IdRef(lo32)],
                ));
                let hi64 = self.fresh();
                instructions.push(Self::inst(
                    Op::UConvert,
                    Some(ulong),
                    Some(hi64),
                    vec![Operand::IdRef(hi32)],
                ));

                let shift = self.const_int(64, 32)?;
                let shifted_hi = self.fresh();
                instructions.push(Self::inst(
                    Op::ShiftLeftLogical,
                    Some(ulong),
                    Some(shifted_hi),
                    vec![Operand::IdRef(hi64), Operand::IdRef(shift)],
                ));
                instructions.push(Self::inst(
                    Op::BitwiseOr,
                    Some(ulong),
                    Some(result),
                    vec![Operand::IdRef(shifted_hi), Operand::IdRef(lo64)],
                ));
                Ok(true)
            }
            "air.simd_shuffle.u.i32" | "air.simd_shuffle.s.i32" => {
                if call.args.len() != 2 {
                    return Err(format!(
                        "native emitter: {} expects 2 operands",
                        call.callee
                    ));
                }
                let result_ty = self.resolve_type(&call.ret)?;
                if result_ty != LlType::Int(32) {
                    return Err(format!(
                        "native emitter: {} returned {result_ty:?}",
                        call.callee
                    ));
                }
                let value_ty = self.resolve_type(&call.args[0].ty)?;
                if value_ty != LlType::Int(32) {
                    return Err(format!(
                        "native emitter: {} value is {value_ty:?}",
                        call.callee
                    ));
                }
                let lane_ty = self.resolve_type(&call.args[1].ty)?;
                if !matches!(lane_ty, LlType::Int(_)) {
                    return Err(format!(
                        "native emitter: {} lane is {lane_ty:?}",
                        call.callee
                    ));
                }
                let result_type = self.type_id(&result_ty)?;
                let result = self.result_id(name, &result_ty)?;
                let value =
                    self.value_id_in(&call.args[0].value, &call.args[0].ty, instructions)?;
                let lane = self.value_id_in(&call.args[1].value, &call.args[1].ty, instructions)?;
                let uint = self.type_id(&LlType::Int(32))?;
                let invocation = if lane_ty == LlType::Int(32) {
                    lane
                } else {
                    let converted = self.fresh();
                    instructions.push(Self::inst(
                        Op::UConvert,
                        Some(uint),
                        Some(converted),
                        vec![Operand::IdRef(lane)],
                    ));
                    converted
                };
                let scope = self.const_uint(Scope::Subgroup as u32)?;
                instructions.push(Self::inst(
                    Op::GroupNonUniformShuffle,
                    Some(result_type),
                    Some(result),
                    vec![
                        Operand::IdScope(scope),
                        Operand::IdRef(value),
                        Operand::IdRef(invocation),
                    ],
                ));
                Ok(true)
            }
            "air.simd_broadcast_first.s.i32" | "air.simd_broadcast_first.u.i32" => {
                // Broadcast the value held by the lowest active lane to every lane of the subgroup.
                if call.args.len() != 1 {
                    return Err(format!("native emitter: {} expects 1 operand", call.callee));
                }
                let result_ty = self.resolve_type(&call.ret)?;
                if result_ty != LlType::Int(32) {
                    return Err(format!(
                        "native emitter: {} returned {result_ty:?}",
                        call.callee
                    ));
                }
                let value_ty = self.resolve_type(&call.args[0].ty)?;
                if value_ty != LlType::Int(32) {
                    return Err(format!(
                        "native emitter: {} value is {value_ty:?}",
                        call.callee
                    ));
                }
                let result_type = self.type_id(&result_ty)?;
                let result = self.result_id(name, &result_ty)?;
                let value =
                    self.value_id_in(&call.args[0].value, &call.args[0].ty, instructions)?;
                let scope = self.const_uint(Scope::Subgroup as u32)?;
                instructions.push(Self::inst(
                    Op::GroupNonUniformBroadcastFirst,
                    Some(result_type),
                    Some(result),
                    vec![Operand::IdScope(scope), Operand::IdRef(value)],
                ));
                Ok(true)
            }
            "air.atomic.local.load.i32" | "air.atomic.global.load.i32" => {
                if call.args.len() != 4 {
                    return Err(format!(
                        "native emitter: {} expects 4 operands",
                        call.callee
                    ));
                }
                let result_ty = self.resolve_type(&call.ret)?;
                if result_ty != LlType::Int(32) {
                    return Err(format!(
                        "native emitter: {} returned {result_ty:?}",
                        call.callee
                    ));
                }
                let result_type = self.type_id(&result_ty)?;
                let result = self.result_id(name, &result_ty)?;
                let ptr = self.atomic_i32_pointer_id(&call.args[0], instructions)?;
                let scope_kind = if call.callee == "air.atomic.global.load.i32" {
                    Scope::Device
                } else {
                    Scope::Workgroup
                };
                let scope = self.const_uint(scope_kind as u32)?;
                let semantics = self.const_uint(MemorySemantics::RELAXED.bits())?;
                instructions.push(Self::inst(
                    Op::AtomicLoad,
                    Some(result_type),
                    Some(result),
                    vec![
                        Operand::IdRef(ptr),
                        Operand::IdScope(scope),
                        Operand::IdMemorySemantics(semantics),
                    ],
                ));
                Ok(true)
            }
            "air.atomic.global.add.f32" => {
                if call.args.len() != 5 {
                    return Err(format!(
                        "native emitter: {} expects 5 operands",
                        call.callee
                    ));
                }
                let result_ty = self.resolve_type(&call.ret)?;
                if result_ty != LlType::Float {
                    return Err(format!(
                        "native emitter: {} returned {result_ty:?}",
                        call.callee
                    ));
                }
                self.require_capability(Capability::AtomicFloat32AddEXT);
                self.require_extension("SPV_EXT_shader_atomic_float_add");
                let result_type = self.type_id(&result_ty)?;
                let result = self.result_id(name, &result_ty)?;
                let ptr = self.atomic_f32_pointer_id(&call.args[0], instructions)?;
                let value =
                    self.value_id_in(&call.args[1].value, &call.args[1].ty, instructions)?;
                let scope = self.const_uint(Scope::Device as u32)?;
                let semantics = self.const_uint(MemorySemantics::RELAXED.bits())?;
                instructions.push(Self::inst(
                    Op::AtomicFAddEXT,
                    Some(result_type),
                    Some(result),
                    vec![
                        Operand::IdRef(ptr),
                        Operand::IdScope(scope),
                        Operand::IdMemorySemantics(semantics),
                        Operand::IdRef(value),
                    ],
                ));
                Ok(true)
            }
            "air.atomic.global.sub.f32" => {
                // SPIR-V has no atomic float subtract; an atomic fetch-sub is exactly an atomic
                // fetch-add of the negated operand (both return the prior value), so negate then
                // AtomicFAddEXT.
                if call.args.len() != 5 {
                    return Err(format!(
                        "native emitter: {} expects 5 operands",
                        call.callee
                    ));
                }
                let result_ty = self.resolve_type(&call.ret)?;
                if result_ty != LlType::Float {
                    return Err(format!(
                        "native emitter: {} returned {result_ty:?}",
                        call.callee
                    ));
                }
                self.require_capability(Capability::AtomicFloat32AddEXT);
                self.require_extension("SPV_EXT_shader_atomic_float_add");
                let result_type = self.type_id(&result_ty)?;
                let result = self.result_id(name, &result_ty)?;
                let ptr = self.atomic_f32_pointer_id(&call.args[0], instructions)?;
                let value =
                    self.value_id_in(&call.args[1].value, &call.args[1].ty, instructions)?;
                let negated = self.fresh();
                instructions.push(Self::inst(
                    Op::FNegate,
                    Some(result_type),
                    Some(negated),
                    vec![Operand::IdRef(value)],
                ));
                let scope = self.const_uint(Scope::Device as u32)?;
                let semantics = self.const_uint(MemorySemantics::RELAXED.bits())?;
                instructions.push(Self::inst(
                    Op::AtomicFAddEXT,
                    Some(result_type),
                    Some(result),
                    vec![
                        Operand::IdRef(ptr),
                        Operand::IdScope(scope),
                        Operand::IdMemorySemantics(semantics),
                        Operand::IdRef(negated),
                    ],
                ));
                Ok(true)
            }
            "air.atomic.local.cmpxchg.weak.i32" | "air.atomic.global.cmpxchg.weak.i32" => {
                if call.args.len() != 7 {
                    return Err(format!(
                        "native emitter: {} expects 7 operands",
                        call.callee
                    ));
                }
                let result_ty = self.resolve_type(&call.ret)?;
                if result_ty != LlType::Int(32) {
                    return Err(format!(
                        "native emitter: {} returned {result_ty:?}",
                        call.callee
                    ));
                }
                let compare_ptr_ty = self.resolve_type(&call.args[1].ty)?;
                if !matches!(compare_ptr_ty, LlType::Ptr(_)) {
                    return Err(format!(
                        "native emitter: {} compare operand is {compare_ptr_ty:?}",
                        call.callee
                    ));
                }
                let result_type = self.type_id(&result_ty)?;
                let result = self.result_id(name, &result_ty)?;
                let ptr = self.atomic_i32_pointer_id(&call.args[0], instructions)?;
                let compare_ptr = self.value_id(&call.args[1].value, &call.args[1].ty)?;
                let compare = self.fresh();
                instructions.push(Self::inst(
                    Op::Load,
                    Some(result_type),
                    Some(compare),
                    vec![Operand::IdRef(compare_ptr)],
                ));
                let value =
                    self.value_id_in(&call.args[2].value, &call.args[2].ty, instructions)?;
                let scope_kind = if call.callee == "air.atomic.global.cmpxchg.weak.i32" {
                    Scope::Device
                } else {
                    Scope::Workgroup
                };
                let scope = self.const_uint(scope_kind as u32)?;
                let semantics = self.const_uint(MemorySemantics::RELAXED.bits())?;
                instructions.push(Self::inst(
                    Op::AtomicCompareExchange,
                    Some(result_type),
                    Some(result),
                    vec![
                        Operand::IdRef(ptr),
                        Operand::IdScope(scope),
                        Operand::IdMemorySemantics(semantics),
                        Operand::IdMemorySemantics(semantics),
                        Operand::IdRef(value),
                        Operand::IdRef(compare),
                    ],
                ));
                instructions.push(Self::inst(
                    Op::Store,
                    None,
                    None,
                    vec![Operand::IdRef(compare_ptr), Operand::IdRef(result)],
                ));
                Ok(true)
            }
            "air.atomic.local.add.s.i32"
            | "air.atomic.local.add.u.i32"
            | "air.atomic.local.max.s.i32"
            | "air.atomic.local.max.u.i32"
            | "air.atomic.local.min.s.i32"
            | "air.atomic.local.min.u.i32"
            | "air.atomic.local.and.u.i32"
            | "air.atomic.local.or.u.i32"
            | "air.atomic.local.xchg.i32"
            | "air.atomic.global.add.s.i32"
            | "air.atomic.global.add.u.i32"
            | "air.atomic.global.and.u.i32"
            | "air.atomic.global.or.u.i32"
            | "air.atomic.global.xchg.i32"
            | "air.atomic.global.max.s.i32"
            | "air.atomic.global.max.u.i32"
            | "air.atomic.global.min.s.i32"
            | "air.atomic.global.min.u.i32"
            | "air.atomic.global.sub.u.i32" => {
                if call.args.len() != 5 {
                    return Err(format!(
                        "native emitter: {} expects 5 operands",
                        call.callee
                    ));
                }
                let result_ty = self.resolve_type(&call.ret)?;
                if result_ty != LlType::Int(32) {
                    return Err(format!(
                        "native emitter: {} returned {result_ty:?}",
                        call.callee
                    ));
                }
                let result_type = self.type_id(&result_ty)?;
                let result = self.result_id(name, &result_ty)?;
                let ptr = self.atomic_i32_pointer_id(&call.args[0], instructions)?;
                let value =
                    self.value_id_in(&call.args[1].value, &call.args[1].ty, instructions)?;
                let (scope_kind, op) = match call.callee.as_str() {
                    "air.atomic.local.add.s.i32" => (Scope::Workgroup, Op::AtomicIAdd),
                    "air.atomic.local.add.u.i32" => (Scope::Workgroup, Op::AtomicIAdd),
                    "air.atomic.local.max.s.i32" => (Scope::Workgroup, Op::AtomicSMax),
                    "air.atomic.local.max.u.i32" => (Scope::Workgroup, Op::AtomicUMax),
                    "air.atomic.local.min.s.i32" => (Scope::Workgroup, Op::AtomicSMin),
                    "air.atomic.local.min.u.i32" => (Scope::Workgroup, Op::AtomicUMin),
                    "air.atomic.local.and.u.i32" => (Scope::Workgroup, Op::AtomicAnd),
                    "air.atomic.global.add.s.i32" => (Scope::Device, Op::AtomicIAdd),
                    "air.atomic.global.add.u.i32" => (Scope::Device, Op::AtomicIAdd),
                    "air.atomic.global.and.u.i32" => (Scope::Device, Op::AtomicAnd),
                    "air.atomic.global.xchg.i32" => (Scope::Device, Op::AtomicExchange),
                    "air.atomic.global.max.s.i32" => (Scope::Device, Op::AtomicSMax),
                    "air.atomic.global.max.u.i32" => (Scope::Device, Op::AtomicUMax),
                    "air.atomic.global.min.s.i32" => (Scope::Device, Op::AtomicSMin),
                    "air.atomic.global.min.u.i32" => (Scope::Device, Op::AtomicUMin),
                    "air.atomic.global.or.u.i32" => (Scope::Device, Op::AtomicOr),
                    "air.atomic.global.sub.u.i32" => (Scope::Device, Op::AtomicISub),
                    "air.atomic.local.or.u.i32" => (Scope::Workgroup, Op::AtomicOr),
                    "air.atomic.local.xchg.i32" => (Scope::Workgroup, Op::AtomicExchange),
                    _ => (Scope::Workgroup, Op::AtomicIAdd),
                };
                let scope = self.const_uint(scope_kind as u32)?;
                let semantics = self.const_uint(MemorySemantics::RELAXED.bits())?;
                instructions.push(Self::inst(
                    op,
                    Some(result_type),
                    Some(result),
                    vec![
                        Operand::IdRef(ptr),
                        Operand::IdScope(scope),
                        Operand::IdMemorySemantics(semantics),
                        Operand::IdRef(value),
                    ],
                ));
                Ok(true)
            }
            _ => Ok(false),
        }
    }
}