vyre-primitives 0.6.3

Compositional primitives for vyre - marker types (always on) + Tier 2.5 LEGO substrate (feature-gated per domain).
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
//! `bitset_any`  -  emit 1 when any bit in the packed bitset is set.
//!
//! Single-lane Program driven by invocation 0: scans every word,
//! ORs them, writes a boolean (0 or 1) to `out[0]`. Used by source-query dialect
//! `exists` / `any(...)` aggregate lowerings.

use std::sync::Arc;

use vyre_foundation::ir::model::expr::Ident;
use vyre_foundation::ir::{BufferAccess, BufferDecl, DataType, Expr, Node, Program};

/// Canonical op id.
pub const OP_ID: &str = "vyre-primitives::bitset::any";

/// Build a Program: `out[0] = 1` iff any bit of `input` is set.
///
/// AUDIT_2026-04-24 F-ANY-01: the inner loop short-circuits once a
/// non-zero word is observed (tracked via `found` flag). The IR has
/// no `break`, so the cheapest escape is to gate the load+or body on
/// `found == 0`  -  subsequent iterations become empty bodies and the
/// scan cost degrades to O(first_nonzero_word) instead of O(words).
/// Bitsets are typically sparse (e.g. taint frontiers with one or
/// two set bits) so the average cut is large.
#[must_use]
pub fn bitset_any(input: &str, out: &str, words: u32) -> Program {
    let body = vec![
        Node::let_bind("acc", Expr::u32(0)),
        Node::let_bind("found", Expr::u32(0)),
        Node::loop_for(
            "w",
            Expr::u32(0),
            Expr::u32(words),
            vec![Node::if_then(
                Expr::eq(Expr::var("found"), Expr::u32(0)),
                vec![
                    Node::assign(
                        "acc",
                        Expr::bitor(Expr::var("acc"), Expr::load(input, Expr::var("w"))),
                    ),
                    Node::if_then(
                        Expr::ne(Expr::var("acc"), Expr::u32(0)),
                        vec![Node::assign("found", Expr::u32(1))],
                    ),
                ],
            )],
        ),
        Node::store(
            out,
            Expr::u32(0),
            Expr::select(
                Expr::ne(Expr::var("acc"), Expr::u32(0)),
                Expr::u32(1),
                Expr::u32(0),
            ),
        ),
    ];
    Program::wrapped(
        vec![
            BufferDecl::storage(input, 0, BufferAccess::ReadOnly, DataType::U32).with_count(words),
            BufferDecl::storage(out, 1, BufferAccess::ReadWrite, DataType::U32).with_count(1),
        ],
        [1, 1, 1],
        vec![Node::Region {
            generator: Ident::from(OP_ID),
            source_region: None,
            body: Arc::new(vec![Node::if_then(
                Expr::eq(Expr::InvocationId { axis: 0 }, Expr::u32(0)),
                body,
            )]),
        }],
    )
}

/// CPU reference.
#[must_use]
#[cfg(any(test, feature = "cpu-parity"))]
pub fn cpu_ref(input: &[u32]) -> u32 {
    if input.iter().any(|w| *w != 0) {
        1
    } else {
        0
    }
}

#[cfg(feature = "inventory-registry")]
inventory::submit! {
    crate::harness::OpEntry::new(
        OP_ID,
        || bitset_any("input", "out", 2),
        Some(|| {
            let to_bytes = |w: &[u32]| crate::wire::pack_u32_slice(w);
            vec![vec![to_bytes(&[0, 1]), to_bytes(&[0])]]
        }),
        Some(|| {
            let to_bytes = |w: &[u32]| crate::wire::pack_u32_slice(w);
            vec![vec![to_bytes(&[1])]]
        }),
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn any_true_when_single_bit_set() {
        assert_eq!(cpu_ref(&[0, 1]), 1);
    }

    #[test]
    fn any_false_when_all_zero() {
        assert_eq!(cpu_ref(&[0, 0]), 0);
    }

    /// GPU parity tests for bitset_any  -  exercise every word boundary
    /// to expose the word-1+ bitset read bug.
    mod gpu_tests {
        use super::*;
        use vyre_driver::DispatchConfig;
        use vyre_driver_cuda::CudaBackend;

        fn run_gpu_any_with_backend(backend: &CudaBackend, input: &[u32]) -> u32 {
            let words = input.len() as u32;
            let program = bitset_any("input", "out", words);
            let outputs = backend
                .dispatch(
                    &program,
                    &[
                        crate::wire::pack_u32_slice(input),
                        crate::wire::pack_u32_slice(&[0]),
                    ],
                    &DispatchConfig::default(),
                )
                .expect(
                    "Fix: CUDA dispatch failed for bitset_any. \
                     Configuration error: GPU present but kernel launch or readback failed.",
                );
            assert_eq!(
                outputs.len(),
                1,
                "Fix: bitset_any must return exactly one output buffer, got {}.",
                outputs.len()
            );
            let result = crate::wire::decode_u32_le_bytes_all(&outputs[0]);
            assert_eq!(
                result.len(),
                1,
                "Fix: bitset_any output buffer must contain exactly one u32 word, got {}.",
                result.len()
            );
            result[0]
        }

        fn run_gpu_any(input: &[u32]) -> u32 {
            let backend = CudaBackend::acquire().expect(
                "Fix: CUDA backend acquisition failed. \
                 Configuration error: no CUDA-capable GPU or driver available on this host.",
            );
            run_gpu_any_with_backend(&backend, input)
        }

        // ---- Word-boundary positive cases (one per word 0..7) ----

        #[test]
        fn gpu_any_true_only_word_0_bit_0() {
            let input = vec![1, 0, 0, 0, 0, 0, 0, 0];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W0B0: GPU output mismatch when only word-0 bit-0 is set"
            );
        }

        #[test]
        fn gpu_any_true_only_word_1_bit_0() {
            let input = vec![0, 1, 0, 0, 0, 0, 0, 0];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W1B0: GPU output mismatch when only word-1 is set"
            );
        }

        #[test]
        fn gpu_any_true_only_word_2_bit_0() {
            let input = vec![0, 0, 1, 0, 0, 0, 0, 0];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W2B0: GPU output mismatch when only word-2 is set"
            );
        }

        #[test]
        fn gpu_any_true_only_word_3_bit_0() {
            let input = vec![0, 0, 0, 1, 0, 0, 0, 0];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W3B0: GPU output mismatch when only word-3 is set"
            );
        }

        #[test]
        fn gpu_any_true_only_word_4_bit_0() {
            let input = vec![0, 0, 0, 0, 1, 0, 0, 0];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W4B0: GPU output mismatch when only word-4 is set"
            );
        }

        #[test]
        fn gpu_any_true_only_word_5_bit_0() {
            let input = vec![0, 0, 0, 0, 0, 1, 0, 0];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W5B0: GPU output mismatch when only word-5 is set"
            );
        }

        #[test]
        fn gpu_any_true_only_word_6_bit_0() {
            let input = vec![0, 0, 0, 0, 0, 0, 1, 0];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W6B0: GPU output mismatch when only word-6 is set"
            );
        }

        #[test]
        fn gpu_any_true_only_word_7_bit_0() {
            let input = vec![0, 0, 0, 0, 0, 0, 0, 1];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W7B0: GPU output mismatch when only word-7 is set"
            );
        }

        // ---- Adversarial corner cases at 32-bit word boundaries ----

        #[test]
        fn gpu_any_true_word_0_bit_31_boundary() {
            let input = vec![0x8000_0000, 0, 0, 0, 0, 0, 0, 0];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W0B31: GPU output mismatch at bit-31 boundary"
            );
        }

        #[test]
        fn gpu_any_true_word_1_bit_32_boundary() {
            let input = vec![0, 0x0000_0001, 0, 0, 0, 0, 0, 0];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W1B0-BIT32: GPU output mismatch at bit-32 boundary"
            );
        }

        #[test]
        fn gpu_any_true_word_1_bit_39_like_node_39() {
            let input = vec![0, 1 << 7, 0, 0, 0, 0, 0, 0];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W1B7: GPU output mismatch at bit-39 (word-1 bit-7)"
            );
        }

        #[test]
        fn gpu_any_true_word_1_bit_63_boundary() {
            let input = vec![0, 0x8000_0000, 0, 0, 0, 0, 0, 0];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W1B31-BIT63: GPU output mismatch at bit-63 boundary"
            );
        }

        #[test]
        fn gpu_any_true_word_2_bit_64_boundary() {
            let input = vec![0, 0, 0x0000_0001, 0, 0, 0, 0, 0];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W2B0-BIT64: GPU output mismatch at bit-64 boundary"
            );
        }

        #[test]
        fn gpu_any_true_word_2_bit_65_like_node_65() {
            let input = vec![0, 0, 1 << 1, 0, 0, 0, 0, 0];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W2B1: GPU output mismatch at bit-65 (word-2 bit-1)"
            );
        }

        #[test]
        fn gpu_any_true_word_3_bit_96_boundary() {
            let input = vec![0, 0, 0, 0x0000_0001, 0, 0, 0, 0];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W3B0-BIT96: GPU output mismatch at bit-96 boundary"
            );
        }

        #[test]
        fn gpu_any_true_word_4_bit_128_boundary() {
            let input = vec![0, 0, 0, 0, 0x0000_0001, 0, 0, 0];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W4B0-BIT128: GPU output mismatch at bit-128 boundary"
            );
        }

        #[test]
        fn gpu_any_true_word_5_bit_160_boundary() {
            let input = vec![0, 0, 0, 0, 0, 0x0000_0001, 0, 0];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W5B0-BIT160: GPU output mismatch at bit-160 boundary"
            );
        }

        #[test]
        fn gpu_any_true_word_6_bit_192_boundary() {
            let input = vec![0, 0, 0, 0, 0, 0, 0x0000_0001, 0];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W6B0-BIT192: GPU output mismatch at bit-192 boundary"
            );
        }

        #[test]
        fn gpu_any_true_word_7_bit_224_boundary() {
            let input = vec![0, 0, 0, 0, 0, 0, 0, 0x0000_0001];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W7B0-BIT224: GPU output mismatch at bit-224 boundary"
            );
        }

        #[test]
        fn gpu_any_true_word_7_bit_255_last_bit() {
            let input = vec![0, 0, 0, 0, 0, 0, 0, 0x8000_0000];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-W7B31-BIT255: GPU output mismatch at last bit-255"
            );
        }

        #[test]
        fn gpu_any_false_all_zero_8_words() {
            let input = vec![0u32; 8];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-ALLZERO: GPU output mismatch for all-zero bitset"
            );
        }

        #[test]
        fn gpu_any_true_all_ones_8_words() {
            let input = vec![0xffff_ffff; 8];
            assert_eq!(
                run_gpu_any(&input),
                cpu_ref(&input),
                "FINDING-GPU-BITSET-ANY-ALLONE: GPU output mismatch for all-ones bitset"
            );
        }

        // ---- Bounded adversarial corpus over 256 nodes ----

        #[test]
        fn gpu_any_matches_cpu_oracle_adversarial_256_corpus() {
            let backend = CudaBackend::acquire().expect(
                "Fix: CUDA backend acquisition failed. \
                 Configuration error: no CUDA-capable GPU or driver available on this host.",
            );
            let mut cases = Vec::with_capacity(96);
            cases.push([0u32; 8]);
            cases.push([u32::MAX; 8]);
            for word in 0..8 {
                let mut first_bit = [0u32; 8];
                first_bit[word] = 1;
                cases.push(first_bit);

                let mut last_bit = [0u32; 8];
                last_bit[word] = 0x8000_0000;
                cases.push(last_bit);

                let mut alternating = [0u32; 8];
                alternating[word] = if word % 2 == 0 {
                    0x5555_5555
                } else {
                    0xaaaa_aaaa
                };
                cases.push(alternating);
            }

            let mut state = 0x9e37_79b9u32;
            for _ in 0..64 {
                let mut input = [0u32; 8];
                for word in &mut input {
                    state ^= state << 13;
                    state ^= state >> 17;
                    state ^= state << 5;
                    *word = state;
                }
                cases.push(input);
            }

            for input in cases {
                let gpu = run_gpu_any_with_backend(&backend, &input);
                let cpu = cpu_ref(&input);
                assert_eq!(
                    gpu, cpu,
                    "FINDING-GPU-BITSET-ANY-PROP: GPU any({:?}) = {}, CPU oracle = {}",
                    input, gpu, cpu
                );
            }
        }
    }
}