vyre-self-substrate 0.6.3

Vyre self-substrate: vyre using its own primitives on its own scheduler problems. The recursion-thesis layer between vyre-primitives and vyre-driver.
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
//! Dead-code elimination as a dispatched vyre Program.
//!
//! The encoder turns the user's `Program` into the canonical 5-buffer
//! ProgramGraph CSR; we ask an `OptimizerDispatcher` to run the optimizer
//! DCE BFS program against those buffers; the
//! returned live-frontier bitset drives a structural rewrite of the
//! input Program. There is no host-reference escape in production. If the
//! encoder cannot yet handle a Program shape it returns `EncodeError`;
//! the caller must either extend the encoder or produce a Program the
//! encoder accepts.
//!
//! The dispatcher trait inverts the dependency on a concrete backend  -
//! production callers wire `vyre-driver-wgpu` or `-cuda`; tests in this
//! crate use the in-tree `CpuOracleDispatcher` (test-only).

use vyre_foundation::ir::Program;
use vyre_primitives::bitset::bitset_words;
use vyre_primitives::graph::program_graph::ProgramGraphShape;

use crate::dispatch_buffers::{
    decode_u32_output_exact, ensure_input_slots, write_u32_slice_le_bytes, write_zero_bytes,
};

use super::dce_program::build_dce_bfs_program;
use super::dispatcher::{DispatchError, OptimizerDispatcher};
use super::encode::{apply_live_mask, encode_program, EncodeError, EncodedProgram, ROOT_GRAPH_ID};

#[derive(Debug, Default)]
struct DceKernelScratch {
    inputs: Vec<Vec<u8>>,
    seed: Vec<u32>,
    frontier: Vec<u32>,
    changed: Vec<u32>,
}

/// DCE as a dispatched analysis Program. Errors are honest:
/// - `Encode` if the input shape is not yet supported by the encoder.
/// - `Dispatch` if the dispatcher rejects the analysis Program.
#[derive(Debug)]
pub enum DceError {
    /// Encoder did not accept the input shape.
    Encode(EncodeError),
    /// Dispatcher rejected or failed to run the analysis Program.
    Dispatch(DispatchError),
}

impl std::fmt::Display for DceError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Encode(err) => write!(f, "gpu_dce encode error: {err:?}"),
            Self::Dispatch(err) => write!(f, "gpu_dce dispatch error: {err}"),
        }
    }
}

impl std::error::Error for DceError {}

/// Run DCE on `program` by encoding it into a ProgramGraph, dispatching
/// `persistent_bfs` through `dispatcher`, and rewriting the input from
/// the live-mask the dispatcher returns.
pub fn gpu_dce(
    program: Program,
    dispatcher: &dyn OptimizerDispatcher,
) -> Result<Program, DceError> {
    let encoded = encode_program(&program).map_err(DceError::Encode)?;
    let mut scratch = DceKernelScratch::default();
    let mut live = Vec::with_capacity(encoded.node_count as usize);
    compute_live_mask_with_scratch_into(&encoded, dispatcher, &mut scratch, &mut live)
        .map_err(DceError::Dispatch)?;
    Ok(apply_live_mask(&program, &encoded, &live))
}

fn compute_live_mask_with_scratch_into(
    encoded: &EncodedProgram,
    dispatcher: &dyn OptimizerDispatcher,
    scratch: &mut DceKernelScratch,
    live: &mut Vec<bool>,
) -> Result<(), DispatchError> {
    let n = encoded.node_count;
    if n == 0 {
        live.clear();
        return Ok(());
    }

    // Build the DCE analysis Program for this exact graph shape. Buffer
    // names + binding indices match the persistent BFS layout, but the DCE
    // variant exposes only the final changed flag instead of large-graph
    // active scratch.
    let shape = ProgramGraphShape::new(encoded.node_count, encoded.edge_count);
    let analysis = build_dce_bfs_program(shape, n.max(1));

    let words = bitset_words(n) as usize;
    scratch.seed.clear();
    scratch.seed.resize(words.max(1), 0);
    let root = ROOT_GRAPH_ID as usize;
    scratch.seed[root / 32] |= 1u32 << (root % 32);

    ensure_input_slots(&mut scratch.inputs, 8);
    write_u32_slice_le_bytes(&mut scratch.inputs[0], &encoded.nodes);
    write_u32_slice_le_bytes(&mut scratch.inputs[1], &encoded.edge_offsets);
    write_padded_one_u32_bytes(&mut scratch.inputs[2], &encoded.edge_targets);
    write_padded_one_u32_bytes(&mut scratch.inputs[3], &encoded.edge_kind_mask);
    write_u32_slice_le_bytes(&mut scratch.inputs[4], &encoded.node_tags);
    write_u32_slice_le_bytes(&mut scratch.inputs[5], &scratch.seed);
    write_zero_bytes(
        &mut scratch.inputs[6],
        words.max(1) * std::mem::size_of::<u32>(),
    );
    write_zero_bytes(&mut scratch.inputs[7], std::mem::size_of::<u32>());

    let outputs = dispatcher.dispatch(&analysis, &scratch.inputs, None)?;
    if outputs.len() != 2 {
        return Err(DispatchError::BackendError(format!(
            "Fix: persistent_bfs dispatch expected exactly 2 outputs (frontier_out, changed), got {}.",
            outputs.len()
        )));
    }
    decode_u32_output_exact(
        &outputs[0],
        words,
        "gpu_dce persistent_bfs frontier_out",
        &mut scratch.frontier,
    )?;
    decode_u32_output_exact(
        &outputs[1],
        1,
        "gpu_dce persistent_bfs changed",
        &mut scratch.changed,
    )?;

    live.clear();
    live.resize(n as usize, false);
    for graph_id in 0..(n as usize) {
        let word = scratch.frontier.get(graph_id / 32).copied().unwrap_or(0);
        if word & (1u32 << (graph_id % 32)) != 0 {
            live[graph_id] = true;
        }
    }
    Ok(())
}

fn write_padded_one_u32_bytes(out: &mut Vec<u8>, buf: &[u32]) {
    if buf.is_empty() {
        write_zero_bytes(out, std::mem::size_of::<u32>());
    } else {
        write_u32_slice_le_bytes(out, buf);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dispatch_buffers::u32_slice_to_le_bytes;
    use crate::optimizer::dispatcher::oracle::CpuOracleDispatcher;
    use crate::optimizer::dispatcher::DispatchError;
    use vyre_foundation::ir::{Expr, Node, Program};
    use vyre_foundation::optimizer::fingerprint_program;
    use vyre_foundation::optimizer::passes::fusion_cse::dce::engine::dce as oracle_cpu_dce;

    fn wrapped_program(entry: Vec<Node>) -> Program {
        Program::wrapped(Vec::new(), [1, 1, 1], entry)
    }

    fn assert_parity(entry: Vec<Node>) {
        let dispatcher = CpuOracleDispatcher::new();
        let oracle_input = wrapped_program(entry.clone());
        let test_input = wrapped_program(entry);

        let oracle_out = oracle_cpu_dce(oracle_input);
        let gpu_out = gpu_dce(test_input, &dispatcher).expect("Fix: encoder accepts program");
        assert_eq!(
            fingerprint_program(&oracle_out),
            fingerprint_program(&gpu_out),
            "encoded DCE must produce a fingerprint-equal Program. oracle entry={:?} gpu entry={:?}",
            oracle_out.entry(),
            gpu_out.entry()
        );
    }

    struct MalformedDispatcher {
        outputs: Vec<Vec<u8>>,
    }

    impl OptimizerDispatcher for MalformedDispatcher {
        fn dispatch(
            &self,
            _program: &Program,
            _inputs: &[Vec<u8>],
            _grid_override: Option<[u32; 3]>,
        ) -> Result<Vec<Vec<u8>>, DispatchError> {
            Ok(self.outputs.clone())
        }
    }

    #[test]
    fn empty_entry_parity() {
        assert_parity(vec![]);
    }

    #[test]
    fn dce_rejects_extra_dispatch_outputs() {
        let program = wrapped_program(vec![Node::store("buf", Expr::u32(0), Expr::u32(1))]);
        let dispatcher = MalformedDispatcher {
            outputs: vec![
                u32_slice_to_le_bytes(&[1]),
                u32_slice_to_le_bytes(&[0]),
                u32_slice_to_le_bytes(&[0]),
            ],
        };
        let err = gpu_dce(program, &dispatcher).expect_err("extra outputs must be rejected");
        assert!(
            matches!(err, DceError::Dispatch(DispatchError::BackendError(_))),
            "unexpected error: {err:?}"
        );
    }

    #[test]
    fn dce_rejects_trailing_changed_bytes() {
        let program = wrapped_program(vec![Node::store("buf", Expr::u32(0), Expr::u32(1))]);
        let encoded = encode_program(&program).expect("Fix: encoder accepts store");
        let words = bitset_words(encoded.node_count) as usize;
        let dispatcher = MalformedDispatcher {
            outputs: vec![u32_slice_to_le_bytes(&vec![1; words]), vec![0, 0, 0, 0, 1]],
        };
        let err = gpu_dce(program, &dispatcher).expect_err("trailing changed bytes rejected");
        assert!(
            matches!(err, DceError::Dispatch(DispatchError::BackendError(_))),
            "unexpected error: {err:?}"
        );
    }

    #[test]
    fn live_mask_with_scratch_reuses_dispatch_decode_and_output_storage() {
        let program = wrapped_program(vec![Node::store("buf", Expr::u32(0), Expr::u32(1))]);
        let encoded = encode_program(&program).expect("Fix: encoder accepts store");
        let words = bitset_words(encoded.node_count) as usize;
        let dispatcher = MalformedDispatcher {
            outputs: vec![
                u32_slice_to_le_bytes(&vec![u32::MAX; words]),
                vec![0, 0, 0, 0],
            ],
        };
        let mut scratch = DceKernelScratch::default();
        let mut live = Vec::with_capacity(encoded.node_count as usize);

        compute_live_mask_with_scratch_into(&encoded, &dispatcher, &mut scratch, &mut live)
            .expect("Fix: dispatch succeeds");

        let input_capacities = scratch.inputs.iter().map(Vec::capacity).collect::<Vec<_>>();
        let seed_capacity = scratch.seed.capacity();
        let frontier_capacity = scratch.frontier.capacity();
        let changed_capacity = scratch.changed.capacity();
        let live_capacity = live.capacity();

        compute_live_mask_with_scratch_into(&encoded, &dispatcher, &mut scratch, &mut live)
            .expect("Fix: dispatch succeeds");

        assert_eq!(
            scratch.inputs.iter().map(Vec::capacity).collect::<Vec<_>>(),
            input_capacities
        );
        assert_eq!(scratch.seed.capacity(), seed_capacity);
        assert_eq!(scratch.frontier.capacity(), frontier_capacity);
        assert_eq!(scratch.changed.capacity(), changed_capacity);
        assert_eq!(live.capacity(), live_capacity);
        assert!(live.iter().all(|&is_live| is_live));
    }

    #[test]
    fn pure_let_with_no_use_is_dropped() {
        assert_parity(vec![Node::let_bind("dead", Expr::u32(7))]);
    }

    #[test]
    fn live_let_used_by_store_is_kept() {
        assert_parity(vec![
            Node::let_bind("x", Expr::u32(7)),
            Node::store("buf", Expr::u32(0), Expr::var("x")),
        ]);
    }

    #[test]
    fn chained_lets_used_by_store_keep_chain() {
        assert_parity(vec![
            Node::let_bind("a", Expr::u32(1)),
            Node::let_bind("b", Expr::var("a")),
            Node::store("buf", Expr::u32(0), Expr::var("b")),
        ]);
    }

    #[test]
    fn unused_chain_is_dropped() {
        assert_parity(vec![
            Node::let_bind("a", Expr::u32(1)),
            Node::let_bind("b", Expr::var("a")),
            Node::let_bind("c", Expr::u32(2)),
            Node::store("buf", Expr::u32(0), Expr::var("c")),
        ]);
    }

    #[test]
    fn return_drops_unreachable_suffix() {
        assert_parity(vec![
            Node::let_bind("live", Expr::u32(1)),
            Node::store("buf", Expr::u32(0), Expr::var("live")),
            Node::Return,
            Node::let_bind("after_return", Expr::u32(99)),
            Node::store("buf", Expr::u32(0), Expr::u32(2)),
        ]);
    }

    #[test]
    fn shadowed_let_only_keeps_most_recent() {
        assert_parity(vec![
            Node::let_bind("x", Expr::u32(1)),
            Node::let_bind("x", Expr::u32(2)),
            Node::store("buf", Expr::u32(0), Expr::var("x")),
        ]);
    }

    #[test]
    fn store_with_index_var_keeps_its_definer() {
        assert_parity(vec![
            Node::let_bind("idx", Expr::u32(3)),
            Node::store("buf", Expr::var("idx"), Expr::u32(99)),
        ]);
    }

    #[test]
    fn assign_is_always_kept() {
        assert_parity(vec![
            Node::Assign {
                name: "x".into(),
                value: Expr::u32(2),
            },
            Node::store("buf", Expr::u32(0), Expr::var("x")),
        ]);
    }

    #[test]
    fn if_with_dead_lets_in_both_branches_drops_them() {
        assert_parity(vec![Node::If {
            cond: Expr::var("c"),
            then: vec![Node::let_bind("dead_then", Expr::u32(0))],
            otherwise: vec![Node::let_bind("dead_else", Expr::u32(0))],
        }]);
    }

    #[test]
    fn if_branch_with_live_store_keeps_outer_definer() {
        assert_parity(vec![
            Node::let_bind("x", Expr::u32(7)),
            Node::If {
                cond: Expr::var("c"),
                then: vec![Node::store("buf", Expr::u32(0), Expr::var("x"))],
                otherwise: vec![Node::let_bind("dead_else", Expr::u32(0))],
            },
        ]);
    }

    #[test]
    fn loop_with_store_using_induction_var_is_kept() {
        assert_parity(vec![Node::loop_for(
            "i",
            Expr::u32(0),
            Expr::u32(10),
            vec![Node::store("buf", Expr::var("i"), Expr::u32(0))],
        )]);
    }

    #[test]
    fn block_with_dead_let_is_kept_with_empty_body() {
        assert_parity(vec![Node::Block(vec![Node::let_bind(
            "dead_in_block",
            Expr::u32(99),
        )])]);
    }

    #[test]
    fn nested_region_with_live_store_keeps_outer_definer() {
        // Cribbed from foundation's `dce_region_live_ins_propagate_to_outer_scope`.
        assert_parity(vec![
            Node::let_bind("live", Expr::u32(7)),
            Node::Region {
                generator: "test".into(),
                source_region: None,
                body: std::sync::Arc::new(vec![Node::store(
                    "out",
                    Expr::u32(0),
                    Expr::var("live"),
                )]),
            },
        ]);
    }

    #[test]
    fn return_inside_region_truncates_region_body() {
        // Cribbed from foundation's `dce_descends_into_region_bodies`.
        assert_parity(vec![Node::Region {
            generator: "test".into(),
            source_region: None,
            body: std::sync::Arc::new(vec![
                Node::let_bind("dead", Expr::u32(1)),
                Node::Return,
                Node::let_bind("unreachable", Expr::u32(2)),
            ]),
        }]);
    }
}