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

use super::*;

impl Emitter {
    /// The operand-resolved core of the `phi` handler. Driven from the parsed (unresolved) phi type + the
    /// `(value, predecessor-label)` pairs — either re-parsed by the text entry above or carried typed on
    /// `TirInst.phi_incoming`. Byte-identical either way: same `parse_phi` output, and the incoming VALUES
    /// are then overlaid from the typed graph (`phi_incoming_values`), labels kept from the pairs.
    pub(in crate::native::emitter) fn emit_phi_resolved(
        &mut self,
        name: String,
        phi_ty: &LlType,
        parsed_incoming: Vec<(LlValue, String)>,
        instructions: &mut Vec<Instruction>,
    ) -> Result<(), String> {
        let result_ty = self.resolve_type(phi_ty)?;
        // R3: source the incoming VALUES from the typed graph (labels stay from the parsed pairs).
        // Byte-identical to the parsed values by tir's phi-operand soundness; falls back on any mismatch.
        let incoming = self.phi_incoming_values(&name, parsed_incoming);
        if self.emit_raw_pointer_phi(&name, &incoming, &result_ty, instructions)? {
            return Ok(());
        }
        if self.emit_unmodeled_pointer_phi(&name, &incoming, &result_ty, instructions)? {
            return Ok(());
        }
        let pointer_meta = self.pointer_merge_meta(
            &incoming.iter().map(|(value, _)| value).collect::<Vec<_>>(),
            &result_ty,
        )?;
        let int_alignment = matches!(result_ty, LlType::Int(_))
            .then(|| self.merged_int_alignment(incoming.iter().map(|(value, _)| value.clone())));
        let pointer_provenance =
            self.emit_pointer_phi_provenance(&name, &incoming, instructions)?;
        self.emit_pointer_nullness_phi(&name, &incoming, &result_ty, instructions)?;
        let result_type = self.pointer_aware_type_id(&result_ty, pointer_meta.as_ref())?;
        let result = self.result_id(&name, &result_ty)?;
        let mut ops = Vec::new();
        let mut seen_incoming: HashMap<Word, Word> = HashMap::new();
        for (value, label) in incoming {
            let value_id = if let Some(meta) = pointer_meta.as_ref() {
                self.pointer_phi_value_id(
                    &value,
                    &result_ty,
                    meta,
                    pointer_provenance.as_ref(),
                    instructions,
                )?
            } else {
                self.phi_value_id(&value, &result_ty, instructions)?
            };
            let label_id = self.label_id(&label)?;
            if let Some(existing) = seen_incoming.insert(label_id, value_id) {
                if existing != value_id {
                    return Err(format!(
                        "native emitter: phi has multiple values from predecessor {}",
                        label
                    ));
                }
                continue;
            }
            ops.push(Operand::IdRef(value_id));
            ops.push(Operand::IdRef(label_id));
        }
        instructions.push(Self::inst(Op::Phi, Some(result_type), Some(result), ops));
        if let Some(meta) = pointer_meta {
            self.record_pointer_meta(name.clone(), meta);
        }
        if let Some(provenance) = pointer_provenance {
            self.gep_provenance.insert(name.clone(), provenance);
        }
        if let Some(alignment) = int_alignment {
            self.record_int_alignment(&name, &result_ty, alignment);
        }
        Ok(())
    }

    pub(in crate::native::emitter) fn pointer_phi_value_id(
        &mut self,
        value: &LlValue,
        result_ty: &LlType,
        meta: &PointerMeta,
        result_provenance: Option<&GepProvenance>,
        instructions: &mut Vec<Instruction>,
    ) -> Result<Word, String> {
        let Some(pointee) = meta.pointee.as_ref() else {
            return self.phi_value_id(value, result_ty, instructions);
        };
        // A `null`/`undef` incoming must carry the phi's resolved pointer type (storage + pointee), the
        // same the select path already does — otherwise it is emitted with the generic default type and
        // mismatches the phi result (the cross-storage `_ptr_UniformConstant_uchar` validation reject).
        if let Some(id) = self.typed_null_or_undef_pointer_id(value, meta.storage, pointee)? {
            return Ok(id);
        }
        let Some(template) = result_provenance else {
            if let LlValue::Local(name) = value {
                if meta.storage == StorageClass::Workgroup && self.param_values.contains(name) {
                    let ptr_ty = self.ptr_type_id(meta.storage, pointee)?;
                    let base = self.value_id(value, result_ty)?;
                    let zero = self.const_uint(0)?;
                    let result = self.fresh();
                    instructions.push(Self::inst(
                        Op::InBoundsAccessChain,
                        Some(ptr_ty),
                        Some(result),
                        vec![Operand::IdRef(base), Operand::IdRef(zero)],
                    ));
                    return Ok(result);
                }
            }
            return self.phi_value_id(value, result_ty, instructions);
        };
        let LlValue::Local(name) = value else {
            return self.phi_value_id(value, result_ty, instructions);
        };
        if self
            .values
            .get(name)
            .is_none_or(|(id, _)| *id != template.root)
        {
            return self.phi_value_id(value, result_ty, instructions);
        }
        let Some(index_ty) = template.indices.first().map(|index| index.ty.clone()) else {
            return self.phi_value_id(value, result_ty, instructions);
        };
        let Some(provenance) =
            self.provenance_for_pointer_value(value, Some(template), Some(&index_ty))?
        else {
            return self.phi_value_id(value, result_ty, instructions);
        };
        if !compatible_pointer_provenance(template, &provenance) {
            return self.phi_value_id(value, result_ty, instructions);
        }
        let ptr_ty = self.ptr_type_id(meta.storage, pointee)?;
        let result = self.fresh();
        let mut ops = vec![Operand::IdRef(provenance.root)];
        let mut indices = gep_spirv_indices(&provenance.indices)?;
        if indices.is_empty() && !provenance.indices.is_empty() {
            indices = provenance.indices.clone();
        }
        for idx in indices {
            ops.push(Operand::IdRef(self.value_id(&idx.value, &idx.ty)?));
        }
        instructions.push(Self::inst(
            Op::InBoundsAccessChain,
            Some(ptr_ty),
            Some(result),
            ops,
        ));
        Ok(result)
    }

    /// The predicate-and-operand-resolved core of the `fcmp` handler — the M-A4 graph walk drives it from
    /// `TirInst.cmp_predicate` (mapped through `fcmp_predicate`) + `TirInst.operands`, byte-identical to
    /// the text path (which sources the same predicate token + operands from the tir carrier).
    pub(in crate::native::emitter) fn emit_fcmp_resolved(
        &mut self,
        pred: Op,
        lhs: TypedValue,
        rhs: TypedValue,
        name: String,
        instructions: &mut Vec<Instruction>,
    ) -> Result<(), String> {
        let operand_ty = self.resolve_type(&lhs.ty)?;
        let result_ty = float_compare_result_type(&operand_ty)?;
        let result_type = self.type_id(&result_ty)?;
        let result = self.result_id(&name, &result_ty)?;
        let lhs_id = self.value_id_in(&lhs.value, &lhs.ty, instructions)?;
        let rhs_id = self.value_id_in(&rhs.value, &rhs.ty, instructions)?;
        instructions.push(Self::inst(
            pred,
            Some(result_type),
            Some(result),
            vec![Operand::IdRef(lhs_id), Operand::IdRef(rhs_id)],
        ));
        Ok(())
    }

    /// The POINTER core of `emit_icmp`: emit a pointer equality/inequality (direct-param constant fold,
    /// provenance compare, payload-word compare, or null compare) given the resolved predicate + typed
    /// operands. Split out so the M-A4 graph walk drives it straight from `TirInst.cmp_predicate` +
    /// `TirInst.operands` (the pointer form was the last opcode still falling through to the text
    /// substrate). `rest` is the operand TEXT, needed ONLY for the two unsupported-form error diagnostics
    /// (which BC fingerprints); the graph walk passes it from the diagnostics-only `TirInst.icmp_rest`
    /// carrier (byte-identical to the text path's `rest`), so no `inst.text` re-lex.
    pub(in crate::native::emitter) fn emit_icmp_ptr_resolved(
        &mut self,
        pred: Op,
        lhs: TypedValue,
        rhs: TypedValue,
        name: String,
        rest: &str,
        instructions: &mut Vec<Instruction>,
    ) -> Result<(), String> {
        let is_equal = match pred {
            Op::IEqual => true,
            Op::INotEqual => false,
            _ => {
                return Err(format!(
                    "native emitter: ordered pointer icmp is not supported: {rest}"
                ))
            }
        };
        if matches!((&lhs.value, &rhs.value), (LlValue::Zero, LlValue::Zero)) {
            let value = self.const_bool(is_equal)?;
            let result_ty = LlType::Bool;
            let result_type = self.type_id(&result_ty)?;
            let result = self.result_id(&name, &result_ty)?;
            instructions.push(Self::inst(
                Op::CopyObject,
                Some(result_type),
                Some(result),
                vec![Operand::IdRef(value)],
            ));
            return Ok(());
        }
        if let (LlValue::Local(lhs_name), LlValue::Local(rhs_name)) = (&lhs.value, &rhs.value) {
            if self.direct_param_values.contains(lhs_name)
                && self.direct_param_values.contains(rhs_name)
            {
                let equal = lhs_name == rhs_name;
                let value = self.const_bool(if is_equal { equal } else { !equal })?;
                let result_ty = LlType::Bool;
                let result_type = self.type_id(&result_ty)?;
                let result = self.result_id(&name, &result_ty)?;
                instructions.push(Self::inst(
                    Op::CopyObject,
                    Some(result_type),
                    Some(result),
                    vec![Operand::IdRef(value)],
                ));
                return Ok(());
            }
        }
        if self.emit_provenance_pointer_icmp(pred, &name, &lhs.value, &rhs.value, instructions)? {
            return Ok(());
        }
        if let (LlValue::Local(lhs_name), LlValue::Local(rhs_name)) = (&lhs.value, &rhs.value) {
            if let (Some((lhs_low, lhs_high)), Some((rhs_low, rhs_high))) = (
                self.pointer_payload_words.get(lhs_name).copied(),
                self.pointer_payload_words.get(rhs_name).copied(),
            ) {
                let result_ty = LlType::Bool;
                let result_type = self.type_id(&result_ty)?;
                let low_equal = self.fresh();
                instructions.push(Self::inst(
                    Op::IEqual,
                    Some(result_type),
                    Some(low_equal),
                    vec![Operand::IdRef(lhs_low), Operand::IdRef(rhs_low)],
                ));
                let high_equal = self.fresh();
                instructions.push(Self::inst(
                    Op::IEqual,
                    Some(result_type),
                    Some(high_equal),
                    vec![Operand::IdRef(lhs_high), Operand::IdRef(rhs_high)],
                ));
                let result = self.result_id(&name, &result_ty)?;
                if is_equal {
                    instructions.push(Self::inst(
                        Op::LogicalAnd,
                        Some(result_type),
                        Some(result),
                        vec![Operand::IdRef(low_equal), Operand::IdRef(high_equal)],
                    ));
                } else {
                    let equal = self.fresh();
                    instructions.push(Self::inst(
                        Op::LogicalAnd,
                        Some(result_type),
                        Some(equal),
                        vec![Operand::IdRef(low_equal), Operand::IdRef(high_equal)],
                    ));
                    instructions.push(Self::inst(
                        Op::LogicalNot,
                        Some(result_type),
                        Some(result),
                        vec![Operand::IdRef(equal)],
                    ));
                }
                return Ok(());
            }
        }
        let nullness = match (&lhs.value, &rhs.value) {
            (LlValue::Zero, value) | (value, LlValue::Zero) => {
                self.pointer_nullness_for_compare(value)?
            }
            _ => {
                return Err(format!(
                    "native emitter: pointer icmp is only supported against null: {rest}"
                ))
            }
        };
        let result_ty = LlType::Bool;
        let result_type = self.type_id(&result_ty)?;
        let result = self.result_id(&name, &result_ty)?;
        if is_equal {
            instructions.push(Self::inst(
                Op::CopyObject,
                Some(result_type),
                Some(result),
                vec![Operand::IdRef(nullness)],
            ));
        } else {
            instructions.push(Self::inst(
                Op::LogicalNot,
                Some(result_type),
                Some(result),
                vec![Operand::IdRef(nullness)],
            ));
        }
        Ok(())
    }

    /// The NON-pointer core of `emit_icmp`: emit a scalar/vector integer compare given its resolved
    /// predicate `Op`, typed operands, and (already-resolved) operand type. Extracted so the M-A4 graph
    /// walk can drive it straight from `TirInst.cmp_predicate` + `TirInst.operands` for the common
    /// (non-pointer) case — byte-identical to the text path. The POINTER icmp forms are driven the same way
    /// via `emit_icmp_ptr_resolved`, with the operand `rest` supplied from the `TirInst.icmp_rest` carrier
    /// for their error diagnostics.
    pub(in crate::native::emitter) fn emit_icmp_int_resolved(
        &mut self,
        pred: Op,
        lhs: TypedValue,
        rhs: TypedValue,
        operand_ty: LlType,
        name: String,
        instructions: &mut Vec<Instruction>,
    ) -> Result<(), String> {
        let result_ty = int_compare_result_type(&operand_ty)?;
        let result_type = self.type_id(&result_ty)?;
        let result = self.result_id(&name, &result_ty)?;
        let lhs_id = self.value_id_in(&lhs.value, &lhs.ty, instructions)?;
        let rhs_id = self.value_id_in(&rhs.value, &rhs.ty, instructions)?;
        instructions.push(Self::inst(
            pred,
            Some(result_type),
            Some(result),
            vec![Operand::IdRef(lhs_id), Operand::IdRef(rhs_id)],
        ));
        Ok(())
    }

    pub(in crate::native::emitter) fn emit_provenance_pointer_icmp(
        &mut self,
        pred: Op,
        name: &str,
        lhs: &LlValue,
        rhs: &LlValue,
        instructions: &mut Vec<Instruction>,
    ) -> Result<bool, String> {
        let Some(lhs_provenance) =
            self.normalized_pointer_icmp_provenance(lhs, name, instructions)?
        else {
            return Ok(false);
        };
        let Some(rhs_provenance) =
            self.normalized_pointer_icmp_provenance(rhs, name, instructions)?
        else {
            return Ok(false);
        };
        if !compatible_pointer_provenance(&lhs_provenance, &rhs_provenance) {
            return Ok(false);
        }
        let Some(equal) = self.emit_pointer_index_equality(
            &lhs_provenance.indices,
            &rhs_provenance.indices,
            instructions,
        )?
        else {
            return Ok(false);
        };

        let result_ty = LlType::Bool;
        let result_type = self.type_id(&result_ty)?;
        let result = self.result_id(name, &result_ty)?;
        match pred {
            Op::IEqual => instructions.push(Self::inst(
                Op::CopyObject,
                Some(result_type),
                Some(result),
                vec![Operand::IdRef(equal)],
            )),
            Op::INotEqual => instructions.push(Self::inst(
                Op::LogicalNot,
                Some(result_type),
                Some(result),
                vec![Operand::IdRef(equal)],
            )),
            _ => return Ok(false),
        }
        Ok(true)
    }

    pub(in crate::native::emitter) fn normalized_pointer_icmp_provenance(
        &mut self,
        value: &LlValue,
        name: &str,
        instructions: &mut Vec<Instruction>,
    ) -> Result<Option<GepProvenance>, String> {
        let LlValue::Local(local) = value else {
            return Ok(None);
        };
        let Some(provenance) = self.gep_provenance.get(local).cloned() else {
            return Ok(None);
        };
        self.flatten_pointer_icmp_provenance(provenance, name, instructions, 0)
            .map(Some)
    }

    pub(in crate::native::emitter) fn flatten_pointer_icmp_provenance(
        &mut self,
        provenance: GepProvenance,
        name: &str,
        instructions: &mut Vec<Instruction>,
        depth: usize,
    ) -> Result<GepProvenance, String> {
        if depth >= 8 {
            return Ok(provenance);
        }
        let Some(root_name) = self.provenance_root_local_name(provenance.root) else {
            return Ok(provenance);
        };
        let Some(root_provenance) = self.gep_provenance.get(&root_name).cloned() else {
            return Ok(provenance);
        };
        let root_provenance =
            self.flatten_pointer_icmp_provenance(root_provenance, name, instructions, depth + 1)?;
        let Some(indices) = self.compose_followup_gep(
            name,
            &root_provenance,
            &provenance.source_ty,
            &provenance.indices,
            instructions,
        )?
        else {
            return Ok(provenance);
        };
        Ok(GepProvenance {
            root: root_provenance.root,
            addrspace: root_provenance.addrspace,
            source_ty: root_provenance.source_ty,
            indices,
            root_is_indexed_container: root_provenance.root_is_indexed_container,
        })
    }

    pub(in crate::native::emitter) fn provenance_root_local_name(
        &self,
        root: Word,
    ) -> Option<String> {
        self.values.iter().find_map(|(name, (id, _))| {
            (*id == root && self.gep_provenance.contains_key(name)).then(|| name.clone())
        })
    }

    pub(in crate::native::emitter) fn emit_pointer_index_equality(
        &mut self,
        lhs: &[TypedValue],
        rhs: &[TypedValue],
        instructions: &mut Vec<Instruction>,
    ) -> Result<Option<Word>, String> {
        if lhs.len() != rhs.len() {
            return Ok(None);
        }
        let result_type = self.type_id(&LlType::Bool)?;
        let mut equal = None;
        for (lhs, rhs) in lhs.iter().zip(rhs) {
            let lhs_ty = self.resolve_type(&lhs.ty)?;
            let rhs_ty = self.resolve_type(&rhs.ty)?;
            if lhs_ty != rhs_ty || !matches!(lhs_ty, LlType::Int(_)) {
                return Ok(None);
            }
            let lhs_id = self.value_id_in(&lhs.value, &lhs.ty, instructions)?;
            let rhs_id = self.value_id_in(&rhs.value, &rhs.ty, instructions)?;
            let index_equal = if lhs_id == rhs_id {
                self.const_bool(true)?
            } else {
                let index_equal = self.fresh();
                instructions.push(Self::inst(
                    Op::IEqual,
                    Some(result_type),
                    Some(index_equal),
                    vec![Operand::IdRef(lhs_id), Operand::IdRef(rhs_id)],
                ));
                index_equal
            };
            equal = Some(if let Some(prev_equal) = equal {
                if index_equal == self.const_bool(true)? {
                    prev_equal
                } else {
                    let combined = self.fresh();
                    instructions.push(Self::inst(
                        Op::LogicalAnd,
                        Some(result_type),
                        Some(combined),
                        vec![Operand::IdRef(prev_equal), Operand::IdRef(index_equal)],
                    ));
                    combined
                }
            } else {
                index_equal
            });
        }
        Ok(Some(equal.unwrap_or(self.const_bool(true)?)))
    }
}