vyre-foundation 0.7.2

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
use crate::ir::{Ident, Node, Program};
use crate::optimizer::fact_substrate::{FactSubstrate, UseFacts};
use crate::optimizer::{vyre_pass, PassAnalysis, PassResult};
use rustc_hash::FxHashSet;
use std::sync::Arc;

/// Remove buffers whose contents cannot contribute to observable output.
#[derive(Debug, Default)]
#[vyre_pass(
    name = "dead_buffer_elim",
    requires = ["fusion"],
    invalidates = ["buffer_layout"],
    phase = "memory",
    boundary_class = "abi_changing",
    cost_model_family = "memory"
)]
pub struct DeadBufferElim;

impl DeadBufferElim {
    /// Decide whether this pass should run.
    #[must_use]
    #[inline]
    fn analyze_impl(program: &Program) -> PassAnalysis {
        if live_buffers(program).len() == program.buffers().len() {
            PassAnalysis::SKIP
        } else {
            PassAnalysis::RUN
        }
    }

    /// Remove dead buffer declarations and stores to dead buffers.
    #[must_use]
    pub fn transform(program: Program) -> PassResult {
        let live = live_buffers(&program);
        if live.len() == program.buffers().len() {
            return PassResult::unchanged(program);
        }
        let workgroup_size = program.workgroup_size();
        let entry_op_id = program.entry_op_id().map(ToOwned::to_owned);
        let non_composable = program.is_non_composable_with_self();

        // Drop stores to dead buffers first; the surviving nodes then decide
        // which DECLARATIONS must stay. Output-liveness answers "which stores
        // are dead", but it is the wrong question for "which buffers to keep":
        // a buffer read only in a control position -- an `If`/`Loop` guard
        // whose body has no live store -- feeds no output yet is still
        // referenced by the surviving guard (`filter_nodes` removes dead-target
        // stores, never the guard itself). Keeping only output-live buffers
        // would drop its declaration and leave a dangling load that fails IR
        // validation ("load from unknown buffer"). The keep-set is therefore
        // output-liveness UNION reference-liveness over the filtered program:
        // keep a buffer if it feeds an output OR is still read/written by a
        // surviving node.
        let staged = Program::wrapped(
            program.buffers().to_vec(),
            workgroup_size,
            filter_nodes(program.entry(), &live),
        );
        let referenced = referenced_buffers(&staged);

        // `live` already counts outputs (the launch shapes carry 60+ buffers),
        // so the kept set is at most the original count; pre-size to avoid
        // grow-by-doubling.
        let mut buffers: Vec<_> = Vec::with_capacity(program.buffers().len());
        buffers.extend(
            program
                .buffers()
                .iter()
                .filter(|buffer| {
                    let name = buffer.name.as_ref();
                    live.contains(name) || referenced.contains(name)
                })
                .cloned(),
        );
        let entry = staged.into_entry_vec();

        if buffers.len() == program.buffers().len() && entry.as_slice() == program.entry() {
            return PassResult::unchanged(program);
        }

        let optimized = Program::wrapped(buffers, workgroup_size, entry)
            .with_optional_entry_op_id(entry_op_id)
            .with_non_composable_with_self(non_composable);
        PassResult {
            program: optimized,
            changed: true,
        }
    }
}

type LiveBufferSet<'a> = FxHashSet<&'a str>;

fn live_buffers(program: &Program) -> LiveBufferSet<'_> {
    let live = cached_live_buffer_idents(program);
    program
        .buffers()
        .iter()
        .filter_map(|buffer| {
            live.contains(buffer.name.as_ref())
                .then_some(buffer.name.as_ref())
        })
        .collect()
}

/// Every buffer still read or written by `staged` (the program AFTER
/// dead-target stores were filtered out). Reuses the canonical use-fact
/// derivation, so control-position reads (`If`/`Loop` guards), addressing
/// reads, `buflen`, collective and async accesses are all counted -- exactly
/// the buffer references the IR validator requires to be declared. A buffer
/// absent from this set AND from output-liveness has no surviving reference
/// and is safe to drop.
///
/// `staged` is reached only for non-opaque programs (an opaque program keeps
/// every buffer live, so `transform` returns before building `staged`), so
/// `buffer_reads`/`buffer_writes` capture the complete reference surface.
fn referenced_buffers(staged: &Program) -> FxHashSet<Ident> {
    let substrate = FactSubstrate::derive_use_only(staged);
    let use_facts = substrate
        .use_facts()
        .unwrap_or_else(|| unreachable!("derive_use_only contract: use_facts is always populated"));
    use_facts
        .buffer_reads
        .keys()
        .chain(use_facts.buffer_writes.keys())
        .cloned()
        .collect()
}

fn cached_live_buffer_idents(program: &Program) -> FxHashSet<Ident> {
    let substrate = FactSubstrate::derive_use_only_cached(program);
    let use_facts = substrate.use_facts().unwrap_or_else(|| {
        unreachable!("derive_use_only_cached contract: use_facts is always populated")
    });
    compute_live_buffer_idents(program, use_facts)
}

fn compute_live_buffer_idents(program: &Program, use_facts: &UseFacts) -> FxHashSet<Ident> {
    if use_facts.has_opaque {
        return program
            .buffers()
            .iter()
            .map(|buffer| Ident::new(Arc::clone(&buffer.name)))
            .collect();
    }

    let mut live = program
        .buffers()
        .iter()
        .filter(|buffer| buffer.is_output() || buffer.is_pipeline_live_out())
        .map(|buffer| Ident::new(Arc::clone(&buffer.name)))
        .collect::<FxHashSet<_>>();
    let mut worklist = Vec::with_capacity(live.len() + use_facts.indirect_dispatch_buffers.len());
    worklist.extend(live.iter().cloned());

    for buffer in &use_facts.indirect_dispatch_buffers {
        let buffer = buffer.clone();
        if live.insert(buffer.clone()) {
            worklist.push(buffer);
        }
    }

    while let Some(buffer) = worklist.pop() {
        let Some(deps) = use_facts.buffer_write_deps.get(&buffer) else {
            continue;
        };
        for dep in deps {
            let dep = dep.clone();
            if live.insert(dep.clone()) {
                worklist.push(dep);
            }
        }
    }

    live
}

fn filter_nodes(nodes: &[Node], live: &LiveBufferSet<'_>) -> Vec<Node> {
    let mut out = Vec::with_capacity(nodes.len());
    out.extend(nodes.iter().filter_map(|node| filter_node(node, live)));
    out
}

fn filter_node(node: &Node, live: &LiveBufferSet<'_>) -> Option<Node> {
    match node {
        Node::Store { buffer, .. } if !live.contains(buffer.as_str()) => None,
        Node::AsyncStore { destination, .. } if !live.contains(destination.as_str()) => None,
        Node::AsyncLoad { destination, .. } if !live.contains(destination.as_str()) => None,
        Node::Region {
            generator,
            source_region,
            body,
        } => Some(Node::Region {
            generator: generator.clone(),
            source_region: source_region.clone(),
            body: Arc::new(filter_nodes(body, live)),
        }),
        Node::If {
            cond,
            then,
            otherwise,
        } => Some(Node::if_then_else(
            cond.clone(),
            filter_nodes(then, live),
            filter_nodes(otherwise, live),
        )),
        Node::Loop {
            var,
            from,
            to,
            body,
        } => Some(Node::loop_for(
            var,
            from.clone(),
            to.clone(),
            filter_nodes(body, live),
        )),
        Node::Block(nodes) => Some(Node::block(filter_nodes(nodes, live))),
        other => Some(other.clone()),
    }
}

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

    #[test]
    fn unread_buffer_removed() {
        let optimized = run(sample_program(false));
        assert!(optimized.buffer("scratch").is_none());
    }

    #[test]
    fn output_buffer_preserved() {
        let optimized = run(sample_program(false));
        assert!(optimized.buffer("out").is_some());
    }

    fn run(program: Program) -> Program {
        DeadBufferElim::transform(program).program
    }

    fn sample_program(read_scratch: bool) -> Program {
        Program::wrapped(
            vec![
                BufferDecl::output("out", 0, DataType::U32).with_count(1),
                BufferDecl::read_write("scratch", 1, DataType::U32).with_count(1),
            ],
            [1, 1, 1],
            if read_scratch {
                vec![
                    Node::store("scratch", Expr::u32(0), Expr::u32(999)),
                    Node::store("out", Expr::u32(0), Expr::load("scratch", Expr::u32(0))),
                ]
            } else {
                vec![
                    Node::store("scratch", Expr::u32(0), Expr::u32(999)),
                    Node::store("out", Expr::u32(0), Expr::u32(7)),
                ]
            },
        )
    }

    #[test]
    fn read_used_buffer_preserved() {
        // scratch IS read by output → must not be eliminated.
        let optimized = run(sample_program(true));
        assert!(
            optimized.buffer("scratch").is_some(),
            "scratch is read by out, must stay"
        );
        assert!(optimized.buffer("out").is_some());
    }

    #[test]
    fn let_mediated_buffer_read_preserves_source_buffer() {
        let program = Program::wrapped(
            vec![
                BufferDecl::output("out", 0, DataType::U32).with_count(1),
                BufferDecl::read_write("scratch", 1, DataType::U32).with_count(1),
            ],
            [1, 1, 1],
            vec![
                Node::store("scratch", Expr::u32(0), Expr::u32(99)),
                Node::let_bind("x", Expr::load("scratch", Expr::u32(0))),
                Node::store("out", Expr::u32(0), Expr::var("x")),
            ],
        );

        let optimized = run(program);
        assert!(
            optimized.buffer("scratch").is_some(),
            "scratch feeds the output through scalar binding `x`; removing it leaves a dangling load"
        );
    }

    #[test]
    fn pipeline_live_out_buffer_preserved() {
        let program = Program::wrapped(
            vec![BufferDecl::read_write("pipeline_buf", 0, DataType::F32)
                .with_count(4)
                .with_pipeline_live_out(true)],
            [1, 1, 1],
            vec![], // no stores at all, but pipeline_live_out keeps it alive
        );
        let optimized = run(program);
        assert!(
            optimized.buffer("pipeline_buf").is_some(),
            "pipeline_live_out buffers must never be eliminated"
        );
    }

    #[test]
    fn transitive_liveness_through_chain() {
        // a → scratch → out: scratch feeds into out, a feeds into scratch.
        let program = Program::wrapped(
            vec![
                BufferDecl::output("out", 0, DataType::U32).with_count(1),
                BufferDecl::read_write("scratch", 1, DataType::U32).with_count(1),
                BufferDecl::read_write("a", 2, DataType::U32).with_count(1),
            ],
            [1, 1, 1],
            vec![
                Node::store("a", Expr::u32(0), Expr::u32(42)),
                Node::store("scratch", Expr::u32(0), Expr::load("a", Expr::u32(0))),
                Node::store("out", Expr::u32(0), Expr::load("scratch", Expr::u32(0))),
            ],
        );
        let optimized = run(program);
        assert!(
            optimized.buffer("a").is_some(),
            "a is transitively live via scratch→out"
        );
        assert!(optimized.buffer("scratch").is_some());
        assert!(optimized.buffer("out").is_some());
    }

    #[test]
    fn scalar_mediated_transitive_liveness_uses_shared_facts() {
        let program = Program::wrapped(
            vec![
                BufferDecl::read("input", 0, DataType::U32).with_count(1),
                BufferDecl::read_write("scratch", 1, DataType::U32).with_count(1),
                BufferDecl::read_write("dead", 2, DataType::U32).with_count(1),
                BufferDecl::output("out", 3, DataType::U32).with_count(1),
            ],
            [1, 1, 1],
            vec![
                Node::let_bind("x", Expr::load("input", Expr::u32(0))),
                Node::store("scratch", Expr::u32(0), Expr::var("x")),
                Node::store("dead", Expr::u32(0), Expr::u32(99)),
                Node::store("out", Expr::u32(0), Expr::load("scratch", Expr::u32(0))),
            ],
        );

        let optimized = run(program);
        assert!(optimized.buffer("input").is_some());
        assert!(optimized.buffer("scratch").is_some());
        assert!(optimized.buffer("out").is_some());
        assert!(optimized.buffer("dead").is_none());
    }

    #[test]
    fn indirect_dispatch_count_buffer_is_live() {
        let program = Program::wrapped(
            vec![
                BufferDecl::read("counts", 0, DataType::U32).with_count(1),
                BufferDecl::read_write("dead", 1, DataType::U32).with_count(1),
            ],
            [1, 1, 1],
            vec![
                Node::store("dead", Expr::u32(0), Expr::u32(99)),
                Node::indirect_dispatch("counts", 0),
            ],
        );

        let optimized = run(program);
        assert!(optimized.buffer("counts").is_some());
        assert!(optimized.buffer("dead").is_none());
    }

    #[test]
    fn analyze_skips_when_all_buffers_live() {
        // Every buffer is either output or read by output → SKIP.
        let program = Program::wrapped(
            vec![BufferDecl::output("out", 0, DataType::U32).with_count(1)],
            [1, 1, 1],
            vec![Node::store("out", Expr::u32(0), Expr::u32(1))],
        );
        assert_eq!(
            crate::optimizer::ProgramPass::analyze(&DeadBufferElim, &program),
            PassAnalysis::SKIP
        );
    }

    /// WHY: liveness may keep every declaration after filtering even when no buffer is an output;
    /// reporting that no-op as changed prevents the fixed-point scheduler from converging.
    #[test]
    fn side_effectful_control_reference_reports_unchanged() {
        let program = Program::wrapped(
            vec![BufferDecl::read_write("state", 0, DataType::U32).with_count(1)],
            [1, 1, 1],
            vec![Node::if_then(
                Expr::atomic_add("state", Expr::u32(0), Expr::u32(1)),
                vec![Node::store("state", Expr::u32(0), Expr::u32(2))],
            )],
        );

        let first = DeadBufferElim::transform(program);
        let second = DeadBufferElim::transform(first.program.clone());

        assert!(first.changed);
        assert!(!second.changed);
        assert_eq!(second.program, first.program);
    }

    #[test]
    fn analyze_runs_when_dead_buffers_present() {
        let program = sample_program(false); // scratch is dead
        assert_eq!(
            crate::optimizer::ProgramPass::analyze(&DeadBufferElim, &program),
            PassAnalysis::RUN
        );
    }
}