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
use super::*;
use crate::native::cfg::BodyBlock;
use crate::native::tir::{RetEmit, TirTerminator};

struct StaticInitializer {
    ordinal: usize,
    name: String,
    blocks: Vec<BodyBlock>,
}

fn terminator_targets(term: &TirTerminator, target: &str) -> bool {
    match term {
        TirTerminator::Br(label) => label == target,
        TirTerminator::BrCond { t, f, .. } => t == target || f == target,
        TirTerminator::Switch { default, cases, .. } => {
            default == target || cases.iter().any(|(_, label)| label == target)
        }
        TirTerminator::Ret(_) | TirTerminator::Unreachable => false,
    }
}

fn eligible_static_initializer(function: &LlFunction) -> Option<Vec<BodyBlock>> {
    if !function.params.is_empty() {
        return None;
    }
    let carriers = function
        .blocks
        .iter()
        .map(|block| block.typed.as_ref())
        .collect::<Option<Vec<_>>>()?;
    if carriers.iter().any(|block| {
        block.insts.iter().any(|instruction| {
            instruction.opcode == "alloca"
                || (matches!(
                    instruction.opcode.as_str(),
                    "call" | "tail" | "musttail" | "notail"
                ) && instruction.call.is_none())
        })
    }) {
        return None;
    }
    let returns = carriers
        .iter()
        .filter(|block| {
            matches!(block.terminator, TirTerminator::Ret(None))
                && matches!(block.ret, RetEmit::Void)
        })
        .count();
    if returns != 1
        || carriers.iter().any(|block| {
            matches!(
                block.terminator,
                TirTerminator::Ret(Some(_)) | TirTerminator::Unreachable
            )
        })
    {
        return None;
    }
    if carriers.len() > 1 {
        let entry = &carriers[0].label;
        if carriers
            .iter()
            .any(|block| terminator_targets(&block.terminator, entry))
        {
            return None;
        }
    }
    Some(function.blocks.clone())
}

impl LlModule {
    /// Inline the one-block suffix of AIR static initializers into the typed entry.
    ///
    /// Multi-block bodies retain their independently emitted CFG and join the producer's complete
    /// typed SPIR-V call-graph closure immediately before serialization. Keeping this early
    /// transform straight-line-only avoids combining caller/callee inference domains before
    /// emission.
    pub(in crate::native) fn inline_simple_static_initializers(&mut self) {
        let Some(entry_name) = self
            .entry_name
            .clone()
            .or_else(|| self.functions.first().map(|function| function.name.clone()))
        else {
            return;
        };
        let Some(entry_index) = self
            .functions
            .iter()
            .position(|function| function.name == entry_name)
        else {
            return;
        };
        if self.functions[entry_index]
            .blocks
            .first()
            .and_then(|block| block.typed.as_ref())
            .is_none()
        {
            return;
        }

        let initializers = self
            .functions
            .iter()
            .enumerate()
            .filter(|(_, function)| {
                function.name != entry_name && function.name.starts_with("_GLOBAL__sub_I")
            })
            .map(|(ordinal, function)| {
                (
                    ordinal,
                    function.name.clone(),
                    (function.blocks.len() == 1)
                        .then(|| eligible_static_initializer(function))
                        .flatten(),
                )
            })
            .collect::<Vec<_>>();
        // Residual calls are injected at entry start. Therefore only an eligible SUFFIX can move
        // ahead of the seam without reordering around an earlier/later callful constructor.
        let suffix_start = initializers
            .iter()
            .rposition(|(_, _, blocks)| blocks.is_none())
            .map_or(0, |index| index + 1);
        let initializers = initializers
            .into_iter()
            .skip(suffix_start)
            .filter_map(|(ordinal, name, blocks)| {
                blocks.map(|blocks| StaticInitializer {
                    ordinal,
                    name,
                    blocks,
                })
            })
            .collect::<Vec<_>>();
        if initializers.is_empty() {
            return;
        }

        let source_pointees = self.ptr_pointees.clone();
        let mut pointees = Vec::new();
        let mut preinlined = Vec::new();
        let entry = &mut self.functions[entry_index];
        let mut cursor = 0;
        for StaticInitializer {
            ordinal,
            name,
            mut blocks,
        } in initializers
        {
            let mut rename = blocks
                .iter()
                .filter_map(|block| block.typed.as_ref())
                .flat_map(|block| block.insts.iter())
                .filter_map(|instruction| instruction.result.as_ref())
                .map(|result| {
                    (
                        result.clone(),
                        format!(
                            "%metal2vulkan.static_init.{ordinal}.{}",
                            result.trim_start_matches('%')
                        ),
                    )
                })
                .collect::<HashMap<_, _>>();
            for block in &blocks {
                rename.insert(
                    block.name.clone(),
                    format!(
                        "%metal2vulkan.static_init.{ordinal}.block.{}",
                        block.name.trim_start_matches('%')
                    ),
                );
            }
            for block in &mut blocks {
                let typed = block
                    .typed
                    .as_mut()
                    .expect("eligible static initializer has typed blocks");
                typed.rename(&rename);
                block.name = typed.label.clone();
            }
            for ((function, local), pointee) in &source_pointees {
                if function == &name {
                    if let Some(renamed) = rename.get(local) {
                        pointees.push(((entry_name.clone(), renamed.clone()), pointee.clone()));
                    }
                }
            }
            preinlined.push(name);

            let block = blocks[0]
                .typed
                .as_mut()
                .expect("eligible static initializer has a typed block");
            let inserted = block.insts.len();
            entry.blocks[0]
                .typed
                .as_mut()
                .expect("entry carrier checked before static-initializer mutation")
                .insts
                .splice(cursor..cursor, block.insts.drain(..));
            cursor += inserted;
        }

        self.ptr_pointees.extend(pointees);
        self.preinlined_static_initializers.extend(preinlined);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn static_initializer_suffix_carries_direct_calls_in_source_order() {
        let ll = r#"
@a = internal addrspace(2) global i8 0

define internal void @_GLOBAL__sub_I_leaf() {
  store i8 1, ptr addrspace(2) @a
  ret void
}

define internal void @_GLOBAL__sub_I_callful() {
  call void @air.test()
  ret void
}

define void @main() {
  store i8 2, ptr addrspace(2) @a
  ret void
}

declare void @air.test()
"#;
        let mut module =
            LlModule::parse_with_stage_meta(ll, None, Some("main")).expect("typed module");
        module.inline_simple_static_initializers();
        assert_eq!(module.preinlined_static_initializers.len(), 2);
        let entry = module
            .functions
            .iter()
            .find(|function| function.name == "main")
            .expect("entry");
        assert_eq!(
            entry
                .carrier_insts()
                .map(|instruction| instruction.opcode.as_str())
                .collect::<Vec<_>>(),
            ["store", "call", "store"],
            "direct-call initializer must retain source order in the typed suffix"
        );
    }

    #[test]
    fn static_initializer_three_block_cfg_remains_independent_until_emitted_closure() {
        let ll = r#"
@a = internal addrspace(2) global i8 0

define internal void @_GLOBAL__sub_I_before() {
  store i8 1, ptr addrspace(2) @a
  ret void
}

define internal void @_GLOBAL__sub_I_cfg() {
entry:
  %c = icmp eq i8 0, 0
  br i1 %c, label %then, label %merge
then:
  store i8 2, ptr addrspace(2) @a
  br label %merge
merge:
  %v = phi i8 [ 3, %entry ], [ 4, %then ]
  store i8 %v, ptr addrspace(2) @a
  ret void
}

define internal void @_GLOBAL__sub_I_after() {
  store i8 5, ptr addrspace(2) @a
  ret void
}

define void @main() {
  store i8 9, ptr addrspace(2) @a
  ret void
}
"#;
        let mut module =
            LlModule::parse_with_stage_meta(ll, None, Some("main")).expect("typed module");
        module.inline_simple_static_initializers();
        assert_eq!(
            module.preinlined_static_initializers,
            HashSet::from(["_GLOBAL__sub_I_after".to_string()]),
            "only the straight-line suffix crosses the pre-emission inference boundary"
        );
        let entry = module
            .functions
            .iter()
            .find(|function| function.name == "main")
            .expect("entry");
        assert_eq!(
            entry
                .carrier_insts()
                .map(|instruction| instruction.opcode.as_str())
                .collect::<Vec<_>>(),
            ["store", "store"],
            "the post-CFG straight-line constructor stays before the entry body"
        );
    }

    #[test]
    fn static_initializer_two_block_cfg_remains_independent_until_emitted_closure() {
        let ll = r#"
@a = internal addrspace(2) global i8 0

define internal void @_GLOBAL__sub_I_cfg() {
entry:
  br label %exit
exit:
  store i8 1, ptr addrspace(2) @a
  ret void
}

define void @main() {
  store i8 9, ptr addrspace(2) @a
  ret void
}
"#;
        let mut module =
            LlModule::parse_with_stage_meta(ll, None, Some("main")).expect("typed module");
        module.inline_simple_static_initializers();
        assert!(module.preinlined_static_initializers.is_empty());
        let entry = module
            .functions
            .iter()
            .find(|function| function.name == "main")
            .expect("entry");
        assert_eq!(entry.blocks.len(), 1);
    }

    #[test]
    fn static_initializer_chained_cfgs_remain_independent_until_emitted_closure() {
        let ll = r#"
define internal void @_GLOBAL__sub_I_first() {
entry:
  %c = icmp eq i8 0, 0
  br i1 %c, label %then, label %merge
then:
  br label %merge
merge:
  ret void
}

define internal void @_GLOBAL__sub_I_second() {
entry:
  %c = icmp eq i8 0, 0
  br i1 %c, label %then, label %merge
then:
  br label %merge
merge:
  ret void
}

define void @main() {
  ret void
}
"#;
        let mut module =
            LlModule::parse_with_stage_meta(ll, None, Some("main")).expect("typed module");
        module.inline_simple_static_initializers();
        assert!(
            module.preinlined_static_initializers.is_empty(),
            "constructor CFGs retain their independent pre-emission domains"
        );
        let entry = module
            .functions
            .iter()
            .find(|function| function.name == "main")
            .expect("entry");
        assert_eq!(entry.blocks.len(), 1);
    }

    #[test]
    fn static_initializer_suffix_leaves_indirect_calls_residual() {
        let ll = r#"
@a = internal addrspace(2) global i8 0

define internal void @_GLOBAL__sub_I_indirect() {
  %fn = inttoptr i64 0 to ptr
  call void %fn()
  ret void
}

define void @main() {
  store i8 2, ptr addrspace(2) @a
  ret void
}
"#;
        let mut module =
            LlModule::parse_with_stage_meta(ll, None, Some("main")).expect("typed module");
        module.inline_simple_static_initializers();
        assert!(
            module.preinlined_static_initializers.is_empty(),
            "indirect-call constructor must remain for the emitted-graph closure"
        );
        let entry = module
            .functions
            .iter()
            .find(|function| function.name == "main")
            .expect("entry");
        assert_eq!(entry.carrier_insts().count(), 1);
    }
}