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
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//! `loop_redundant_bound_check_elide` — drop `if loop_var < to { ... }`
//! guards that re-check the enclosing loop's own upper bound.
//!
//! Op id: `vyre-foundation::optimizer::passes::loop_redundant_bound_check_elide`.
//! Soundness: `Exact` — the loop's `from..to` range guarantees `loop_var < to`
//! on every iteration, so the inner if-guard is statically true. Replacing
//! the if-node with its `then` branch wrapped in a `Block` is observationally
//! equivalent. Cost-direction: monotone-down on `node_count`,
//! `control_flow_count`, and `instruction_count` (one fewer comparison per
//! iteration). Preserves: every analysis. Invalidates: nothing.
//!
//! ## Why
//!
//! Vyre primitives uniformly emit a guard pattern at the start of every
//! per-thread Region body:
//!
//! ```text
//! let limb_idx = InvocationId { axis: 0 };
//! if limb_idx < N { ... }
//! ```
//!
//! When that pattern lives inside an unrolled `Node::Loop { from: 0, to: N }`
//! whose loop variable is also `limb_idx` (or a freshly bound variable
//! initialized from `InvocationId`), the inner if-guard is statically
//! redundant because the loop's own range gate already ensures it. Removing
//! the guard eliminates one comparison and branch per iteration in every
//! backend or interpreter that materializes structured control flow directly.
//!
//! ## Rule
//!
//! For every `Node::Loop { var, from: _, to: Lit, body }`, scan `body` for
//! `Node::If { cond: BinOp(Lt, Var(var), Lit_to), then, otherwise: empty }`
//! where `Lit_to` matches the loop's `to` literal. Replace the If with
//! `Node::Block(then)`.
//!
//! ## Conservatism
//!
//! Only fires when:
//!   1. Loop's `to` is a `LitU32` (literal upper bound).
//!   2. If-guard's RHS is the same literal.
//!   3. If-guard's LHS is `Var(loop_var)` (literal name match).
//!   4. If-guard's `otherwise` branch is empty (else-bodies require
//!      different rewrite rules).
//!   5. The comparison op is `BinOp::Lt`.
//!
//! Conservative because the IR value-set fact table only admits this exact
//! literal-bound implication today.

use crate::ir::{BinOp, Expr, Node, Program};
use crate::optimizer::{fingerprint_program, vyre_pass, PassAnalysis, PassResult};

/// Drop redundant `if loop_var < to { ... }` guards inside loops with
/// matching literal upper bounds.
#[derive(Debug, Default)]
#[vyre_pass(
    name = "loop_redundant_bound_check_elide",
    requires = [],
    invalidates = []
)]
pub struct LoopRedundantBoundCheckElidePass;

impl LoopRedundantBoundCheckElidePass {
    /// Skip programs without any loop containing an inner if-guard.
    #[must_use]
    pub fn analyze(program: &Program) -> PassAnalysis {
        if program.entry().iter().any(node_has_redundant_guard) {
            PassAnalysis::RUN
        } else {
            PassAnalysis::SKIP
        }
    }

    /// Walk the entry tree and elide redundant bound checks.
    #[must_use]
    pub fn transform(program: Program) -> PassResult {
        let scaffold = program.with_rewritten_entry(Vec::new());
        let mut changed = false;
        let entry = program
            .into_entry_vec()
            .into_iter()
            .map(|n| elide_in_node(n, &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)
    }
}

/// Try to extract a literal `u32` from `expr` (matches `Expr::LitU32`).
fn lit_u32_value(expr: &Expr) -> Option<u32> {
    match expr {
        Expr::LitU32(n) => Some(*n),
        _ => None,
    }
}

/// Check if `cond` is `Var(var) < Lit_to`. Returns the literal upper bound
/// the cond compares against, when so.
fn cond_matches_loop_var_lt_lit(cond: &Expr, var: &str) -> Option<u32> {
    if let Expr::BinOp { op, left, right } = cond {
        if matches!(op, BinOp::Lt) {
            if let (Expr::Var(v), Some(rhs_lit)) = (left.as_ref(), lit_u32_value(right)) {
                if v.as_str() == var {
                    return Some(rhs_lit);
                }
            }
        }
    }
    None
}

/// Walk a sequence of nodes and elide redundant guards within any nested loop.
fn elide_in_sequence(
    body: Vec<Node>,
    loop_ctx: Option<(&str, u32)>,
    changed: &mut bool,
) -> Vec<Node> {
    body.into_iter()
        .map(|n| elide_in_node_with_ctx(n, loop_ctx, changed))
        .collect()
}

/// Recurse into a node with the surrounding-loop context (var name + to-lit).
fn elide_in_node_with_ctx(node: Node, loop_ctx: Option<(&str, u32)>, changed: &mut bool) -> Node {
    match node {
        Node::If {
            cond,
            then,
            otherwise,
        } => {
            // Check if this if matches the redundant pattern.
            if otherwise.is_empty() {
                if let Some((loop_var, loop_to)) = loop_ctx {
                    if let Some(rhs_lit) = cond_matches_loop_var_lt_lit(&cond, loop_var) {
                        if rhs_lit == loop_to {
                            *changed = true;
                            // Recurse into `then` with the same context, then
                            // wrap in a Block. (The Block makes the unwrap
                            // safe even if `then` contained nested Ifs that
                            // also got elided.)
                            let then_elided = elide_in_sequence(then, loop_ctx, changed);
                            return Node::Block(then_elided);
                        }
                    }
                }
            }
            // Not the redundant pattern — recurse into both branches.
            Node::If {
                cond,
                then: elide_in_sequence(then, loop_ctx, changed),
                otherwise: elide_in_sequence(otherwise, loop_ctx, changed),
            }
        }
        Node::Loop {
            var,
            from,
            to,
            body,
        } => {
            // The loop establishes a fresh ctx; only fires when `to` is a
            // literal (the conservative analysis we documented above).
            let new_ctx = lit_u32_value(&to).map(|to_lit| (var.as_str(), to_lit));
            let body = elide_in_sequence(body, new_ctx, changed);
            Node::Loop {
                var,
                from,
                to,
                body,
            }
        }
        Node::Block(body) => Node::Block(elide_in_sequence(body, loop_ctx, changed)),
        Node::Region {
            generator,
            source_region,
            body,
        } => {
            let body_vec: Vec<Node> = match std::sync::Arc::try_unwrap(body) {
                Ok(v) => v,
                Err(arc) => (*arc).clone(),
            };
            // A Region is a fresh scope — drop the loop_ctx because the
            // inner body's loop var is in a different binding scope.
            let body_vec = elide_in_sequence(body_vec, None, changed);
            Node::Region {
                generator,
                source_region,
                body: std::sync::Arc::new(body_vec),
            }
        }
        other => other,
    }
}

/// Top-level entry into a node (no enclosing loop context).
fn elide_in_node(node: Node, changed: &mut bool) -> Node {
    elide_in_node_with_ctx(node, None, changed)
}

/// True iff `node` (or any descendant) contains a Loop whose body holds
/// at least one redundant `if loop_var < to_lit` guard.
fn node_has_redundant_guard(node: &Node) -> bool {
    has_redundant_guard_with_ctx(node, None)
}

fn has_redundant_guard_with_ctx(node: &Node, loop_ctx: Option<(&str, u32)>) -> bool {
    match node {
        Node::If {
            cond,
            then,
            otherwise,
        } => {
            if otherwise.is_empty() {
                if let Some((loop_var, loop_to)) = loop_ctx {
                    if let Some(rhs_lit) = cond_matches_loop_var_lt_lit(cond, loop_var) {
                        if rhs_lit == loop_to {
                            return true;
                        }
                    }
                }
            }
            then.iter()
                .any(|n| has_redundant_guard_with_ctx(n, loop_ctx))
                || otherwise
                    .iter()
                    .any(|n| has_redundant_guard_with_ctx(n, loop_ctx))
        }
        Node::Loop { var, to, body, .. } => {
            let new_ctx = lit_u32_value(to).map(|to_lit| (var.as_str(), to_lit));
            body.iter()
                .any(|n| has_redundant_guard_with_ctx(n, new_ctx))
        }
        Node::Block(body) => body
            .iter()
            .any(|n| has_redundant_guard_with_ctx(n, loop_ctx)),
        Node::Region { body, .. } => body.iter().any(|n| has_redundant_guard_with_ctx(n, None)),
        _ => false,
    }
}

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

    fn buf() -> BufferDecl {
        BufferDecl::storage("buf", 0, BufferAccess::ReadWrite, DataType::U32).with_count(4)
    }

    fn program_with_entry(entry: Vec<Node>) -> Program {
        Program::wrapped(vec![buf()], [1, 1, 1], entry)
    }

    fn loop_with_body(var: &str, to: u32, body: Vec<Node>) -> Node {
        Node::Loop {
            var: Ident::from(var),
            from: Expr::u32(0),
            to: Expr::u32(to),
            body,
        }
    }

    fn count_redundant_if_guards(node: &Node, loop_ctx: Option<(&str, u32)>) -> usize {
        let mut count = 0;
        match node {
            Node::If {
                cond,
                then,
                otherwise,
            } => {
                if otherwise.is_empty() {
                    if let Some((loop_var, loop_to)) = loop_ctx {
                        if let Some(rhs_lit) = cond_matches_loop_var_lt_lit(cond, loop_var) {
                            if rhs_lit == loop_to {
                                count += 1;
                            }
                        }
                    }
                }
                for n in then {
                    count += count_redundant_if_guards(n, loop_ctx);
                }
                for n in otherwise {
                    count += count_redundant_if_guards(n, loop_ctx);
                }
            }
            Node::Loop { var, to, body, .. } => {
                let new_ctx = lit_u32_value(to).map(|to_lit| (var.as_str(), to_lit));
                for n in body {
                    count += count_redundant_if_guards(n, new_ctx);
                }
            }
            Node::Block(body) => {
                for n in body {
                    count += count_redundant_if_guards(n, loop_ctx);
                }
            }
            Node::Region { body, .. } => {
                for n in body.iter() {
                    count += count_redundant_if_guards(n, None);
                }
            }
            _ => {}
        }
        count
    }

    #[test]
    fn skip_analysis_on_program_without_loop() {
        let entry = vec![Node::store("buf", Expr::u32(0), Expr::u32(1))];
        let program = program_with_entry(entry);
        assert_eq!(
            LoopRedundantBoundCheckElidePass::analyze(&program),
            PassAnalysis::SKIP
        );
    }

    #[test]
    fn skip_analysis_on_loop_without_redundant_guard() {
        // Loop body has no inner if at all.
        let entry = vec![loop_with_body(
            "i",
            10,
            vec![Node::store("buf", Expr::var("i"), Expr::u32(7))],
        )];
        let program = program_with_entry(entry);
        assert_eq!(
            LoopRedundantBoundCheckElidePass::analyze(&program),
            PassAnalysis::SKIP
        );
    }

    #[test]
    fn run_analysis_on_loop_with_redundant_guard() {
        let entry = vec![loop_with_body(
            "i",
            10,
            vec![Node::if_then(
                Expr::lt(Expr::var("i"), Expr::u32(10)),
                vec![Node::store("buf", Expr::var("i"), Expr::u32(7))],
            )],
        )];
        let program = program_with_entry(entry);
        assert_eq!(
            LoopRedundantBoundCheckElidePass::analyze(&program),
            PassAnalysis::RUN
        );
    }

    #[test]
    fn transform_elides_simple_redundant_guard() {
        let entry = vec![loop_with_body(
            "i",
            10,
            vec![Node::if_then(
                Expr::lt(Expr::var("i"), Expr::u32(10)),
                vec![Node::store("buf", Expr::var("i"), Expr::u32(7))],
            )],
        )];
        let program = program_with_entry(entry);
        let result = LoopRedundantBoundCheckElidePass::transform(program);
        assert!(result.changed);
        let total: usize = result
            .program
            .entry()
            .iter()
            .map(|n| count_redundant_if_guards(n, None))
            .sum();
        assert_eq!(
            total, 0,
            "no redundant guards must remain after the elision pass"
        );
    }

    #[test]
    fn transform_does_not_elide_when_lit_does_not_match_loop_to() {
        // Guard checks i < 8 inside a loop with to=10. The guard is NOT
        // redundant — inside the loop the i can hit 9.
        let entry = vec![loop_with_body(
            "i",
            10,
            vec![Node::if_then(
                Expr::lt(Expr::var("i"), Expr::u32(8)),
                vec![Node::store("buf", Expr::var("i"), Expr::u32(7))],
            )],
        )];
        let program = program_with_entry(entry);
        let result = LoopRedundantBoundCheckElidePass::transform(program);
        assert!(
            !result.changed,
            "non-matching literal must not trigger elision"
        );
    }

    #[test]
    fn transform_does_not_elide_when_var_name_does_not_match() {
        // Guard checks j < 10 (unrelated name) inside a loop over i. The
        // guard is NOT redundant.
        let entry = vec![loop_with_body(
            "i",
            10,
            vec![Node::if_then(
                Expr::lt(Expr::var("j"), Expr::u32(10)),
                vec![Node::store("buf", Expr::u32(0), Expr::u32(7))],
            )],
        )];
        let program = program_with_entry(entry);
        let result = LoopRedundantBoundCheckElidePass::transform(program);
        assert!(
            !result.changed,
            "different var name must not trigger elision"
        );
    }

    #[test]
    fn transform_does_not_elide_when_loop_to_is_not_literal() {
        // Loop bound is `Var("n")` not a literal — no redundancy proof.
        let entry = vec![Node::Loop {
            var: Ident::from("i"),
            from: Expr::u32(0),
            to: Expr::var("n"),
            body: vec![Node::if_then(
                Expr::lt(Expr::var("i"), Expr::u32(10)),
                vec![Node::store("buf", Expr::var("i"), Expr::u32(7))],
            )],
        }];
        let program = program_with_entry(entry);
        let result = LoopRedundantBoundCheckElidePass::transform(program);
        assert!(
            !result.changed,
            "non-literal loop bound must not trigger elision"
        );
    }

    #[test]
    fn transform_does_not_elide_when_else_branch_is_nonempty() {
        // The pass refuses if-then-else patterns — only if-then.
        let entry = vec![loop_with_body(
            "i",
            10,
            vec![Node::if_then_else(
                Expr::lt(Expr::var("i"), Expr::u32(10)),
                vec![Node::store("buf", Expr::var("i"), Expr::u32(7))],
                vec![Node::store("buf", Expr::var("i"), Expr::u32(0))],
            )],
        )];
        let program = program_with_entry(entry);
        let result = LoopRedundantBoundCheckElidePass::transform(program);
        assert!(
            !result.changed,
            "if-then-else (with else body) must not be elided"
        );
    }

    #[test]
    fn transform_handles_nested_loops_independently() {
        // Outer loop var i, to=10. Inner loop var j, to=5. The inner if
        // checks j < 5 — redundant for the inner loop but not the outer.
        let inner = loop_with_body(
            "j",
            5,
            vec![Node::if_then(
                Expr::lt(Expr::var("j"), Expr::u32(5)),
                vec![Node::store("buf", Expr::var("j"), Expr::u32(7))],
            )],
        );
        let entry = vec![loop_with_body("i", 10, vec![inner])];
        let program = program_with_entry(entry);
        let result = LoopRedundantBoundCheckElidePass::transform(program);
        assert!(result.changed, "inner-loop guard must be elided");
        let total: usize = result
            .program
            .entry()
            .iter()
            .map(|n| count_redundant_if_guards(n, None))
            .sum();
        assert_eq!(total, 0);
    }

    #[test]
    fn transform_does_not_elide_inner_guard_against_outer_loop_var() {
        // Inner loop has its own var; the inner if checks i < 10 (outer
        // var). Inner ctx (j, 5) doesn't match (i, 10). Outer ctx (i, 10)
        // is shadowed once we enter the inner loop. The pass should NOT
        // elide because at the if-site, only the inner loop ctx is
        // available.
        let inner = Node::Loop {
            var: Ident::from("j"),
            from: Expr::u32(0),
            to: Expr::u32(5),
            body: vec![Node::if_then(
                Expr::lt(Expr::var("i"), Expr::u32(10)),
                vec![Node::store("buf", Expr::var("j"), Expr::u32(7))],
            )],
        };
        let entry = vec![loop_with_body("i", 10, vec![inner])];
        let program = program_with_entry(entry);
        let result = LoopRedundantBoundCheckElidePass::transform(program);
        assert!(
            !result.changed,
            "inner-loop scope must shadow outer; if-guard against outer var stays"
        );
    }

    #[test]
    fn transform_is_idempotent() {
        let entry = vec![loop_with_body(
            "i",
            10,
            vec![Node::if_then(
                Expr::lt(Expr::var("i"), Expr::u32(10)),
                vec![Node::store("buf", Expr::var("i"), Expr::u32(7))],
            )],
        )];
        let program = program_with_entry(entry);
        let once = LoopRedundantBoundCheckElidePass::transform(program);
        let twice = LoopRedundantBoundCheckElidePass::transform(once.program.clone());
        assert!(once.changed);
        assert!(!twice.changed, "second run must report no change");
    }

    #[test]
    fn transform_handles_empty_program() {
        let program = Program::wrapped(vec![buf()], [1, 1, 1], vec![]);
        let result = LoopRedundantBoundCheckElidePass::transform(program);
        assert!(!result.changed);
    }

    #[test]
    fn transform_does_not_drop_then_body_during_elision() {
        // The `then` body MUST survive the elision (wrapped in a Block).
        let entry = vec![loop_with_body(
            "i",
            10,
            vec![Node::if_then(
                Expr::lt(Expr::var("i"), Expr::u32(10)),
                vec![
                    Node::store("buf", Expr::var("i"), Expr::u32(7)),
                    Node::store("buf", Expr::u32(0), Expr::u32(8)),
                ],
            )],
        )];
        let program = program_with_entry(entry);
        let result = LoopRedundantBoundCheckElidePass::transform(program);
        assert!(result.changed);
        // Walk the loop body and count Stores — must be 2.
        fn count_stores(node: &Node) -> usize {
            let mut count = 0;
            match node {
                Node::Store { .. } => count += 1,
                Node::Loop { body, .. } => {
                    for n in body {
                        count += count_stores(n);
                    }
                }
                Node::Block(body) => {
                    for n in body {
                        count += count_stores(n);
                    }
                }
                Node::Region { body, .. } => {
                    for n in body.iter() {
                        count += count_stores(n);
                    }
                }
                Node::If {
                    then, otherwise, ..
                } => {
                    for n in then {
                        count += count_stores(n);
                    }
                    for n in otherwise {
                        count += count_stores(n);
                    }
                }
                _ => {}
            }
            count
        }
        let total: usize = result.program.entry().iter().map(count_stores).sum();
        assert_eq!(
            total, 2,
            "both stores from the elided then-body must still be present"
        );
    }

    #[test]
    fn fingerprint_returns_stable_value() {
        let entry = vec![loop_with_body(
            "i",
            10,
            vec![Node::if_then(
                Expr::lt(Expr::var("i"), Expr::u32(10)),
                vec![Node::store("buf", Expr::var("i"), Expr::u32(7))],
            )],
        )];
        let program = program_with_entry(entry);
        let fp1 = LoopRedundantBoundCheckElidePass::fingerprint(&program);
        let fp2 = LoopRedundantBoundCheckElidePass::fingerprint(&program);
        assert_eq!(fp1, fp2);
    }

    #[test]
    fn cond_matches_loop_var_lt_lit_extracts_correct_literal() {
        let cond = Expr::lt(Expr::var("i"), Expr::u32(42));
        assert_eq!(cond_matches_loop_var_lt_lit(&cond, "i"), Some(42));
    }

    #[test]
    fn cond_matches_loop_var_lt_lit_rejects_wrong_var_name() {
        let cond = Expr::lt(Expr::var("j"), Expr::u32(42));
        assert_eq!(cond_matches_loop_var_lt_lit(&cond, "i"), None);
    }

    #[test]
    fn cond_matches_loop_var_lt_lit_rejects_non_lt_op() {
        let cond = Expr::eq(Expr::var("i"), Expr::u32(42));
        assert_eq!(cond_matches_loop_var_lt_lit(&cond, "i"), None);
    }
}