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
use super::*;
use crate::native::tir::TirOperand;

fn parsed(ll: &str) -> LlModule {
    LlModule::parse_with_stage_meta(ll, None, Some("main")).expect("typed module")
}

fn function<'a>(module: &'a LlModule, name: &str) -> &'a LlFunction {
    module
        .functions
        .iter()
        .find(|function| function.name == name)
        .unwrap_or_else(|| panic!("missing function {name}"))
}

fn call_names(function: &LlFunction) -> Vec<&str> {
    function
        .carrier_insts()
        .filter_map(|instruction| instruction.call.as_ref())
        .map(|call| call.callee.as_str())
        .collect()
}

fn inline_bindings(function: &LlFunction) -> Vec<&crate::native::tir::TirInst> {
    function
        .carrier_insts()
        .filter(|instruction| instruction.opcode == "metal2vulkan.inline_parameter")
        .collect()
}

#[test]
fn inlines_the_general_one_block_leaf_axes() {
    let ll = r#"
define internal i32 @constant() {
  ret i32 7
}

define internal float @root(float %value) {
  %result = tail call float @air.sqrt(float %value)
  ret float %result
}

define internal i32 @lane(<2 x i32> %value) {
  %result = extractelement <2 x i32> %value, i64 1
  ret i32 %result
}

define void @main() {
  %slot = alloca i32
  %vector_slot = alloca <2 x i32>
  %vector = load <2 x i32>, ptr %vector_slot
  %constant = call i32 @constant()
  %root = call float @root(float 4.000000e+00)
  %lane = call i32 @lane(<2 x i32> %vector)
  %sum = add i32 %constant, %lane
  ret void
}

declare float @air.sqrt(float)
"#;
    let mut module = parsed(ll);

    let stats = module.inline_ordinary_leaf_helpers();

    assert_eq!(
        stats,
        TypedInlineStats {
            splices: 3,
            helper_instances: 3,
        }
    );
    let main = function(&module, "main");
    let calls = call_names(main);
    assert_eq!(calls, vec!["air.sqrt"], "only the declaration call remains");
    assert_eq!(
        module.functions.len(),
        1,
        "fully migrated helper bodies are pruned before emission"
    );

    let root_call = main
        .carrier_insts()
        .find(|instruction| {
            instruction
                .call
                .as_ref()
                .is_some_and(|call| call.callee == "air.sqrt")
        })
        .expect("cloned declaration call");
    assert!(matches!(
        root_call.operands.first(),
        Some(TirOperand::Value { name, ty: LlType::Float })
            if name.contains(".param.")
    ));
    assert!(inline_bindings(main).iter().any(|binding| matches!(
        binding.operands.first(),
        Some(TirOperand::Const {
            value: LlValue::Float(value),
            ty: LlType::Float,
        }) if *value == 4.0
    )));

    let sum = main
        .carrier_insts()
        .find(|instruction| instruction.result.as_deref() == Some("%sum"))
        .expect("sum");
    assert!(matches!(
        sum.operands.first(),
        Some(TirOperand::Const {
            value: LlValue::Int(7),
            ty: LlType::Int(32),
        })
    ));
}

#[test]
fn records_type_capabilities_from_pruned_functions() {
    let ll = r#"
define internal half @half_leaf(half %value) {
  ret half %value
}

define internal i64 @wide_leaf(i64 %value) {
  ret i64 %value
}

define void @main() {
  %half = call half @half_leaf(half 0xH0000)
  %wide = call i64 @wide_leaf(i64 7)
  ret void
}
"#;
    let mut module = parsed(ll);

    module.inline_ordinary_leaf_helpers();

    assert_eq!(
        module.preinlined_helper_type_capabilities,
        HashSet::from([LlTypeCapability::Float16, LlTypeCapability::Int64,])
    );

    let mut pointer_module = parsed(
        r#"
define internal void @pointer_leaf(ptr %pointer) {
  ret void
}

define void @main(ptr %pointer) {
  call void @pointer_leaf(ptr %pointer)
  ret void
}
"#,
    );
    pointer_module.inline_ordinary_leaf_helpers();
    assert_eq!(
        pointer_module.preinlined_helper_type_capabilities,
        HashSet::from([LlTypeCapability::Int8])
    );
}

#[test]
fn inlines_pointer_parameter_in_pointer_only_module() {
    let ll = r#"
define internal void @write(ptr %pointer, i32 %value) {
  store i32 %value, ptr %pointer
  ret void
}

define void @main() {
  %slot = alloca i32
  call void @write(ptr %slot, i32 9)
  ret void
}
"#;
    let mut module = parsed(ll);

    let stats = module.inline_ordinary_leaf_helpers();

    assert_eq!(
        stats,
        TypedInlineStats {
            splices: 1,
            helper_instances: 1,
        }
    );
    let main = function(&module, "main");
    assert!(call_names(main).is_empty());
    let store = main
        .carrier_insts()
        .find(|instruction| instruction.opcode == "store")
        .expect("cloned store");
    assert!(matches!(
        store.operands.as_slice(),
        [
            TirOperand::Value {
                name: value,
                ty: LlType::Int(32),
            },
            TirOperand::Value {
                name: pointer,
                ty: LlType::Ptr(0),
            },
        ] if value.contains(".param.") && pointer.contains(".param.")
    ));
}

#[test]
fn repeated_calls_share_one_helper_instance_and_keep_names_hygienic() {
    let ll = r#"
define internal i32 @add(i32 %left, i32 %right) {
  %sum = add i32 %left, %right
  ret i32 %sum
}

define void @main() {
  %slot = alloca i32
  %sum = load i32, ptr %slot
  %first = call i32 @add(i32 %sum, i32 1)
  %second = call i32 @add(i32 %first, i32 2)
  store i32 %second, ptr %slot
  ret void
}
"#;
    let mut module = parsed(ll);

    let stats = module.inline_ordinary_leaf_helpers();

    assert_eq!(
        stats,
        TypedInlineStats {
            splices: 2,
            helper_instances: 1,
        }
    );
    let main = function(&module, "main");
    assert!(call_names(main).is_empty());
    let additions = main
        .carrier_insts()
        .filter(|instruction| instruction.opcode == "add")
        .collect::<Vec<_>>();
    assert_eq!(additions.len(), 2);
    let first_result = additions[0].result.as_deref().expect("first cloned result");
    let second_result = additions[1]
        .result
        .as_deref()
        .expect("second cloned result");
    assert_ne!(first_result, second_result);
    assert!(matches!(
        additions[0].operands.first(),
        Some(TirOperand::Value { name, .. }) if name.contains(".param.")
    ));
    assert!(matches!(
        additions[1].operands.first(),
        Some(TirOperand::Value { name, .. }) if name.contains(".param.")
    ));
    let bindings = inline_bindings(main);
    assert!(bindings.iter().any(|binding| matches!(
        binding.operands.first(),
        Some(TirOperand::Value { name, .. }) if name == "%sum"
    )));
    assert!(bindings.iter().any(|binding| matches!(
        binding.operands.first(),
        Some(TirOperand::Value { name, .. }) if name == first_result
    )));
    let store = main
        .carrier_insts()
        .find(|instruction| instruction.opcode == "store")
        .expect("store");
    assert!(matches!(
        store.operands.first(),
        Some(TirOperand::Value { name, .. }) if name == second_result
    ));
}

#[test]
fn leaves_multiblock_and_other_mechanisms_residual() {
    let ll = r#"
define internal i32 @leaf(i32 %value) {
  %result = add i32 %value, 1
  ret i32 %result
}

define internal i32 @has_alloca(i32 %value) {
  %slot = alloca i32
  store i32 %value, ptr %slot
  %result = load i32, ptr %slot
  ret i32 %result
}

define internal i32 @has_bodied_callee(i32 %value) {
  %result = call i32 @leaf(i32 %value)
  ret i32 %result
}

define internal void @has_indirect(ptr %function) {
  call void %function()
  ret void
}

define internal i32 @has_cfg(i32 %value) {
entry:
  %condition = icmp eq i32 %value, 0
  br i1 %condition, label %zero, label %other
zero:
  ret i32 0
other:
  ret i32 %value
}

define void @main(ptr %function) {
  %a = call i32 @has_alloca(i32 1)
  %b = call i32 @has_bodied_callee(i32 %a)
  call void @has_indirect(ptr %function)
  %c = call i32 @has_cfg(i32 %b)
  ret void
}
"#;
    let mut module = parsed(ll);

    let stats = module.inline_ordinary_leaf_helpers();

    assert_eq!(
        stats,
        TypedInlineStats {
            splices: 1,
            helper_instances: 1,
        },
        "only the reachable one-block leaf is inlined"
    );
    assert_eq!(
        call_names(function(&module, "main")),
        vec!["has_alloca", "has_bodied_callee", "has_indirect", "has_cfg"]
    );
    assert!(
        call_names(function(&module, "has_bodied_callee")).is_empty(),
        "the leaf call inside the residual wrapper was spliced"
    );
}

#[test]
fn multiblock_entry_loop_remains_residual() {
    let ll = r#"
define internal i32 @count(i32 %limit) {
entry:
  %done = icmp eq i32 %limit, 4
  br i1 %done, label %exit, label %entry
exit:
  ret i32 %limit
}

define i32 @main() {
entry:
  %value = call i32 @count(i32 4)
  br label %merge
merge:
  %result = phi i32 [ %value, %entry ]
  ret i32 %result
}
"#;
    let mut module = parsed(ll);

    let stats = module.inline_ordinary_leaf_helpers();

    assert_eq!(stats, TypedInlineStats::default());
    let main = function(&module, "main");
    assert_eq!(call_names(main), vec!["count"]);
    assert_eq!(main.blocks.len(), 2);
}

#[test]
fn propagates_cloned_pointer_pointee_facts() {
    let ll = r#"
%Pair = type { i32, i32 }

define internal void @write_second(ptr %pair, i32 %value) {
  %field = getelementptr inbounds %Pair, ptr %pair, i64 0, i32 1
  store i32 %value, ptr %field
  ret void
}

define void @main() {
  %pair = alloca %Pair
  call void @write_second(ptr %pair, i32 5)
  ret void
}
"#;
    let mut module = parsed(ll);
    assert!(module
        .ptr_pointees
        .contains_key(&("write_second".to_string(), "%pair".to_string())));

    let stats = module.inline_ordinary_leaf_helpers();

    assert_eq!(stats.splices, 1);
    assert!(module
        .ptr_pointees
        .iter()
        .any(|((function, local), pointee)| {
            function == "main"
                && local.contains(".param.")
                && pointee == &LlType::Named("%Pair".to_string())
        }));
}

#[test]
fn conflicting_caller_and_helper_pointer_pointees_remain_residual() {
    let ll = r#"
define float @main() {
entry:
  %slot = alloca float
  store float 0.000000e+00, ptr %slot
  %value = call float @load_bits(ptr %slot)
  ret float %value
}

define internal float @load_bits(ptr %pointer) {
entry:
  %bits = load i32, ptr %pointer
  %value = bitcast i32 %bits to float
    ret float %value
}
"#;
    let mut module = parsed(ll);
    assert_eq!(
        module
            .ptr_pointees
            .get(&("load_bits".to_string(), "%pointer".to_string())),
        Some(&LlType::Int(32))
    );

    let stats = module.inline_ordinary_leaf_helpers();

    assert_eq!(stats, TypedInlineStats::default());
    assert_eq!(call_names(function(&module, "main")), vec!["load_bits"]);
}

#[test]
fn mixed_pointer_and_value_helpers_keep_pointer_helper_residual() {
    let ll = r#"
define internal void @write(ptr %pointer, i32 %value) {
  store i32 %value, ptr %pointer
  ret void
}

define internal i32 @increment(i32 %value) {
  %result = add i32 %value, 1
  ret i32 %result
}

define void @main() {
  %slot = alloca i32
  %value = call i32 @increment(i32 8)
  call void @write(ptr %slot, i32 %value)
  ret void
}
"#;
    let mut module = parsed(ll);

    let stats = module.inline_ordinary_leaf_helpers();

    assert_eq!(
        stats,
        TypedInlineStats {
            splices: 1,
            helper_instances: 1,
        }
    );
    assert_eq!(call_names(function(&module, "main")), vec!["write"]);
}

#[test]
fn residual_static_initializer_is_a_reachability_root() {
    let ll = r#"
define internal void @leaf(ptr %pointer) {
  store i32 1, ptr %pointer
  ret void
}

define internal void @_GLOBAL__sub_I_residual() {
  %slot = alloca i32
  call void @leaf(ptr %slot)
  ret void
}

define void @main() {
  ret void
}
"#;
    let mut module = parsed(ll);

    let stats = module.inline_ordinary_leaf_helpers();

    assert_eq!(
        stats,
        TypedInlineStats {
            splices: 1,
            helper_instances: 1,
        }
    );
    assert!(
        call_names(function(&module, "_GLOBAL__sub_I_residual")).is_empty(),
        "the emitter-injected constructor root must not hide its reachable leaf"
    );
}