vyre-foundation 0.6.1

Foundation layer: IR, type system, memory model, wire format. Zero application semantics. Part of the vyre GPU compiler.
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
use super::Program;
use crate::ir::{DataType, Expr, Node};

const CAP_SUBGROUP_OPS: u32 = 1 << 0;
const CAP_F16: u32 = 1 << 1;
const CAP_BF16: u32 = 1 << 2;
const CAP_F64: u32 = 1 << 3;
const CAP_ASYNC_DISPATCH: u32 = 1 << 4;
const CAP_INDIRECT_DISPATCH: u32 = 1 << 5;
const CAP_TENSOR_OPS: u32 = 1 << 6;
const CAP_TRAP: u32 = 1 << 7;
const CAP_DISTRIBUTED_COLLECTIVES: u32 = 1 << 8;

// Bit positions for `ProgramStats::node_kinds_present`. Mirrors the
// variant declaration order in `ir_inner::model::generated::Node` and
// matches `optimizer::program_soa::NodeKind` so the optimizer can use
// either source of truth interchangeably. The
// `node_kinds_present_matches_program_soa_node_kind` test enforces
// the alignment.
/// `Node::Let`.
pub const NODE_KIND_LET: u32 = 1 << 0;
/// `Node::Assign`.
pub const NODE_KIND_ASSIGN: u32 = 1 << 1;
/// `Node::Store`.
pub const NODE_KIND_STORE: u32 = 1 << 2;
/// `Node::If`.
pub const NODE_KIND_IF: u32 = 1 << 3;
/// `Node::Loop`.
pub const NODE_KIND_LOOP: u32 = 1 << 4;
/// `Node::IndirectDispatch`.
pub const NODE_KIND_INDIRECT_DISPATCH: u32 = 1 << 5;
/// `Node::AsyncLoad`.
pub const NODE_KIND_ASYNC_LOAD: u32 = 1 << 6;
/// `Node::AsyncStore`.
pub const NODE_KIND_ASYNC_STORE: u32 = 1 << 7;
/// `Node::AsyncWait`.
pub const NODE_KIND_ASYNC_WAIT: u32 = 1 << 8;
/// `Node::Trap`.
pub const NODE_KIND_TRAP: u32 = 1 << 9;
/// `Node::Resume`.
pub const NODE_KIND_RESUME: u32 = 1 << 10;
/// `Node::Return`.
pub const NODE_KIND_RETURN: u32 = 1 << 11;
/// `Node::Barrier`.
pub const NODE_KIND_BARRIER: u32 = 1 << 12;
/// `Node::Block`.
pub const NODE_KIND_BLOCK: u32 = 1 << 13;
/// `Node::Region`.
pub const NODE_KIND_REGION: u32 = 1 << 14;
/// `Node::Opaque`.
pub const NODE_KIND_ALL_REDUCE: u32 = 1 << 15;
/// `Node::AllGather`.
pub const NODE_KIND_ALL_GATHER: u32 = 1 << 16;
/// `Node::ReduceScatter`.
pub const NODE_KIND_REDUCE_SCATTER: u32 = 1 << 17;
/// `Node::Broadcast`.
pub const NODE_KIND_BROADCAST: u32 = 1 << 18;
/// `Node::Opaque`.
pub const NODE_KIND_OPAQUE: u32 = 1 << 19;

/// Mask covering every node kind that owns an `Expr` tree, i.e. every
/// kind a generic expression-rewriting pass (`canonicalize`, `const_fold`,
/// `strength_reduce`, ...) could possibly affect. A program whose
/// `node_kinds_present` and this mask AND to zero is structurally
/// expression-free and any such pass can SKIP without walking.
pub const NODE_KIND_EXPRESSION_BEARING_MASK: u32 = NODE_KIND_LET
    | NODE_KIND_ASSIGN
    | NODE_KIND_STORE
    | NODE_KIND_IF
    | NODE_KIND_LOOP
    | NODE_KIND_ASYNC_LOAD
    | NODE_KIND_ASYNC_STORE
    | NODE_KIND_TRAP;

/// Aggregated statistics computed from a single walk of a [`Program`].
///
/// This struct is cached inside [`Program`] via a [`std::sync::OnceLock`]
/// so that planning passes (execution plan, capability scan, provenance,
/// fusion) can read constant-time summaries instead of re-walking the IR.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ProgramStats {
    /// Total statement-node count (includes nested children).
    pub node_count: usize,
    /// Number of `Node::Region` nodes in the full tree.
    pub region_count: u32,
    /// Number of `Expr::Call` expressions.
    pub call_count: u32,
    /// Number of `Node::Opaque` nodes and `Expr::Opaque` expressions.
    pub opaque_count: u32,
    /// Number of top-level `Node::Region` wrappers in `program.entry()`.
    pub top_level_regions: u32,
    /// Sum of statically-known buffer byte sizes.
    pub static_storage_bytes: u64,
    /// Estimated scalar/vector IR instruction count.
    pub instruction_count: u64,
    /// Number of explicit memory operations (loads, stores, async copies).
    pub memory_op_count: u64,
    /// Number of atomic read-modify-write expressions.
    pub atomic_op_count: u64,
    /// Number of control-flow operations.
    pub control_flow_count: u64,
    /// Coarse register pressure estimate from simultaneously named SSA-ish values.
    pub register_pressure_estimate: u32,
    /// Bitmask of capability requirements (see `CAP_*` constants).
    pub capability_bits: u32,
    /// Bitset of every `Node` variant observed during the stats walk
    /// (see the `NODE_KIND_*` constants in this module). Lets pass
    /// `analyze_impl` predicates do an O(1) bit test against the
    /// shared, OnceLock-cached `ProgramStats` instead of recursing
    /// the entry tree just to check 'does this program contain at
    /// least one Loop / If / Atomic / etc.'.
    pub node_kinds_present: u32,
}

mod methods;
impl Program {
    /// Return cached statistics for this program, computing them on first call.
    #[must_use]
    #[inline]
    pub fn stats(&self) -> &ProgramStats {
        self.stats
            .get_or_init(|| std::sync::Arc::new(compute_stats(self)))
            .as_ref()
    }
}

/// Single-pass preorder walk that accumulates every field of [`ProgramStats`].
pub(crate) fn compute_stats(program: &Program) -> ProgramStats {
    let mut node_count = 0usize;
    let mut region_count = 0u32;
    let mut call_count = 0u32;
    let mut opaque_count = 0u32;
    let mut capability_bits = 0u32;
    let mut node_kinds_present = 0u32;
    let mut static_storage_bytes = 0u64;
    let mut ir = IrCounters::default();

    for decl in program.buffers.iter() {
        let count = decl.count();
        if count != 0 {
            if let Some(elem) = decl.element().size_bytes() {
                static_storage_bytes =
                    static_storage_bytes.saturating_add(u64::from(count) * elem as u64);
            }
        }
        mark_datatype_bits(&decl.element(), &mut capability_bits);
    }

    for node in program.entry.iter() {
        walk_node(
            node,
            &mut node_count,
            &mut region_count,
            &mut call_count,
            &mut opaque_count,
            &mut capability_bits,
            &mut node_kinds_present,
            &mut ir,
        );
    }

    let top_level_regions = program
        .entry()
        .iter()
        .filter(|n| matches!(n, Node::Region { .. }))
        .count()
        .try_into()
        .unwrap_or(u32::MAX);

    ProgramStats {
        node_count,
        region_count,
        call_count,
        opaque_count,
        top_level_regions,
        static_storage_bytes,
        instruction_count: ir.instruction_count,
        memory_op_count: ir.memory_op_count,
        atomic_op_count: ir.atomic_op_count,
        control_flow_count: ir.control_flow_count,
        register_pressure_estimate: ir.register_pressure_estimate(),
        capability_bits,
        node_kinds_present,
    }
}

#[derive(Default)]
struct IrCounters {
    instruction_count: u64,
    memory_op_count: u64,
    atomic_op_count: u64,
    control_flow_count: u64,
    live_names: u32,
    max_live_names: u32,
}

impl IrCounters {
    fn instruction(&mut self) {
        self.instruction_count = self.instruction_count.saturating_add(1);
    }

    fn memory(&mut self) {
        self.memory_op_count = self.memory_op_count.saturating_add(1);
        self.instruction();
    }

    fn atomic(&mut self) {
        self.atomic_op_count = self.atomic_op_count.saturating_add(1);
        self.memory();
    }

    fn control_flow(&mut self) {
        self.control_flow_count = self.control_flow_count.saturating_add(1);
        self.instruction();
    }

    fn bind_name(&mut self) {
        self.live_names = self.live_names.saturating_add(1);
        self.max_live_names = self.max_live_names.max(self.live_names);
    }

    fn enter_scope(&mut self) -> u32 {
        self.live_names
    }

    fn leave_scope(&mut self, saved: u32) {
        self.live_names = saved;
    }

    fn register_pressure_estimate(&self) -> u32 {
        self.max_live_names
    }
}

#[inline]
fn mark_datatype_bits(ty: &DataType, bits: &mut u32) {
    match ty {
        DataType::F16 => *bits |= CAP_F16,
        DataType::BF16 => *bits |= CAP_BF16,
        DataType::F64 => *bits |= CAP_F64,
        DataType::Tensor | DataType::TensorShaped { .. } => *bits |= CAP_TENSOR_OPS,
        _ => {}
    }
}

#[allow(clippy::too_many_arguments)]
#[expect(
    clippy::too_many_lines,
    reason = "single-pass ProgramStats walker keeps all counters hot and avoids repeated IR traversals"
)]
fn walk_node(
    node: &Node,
    nodes: &mut usize,
    regions: &mut u32,
    calls: &mut u32,
    opaque: &mut u32,
    bits: &mut u32,
    kinds: &mut u32,
    ir: &mut IrCounters,
) {
    *nodes = nodes.saturating_add(1);
    match node {
        Node::Let { value, .. } => {
            *kinds |= NODE_KIND_LET;
            ir.instruction();
            ir.bind_name();
            walk_expr(value, nodes, regions, calls, opaque, bits, kinds, ir);
        }
        Node::Assign { value, .. } => {
            *kinds |= NODE_KIND_ASSIGN;
            ir.instruction();
            walk_expr(value, nodes, regions, calls, opaque, bits, kinds, ir);
        }
        Node::Store { index, value, .. } => {
            *kinds |= NODE_KIND_STORE;
            ir.memory();
            walk_expr(index, nodes, regions, calls, opaque, bits, kinds, ir);
            walk_expr(value, nodes, regions, calls, opaque, bits, kinds, ir);
        }
        Node::If {
            cond,
            then,
            otherwise,
        } => {
            *kinds |= NODE_KIND_IF;
            ir.control_flow();
            walk_expr(cond, nodes, regions, calls, opaque, bits, kinds, ir);
            let saved = ir.enter_scope();
            for child in then.iter().chain(otherwise.iter()) {
                walk_node(child, nodes, regions, calls, opaque, bits, kinds, ir);
            }
            ir.leave_scope(saved);
        }
        Node::Loop { from, to, body, .. } => {
            *kinds |= NODE_KIND_LOOP;
            ir.control_flow();
            walk_expr(from, nodes, regions, calls, opaque, bits, kinds, ir);
            walk_expr(to, nodes, regions, calls, opaque, bits, kinds, ir);
            let saved = ir.enter_scope();
            for child in body {
                walk_node(child, nodes, regions, calls, opaque, bits, kinds, ir);
            }
            ir.leave_scope(saved);
        }
        Node::Block(children) => {
            *kinds |= NODE_KIND_BLOCK;
            let saved = ir.enter_scope();
            for child in children {
                walk_node(child, nodes, regions, calls, opaque, bits, kinds, ir);
            }
            ir.leave_scope(saved);
        }
        Node::Region { body, .. } => {
            *kinds |= NODE_KIND_REGION;
            *regions = regions.saturating_add(1);
            let saved = ir.enter_scope();
            for child in body.iter() {
                walk_node(child, nodes, regions, calls, opaque, bits, kinds, ir);
            }
            ir.leave_scope(saved);
        }
        Node::AsyncLoad { offset, size, .. } => {
            *kinds |= NODE_KIND_ASYNC_LOAD;
            *bits |= CAP_ASYNC_DISPATCH;
            ir.memory();
            walk_expr(offset, nodes, regions, calls, opaque, bits, kinds, ir);
            walk_expr(size, nodes, regions, calls, opaque, bits, kinds, ir);
        }
        Node::AsyncStore { offset, size, .. } => {
            *kinds |= NODE_KIND_ASYNC_STORE;
            *bits |= CAP_ASYNC_DISPATCH;
            ir.memory();
            walk_expr(offset, nodes, regions, calls, opaque, bits, kinds, ir);
            walk_expr(size, nodes, regions, calls, opaque, bits, kinds, ir);
        }
        Node::AsyncWait { .. } => {
            *kinds |= NODE_KIND_ASYNC_WAIT;
            *bits |= CAP_ASYNC_DISPATCH;
            ir.control_flow();
        }
        Node::IndirectDispatch { .. } => {
            *kinds |= NODE_KIND_INDIRECT_DISPATCH;
            *bits |= CAP_INDIRECT_DISPATCH;
            ir.control_flow();
        }
        Node::Trap { address, .. } => {
            *kinds |= NODE_KIND_TRAP;
            *bits |= CAP_TRAP;
            ir.control_flow();
            walk_expr(address, nodes, regions, calls, opaque, bits, kinds, ir);
        }
        Node::AllReduce { .. } => {
            *kinds |= NODE_KIND_ALL_REDUCE;
            *bits |= CAP_DISTRIBUTED_COLLECTIVES;
            ir.memory();
        }
        Node::AllGather { .. } => {
            *kinds |= NODE_KIND_ALL_GATHER;
            *bits |= CAP_DISTRIBUTED_COLLECTIVES;
            ir.memory();
        }
        Node::ReduceScatter { .. } => {
            *kinds |= NODE_KIND_REDUCE_SCATTER;
            *bits |= CAP_DISTRIBUTED_COLLECTIVES;
            ir.memory();
        }
        Node::Broadcast { .. } => {
            *kinds |= NODE_KIND_BROADCAST;
            *bits |= CAP_DISTRIBUTED_COLLECTIVES;
            ir.memory();
        }
        Node::Opaque(_) => {
            *kinds |= NODE_KIND_OPAQUE;
            *opaque = opaque.saturating_add(1);
            ir.instruction();
        }
        Node::Return => {
            *kinds |= NODE_KIND_RETURN;
            ir.control_flow();
        }
        Node::Barrier { .. } => {
            *kinds |= NODE_KIND_BARRIER;
            ir.control_flow();
        }
        Node::Resume { .. } => {
            *kinds |= NODE_KIND_RESUME;
            ir.control_flow();
        }
    }
}

#[allow(clippy::only_used_in_recursion, clippy::too_many_arguments)]
fn walk_expr(
    expr: &Expr,
    nodes: &mut usize,
    regions: &mut u32,
    calls: &mut u32,
    opaque: &mut u32,
    bits: &mut u32,
    kinds: &mut u32,
    ir: &mut IrCounters,
) {
    match expr {
        Expr::SubgroupAdd { value } => {
            *bits |= CAP_SUBGROUP_OPS;
            ir.instruction();
            walk_expr(value, nodes, regions, calls, opaque, bits, kinds, ir);
        }
        Expr::SubgroupBallot { cond } => {
            *bits |= CAP_SUBGROUP_OPS;
            ir.instruction();
            walk_expr(cond, nodes, regions, calls, opaque, bits, kinds, ir);
        }
        Expr::SubgroupShuffle { value, lane } => {
            *bits |= CAP_SUBGROUP_OPS;
            ir.instruction();
            walk_expr(value, nodes, regions, calls, opaque, bits, kinds, ir);
            walk_expr(lane, nodes, regions, calls, opaque, bits, kinds, ir);
        }
        Expr::BinOp { left, right, .. } => {
            ir.instruction();
            walk_expr(left, nodes, regions, calls, opaque, bits, kinds, ir);
            walk_expr(right, nodes, regions, calls, opaque, bits, kinds, ir);
        }
        Expr::UnOp { operand, .. } => {
            ir.instruction();
            walk_expr(operand, nodes, regions, calls, opaque, bits, kinds, ir);
        }
        Expr::Fma { a, b, c } => {
            ir.instruction();
            walk_expr(a, nodes, regions, calls, opaque, bits, kinds, ir);
            walk_expr(b, nodes, regions, calls, opaque, bits, kinds, ir);
            walk_expr(c, nodes, regions, calls, opaque, bits, kinds, ir);
        }
        Expr::Select {
            cond,
            true_val,
            false_val,
        } => {
            ir.instruction();
            walk_expr(cond, nodes, regions, calls, opaque, bits, kinds, ir);
            walk_expr(true_val, nodes, regions, calls, opaque, bits, kinds, ir);
            walk_expr(false_val, nodes, regions, calls, opaque, bits, kinds, ir);
        }
        Expr::Cast { target, value } => {
            mark_datatype_bits(target, bits);
            ir.instruction();
            walk_expr(value, nodes, regions, calls, opaque, bits, kinds, ir);
        }
        Expr::Load { index, .. } => {
            ir.memory();
            walk_expr(index, nodes, regions, calls, opaque, bits, kinds, ir);
        }
        Expr::Call { op_id, args } => {
            if is_subgroup_intrinsic_id(op_id) {
                *bits |= CAP_SUBGROUP_OPS;
            }
            *calls = calls.saturating_add(1);
            ir.instruction();
            for arg in args {
                walk_expr(arg, nodes, regions, calls, opaque, bits, kinds, ir);
            }
        }
        Expr::Atomic {
            index,
            expected,
            value,
            ..
        } => {
            ir.atomic();
            walk_expr(index, nodes, regions, calls, opaque, bits, kinds, ir);
            if let Some(expected) = expected.as_deref() {
                walk_expr(expected, nodes, regions, calls, opaque, bits, kinds, ir);
            }
            walk_expr(value, nodes, regions, calls, opaque, bits, kinds, ir);
        }
        Expr::Opaque(_) => {
            *opaque = opaque.saturating_add(1);
            ir.instruction();
        }
        Expr::SubgroupLocalId | Expr::SubgroupSize => {
            *bits |= CAP_SUBGROUP_OPS;
            ir.instruction();
        }
        Expr::LitU32(_)
        | Expr::LitI32(_)
        | Expr::LitF32(_)
        | Expr::LitBool(_)
        | Expr::Var(_)
        | Expr::BufLen { .. }
        | Expr::InvocationId { .. }
        | Expr::WorkgroupId { .. }
        | Expr::LocalId { .. } => {}
    }
}


fn is_subgroup_intrinsic_id(op_id: &str) -> bool {
    const MARKERS: &[&str] = &[
        "subgroup_",
        "::subgroup::",
        "::subgroup",
        "wave_",
        "::wave::",
        "warp_",
        "::warp::",
    ];
    MARKERS.iter().any(|marker| op_id.contains(marker))
}