vyre-foundation 0.4.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
511
512
513
514
515
516
//! ROADMAP A15 — buffer aliasing facts into load elision.
//!
//! Read-only-buffer slice shipped here. When both arms of an
//! `Node::If` begin with a `Let(name, Load(buf, idx))` whose
//! `buf` is declared `BufferAccess::ReadOnly` AND the same name +
//! same index, the Load is hoisted before the If. The ReadOnly
//! declaration is the alias proof: a ReadOnly buffer is fully
//! initialised by the host before kernel launch, so the Load is
//! observably-safe to execute on the unconditional path — there
//! is no observable difference between "load was already issued"
//! and "load was about to be issued in one arm only".
//!
//! Op id: `vyre-foundation::optimizer::passes::read_only_load_hoist`.
//! Soundness: `Exact`. The ReadOnly access mode is enforced by the
//! buffer table; any pass that mutates a ReadOnly buffer is a
//! validation error caught by `Program::validate()`. Therefore the
//! Load result is invariant under the If's two execution paths,
//! and hoisting the Load to the unconditional path produces the
//! same value at every read site.
//!
//! Cost direction: monotone-down on `node_count` (one fewer Let
//! per fired hoist) and monotone-down on per-arm dispatch overhead
//! (the Load is issued once instead of once per branch).
//!
//! Preserves: every analysis. Invalidates: nothing — the hoisted
//! Load is the alias-proof-licensed counterpart of A18's
//! observably-free prefix hoist for non-Load values.
//!
//! ## Pattern
//!
//! ```text
//! If(cond,
//!    [Let(x, Load(ro_buf, idx)), then_rest...],
//!    [Let(x, Load(ro_buf, idx)), other_rest...])
//!     where program.buffer(ro_buf).access() == BufferAccess::ReadOnly
//!     AND idx is observably-free
//! → Let(x, Load(ro_buf, idx)); If(cond, [then_rest...], [other_rest...])
//! ```
//!
//! Idx must be observably-free because the index expression also
//! becomes unconditional after the hoist.
//!
//! ## Why this is A15
//!
//! A15 says "buffer aliasing facts into load elision". The full
//! alias substrate (proving two arbitrary buffers don't alias) is
//! a weir analysis. ReadOnly is the trivial alias proof: a buffer
//! that nobody writes cannot alias with any write target, so its
//! Loads are invariant across control flow. Shipping the trivial
//! slice here gives the hot path the same code-size win that the
//! full aliasing substrate would deliver, while the fact-driven
//! variant lands beside the weir alias pass.

use crate::ir::{BufferAccess, Expr, Node, Program};
use crate::optimizer::{fingerprint_program, vyre_pass, PassAnalysis, PassResult};
use rustc_hash::FxHashSet;
use std::sync::Arc;

/// Hoist Loads on declared-ReadOnly buffers out of common
/// branch prefixes.
#[derive(Debug, Default)]
#[vyre_pass(
    name = "read_only_load_hoist",
    requires = [],
    invalidates = []
)]
pub struct ReadOnlyLoadHoistPass;

impl ReadOnlyLoadHoistPass {
    /// Skip programs with no candidate `If`.
    #[must_use]
    pub fn analyze(program: &Program) -> PassAnalysis {
        let read_only = read_only_buffer_set(program);
        if read_only.is_empty() {
            return PassAnalysis::SKIP;
        }
        let mut found = false;
        for node in program.entry() {
            if has_candidate(node, &read_only) {
                found = true;
                break;
            }
        }
        if found {
            PassAnalysis::RUN
        } else {
            PassAnalysis::SKIP
        }
    }

    /// Walk the entry tree and hoist common Read-Only-Load prefixes.
    #[must_use]
    pub fn transform(program: Program) -> PassResult {
        let read_only = read_only_buffer_set(&program);
        if read_only.is_empty() {
            return PassResult {
                program,
                changed: false,
            };
        }
        let scaffold = program.with_rewritten_entry(Vec::new());
        let mut changed = false;
        let entry: Vec<Node> = program
            .into_entry_vec()
            .into_iter()
            .flat_map(|node| hoist_prefix(node, &read_only, &mut changed))
            .collect();
        PassResult {
            program: scaffold.with_rewritten_entry(entry),
            changed,
        }
    }

    /// Fingerprint the program state.
    #[must_use]
    pub fn fingerprint(program: &Program) -> u64 {
        fingerprint_program(program)
    }
}

fn read_only_buffer_set(program: &Program) -> FxHashSet<crate::ir::Ident> {
    program
        .buffers()
        .iter()
        .filter(|b| matches!(b.access(), BufferAccess::ReadOnly))
        .map(|b| crate::ir::Ident::from(b.name.as_ref()))
        .collect()
}

fn hoist_prefix(
    node: Node,
    read_only: &FxHashSet<crate::ir::Ident>,
    changed: &mut bool,
) -> Vec<Node> {
    let recursed = recurse_children(node, read_only, changed);
    if let Node::If {
        cond,
        then,
        otherwise,
    } = recursed
    {
        let (prefix, new_then, new_otherwise) = extract_common_prefix(then, otherwise, read_only);
        if !prefix.is_empty() {
            *changed = true;
            let mut out = prefix;
            out.push(Node::If {
                cond,
                then: new_then,
                otherwise: new_otherwise,
            });
            return out;
        }
        return vec![Node::If {
            cond,
            then: new_then,
            otherwise: new_otherwise,
        }];
    }
    vec![recursed]
}

fn recurse_children(
    node: Node,
    read_only: &FxHashSet<crate::ir::Ident>,
    changed: &mut bool,
) -> Node {
    match node {
        Node::If {
            cond,
            then,
            otherwise,
        } => Node::If {
            cond,
            then: then
                .into_iter()
                .flat_map(|n| hoist_prefix(n, read_only, changed))
                .collect(),
            otherwise: otherwise
                .into_iter()
                .flat_map(|n| hoist_prefix(n, read_only, changed))
                .collect(),
        },
        Node::Loop {
            var,
            from,
            to,
            body,
        } => Node::Loop {
            var,
            from,
            to,
            body: body
                .into_iter()
                .flat_map(|n| hoist_prefix(n, read_only, changed))
                .collect(),
        },
        Node::Block(body) => Node::Block(
            body.into_iter()
                .flat_map(|n| hoist_prefix(n, read_only, changed))
                .collect(),
        ),
        Node::Region {
            generator,
            source_region,
            body,
        } => {
            let body_vec: Vec<Node> = match Arc::try_unwrap(body) {
                Ok(v) => v,
                Err(arc) => (*arc).clone(),
            };
            Node::Region {
                generator,
                source_region,
                body: Arc::new(
                    body_vec
                        .into_iter()
                        .flat_map(|n| hoist_prefix(n, read_only, changed))
                        .collect(),
                ),
            }
        }
        other => other,
    }
}

fn extract_common_prefix(
    mut then: Vec<Node>,
    mut otherwise: Vec<Node>,
    read_only: &FxHashSet<crate::ir::Ident>,
) -> (Vec<Node>, Vec<Node>, Vec<Node>) {
    let prefix_len = then
        .iter()
        .zip(otherwise.iter())
        .take_while(|(t, o)| is_hoistable_pair(t, o, read_only))
        .count();
    if prefix_len == 0 {
        return (Vec::new(), then, otherwise);
    }
    let prefix = then.drain(..prefix_len).collect();
    otherwise.drain(..prefix_len);
    (prefix, then, otherwise)
}

fn is_hoistable_pair(a: &Node, b: &Node, read_only: &FxHashSet<crate::ir::Ident>) -> bool {
    let (name_a, value_a) = match a {
        Node::Let { name, value } => (name, value),
        _ => return false,
    };
    let (name_b, value_b) = match b {
        Node::Let { name, value } => (name, value),
        _ => return false,
    };
    if name_a != name_b || value_a != value_b {
        return false;
    }
    matches!(value_a, Expr::Load { buffer, index } if read_only.contains(buffer) && index_is_observably_free(index))
}

fn index_is_observably_free(expr: &Expr) -> bool {
    match expr {
        Expr::Load { .. }
        | Expr::Atomic { .. }
        | Expr::Call { .. }
        | Expr::Opaque(_)
        | Expr::SubgroupBallot { .. }
        | Expr::SubgroupShuffle { .. }
        | Expr::SubgroupAdd { .. }
        | Expr::SubgroupLocalId
        | Expr::SubgroupSize => false,
        Expr::LitU32(_)
        | Expr::LitI32(_)
        | Expr::LitF32(_)
        | Expr::LitBool(_)
        | Expr::Var(_)
        | Expr::BufLen { .. }
        | Expr::InvocationId { .. }
        | Expr::WorkgroupId { .. }
        | Expr::LocalId { .. } => true,
        Expr::BinOp { left, right, .. } => {
            index_is_observably_free(left) && index_is_observably_free(right)
        }
        Expr::UnOp { operand, .. } => index_is_observably_free(operand),
        Expr::Select {
            cond,
            true_val,
            false_val,
        } => {
            index_is_observably_free(cond)
                && index_is_observably_free(true_val)
                && index_is_observably_free(false_val)
        }
        Expr::Cast { value, .. } => index_is_observably_free(value),
        Expr::Fma { a, b, c } => {
            index_is_observably_free(a)
                && index_is_observably_free(b)
                && index_is_observably_free(c)
        }
    }
}

fn has_candidate(node: &Node, read_only: &FxHashSet<crate::ir::Ident>) -> bool {
    match node {
        Node::If {
            then, otherwise, ..
        } => match (then.first(), otherwise.first()) {
            (Some(t), Some(o)) => {
                is_hoistable_pair(t, o, read_only)
                    || then.iter().any(|n| has_candidate(n, read_only))
                    || otherwise.iter().any(|n| has_candidate(n, read_only))
            }
            _ => {
                then.iter().any(|n| has_candidate(n, read_only))
                    || otherwise.iter().any(|n| has_candidate(n, read_only))
            }
        },
        Node::Loop { body, .. } => body.iter().any(|n| has_candidate(n, read_only)),
        Node::Block(body) => body.iter().any(|n| has_candidate(n, read_only)),
        Node::Region { body, .. } => body.iter().any(|n| has_candidate(n, read_only)),
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ir::{BufferDecl, DataType, Expr, Ident, Node};

    fn ro_buf(name: &str) -> BufferDecl {
        BufferDecl::storage(name, 0, BufferAccess::ReadOnly, DataType::U32).with_count(8)
    }

    fn rw_buf(name: &str) -> BufferDecl {
        BufferDecl::storage(name, 1, BufferAccess::ReadWrite, DataType::U32).with_count(8)
    }

    fn program(buffers: Vec<BufferDecl>, entry: Vec<Node>) -> Program {
        Program::wrapped(buffers, [1, 1, 1], entry)
    }

    fn find_siblings(nodes: &[Node]) -> Option<&[Node]> {
        if nodes
            .iter()
            .any(|n| matches!(n, Node::Let { .. } | Node::If { .. }))
        {
            return Some(nodes);
        }
        for n in nodes {
            let body = match n {
                Node::Block(body) => body.as_slice(),
                Node::Region { body, .. } => body.as_ref().as_slice(),
                _ => continue,
            };
            if let Some(found) = find_siblings(body) {
                return Some(found);
            }
        }
        None
    }

    /// Positive: Load on a ReadOnly buffer at the start of both arms
    /// hoists out before the If.
    #[test]
    fn hoists_read_only_load_prefix() {
        let load = Expr::Load {
            buffer: Ident::from("ro"),
            index: Box::new(Expr::u32(0)),
        };
        let entry = vec![Node::If {
            cond: Expr::var("c"),
            then: vec![
                Node::let_bind("x", load.clone()),
                Node::store("rw", Expr::u32(0), Expr::var("x")),
            ],
            otherwise: vec![
                Node::let_bind("x", load),
                Node::store("rw", Expr::u32(1), Expr::var("x")),
            ],
        }];
        let prog = program(vec![ro_buf("ro"), rw_buf("rw")], entry);
        let result = ReadOnlyLoadHoistPass::transform(prog);
        assert!(result.changed, "ReadOnly Load prefix must hoist");
        let siblings = find_siblings(result.program.entry()).expect("hoisted Let + If present");
        assert!(matches!(&siblings[0], Node::Let { name, value }
            if name.as_str() == "x" && matches!(value, Expr::Load { .. })));
        assert!(matches!(&siblings[1], Node::If { .. }));
    }

    /// Negative: Load on a ReadWrite buffer must NOT hoist (alias
    /// proof unavailable; another arm could write between the If and
    /// the post-If sequencing).
    #[test]
    fn keeps_read_write_load() {
        let load = Expr::Load {
            buffer: Ident::from("rw"),
            index: Box::new(Expr::u32(0)),
        };
        let entry = vec![Node::If {
            cond: Expr::var("c"),
            then: vec![Node::let_bind("x", load.clone())],
            otherwise: vec![Node::let_bind("x", load)],
        }];
        let prog = program(vec![rw_buf("rw")], entry);
        let result = ReadOnlyLoadHoistPass::transform(prog);
        assert!(!result.changed, "ReadWrite Load must not hoist");
    }

    /// Negative: differing names block the hoist.
    #[test]
    fn keeps_when_names_differ() {
        let load = Expr::Load {
            buffer: Ident::from("ro"),
            index: Box::new(Expr::u32(0)),
        };
        let entry = vec![Node::If {
            cond: Expr::var("c"),
            then: vec![Node::let_bind("x", load.clone())],
            otherwise: vec![Node::let_bind("y", load)],
        }];
        let prog = program(vec![ro_buf("ro"), rw_buf("rw")], entry);
        let result = ReadOnlyLoadHoistPass::transform(prog);
        assert!(!result.changed, "differing names must not hoist");
    }

    /// Negative: differing indices block the hoist.
    #[test]
    fn keeps_when_indices_differ() {
        let entry = vec![Node::If {
            cond: Expr::var("c"),
            then: vec![Node::let_bind(
                "x",
                Expr::Load {
                    buffer: Ident::from("ro"),
                    index: Box::new(Expr::u32(0)),
                },
            )],
            otherwise: vec![Node::let_bind(
                "x",
                Expr::Load {
                    buffer: Ident::from("ro"),
                    index: Box::new(Expr::u32(1)),
                },
            )],
        }];
        let prog = program(vec![ro_buf("ro"), rw_buf("rw")], entry);
        let result = ReadOnlyLoadHoistPass::transform(prog);
        assert!(!result.changed, "differing indices must not hoist");
    }

    /// Negative: an index expression that itself contains a Load
    /// blocks the hoist (the index Load could observe state that
    /// the unconditional path shouldn't trigger).
    #[test]
    fn keeps_when_index_reads_memory() {
        let load = Expr::Load {
            buffer: Ident::from("ro"),
            index: Box::new(Expr::Load {
                buffer: Ident::from("rw"),
                index: Box::new(Expr::u32(0)),
            }),
        };
        let entry = vec![Node::If {
            cond: Expr::var("c"),
            then: vec![Node::let_bind("x", load.clone())],
            otherwise: vec![Node::let_bind("x", load)],
        }];
        let prog = program(vec![ro_buf("ro"), rw_buf("rw")], entry);
        let result = ReadOnlyLoadHoistPass::transform(prog);
        assert!(!result.changed, "index that reads memory must block hoist");
    }

    /// `analyze` short-circuits when the program declares no
    /// ReadOnly buffer.
    #[test]
    fn analyze_skips_program_with_no_read_only_buffer() {
        let entry = vec![Node::store("rw", Expr::u32(0), Expr::u32(1))];
        let prog = program(vec![rw_buf("rw")], entry);
        match ReadOnlyLoadHoistPass::analyze(&prog) {
            PassAnalysis::SKIP => {}
            other => panic!("expected SKIP, got {other:?}"),
        }
    }

    /// Positive end-to-end smoke: chain of two ReadOnly Loads with
    /// different indices in the prefix hoists both.
    #[test]
    fn hoists_chain_of_read_only_loads() {
        let load_a = Expr::Load {
            buffer: Ident::from("ro"),
            index: Box::new(Expr::u32(0)),
        };
        let load_b = Expr::Load {
            buffer: Ident::from("ro"),
            index: Box::new(Expr::u32(1)),
        };
        let entry = vec![Node::If {
            cond: Expr::var("c"),
            then: vec![
                Node::let_bind("a", load_a.clone()),
                Node::let_bind("b", load_b.clone()),
                Node::store("rw", Expr::u32(0), Expr::var("a")),
            ],
            otherwise: vec![
                Node::let_bind("a", load_a),
                Node::let_bind("b", load_b),
                Node::store("rw", Expr::u32(1), Expr::var("b")),
            ],
        }];
        let prog = program(vec![ro_buf("ro"), rw_buf("rw")], entry);
        let result = ReadOnlyLoadHoistPass::transform(prog);
        assert!(result.changed, "chain of ReadOnly Loads must hoist");
        let siblings = find_siblings(result.program.entry()).expect("hoisted Lets + If present");
        assert!(siblings.len() >= 3);
        assert!(matches!(&siblings[0], Node::Let { name, .. } if name.as_str() == "a"));
        assert!(matches!(&siblings[1], Node::Let { name, .. } if name.as_str() == "b"));
    }
}