vyre-libs 0.6.3

vyre Category A library ecosystem - pure-IR compositions over vyre-ops hardware primitives
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
//! GPU integer-literal scanner.
//!
//! Phase 17b.2: per starting byte position in `source`, scan a C
//! integer literal (hex `0x`/`0X`, binary `0b`/`0B`, octal leading-`0`,
//! else decimal) and emit `(value, bytes_consumed)`. Suffix tolerance:
//! any combination of `u`/`U`/`l`/`L` is consumed and ignored.
//!
//! `value` is computed as `u32` with saturating arithmetic. Literals
//! larger than `u32::MAX` consume their full scanned digit run and emit
//! `u32::MAX`, matching the preprocessor's conservative truthiness needs
//! without wrapping back to a small value.
//!
//! ## Wire layout
//!
//! Inputs:
//!   - `source` (U8)
//!   - `start_pos` (U32, single element)  -  byte offset to start scanning.
//!
//! Outputs:
//!   - `value_out` (U32, single element).
//!   - `bytes_consumed_out` (U32, single element). `0` indicates "not
//!     an integer literal at this position"  -  the caller treats this
//!     the same way the CPU `consume_integer` returns `None`.

use super::gpu_source_bytes::{
    literal_scan_common_buffers, literal_scan_program, packed_source_byte_len_expr,
    safe_load_source_byte_expr,
};
use vyre::ir::{Expr, Node, Program};

/// Canonical op id.
pub const OP_ID: &str = "vyre-libs::parsing::c::preprocess::gpu_int_literal_scan";

/// Canonical binding indices.
pub const BINDING_SOURCE: u32 = 0;
/// Canonical binding for the input start position.
pub const BINDING_START_POS: u32 = 1;
/// Canonical binding for the output literal value.
pub const BINDING_VALUE_OUT: u32 = 2;
/// Canonical binding for the output bytes-consumed count.
pub const BINDING_BYTES_CONSUMED_OUT: u32 = 3;

/// Maximum digits scanned per literal. Any longer literal saturates.
/// 32 is enough for the longest meaningful u32 representation in any
/// supported radix (binary u32 is at most 32 digits; hex u32 is 8;
/// decimal u32 is 10; octal u32 is 11).
pub const MAX_DIGITS: u32 = 32;

/// Maximum suffix bytes consumed (`u`/`U`/`l`/`L`/`z`/`Z`/`wb`/`WB`).
pub const MAX_SUFFIX: u32 = 4;

/// Build the 17b.2 integer-literal scanner `Program`.
///
/// `source_len` is retained for source compatibility. The source byte
/// bound is read from the runtime-sized `source` buffer so one resident
/// scanner program serves every translation unit size.
#[must_use]
pub fn gpu_int_literal_scan(source_len: u32) -> Program {
    let _ = source_len;
    let source_byte_len = packed_source_byte_len_expr();
    let safe_load =
        |addr: Expr| -> Expr { safe_load_source_byte_expr(addr, source_byte_len.clone()) };

    let body: Vec<Node> = vec![Node::if_then(
        Expr::eq(Expr::InvocationId { axis: 0 }, Expr::u32(0)),
        vec![
            Node::let_bind("start", Expr::load("start_pos", Expr::u32(0))),
            // Determine radix and digits-start.
            Node::let_bind("b0", safe_load(Expr::var("start"))),
            Node::let_bind("b1", safe_load(Expr::add(Expr::var("start"), Expr::u32(1)))),
            Node::let_bind(
                "is_hex",
                Expr::select(
                    Expr::and(
                        Expr::eq(Expr::var("b0"), Expr::u32(b'0' as u32)),
                        Expr::or(
                            Expr::eq(Expr::var("b1"), Expr::u32(b'x' as u32)),
                            Expr::eq(Expr::var("b1"), Expr::u32(b'X' as u32)),
                        ),
                    ),
                    Expr::u32(1),
                    Expr::u32(0),
                ),
            ),
            Node::let_bind(
                "is_bin",
                Expr::select(
                    Expr::and(
                        Expr::eq(Expr::var("b0"), Expr::u32(b'0' as u32)),
                        Expr::or(
                            Expr::eq(Expr::var("b1"), Expr::u32(b'b' as u32)),
                            Expr::eq(Expr::var("b1"), Expr::u32(b'B' as u32)),
                        ),
                    ),
                    Expr::u32(1),
                    Expr::u32(0),
                ),
            ),
            Node::let_bind(
                "is_oct",
                Expr::select(
                    Expr::and(
                        Expr::eq(Expr::var("b0"), Expr::u32(b'0' as u32)),
                        Expr::and(
                            Expr::eq(Expr::var("is_hex"), Expr::u32(0)),
                            Expr::eq(Expr::var("is_bin"), Expr::u32(0)),
                        ),
                    ),
                    Expr::u32(1),
                    Expr::u32(0),
                ),
            ),
            Node::let_bind(
                "radix",
                Expr::select(
                    Expr::eq(Expr::var("is_hex"), Expr::u32(1)),
                    Expr::u32(16),
                    Expr::select(
                        Expr::eq(Expr::var("is_bin"), Expr::u32(1)),
                        Expr::u32(2),
                        Expr::select(
                            Expr::eq(Expr::var("is_oct"), Expr::u32(1)),
                            Expr::u32(8),
                            Expr::u32(10),
                        ),
                    ),
                ),
            ),
            // Hex/bin advance start by 2; oct/dec advance by 0.
            Node::let_bind(
                "digit_start_offset",
                Expr::select(
                    Expr::or(
                        Expr::eq(Expr::var("is_hex"), Expr::u32(1)),
                        Expr::eq(Expr::var("is_bin"), Expr::u32(1)),
                    ),
                    Expr::u32(2),
                    Expr::u32(0),
                ),
            ),
            Node::let_bind(
                "digits_start",
                Expr::add(Expr::var("start"), Expr::var("digit_start_offset")),
            ),
            Node::let_bind("idx", Expr::var("digits_start")),
            Node::let_bind("value", Expr::u32(0)),
            Node::let_bind("done_digits", Expr::u32(0)),
            Node::loop_for(
                "k",
                Expr::u32(0),
                Expr::u32(MAX_DIGITS),
                vec![Node::if_then(
                    Expr::eq(Expr::var("done_digits"), Expr::u32(0)),
                    vec![
                        Node::let_bind("byte", safe_load(Expr::var("idx"))),
                        Node::let_bind(
                            "next_byte",
                            safe_load(Expr::add(Expr::var("idx"), Expr::u32(1))),
                        ),
                        // Compute digit value.
                        Node::let_bind(
                            "is_dec_digit",
                            Expr::select(
                                Expr::and(
                                    Expr::ge(Expr::var("byte"), Expr::u32(b'0' as u32)),
                                    Expr::le(Expr::var("byte"), Expr::u32(b'9' as u32)),
                                ),
                                Expr::u32(1),
                                Expr::u32(0),
                            ),
                        ),
                        Node::let_bind(
                            "is_hex_lower",
                            Expr::select(
                                Expr::and(
                                    Expr::ge(Expr::var("byte"), Expr::u32(b'a' as u32)),
                                    Expr::le(Expr::var("byte"), Expr::u32(b'f' as u32)),
                                ),
                                Expr::u32(1),
                                Expr::u32(0),
                            ),
                        ),
                        Node::let_bind(
                            "is_hex_upper",
                            Expr::select(
                                Expr::and(
                                    Expr::ge(Expr::var("byte"), Expr::u32(b'A' as u32)),
                                    Expr::le(Expr::var("byte"), Expr::u32(b'F' as u32)),
                                ),
                                Expr::u32(1),
                                Expr::u32(0),
                            ),
                        ),
                        Node::let_bind(
                            "raw_digit",
                            Expr::select(
                                Expr::eq(Expr::var("is_dec_digit"), Expr::u32(1)),
                                Expr::sub(Expr::var("byte"), Expr::u32(b'0' as u32)),
                                Expr::select(
                                    Expr::eq(Expr::var("is_hex_lower"), Expr::u32(1)),
                                    Expr::add(
                                        Expr::sub(Expr::var("byte"), Expr::u32(b'a' as u32)),
                                        Expr::u32(10),
                                    ),
                                    Expr::select(
                                        Expr::eq(Expr::var("is_hex_upper"), Expr::u32(1)),
                                        Expr::add(
                                            Expr::sub(Expr::var("byte"), Expr::u32(b'A' as u32)),
                                            Expr::u32(10),
                                        ),
                                        // Sentinel: 99 is greater than
                                        // any valid digit in any radix
                                        // we support, so the radix-bounds
                                        // check below rejects this.
                                        Expr::u32(99),
                                    ),
                                ),
                            ),
                        ),
                        Node::let_bind(
                            "digit_in_range",
                            Expr::select(
                                Expr::lt(Expr::var("raw_digit"), Expr::var("radix")),
                                Expr::u32(1),
                                Expr::u32(0),
                            ),
                        ),
                        Node::let_bind(
                            "next_is_dec_digit",
                            Expr::select(
                                Expr::and(
                                    Expr::ge(Expr::var("next_byte"), Expr::u32(b'0' as u32)),
                                    Expr::le(Expr::var("next_byte"), Expr::u32(b'9' as u32)),
                                ),
                                Expr::u32(1),
                                Expr::u32(0),
                            ),
                        ),
                        Node::let_bind(
                            "next_is_hex_lower",
                            Expr::select(
                                Expr::and(
                                    Expr::ge(Expr::var("next_byte"), Expr::u32(b'a' as u32)),
                                    Expr::le(Expr::var("next_byte"), Expr::u32(b'f' as u32)),
                                ),
                                Expr::u32(1),
                                Expr::u32(0),
                            ),
                        ),
                        Node::let_bind(
                            "next_is_hex_upper",
                            Expr::select(
                                Expr::and(
                                    Expr::ge(Expr::var("next_byte"), Expr::u32(b'A' as u32)),
                                    Expr::le(Expr::var("next_byte"), Expr::u32(b'F' as u32)),
                                ),
                                Expr::u32(1),
                                Expr::u32(0),
                            ),
                        ),
                        Node::let_bind(
                            "next_raw_digit",
                            Expr::select(
                                Expr::eq(Expr::var("next_is_dec_digit"), Expr::u32(1)),
                                Expr::sub(Expr::var("next_byte"), Expr::u32(b'0' as u32)),
                                Expr::select(
                                    Expr::eq(Expr::var("next_is_hex_lower"), Expr::u32(1)),
                                    Expr::add(
                                        Expr::sub(Expr::var("next_byte"), Expr::u32(b'a' as u32)),
                                        Expr::u32(10),
                                    ),
                                    Expr::select(
                                        Expr::eq(Expr::var("next_is_hex_upper"), Expr::u32(1)),
                                        Expr::add(
                                            Expr::sub(
                                                Expr::var("next_byte"),
                                                Expr::u32(b'A' as u32),
                                            ),
                                            Expr::u32(10),
                                        ),
                                        Expr::u32(99),
                                    ),
                                ),
                            ),
                        ),
                        Node::let_bind(
                            "separator_in_range",
                            Expr::select(
                                Expr::and(
                                    Expr::eq(Expr::var("byte"), Expr::u32(b'\'' as u32)),
                                    Expr::lt(Expr::var("next_raw_digit"), Expr::var("radix")),
                                ),
                                Expr::u32(1),
                                Expr::u32(0),
                            ),
                        ),
                        Node::if_then_else(
                            Expr::or(
                                Expr::eq(Expr::var("digit_in_range"), Expr::u32(1)),
                                Expr::eq(Expr::var("separator_in_range"), Expr::u32(1)),
                            ),
                            vec![
                                // value = value * radix + raw_digit (saturating).
                                Node::let_bind(
                                    "sat_limit",
                                    Expr::div(
                                        Expr::sub(Expr::u32(u32::MAX), Expr::var("raw_digit")),
                                        Expr::var("radix"),
                                    ),
                                ),
                                Node::let_bind(
                                    "would_overflow",
                                    Expr::select(
                                        Expr::gt(Expr::var("value"), Expr::var("sat_limit")),
                                        Expr::u32(1),
                                        Expr::u32(0),
                                    ),
                                ),
                                Node::assign(
                                    "value",
                                    Expr::select(
                                        Expr::eq(Expr::var("separator_in_range"), Expr::u32(1)),
                                        Expr::var("value"),
                                        Expr::select(
                                            Expr::eq(Expr::var("would_overflow"), Expr::u32(1)),
                                            Expr::u32(u32::MAX),
                                            Expr::add(
                                                Expr::mul(Expr::var("value"), Expr::var("radix")),
                                                Expr::var("raw_digit"),
                                            ),
                                        ),
                                    ),
                                ),
                                Node::assign("idx", Expr::add(Expr::var("idx"), Expr::u32(1))),
                            ],
                            vec![Node::assign("done_digits", Expr::u32(1))],
                        ),
                    ],
                )],
            ),
            Node::let_bind(
                "saw_digits",
                Expr::select(
                    Expr::gt(Expr::var("idx"), Expr::var("digits_start")),
                    Expr::u32(1),
                    Expr::u32(0),
                ),
            ),
            // If no digits at all → not a literal. Bytes consumed = 0.
            // Otherwise consume up to 4 trailing u/U/l/L/z/Z/wb/WB suffix bytes.
            Node::let_bind("done_suffix", Expr::u32(0)),
            Node::if_then(
                Expr::eq(Expr::var("saw_digits"), Expr::u32(1)),
                vec![Node::loop_for(
                    "s",
                    Expr::u32(0),
                    Expr::u32(MAX_SUFFIX),
                    vec![Node::if_then(
                        Expr::eq(Expr::var("done_suffix"), Expr::u32(0)),
                        vec![
                            Node::let_bind("sb", safe_load(Expr::var("idx"))),
                            Node::let_bind(
                                "sb1",
                                safe_load(Expr::add(Expr::var("idx"), Expr::u32(1))),
                            ),
                            Node::let_bind(
                                "is_single_suffix",
                                Expr::select(
                                    Expr::or(
                                        Expr::or(
                                            Expr::eq(Expr::var("sb"), Expr::u32(b'u' as u32)),
                                            Expr::eq(Expr::var("sb"), Expr::u32(b'U' as u32)),
                                        ),
                                        Expr::or(
                                            Expr::eq(Expr::var("sb"), Expr::u32(b'l' as u32)),
                                            Expr::eq(Expr::var("sb"), Expr::u32(b'L' as u32)),
                                        ),
                                    ),
                                    Expr::u32(1),
                                    Expr::u32(0),
                                ),
                            ),
                            Node::let_bind(
                                "is_z_suffix",
                                Expr::select(
                                    Expr::or(
                                        Expr::eq(Expr::var("sb"), Expr::u32(b'z' as u32)),
                                        Expr::eq(Expr::var("sb"), Expr::u32(b'Z' as u32)),
                                    ),
                                    Expr::u32(1),
                                    Expr::u32(0),
                                ),
                            ),
                            Node::let_bind(
                                "is_wb_suffix",
                                Expr::select(
                                    Expr::and(
                                        Expr::or(
                                            Expr::eq(Expr::var("sb"), Expr::u32(b'w' as u32)),
                                            Expr::eq(Expr::var("sb"), Expr::u32(b'W' as u32)),
                                        ),
                                        Expr::or(
                                            Expr::eq(Expr::var("sb1"), Expr::u32(b'b' as u32)),
                                            Expr::eq(Expr::var("sb1"), Expr::u32(b'B' as u32)),
                                        ),
                                    ),
                                    Expr::u32(1),
                                    Expr::u32(0),
                                ),
                            ),
                            Node::if_then_else(
                                Expr::or(
                                    Expr::or(
                                        Expr::eq(Expr::var("is_single_suffix"), Expr::u32(1)),
                                        Expr::eq(Expr::var("is_z_suffix"), Expr::u32(1)),
                                    ),
                                    Expr::eq(Expr::var("is_wb_suffix"), Expr::u32(1)),
                                ),
                                vec![Node::assign(
                                    "idx",
                                    Expr::add(
                                        Expr::var("idx"),
                                        Expr::select(
                                            Expr::eq(Expr::var("is_wb_suffix"), Expr::u32(1)),
                                            Expr::u32(2),
                                            Expr::u32(1),
                                        ),
                                    ),
                                )],
                                vec![Node::assign("done_suffix", Expr::u32(1))],
                            ),
                        ],
                    )],
                )],
            ),
            // Compute consumed bytes. If saw_digits is false, emit 0.
            Node::let_bind(
                "consumed",
                Expr::select(
                    Expr::eq(Expr::var("saw_digits"), Expr::u32(1)),
                    Expr::sub(Expr::var("idx"), Expr::var("start")),
                    Expr::u32(0),
                ),
            ),
            Node::let_bind(
                "value_final",
                Expr::select(
                    Expr::eq(Expr::var("saw_digits"), Expr::u32(1)),
                    Expr::var("value"),
                    Expr::u32(0),
                ),
            ),
            Node::store("value_out", Expr::u32(0), Expr::var("value_final")),
            Node::store("bytes_consumed_out", Expr::u32(0), Expr::var("consumed")),
        ],
    )];

    literal_scan_program(
        literal_scan_common_buffers(
            BINDING_SOURCE,
            BINDING_START_POS,
            BINDING_VALUE_OUT,
            BINDING_BYTES_CONSUMED_OUT,
        ),
        body,
        OP_ID,
    )
}

#[cfg(test)]

mod tests {
    use super::*;
    use vyre_reference::value::Value;

    fn run_literal_scan(source: &[u8], start: u32) -> (u32, u32) {
        let program = gpu_int_literal_scan(source.len() as u32);
        let mut packed_source = Vec::with_capacity(source.len().div_ceil(4).max(1) * 4);
        for chunk in source.chunks(4) {
            let mut word = [0u8; 4];
            word[..chunk.len()].copy_from_slice(chunk);
            packed_source.extend_from_slice(&word);
        }
        if packed_source.is_empty() {
            packed_source.extend_from_slice(&0u32.to_le_bytes());
        }
        let inputs = vec![
            Value::Bytes(packed_source.into()),
            Value::Bytes(start.to_le_bytes().to_vec().into()),
            Value::Bytes(vec![0u8; 4].into()),
            Value::Bytes(vec![0u8; 4].into()),
        ];
        let outputs = vyre_reference::reference_eval(&program, &inputs)
            .expect("Fix: GPU integer literal scanner reference evaluation must run.");
        let value = outputs[0].to_bytes();
        let consumed = outputs[1].to_bytes();
        (
            vyre_primitives::wire::read_u32_le_word(&value, 0, "int-literal value")
                .expect("Fix: integer literal value output must contain one u32."),
            vyre_primitives::wire::read_u32_le_word(&consumed, 0, "int-literal consumed")
                .expect("Fix: integer literal consumed output must contain one u32."),
        )
    }

    #[test]
    fn op_id_is_canonical_and_stable() {
        assert_eq!(
            OP_ID,
            "vyre-libs::parsing::c::preprocess::gpu_int_literal_scan"
        );
    }

    #[test]
    fn binding_indices_are_canonical_and_stable() {
        assert_eq!(BINDING_SOURCE, 0);
        assert_eq!(BINDING_START_POS, 1);
        assert_eq!(BINDING_VALUE_OUT, 2);
        assert_eq!(BINDING_BYTES_CONSUMED_OUT, 3);
    }

    #[test]
    fn build_program_returns_well_formed_program() {
        let p = gpu_int_literal_scan(64);
        assert_eq!(p.buffers().len(), 4);
        assert_eq!(p.workgroup_size(), [256, 1, 1]);
    }

    #[test]
    fn source_buffer_is_runtime_sized_not_source_length_specialized() {
        let p = gpu_int_literal_scan(64);
        let source = p
            .buffers()
            .iter()
            .find(|buffer| buffer.name() == "source")
            .expect("Fix: source buffer must exist");
        assert_eq!(
            source.count(),
            0,
            "source must be runtime-sized so one scanner program serves all source lengths"
        );
    }

    #[test]
    fn reference_eval_consumes_digit_separators_and_modern_suffixes() {
        assert_eq!(run_literal_scan(b"1'024ULL", 0), (1024, 8));
        assert_eq!(run_literal_scan(b"xx0xFF'00z", 2), (65_280, 8));
        assert_eq!(run_literal_scan(b"0b1010'0101WB", 0), (165, 13));
    }
}