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
//! ROADMAP H5 (foundation_optimizer half)  -  region-fusion hint pass.
//!
//! Detects adjacent `Node::Region` pairs whose generator names
//! match a fusion rule in the built-in table (e.g.
//! `vyre-libs::nn::linear` followed by `vyre-libs::nn::relu`),
//! and rewrites them to a single Region whose generator is the
//! fused-primitive id (`vyre-libs::nn::linear_relu`). The fused
//! Region's body is the concatenation of the two arms; downstream
//! lowering reads the generator id and dispatches the existing
//! fused libs primitive instead of two separate dispatches.
//!
//! Soundness: `Approximate`  -  the rewrite is correct only if the
//! fused primitive is observably equivalent to the two-stage
//! sequence, which is true for the entries in the built-in fusion
//! table (linear+relu, linear+silu) by construction. The table is
//! the contract; new fusion candidates land alongside their fused
//! libs primitive.
//!
//! Cost direction: monotone-down on dispatch count (one less
//! kernel launch per fired fusion) and on global memory traffic
//! (the intermediate buffer between the two stages stays in
//! registers / scratch instead of round-tripping through global).
//!
//! Preserves: every analysis. Invalidates: nothing  -  the fused
//! Region produces the same observable output the two-stage
//! sequence did.

use crate::ir::{Node, Program};
use crate::optimizer::{vyre_pass, PassAnalysis, PassResult};
use std::sync::Arc;

/// Fuse adjacent compatible Regions per the built-in fusion table.
#[derive(Debug, Default)]
#[vyre_pass(
    name = "region_fusion_hint",
    requires = [],
    invalidates = [],
    phase = "cleanup",
    boundary_class = "abi_preserving",
    cost_model_family = "fusion"
)]
pub struct RegionFusionHintPass;

impl RegionFusionHintPass {
    /// Skip programs without a candidate Region pair. Checks both the
    /// top-level entry vec (transform fuses adjacent siblings there too)
    /// and every nested If/Loop/Block/Region body.
    #[must_use]
    fn analyze_impl(program: &Program) -> PassAnalysis {
        // Pair-fusion needs at least two Regions; even one Region is
        // necessary. Bit-test cached stats first.
        if !program
            .stats()
            .has_any_node_kind(crate::ir::stats::NODE_KIND_REGION)
        {
            return PassAnalysis::SKIP;
        }
        if entry_has_top_level_candidate_pair(program.entry())
            || program.entry().iter().any(has_candidate_pair)
        {
            PassAnalysis::RUN
        } else {
            PassAnalysis::SKIP
        }
    }

    /// Walk the entry tree and fuse every matching Region pair.
    #[must_use]
    pub fn transform(program: Program) -> PassResult {
        let mut changed = false;
        let program = program.map_entry(|entry| fuse_in_body(entry, &mut changed));
        PassResult { program, changed }
    }
}

/// Built-in fusion table. Adding a new fusion candidate requires
/// (1) shipping the fused libs primitive, (2) adding the (left,
/// right, fused) triple here. Order matters: `left` is the upstream
/// region producing the intermediate, `right` consumes it.
const FUSION_RULES: &[(&str, &str, &str)] = &[
    (
        "vyre-libs::nn::linear",
        "vyre-libs::nn::relu",
        "vyre-libs::nn::linear_relu",
    ),
    (
        "vyre-libs::nn::linear",
        "vyre-libs::nn::silu",
        "vyre-libs::nn::linear_silu",
    ),
];

fn lookup_fused(left_gen: &str, right_gen: &str) -> Option<&'static str> {
    FUSION_RULES
        .iter()
        .find(|(l, r, _)| *l == left_gen && *r == right_gen)
        .map(|(_, _, f)| *f)
}

fn fuse_in_body(body: Vec<Node>, changed: &mut bool) -> Vec<Node> {
    let body: Vec<Node> = body.into_iter().map(|n| recurse(n, changed)).collect();
    let mut out: Vec<Node> = Vec::with_capacity(body.len());
    let mut iter = body.into_iter().peekable();
    while let Some(node) = iter.next() {
        let Node::Region {
            generator,
            source_region,
            body,
        } = node
        else {
            out.push(node);
            continue;
        };
        let next_match = matches!(
            iter.peek(),
            Some(Node::Region {
                generator: g, ..
            }) if lookup_fused(generator.as_str(), g.as_str()).is_some()
        );
        if !next_match {
            out.push(Node::Region {
                generator,
                source_region,
                body,
            });
            continue;
        }
        let Some(Node::Region {
            generator: gen_b,
            source_region: _src_b,
            body: body_b,
        }) = iter.next()
        else {
            unreachable!("peek confirmed Region above");
        };
        let fused_gen = lookup_fused(generator.as_str(), gen_b.as_str())
            .unwrap_or_else(|| unreachable!("has_candidate_pair confirmed a fusable pair above"));
        let mut fused_body: Vec<Node> = match Arc::try_unwrap(body) {
            Ok(v) => v,
            Err(arc) => (*arc).clone(),
        };
        let body_b_vec: Vec<Node> = match Arc::try_unwrap(body_b) {
            Ok(v) => v,
            Err(arc) => (*arc).clone(),
        };
        fused_body.extend(body_b_vec);
        *changed = true;
        out.push(Node::Region {
            generator: crate::ir::Ident::from(fused_gen),
            source_region,
            body: Arc::new(fused_body),
        });
    }
    out
}

fn recurse(node: Node, changed: &mut bool) -> Node {
    match node {
        Node::If {
            cond,
            then,
            otherwise,
        } => Node::If {
            cond,
            then: fuse_in_body(then, changed),
            otherwise: fuse_in_body(otherwise, changed),
        },
        Node::Loop {
            var,
            from,
            to,
            body,
        } => Node::Loop {
            var,
            from,
            to,
            body: fuse_in_body(body, changed),
        },
        Node::Block(body) => Node::Block(fuse_in_body(body, changed)),
        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(fuse_in_body(body_vec, changed)),
            }
        }
        other => other,
    }
}

fn entry_has_top_level_candidate_pair(entry: &[Node]) -> bool {
    for window in entry.windows(2) {
        if let (Node::Region { generator: a, .. }, Node::Region { generator: b, .. }) =
            (&window[0], &window[1])
        {
            if lookup_fused(a.as_str(), b.as_str()).is_some() {
                return true;
            }
        }
    }
    false
}

fn has_candidate_pair(node: &Node) -> bool {
    match node {
        Node::Region { body, .. } => {
            let body = body.as_ref();
            for window in body.windows(2) {
                if let (Node::Region { generator: a, .. }, Node::Region { generator: b, .. }) =
                    (&window[0], &window[1])
                {
                    if lookup_fused(a.as_str(), b.as_str()).is_some() {
                        return true;
                    }
                }
            }
            body.iter().any(has_candidate_pair)
        }
        Node::If {
            then, otherwise, ..
        } => {
            for window in then.windows(2) {
                if let (Node::Region { generator: a, .. }, Node::Region { generator: b, .. }) =
                    (&window[0], &window[1])
                {
                    if lookup_fused(a.as_str(), b.as_str()).is_some() {
                        return true;
                    }
                }
            }
            for window in otherwise.windows(2) {
                if let (Node::Region { generator: a, .. }, Node::Region { generator: b, .. }) =
                    (&window[0], &window[1])
                {
                    if lookup_fused(a.as_str(), b.as_str()).is_some() {
                        return true;
                    }
                }
            }
            then.iter().any(has_candidate_pair) || otherwise.iter().any(has_candidate_pair)
        }
        Node::Loop { body, .. } => {
            for window in body.windows(2) {
                if let (Node::Region { generator: a, .. }, Node::Region { generator: b, .. }) =
                    (&window[0], &window[1])
                {
                    if lookup_fused(a.as_str(), b.as_str()).is_some() {
                        return true;
                    }
                }
            }
            body.iter().any(has_candidate_pair)
        }
        Node::Block(body) => {
            for window in body.windows(2) {
                if let (Node::Region { generator: a, .. }, Node::Region { generator: b, .. }) =
                    (&window[0], &window[1])
                {
                    if lookup_fused(a.as_str(), b.as_str()).is_some() {
                        return true;
                    }
                }
            }
            body.iter().any(has_candidate_pair)
        }
        _ => false,
    }
}

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

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

    fn region(generator_name: &str, body: Vec<Node>) -> Node {
        Node::Region {
            generator: Ident::from(generator_name),
            source_region: None,
            body: Arc::new(body),
        }
    }

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

    fn region_generators(nodes: &[Node]) -> Vec<String> {
        let mut out = Vec::new();
        fn walk(nodes: &[Node], out: &mut Vec<String>) {
            for n in nodes {
                if let Node::Region {
                    generator, body, ..
                } = n
                {
                    out.push(generator.as_str().to_owned());
                    walk(body.as_ref(), out);
                }
                match n {
                    Node::If {
                        then, otherwise, ..
                    } => {
                        walk(then, out);
                        walk(otherwise, out);
                    }
                    Node::Loop { body, .. } => walk(body, out),
                    Node::Block(body) => walk(body, out),
                    _ => {}
                }
            }
        }
        walk(nodes, &mut out);
        out
    }

    /// Adjacent linear + relu Regions fuse into linear_relu.
    #[test]
    fn fuses_linear_then_relu() {
        let entry = vec![
            region("vyre-libs::nn::linear", vec![Node::Return]),
            region("vyre-libs::nn::relu", vec![Node::Return]),
        ];
        let result = RegionFusionHintPass::transform(program(entry));
        assert!(result.changed, "linear+relu must fuse");
        let gens = region_generators(result.program.entry());
        assert!(
            gens.iter().any(|g| g == "vyre-libs::nn::linear_relu"),
            "generators after fuse: {gens:?}"
        );
    }

    /// Adjacent linear + silu Regions fuse into linear_silu.
    #[test]
    fn fuses_linear_then_silu() {
        let entry = vec![
            region("vyre-libs::nn::linear", vec![Node::Return]),
            region("vyre-libs::nn::silu", vec![Node::Return]),
        ];
        let result = RegionFusionHintPass::transform(program(entry));
        assert!(result.changed, "linear+silu must fuse");
        let gens = region_generators(result.program.entry());
        assert!(
            gens.iter().any(|g| g == "vyre-libs::nn::linear_silu"),
            "generators after fuse: {gens:?}"
        );
    }

    /// relu + linear (wrong order) does not fuse.
    #[test]
    fn keeps_when_order_reversed() {
        let entry = vec![
            region("vyre-libs::nn::relu", vec![Node::Return]),
            region("vyre-libs::nn::linear", vec![Node::Return]),
        ];
        let result = RegionFusionHintPass::transform(program(entry));
        assert!(!result.changed, "wrong order must not fuse");
    }

    /// Two unrelated Regions do not fuse.
    #[test]
    fn keeps_when_no_rule_matches() {
        let entry = vec![
            region("foo::bar", vec![Node::Return]),
            region("baz::qux", vec![Node::Return]),
        ];
        let result = RegionFusionHintPass::transform(program(entry));
        assert!(!result.changed);
    }

    /// `analyze` short-circuits when no candidate pair exists.
    #[test]
    fn analyze_skips_when_no_candidate() {
        let entry = vec![region("foo::bar", vec![Node::Return])];
        let prog = program(entry);
        match crate::optimizer::ProgramPass::analyze(&RegionFusionHintPass, &prog) {
            PassAnalysis::SKIP => {}
            other => panic!("expected SKIP, got {other:?}"),
        }
    }

    /// Nested fusion: linear+relu inside a wrapping Region also fuses.
    #[test]
    fn fuses_inside_wrapping_region() {
        let inner = vec![
            region("vyre-libs::nn::linear", vec![Node::Return]),
            region("vyre-libs::nn::relu", vec![Node::Return]),
        ];
        let entry = vec![region("wrapper", inner)];
        let result = RegionFusionHintPass::transform(program(entry));
        assert!(result.changed);
        let gens = region_generators(result.program.entry());
        assert!(
            gens.iter().any(|g| g == "vyre-libs::nn::linear_relu"),
            "generators: {gens:?}"
        );
    }

    /// Two adjacent fusable Regions sitting at the TOP LEVEL of the
    /// entry Vec must trip analyze. The previous shallow check ran
    /// has_candidate_pair on each entry node individually, never
    /// looking at sibling pairs in the entry vec, so analyze SKIPPed
    /// while transform would happily fuse them.
    #[test]
    fn analyze_runs_for_top_level_adjacent_fusable_pair() {
        let entry = vec![
            region("vyre-libs::nn::linear", vec![Node::Return]),
            region("vyre-libs::nn::relu", vec![Node::Return]),
        ];
        let prog = program(entry);
        assert_eq!(
            crate::optimizer::ProgramPass::analyze(&RegionFusionHintPass, &prog),
            PassAnalysis::RUN,
            "top-level adjacent fusable Region pair must trigger analyze"
        );
    }
}