vyre-runtime 0.6.1

Persistent megakernel + io_uring zero-copy streaming runtime for vyre - GPU as VIR0 bytecode interpreter
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
use super::super::protocol::slot;
use super::{
    claim_io_requests_into, complete_io_request, complete_io_requests_batch, encode_empty_io_queue,
    io_completion_poll_body, io_op, io_status, io_word, poll_io_requests,
    try_claim_io_requests_into, try_encode_empty_io_queue_into, try_poll_io_requests_into,
    MegakernelIoQueue, IO_SLOT_COUNT, IO_SLOT_WORDS,
};
use crate::PipelineError;

#[test]
fn empty_io_queue_has_no_requests() {
    let buf = encode_empty_io_queue(4).unwrap();
    let reqs = poll_io_requests(&buf)
        .expect("Fix: empty aligned queue must poll; restore this invariant before continuing.");
    assert!(reqs.is_empty());
}

#[test]
fn empty_io_queue_encode_into_reuses_capacity() {
    let mut buf = Vec::with_capacity((IO_SLOT_WORDS as usize) * 8 * 4);
    let ptr = buf.as_ptr();
    try_encode_empty_io_queue_into(4, &mut buf).unwrap();

    assert_eq!(buf.len(), (IO_SLOT_WORDS as usize) * 4 * 4);
    assert!(
        buf.iter().all(|byte| *byte == 0),
        "Fix: encode_into must zero every IO queue byte before upload."
    );
    assert_eq!(
        buf.as_ptr(),
        ptr,
        "Fix: encode_into should retain caller-owned capacity for same-size queues."
    );
}

#[test]
fn published_io_slot_is_detected() {
    let mut buf = encode_empty_io_queue(4).unwrap();
    // Publish slot 1: READ, src=5, dst=6, offset=0x1000, count=4096, tag=42
    let base = IO_SLOT_WORDS as usize * 4;
    let write_word = |buf: &mut Vec<u8>, word: u32, val: u32| {
        let off = base + word as usize * 4;
        buf[off..off + 4].copy_from_slice(&val.to_le_bytes());
    };
    write_word(&mut buf, io_word::OP_TYPE, io_op::READ);
    write_word(&mut buf, io_word::SRC_HANDLE, 5);
    write_word(&mut buf, io_word::DST_HANDLE, 6);
    write_word(&mut buf, io_word::OFFSET_LO, 0x1000);
    write_word(&mut buf, io_word::OFFSET_HI, 0);
    write_word(&mut buf, io_word::BYTE_COUNT, 4096);
    write_word(&mut buf, io_word::STATUS, slot::PUBLISHED);
    write_word(&mut buf, io_word::TAG, 42);

    let reqs = poll_io_requests(&buf).expect(
        "Fix: published aligned queue must poll; restore this invariant before continuing.",
    );
    assert_eq!(reqs.len(), 1);
    assert_eq!(reqs[0].slot_idx, 1);
    assert_eq!(reqs[0].op_type, io_op::READ);
    assert_eq!(reqs[0].offset, 0x1000);
    assert_eq!(reqs[0].byte_count, 4096);
}

#[test]
fn poll_io_requests_into_reuses_request_storage() {
    let mut buf = encode_empty_io_queue(4).unwrap();
    let base = IO_SLOT_WORDS as usize * 4;
    let write_word = |buf: &mut Vec<u8>, word: u32, val: u32| {
        let off = base + word as usize * 4;
        buf[off..off + 4].copy_from_slice(&val.to_le_bytes());
    };
    write_word(&mut buf, io_word::OP_TYPE, io_op::READ);
    write_word(&mut buf, io_word::DST_HANDLE, 9);
    write_word(&mut buf, io_word::BYTE_COUNT, 128);
    write_word(&mut buf, io_word::STATUS, slot::PUBLISHED);

    let mut requests = Vec::with_capacity(4);
    let initial_capacity = requests.capacity();
    try_poll_io_requests_into(&buf, &mut requests)
        .expect("Fix: reusable IO polling must accept aligned queue bytes");
    assert_eq!(requests.len(), 1);
    assert_eq!(requests[0].dst_handle, 9);
    assert_eq!(requests.capacity(), initial_capacity);

    try_poll_io_requests_into(&buf, &mut requests)
        .expect("Fix: repeated reusable IO polling must not allocate on a warm buffer");
    assert_eq!(requests.len(), 1);
    assert_eq!(requests.capacity(), initial_capacity);
}

#[test]
fn poll_io_requests_into_reserves_only_published_slots() {
    let mut buf = encode_empty_io_queue(IO_SLOT_COUNT).unwrap();
    let base = IO_SLOT_WORDS as usize * 3 * 4;
    let write_word = |buf: &mut Vec<u8>, word: u32, val: u32| {
        let off = base + word as usize * 4;
        buf[off..off + 4].copy_from_slice(&val.to_le_bytes());
    };
    write_word(&mut buf, io_word::OP_TYPE, io_op::READ);
    write_word(&mut buf, io_word::DST_HANDLE, 17);
    write_word(&mut buf, io_word::BYTE_COUNT, 512);
    write_word(&mut buf, io_word::STATUS, slot::PUBLISHED);

    let mut requests = Vec::new();
    try_poll_io_requests_into(&buf, &mut requests)
        .expect("Fix: sparse IO queue polling must reserve only published requests");

    assert_eq!(requests.len(), 1);
    assert_eq!(requests[0].slot_idx, 3);
    assert!(
        requests.capacity() < IO_SLOT_COUNT as usize,
        "Fix: sparse IO polling must not reserve one request slot for every empty queue slot."
    );
}

#[test]
fn poll_io_requests_into_does_not_allocate_for_empty_queue() {
    let buf = encode_empty_io_queue(IO_SLOT_COUNT).unwrap();
    let mut requests = Vec::new();

    try_poll_io_requests_into(&buf, &mut requests)
        .expect("Fix: empty IO queue polling must not require request storage");

    assert!(requests.is_empty());
    assert_eq!(
        requests.capacity(),
        0,
        "Fix: empty IO polling must not allocate the full compiled queue window."
    );
}

#[test]
fn claim_io_requests_marks_published_slots_claimed_once() {
    let mut buf = encode_empty_io_queue(4).unwrap();
    let base = IO_SLOT_WORDS as usize * 4;
    let write_word = |buf: &mut Vec<u8>, word: u32, val: u32| {
        let off = base + word as usize * 4;
        buf[off..off + 4].copy_from_slice(&val.to_le_bytes());
    };
    write_word(&mut buf, io_word::OP_TYPE, io_op::READ);
    write_word(&mut buf, io_word::SRC_HANDLE, 5);
    write_word(&mut buf, io_word::DST_HANDLE, 6);
    write_word(&mut buf, io_word::OFFSET_LO, 0x1000);
    write_word(&mut buf, io_word::BYTE_COUNT, 4096);
    write_word(&mut buf, io_word::STATUS, slot::PUBLISHED);
    write_word(&mut buf, io_word::TAG, 42);

    let mut requests = Vec::with_capacity(4);
    claim_io_requests_into(&mut buf, &mut requests)
        .expect("Fix: published aligned queue must claim exactly once");
    assert_eq!(requests.len(), 1);
    assert_eq!(requests[0].slot_idx, 1);

    let status_off = base + io_word::STATUS as usize * 4;
    let status = u32::from_le_bytes(buf[status_off..status_off + 4].try_into().unwrap());
    assert_eq!(status, slot::CLAIMED);

    claim_io_requests_into(&mut buf, &mut requests)
        .expect("Fix: claimed slots must stay pollable without resubmission");
    assert!(requests.is_empty());
}

#[test]
fn claim_io_requests_into_reuses_request_storage() {
    let mut buf = encode_empty_io_queue(4).unwrap();
    let base = IO_SLOT_WORDS as usize * 4;
    let write_word = |buf: &mut Vec<u8>, word: u32, val: u32| {
        let off = base + word as usize * 4;
        buf[off..off + 4].copy_from_slice(&val.to_le_bytes());
    };
    write_word(&mut buf, io_word::OP_TYPE, io_op::READ);
    write_word(&mut buf, io_word::DST_HANDLE, 11);
    write_word(&mut buf, io_word::BYTE_COUNT, 256);
    write_word(&mut buf, io_word::STATUS, slot::PUBLISHED);

    let mut requests = Vec::with_capacity(4);
    let initial_capacity = requests.capacity();
    try_claim_io_requests_into(&mut buf, &mut requests)
        .expect("Fix: reusable IO claim must accept aligned queue bytes");
    assert_eq!(requests.len(), 1);
    assert_eq!(requests[0].dst_handle, 11);
    assert_eq!(requests.capacity(), initial_capacity);

    buf[base + io_word::STATUS as usize * 4..base + io_word::STATUS as usize * 4 + 4]
        .copy_from_slice(&slot::PUBLISHED.to_le_bytes());
    try_claim_io_requests_into(&mut buf, &mut requests)
        .expect("Fix: repeated reusable IO claim must not allocate on a warm buffer");
    assert_eq!(requests.len(), 1);
    assert_eq!(requests.capacity(), initial_capacity);
}

#[test]
fn claim_io_requests_into_does_not_allocate_for_empty_queue() {
    let mut buf = encode_empty_io_queue(IO_SLOT_COUNT).unwrap();
    let mut requests = Vec::new();

    try_claim_io_requests_into(&mut buf, &mut requests)
        .expect("Fix: empty IO queue claiming must not require request storage");

    assert!(requests.is_empty());
    assert_eq!(
        requests.capacity(),
        0,
        "Fix: empty IO claim polling must not allocate the full compiled queue window."
    );
}

#[test]
fn complete_sets_status_after_claim() {
    let mut buf = encode_empty_io_queue(2).unwrap();
    let write_word = |buf: &mut Vec<u8>, word: u32, val: u32| {
        let off = word as usize * 4;
        buf[off..off + 4].copy_from_slice(&val.to_le_bytes());
    };
    write_word(&mut buf, io_word::STATUS, slot::PUBLISHED);
    let mut requests = Vec::new();
    claim_io_requests_into(&mut buf, &mut requests)
        .expect("Fix: published request must be claimable before completion");

    complete_io_request(&mut buf, 0, true).expect(
        "Fix: claimed completion slot must update; restore this invariant before continuing.",
    );
    let status_off = io_word::STATUS as usize * 4;
    let status = u32::from_le_bytes(buf[status_off..status_off + 4].try_into().unwrap());
    assert_eq!(status, io_status::OK);
}

#[test]
fn batch_completion_validates_before_mutating() {
    let mut buf = encode_empty_io_queue(2).unwrap();
    let status0 = io_word::STATUS as usize * 4;
    let status1 = (IO_SLOT_WORDS as usize + io_word::STATUS as usize) * 4;
    buf[status0..status0 + 4].copy_from_slice(&slot::CLAIMED.to_le_bytes());
    let before = buf.clone();

    let error = complete_io_requests_batch(&mut buf, &[(0, true), (1, true)])
        .expect_err("batch completion must reject unclaimed slots before writing any status");
    match error {
        PipelineError::QueueFull { fix, .. } => assert!(
            fix.contains("CLAIMED request"),
            "batch ownership error must be actionable, got `{fix}`"
        ),
        other => panic!("expected QueueFull for unclaimed batch slot, got {other:?}"),
    }
    assert_eq!(buf, before);

    buf[status1..status1 + 4].copy_from_slice(&slot::CLAIMED.to_le_bytes());
    complete_io_requests_batch(&mut buf, &[(0, true), (1, false)])
        .expect("Fix: claimed batch completions must publish together");
    assert_eq!(
        u32::from_le_bytes(buf[status0..status0 + 4].try_into().unwrap()),
        io_status::OK
    );
    assert_eq!(
        u32::from_le_bytes(buf[status1..status1 + 4].try_into().unwrap()),
        io_status::ERROR
    );
}

#[test]
fn completion_without_claim_is_rejected() {
    let mut buf = encode_empty_io_queue(1).unwrap();
    let error = complete_io_request(&mut buf, 0, true)
        .expect_err("unclaimed IO slots must not be completed");
    match error {
        PipelineError::QueueFull { fix, .. } => assert!(
            fix.contains("CLAIMED request"),
            "completion ownership error must be actionable, got `{fix}`"
        ),
        other => panic!("expected QueueFull for unclaimed completion, got {other:?}"),
    }
}

#[test]
fn io_completion_poll_produces_valid_ir() {
    let nodes = io_completion_poll_body();
    assert_eq!(nodes.len(), 1); // one loop_for
}

#[test]
fn host_publish_slot_round_trips() {
    let mut queue = MegakernelIoQueue::new(4).unwrap();
    assert_eq!(queue.as_bytes().as_ptr() as usize % 4, 0);
    queue.publish_slot(2, 7, 4096, 99).unwrap();
    let completion = queue
        .completion(2)
        .expect("Fix: published slot present; restore this invariant before continuing.");
    assert_eq!(completion.mapped_slot, 7);
    assert_eq!(completion.byte_count, 4096);
    assert_eq!(completion.tag, 99);
    assert_eq!(
        u32::from_le_bytes(
            queue.as_bytes()[((2 * IO_SLOT_WORDS + io_word::STATUS) as usize * 4)
                ..((2 * IO_SLOT_WORDS + io_word::STATUS) as usize * 4 + 4)]
                .try_into()
                .unwrap()
        ),
        slot::PUBLISHED
    );
}

#[test]
fn host_queue_byte_view_stays_aligned_after_mutation() {
    let mut queue = MegakernelIoQueue::new(IO_SLOT_COUNT).unwrap();
    assert_eq!(queue.as_mut_bytes().as_ptr() as usize % 4, 0);
    queue.publish_slot(0, 3, 512, 77).unwrap();
    assert_eq!(queue.as_bytes().as_ptr() as usize % 4, 0);
}

#[test]
fn oversized_queue_is_rejected_with_actionable_error() {
    let error = MegakernelIoQueue::new(IO_SLOT_COUNT + 1)
        .expect_err("queues larger than the compiled 64-slot poll window must fail");
    match error {
        PipelineError::QueueFull { fix, .. } => {
            assert!(
                fix.contains("64 slots"),
                "overflow error must explain the compiled queue limit, got `{fix}`"
            );
        }
        other => panic!("expected QueueFull overflow error, got {other:?}"),
    }
}

#[test]
fn publishing_the_sixty_fifth_completion_errors_instead_of_dropping() {
    let mut queue = MegakernelIoQueue::new(IO_SLOT_COUNT).unwrap();
    for slot in 0..IO_SLOT_COUNT {
        queue.publish_slot(slot, slot, 4096, slot).unwrap();
        let base = (slot * IO_SLOT_WORDS + io_word::STATUS) as usize * 4;
        queue.as_mut_bytes()[base..base + 4].copy_from_slice(&io_status::OK.to_le_bytes());
    }

    let error = queue
        .publish_slot(IO_SLOT_COUNT, IO_SLOT_COUNT, 4096, IO_SLOT_COUNT)
        .expect_err("the 65th published completion must fail loudly");
    match error {
        PipelineError::QueueFull { fix, .. } => {
            assert!(
                fix.contains("valid slot id"),
                "overflow error must stay actionable, got `{fix}`"
            );
        }
        other => panic!("expected QueueFull on 65th publish, got {other:?}"),
    }
}

#[test]
fn complete_io_request_only_mutates_status_word() {
    let mut buf = encode_empty_io_queue(1).unwrap();
    for (idx, byte) in buf.iter_mut().enumerate() {
        *byte = (idx % 251) as u8;
    }
    let status_off = (io_word::STATUS as usize) * 4;
    buf[status_off..status_off + 4].copy_from_slice(&slot::CLAIMED.to_le_bytes());
    let before = buf.clone();
    complete_io_request(&mut buf, 0, false).expect(
        "Fix: valid completion slot must update; restore this invariant before continuing.",
    );
    for idx in 0..buf.len() {
        let in_status_word = (status_off..status_off + 4).contains(&idx);
        if !in_status_word {
            assert_eq!(
                buf[idx], before[idx],
                "status completion must not touch non-status byte index {idx}"
            );
        }
    }
    let status = u32::from_le_bytes(buf[status_off..status_off + 4].try_into().unwrap());
    assert_eq!(status, io_status::ERROR);
}

#[test]
fn io_module_avoids_byte_width_atomic_types() {
    // Check every production I/O source slice (tests excluded) so byte-width
    // atomics cannot hide in runtime megakernel queue code.
    let prod_files = [
        include_str!("mod.rs"),
        include_str!("queue.rs"),
        include_str!("poll.rs"),
        include_str!("complete.rs"),
        include_str!("encode.rs"),
        include_str!("helpers.rs"),
    ];
    for src in prod_files {
        assert!(
            !src.contains("AtomicU8") && !src.contains("AtomicI8"),
            "byte-width atomics are forbidden for io_queue protocol words"
        );
        assert!(
            !src.contains("AtomicU16") && !src.contains("AtomicI16"),
            "sub-word atomics are forbidden for io_queue protocol words"
        );
    }
}

#[test]
fn submit_dma_read_publishes_read_request() {
    let mut queue = MegakernelIoQueue::new(4).unwrap();
    queue.submit_dma_read(2, 10, 20, 4096, 99).unwrap();

    let reqs = poll_io_requests(queue.as_bytes()).unwrap();
    assert_eq!(reqs.len(), 1);
    assert_eq!(reqs[0].slot_idx, 2);
    assert_eq!(reqs[0].op_type, io_op::READ);
    assert_eq!(reqs[0].src_handle, 10);
    assert_eq!(reqs[0].dst_handle, 20);
    assert_eq!(reqs[0].byte_count, 4096);
    assert_eq!(reqs[0].tag, 99);
}

#[test]
fn submit_dma_read_rejects_non_empty_slot() {
    let mut queue = MegakernelIoQueue::new(4).unwrap();
    queue.submit_dma_read(1, 10, 20, 4096, 99).unwrap();
    let err = queue.submit_dma_read(1, 11, 21, 8192, 100).unwrap_err();
    assert!(
        matches!(err, PipelineError::QueueFull { .. }),
        "Fix: re-submitting to an in-flight slot must return QueueFull"
    );
}