zust-vm-spirv 0.9.4

SPIR-V code generation backend for the Zust scripting language.
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
use super::*;

#[test]
fn compiles_simple_arithmetic_kernel() {
    let kernel = compile_source(
        br#"
            pub fn add(a: i32, b: i32) {
                a + b
            }
            "#,
        "test",
        "add",
    )
    .unwrap();

    assert_eq!(kernel.entry.as_str(), "main");
    assert_eq!(kernel.arg_tys, vec![Type::I32, Type::I32]);
    assert_eq!(kernel.ret_ty, Type::I32);
    assert!(!kernel.spirv.words().is_empty());
    assert!(kernel.spirv.disassemble().contains("OpEntryPoint GLCompute"));
}

#[test]
fn compiles_workgroup_static_and_atomic_task_counter() {
    let kernel = compile_source_with_workgroup_size(
        br#"
            static task_mgr: u32;

            pub fn main(buf: Vec<u32>) {
                let local = spirv::local_id();
                if local[0] == 0u32 {
                    task_mgr = 0u32;
                }
                spirv::barrier();
                let idx = task_mgr.atomic_add();
                buf[idx] = local[0];
            }
            "#,
        "workgroup_static_ref",
        "main",
        [4, 1, 1],
    )
    .unwrap();

    let asm = kernel.spirv.disassemble();
    assert!(asm.contains("Workgroup"));
    assert!(asm.contains("OpAtomicIAdd"));
    assert!(asm.contains("OpControlBarrier"));
}

#[test]
fn compiles_reference_mandelbrot_escape_control_flow() {
    let kernel = compile_source(
        br#"
            pub fn escape(x: f64, y: f64, max_iter: u32) {
                let iter = 0u32;
                let zx = 0.0f64;
                let zy = 0.0f64;
                while iter < max_iter {
                    let zx2 = zx * zx;
                    let zy2 = zy * zy;
                    if zx2 + zy2 > 4.0f64 {
                        break;
                    }
                    let tmp = zx2 - zy2 + x;
                    zy = 2.0f64 * zx * zy + y;
                    zx = tmp;
                    iter += 1u32;
                }
                return iter;
            }
            "#,
        "mandelbrot_ref",
        "escape",
    )
    .unwrap();

    let asm = kernel.spirv.disassemble();
    assert_eq!(kernel.arg_tys, vec![Type::F64, Type::F64, Type::U32]);
    assert_eq!(kernel.ret_ty, Type::U32);
    assert!(asm.contains("OpLoopMerge"));
    assert!(asm.contains("OpPhi"));
}

#[test]
fn compiles_escape_calling_external_module_function() {
    let kernel = compile_source_with_externs(
        br#"
            pub fn escape(x: f64, y: f64, max_iter: u32) {
                let iter = 0u32;
                let zx = 0.0f64;
                let zy = 0.0f64;
                while iter < max_iter {
                    let zx2 = zx * zx;
                    let zy2 = zy * zy;
                    if math::sqrt(zx2 + zy2) > 2.0f64 {
                        break;
                    }
                    let tmp = zx2 - zy2 + x;
                    zy = 2.0f64 * zx * zy + y;
                    zx = tmp;
                    iter += 1u32;
                }
                return iter;
            }
            "#,
        "mandelbrot_ext_ref",
        "escape",
        spirv_builtins().into_iter().chain([ExternalFn::glsl_unary("math::sqrt", Type::F64, Type::F64, spirv::GlslStd450Op::Sqrt, None)]),
    )
    .unwrap();

    let asm = kernel.spirv.disassemble();
    assert_eq!(kernel.ret_ty, Type::U32);
    assert!(asm.contains("GLSL.std.450"));
    assert!(asm.contains("Sqrt"));
    assert!(asm.contains("OpLoopMerge"));
}

#[test]
fn compiles_spirv_group_and_local_id_builtins() {
    let kernel = compile_source(
        br#"
            pub fn coord() {
                let group = spirv::group_id();
                let local = spirv::local_id();
                return group[0] * 32u32 + local[0];
            }
            "#,
        "spirv_builtin_ref",
        "coord",
    )
    .unwrap();

    let asm = kernel.spirv.disassemble();
    assert_eq!(kernel.ret_ty, Type::U32);
    assert!(asm.contains("BuiltIn WorkgroupId"));
    assert!(asm.contains("BuiltIn LocalInvocationId"));
    assert!(asm.contains("OpCompositeExtract"));
}

#[test]
fn compiles_spirv_barrier_builtin() {
    let kernel = compile_source(
        br#"
            pub fn sync_then_value() {
                spirv::barrier();
                return 1u32;
            }
            "#,
        "spirv_barrier_ref",
        "sync_then_value",
    )
    .unwrap();

    let asm = kernel.spirv.disassemble();
    assert_eq!(kernel.ret_ty, Type::U32);
    assert!(asm.contains("OpControlBarrier"));
}

#[test]
fn compiles_default_glsl_math_builtins() {
    let kernel = compile_source(
        br#"
            pub fn run(x: f32, y: f32) {
                let curved = pow(max(sin(x) + sqrt(y), exp(min(x, y))), 2.0f32);
                let shaped = smoothstep(0.0f32, 1.0f32, clamp(curved, 0.0f32, 1.0f32));
                return fma(mix(x, y, shaped), step(0.5f32, shaped), atan2(y, x));
            }
            "#,
        "spirv_default_math",
        "run",
    )
    .unwrap();

    let asm = kernel.spirv.disassemble();
    assert_eq!(kernel.ret_ty, Type::F32);
    assert!(asm.contains("GLSL.std.450"));
    assert!(asm.contains("Sin"));
    assert!(asm.contains("Sqrt"));
    assert!(asm.contains("Exp"));
    assert!(asm.contains("FMin"));
    assert!(asm.contains("FMax"));
    assert!(asm.contains("Pow"));
    assert!(asm.contains("SmoothStep"));
    assert!(asm.contains("FClamp"));
    assert!(asm.contains("FMix"));
    assert!(asm.contains("Step"));
    assert!(asm.contains("Fma"));
    assert!(asm.contains("Atan2"));
}

#[test]
fn compiles_reference_bitonic_compare_condition() {
    let kernel = compile_source(
        br#"
            pub fn cas_needed(val1: u32, val2: u32, direct: bool) {
                return (val1 > val2 && direct) || (val1 < val2 && !direct);
            }
            "#,
        "bitonic_ref",
        "cas_needed",
    )
    .unwrap();

    let asm = kernel.spirv.disassemble();
    assert_eq!(kernel.arg_tys, vec![Type::U32, Type::U32, Type::Bool]);
    assert_eq!(kernel.ret_ty, Type::Bool);
    assert!(asm.contains("OpLogicalAnd"));
    assert!(asm.contains("OpLogicalOr"));
}

#[test]
fn compiles_reference_bitonic_kernel() {
    let kernel = compile_source_with_workgroup_size(include_bytes!("../../zusts/gpu/bitonic.zs"), "bitonic", "main", [256, 1, 1]).unwrap();

    let asm = kernel.spirv.disassemble();
    assert_eq!(kernel.arg_tys.len(), 2);
    assert_eq!(kernel.ret_ty, Type::Void);
    assert!(asm.contains("BuiltIn WorkgroupId"));
    assert!(asm.contains("BuiltIn LocalInvocationId"));
    assert!(asm.contains("OpBitwiseXor"));
    assert!(asm.contains("OpBitwiseAnd"));
    assert!(asm.contains("OpStore"));
}

#[test]
fn compiles_user_function_call() {
    let kernel = compile_source(
        br#"
            fn inc(x: u32) {
                x + 1u32
            }

            pub fn run(x: u32) {
                return inc(x) * 2u32;
            }
            "#,
        "spirv_user_fn",
        "run",
    )
    .unwrap();

    let asm = kernel.spirv.disassemble();
    assert_eq!(kernel.ret_ty, Type::U32);
    assert!(asm.contains("OpFunctionCall"));
    assert!(asm.contains("OpIAdd"));
    assert!(asm.contains("OpIMul"));
}

#[test]
fn compiles_user_struct_method_call() {
    let kernel = compile_source(
        br#"
            struct Counter {
                value: u32,
            }

            impl Counter {
                fn add(self, amount: u32) {
                    self[0u32] + amount
                }
            }

            pub fn run(counter: Counter, amount: u32) {
                return counter.add(amount);
            }
            "#,
        "spirv_user_struct_method",
        "run",
    )
    .unwrap();

    let asm = kernel.spirv.disassemble();
    assert_eq!(kernel.ret_ty, Type::U32);
    assert!(asm.contains("OpFunctionCall"));
    assert!(asm.contains("OpCompositeExtract") || asm.contains("OpAccessChain"));
    assert!(asm.contains("OpIAdd"));
}

#[test]
fn compiles_bigfloat_f32_roundtrip_kernel() {
    let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).parent().expect("workspace root");
    let mut source = std::fs::read_to_string(root.join("zusts").join("bigfloat.zs")).expect("read bigfloat.zs");
    source.push_str(
        r#"

            pub fn run(value: f32) {
                BigFloat<2>::from_f32(value).to_f32()
            }
            "#,
    );
    let kernel = compile_source(source, "spirv_bigfloat_roundtrip", "run").unwrap();

    assert_eq!(kernel.arg_tys, vec![Type::F32]);
    assert_eq!(kernel.ret_ty, Type::F32);
}

#[test]
fn compiles_bigfloat_add_sub_mul_kernels() {
    let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).parent().expect("workspace root");
    let mut source = std::fs::read_to_string(root.join("zusts").join("bigfloat.zs")).expect("read bigfloat.zs");
    source.push_str(
        r#"

            pub fn add_run(a: f32, b: f32) {
                BigFloat<2>::from_f32(a).add(BigFloat<2>::from_f32(b)).to_f32()
            }

            pub fn sub_run(a: f32, b: f32) {
                BigFloat<2>::from_f32(a).sub(BigFloat<2>::from_f32(b)).to_f32()
            }

            pub fn mul_run(a: f32, b: f32) {
                BigFloat<2>::from_f32(a).mul(BigFloat<2>::from_f32(b)).to_f32()
            }
            "#,
    );

    for name in ["add_run", "sub_run", "mul_run"] {
        let kernel = compile_source(source.clone(), "spirv_bigfloat_ops", name).unwrap();
        assert_eq!(kernel.arg_tys, vec![Type::F32, Type::F32]);
        assert_eq!(kernel.ret_ty, Type::F32);
    }
}

#[test]
fn compiles_bigfloat_32_mul_kernel() {
    let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).parent().expect("workspace root");
    let mut source = std::fs::read_to_string(root.join("zusts").join("bigfloat.zs")).expect("read bigfloat.zs");
    source.push_str(
        r#"

            pub fn run(a: f32, b: f32) {
                BigFloat<32>::from_f32(a).mul(BigFloat<32>::from_f32(b)).to_f32()
            }
            "#,
    );
    let kernel = compile_source(source, "spirv_bigfloat32_mul", "run").unwrap();

    assert_eq!(kernel.arg_tys, vec![Type::F32, Type::F32]);
    assert_eq!(kernel.ret_ty, Type::F32);
}

#[test]
fn generic_inference_cache_separates_explicit_const_args() {
    let kernel = compile_source(
        br#"
            pub struct BfBox<N> {
                data: [u32; N],
            }

            impl BfBox<N> {
                pub fn make(value: u32) {
                    let data: [u32; N] = [0u32; N];
                    data[0u32] = value;
                    BfBox<N>{ data }
                }
            }

            pub fn run(a: u32, b: u32) {
                let small = BfBox<2>::make(a);
                let wide = BfBox<8>::make(b);
                small.data[0u32] + wide.data[0u32]
            }
            "#,
        "spirv_generic_const_arg_cache",
        "run",
    )
    .unwrap();

    assert_eq!(kernel.arg_tys, vec![Type::U32, Type::U32]);
    assert_eq!(kernel.ret_ty, Type::U32);
}

#[test]
fn compiles_user_vec_method_call_by_inlining() {
    let kernel = compile_source(
        br#"
            impl Vec<T> {
                fn first_plus(self, amount: u32) {
                    self[0u32] + amount
                }
            }

            pub fn run(values: Vec<u32>, amount: u32) {
                return values.first_plus(amount);
            }
            "#,
        "spirv_user_vec_method",
        "run",
    )
    .unwrap();

    let asm = kernel.spirv.disassemble();
    assert_eq!(kernel.ret_ty, Type::U32);
    assert!(!asm.contains("OpFunctionCall"));
    assert!(asm.contains("OpAccessChain"));
    assert!(asm.contains("OpIAdd"));
}

#[test]
fn specializes_generic_vec_method_from_receiver_type() {
    let source = br#"
            impl Vec<T> {
                fn first(self) {
                    self[0u32]
                }
            }

            pub fn first_u32(values: Vec<u32>) {
                return values.first();
            }

            pub fn first_f32(values: Vec<f32>) {
                return values.first();
            }
        "#;

    let u32_kernel = compile_source(source, "spirv_generic_vec_u32", "first_u32").unwrap();
    let f32_kernel = compile_source(source, "spirv_generic_vec_f32", "first_f32").unwrap();

    assert_eq!(u32_kernel.ret_ty, Type::U32);
    assert_eq!(f32_kernel.ret_ty, Type::F32);
    assert!(u32_kernel.spirv.disassemble().contains("OpAccessChain"));
    assert!(f32_kernel.spirv.disassemble().contains("OpAccessChain"));
}

#[test]
fn compiles_reference_world_random_hash_math() {
    let kernel = compile_source(
        br#"
            pub fn random(seed: u32) {
                let x = seed * 747796405u32 + 2891336453u32;
                let y = (x >> 16u32) ^ x;
                let z = y * 2246822519u32 + 3266489917u32;
                let w = (z >> 16u32) ^ z;
                return ((w & 2147483647u32) as f32) / 2147483647.0f32;
            }
            "#,
        "world_ref",
        "random",
    )
    .unwrap();

    let asm = kernel.spirv.disassemble();
    assert_eq!(kernel.arg_tys, vec![Type::U32]);
    assert_eq!(kernel.ret_ty, Type::F32);
    assert!(asm.contains("OpBitwiseXor"));
    assert!(asm.contains("OpConvertUToF"));
}

#[test]
fn compiles_for_loop_with_range() {
    let kernel = compile_source(
        br#"
            pub fn sum(n: u32) {
                let total = 0u32;
                for idx in 0..n {
                    total += idx;
                }
                return total;
            }
            "#,
        "for_test",
        "sum",
    )
    .unwrap();

    let asm = kernel.spirv.disassemble();
    assert_eq!(kernel.arg_tys, vec![Type::U32]);
    assert_eq!(kernel.ret_ty, Type::U32);
    assert!(asm.contains("OpLoopMerge"));
    assert!(asm.contains("OpPhi"));
}

#[test]
fn compiles_for_loop_with_inclusive_range() {
    let kernel = compile_source(
        br#"
            pub fn sum_inclusive(n: u32) {
                let total = 0u32;
                for idx in 0..=n {
                    total += idx;
                }
                return total;
            }
            "#,
        "for_inclusive_test",
        "sum_inclusive",
    )
    .unwrap();

    let asm = kernel.spirv.disassemble();
    assert_eq!(kernel.arg_tys, vec![Type::U32]);
    assert_eq!(kernel.ret_ty, Type::U32);
    assert!(asm.contains("OpLoopMerge"));
    assert!(asm.contains("OpULessThanEqual"));
}

#[test]
fn operator_precedence_sub_mul_add() {
    // (a - 1) * b + c  should compute as ((a-1)*b)+c
    // NOT as a - (1*b + c) or a - 1*b + c with wrong association
    let kernel = compile_source(
        br#"
            pub fn calc(a: u32, b: u32, c: u32) {
                return (a - 1u32) * b + c;
            }
            "#,
        "prec_test",
        "calc",
    )
    .unwrap();

    let asm = kernel.spirv.disassemble();
    // There should be an ISub before the IMul (computing a-1 first)
    // and the final operation should be IAdd
    assert!(asm.contains("OpISub"), "should have subtraction: {asm}");
    assert!(asm.contains("OpIMul"), "should have multiplication: {asm}");
    assert!(asm.contains("OpIAdd"), "should have addition: {asm}");

    // Verify order: ISub result feeds into IMul, IMul result feeds into IAdd
    // by checking that the instruction sequence follows the correct dependency chain
    eprintln!("{asm}");
}

#[test]
fn compiles_point_in_poly_with_for_loop() {
    // Simplified point-in-polygon using for loop
    let kernel = compile_source(
        br#"
            pub fn point_in_poly(data: Vec<f32>, n: u32, px: i32, py: i32) {
                let inside = false;
                for idx in 0u32..n {
                    let val = data[idx];
                    if val > 0.0f32 {
                        inside = !inside;
                    }
                }
                return inside;
            }
            "#,
        "poly_test",
        "point_in_poly",
    )
    .unwrap();

    let asm = kernel.spirv.disassemble();
    assert!(asm.contains("OpLoopMerge"));
    assert!(asm.contains("OpPhi"));
}