vyre-conform 0.1.0

Conformance suite for vyre backends — proves byte-identical output to CPU reference
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
//! Atomic sequential-consistency enforcer.
//!
//! Verifies that a backend implements vyre atomics with sequential consistency:
//! all atomic operations on a given location are consistent with some sequential
//! ordering of invocations.

use checks::{
    check_atomic, check_compare_exchange, make_atomic_program, make_compare_exchange_program,
    parse_output,
};
use vyre::ir::{AtomicOp, Program};

/// Report produced by the atomics enforcer.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AtomicsReport {
    /// All detected violations.
    pub findings: Vec<AtomicFinding>,
}

/// A single detected atomic consistency violation.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AtomicFinding {
    /// Which atomic operation was tested.
    pub op: AtomicOp,
    /// Number of invocations.
    pub invocations: u32,
    /// Human-readable diagnostic message with "Fix: ..." guidance.
    pub message: String,
    /// Run index at which the violation was first observed.
    pub run: u32,
}

/// Enforce sequential consistency for all vyre atomic operations.
///
/// Constructs IR programs that exercise each atomic op with a sweep of
/// invocation counts around power-of-two boundaries (1, 63, 65, 127, 129,
/// 255, 257, 1024) plus the backend's reported max workgroup size when
/// available. Each program repeats 5 times. A backend that passes once but
/// fails once is flagged as inconsistent.
#[inline]
pub(crate) fn enforce_atomics(backend: &dyn vyre::VyreBackend) -> AtomicsReport {
    let mut findings = Vec::new();
    let mut invocation_counts = vec![1u32, 63, 65, 127, 129, 255, 257, 1024];

    // Canonical operands for atomic smoke tests.
    // - Add(1): unit increment, the simplest non-trivial atomic add.
    // - Or(0x1234_5678): dense bit pattern with mixed 1s/0s to verify bitwise OR.
    // - And(0xF0F0_F0F0): alternating nibble mask to verify bitwise AND.
    // - Xor(0xAAAA_AAAA): alternating bit pattern to verify bitwise XOR.
    // - Min/Max(42): arbitrary interior value to test absorbing min/max semantics.
    // - Exchange(0xDEAD_BEEF): distinctive sentinel to verify unconditional store.
    let atomic_configs: &[(AtomicOp, u32)] = &[
        (AtomicOp::Add, 1),
        (AtomicOp::Or, 0x1234_5678),
        (AtomicOp::And, 0xF0F0_F0F0),
        (AtomicOp::Xor, 0xAAAA_AAAA),
        (AtomicOp::Min, 42),
        (AtomicOp::Max, 42),
        (AtomicOp::Exchange, 0xDEAD_BEEF),
    ];

    // Boundary operands per T26 integer-extreme-value analysis and
    // IEEE-754 boundary principles (ported to unsigned 32-bit integers):
    // - 0: identity for Add/Or/Xor; absorbing for And; extreme for Min/Max.
    // - 1: smallest non-zero, catches off-by-one in sequential reductions.
    // - u32::MAX: wrap-around boundary for Add; absolute maximum for ordering ops.
    // - u32::MAX - 1: one-below-max, catches fence-post errors near overflow.
    // - 0x8000_0000: MSB-set boundary (sign bit in two's-complement).
    // - 0x7FFF_FFFF: MSB-clear maximum, paired with 0x8000_0000 for full coverage.
    let boundary_operands: &[u32] = &[0, 1, u32::MAX, u32::MAX - 1, 0x8000_0000, 0x7FFF_FFFF];

    for &n in &invocation_counts {
        for (op, operand) in atomic_configs.iter().cloned() {
            if let Some(finding) = run_atomic_pass(backend, op, operand, n) {
                findings.push(finding);
            }
        }

        for &operand in boundary_operands {
            for op in [
                AtomicOp::Add,
                AtomicOp::Or,
                AtomicOp::And,
                AtomicOp::Xor,
                AtomicOp::Min,
                AtomicOp::Max,
                AtomicOp::Exchange,
            ] {
                if let Some(finding) = run_atomic_pass(backend, op, operand, n) {
                    findings.push(finding);
                }
            }
        }

        // CompareExchange uses its own test harness.
        if let Some(finding) = run_compare_exchange_pass(backend, n) {
            findings.push(finding);
        }
    }

    AtomicsReport { findings }
}

/// Enforce one atomic operation at one invocation count.
///
/// This is the same check used by [`enforce_atomics`] and exists so callers can
/// pin regressions to adversarial invocation counts instead of only using the
/// standard sweep.
#[inline]
pub fn enforce_atomic_operation(
    backend: &dyn vyre::VyreBackend,
    op: AtomicOp,
    operand: u32,
    invocations: u32,
) -> Option<AtomicFinding> {
    run_atomic_pass(backend, op, operand, invocations)
}

/// Enforce compare-exchange at one invocation count.
///
/// This is the same compare-exchange check used by [`enforce_atomics`].
#[inline]
pub fn enforce_compare_exchange_operation(
    backend: &dyn vyre::VyreBackend,
    invocations: u32,
) -> Option<AtomicFinding> {
    run_compare_exchange_pass(backend, invocations)
}

fn run_atomic_pass(
    backend: &dyn vyre::VyreBackend,
    op: AtomicOp,
    operand: u32,
    n: u32,
) -> Option<AtomicFinding> {
    if let Some(finding) = invalid_invocation_count_finding(op.clone(), n) {
        return Some(finding);
    }
    let output_size = match atomic_output_size(n) {
        Ok(size) => size,
        Err(message) => {
            return Some(AtomicFinding {
                op: op.clone(),
                invocations: n,
                message,
                run: 0,
            })
        }
    };
    let program = match make_atomic_program(op.clone(), operand, n) {
        Ok(program) => program,
        Err(message) => {
            return Some(AtomicFinding {
                op: op.clone(),
                invocations: n,
                message,
                run: 0,
            })
        }
    };
    let input = Vec::new();

    let mut first_run: Option<(Vec<u32>, u32)> = None;

    for run in 0..5 {
        let output = match dispatch_exact(backend, &program, &[input.clone()], output_size) {
            Ok(bytes) => bytes,
            Err(err) => {
                // Retry with a smaller workgroup size if the backend rejected n.
                if n > 64 && n % 64 == 0 {
                    let fallback_program = with_workgroup_size(&program, [64, 1, 1]);
                    match dispatch_exact(backend, &fallback_program, &[input.clone()], output_size)
                    {
                        Ok(bytes) => bytes,
                        Err(err2) => {
                            return Some(AtomicFinding {
                                op: op.clone(),
                                invocations: n,
                                message: format!(
                                    "dispatch failed at {n} invocations (run {run}): {err2}. \
                                     Original error: {err}. Fix: ensure backend supports the requested workgroup configuration."
                                ),
                                run,
                            });
                        }
                    }
                } else {
                    return Some(AtomicFinding {
                        op: op.clone(),
                        invocations: n,
                        message: format!(
                            "dispatch failed at {n} invocations (run {run}): {err}. Fix: ensure backend can compile and run atomic workloads."
                        ),
                        run,
                    });
                }
            }
        };

        if let Some(finding) = output_len_finding(op.clone(), n, run, output.len(), output_size) {
            return Some(finding);
        }

        let (pre_values, final_val) = match parse_output(&output, n) {
            Ok(parsed) => parsed,
            Err(message) => {
                return Some(AtomicFinding {
                    op: op.clone(),
                    invocations: n,
                    message,
                    run,
                })
            }
        };

        if let Some(msg) = check_atomic(
            op.clone(),
            &pre_values,
            final_val,
            n,
            operand,
            atomic_initial(op.clone()),
        ) {
            return Some(AtomicFinding {
                op: op.clone(),
                invocations: n,
                message: msg,
                run,
            });
        }

        if let Some((ref first_pre, first_final)) = first_run {
            if first_pre != &pre_values || first_final != final_val {
                return Some(AtomicFinding {
                    op: op.clone(),
                    invocations: n,
                    message: format!(
                        "atomic {op:?} results diverged across runs: run 0 had final={first_final} pre-values={first_pre:?}, \
                         run {run} had final={final_val} pre-values={pre_values:?}. \
                         Fix: a conforming backend must produce sequentially-consistent atomic results deterministically."
                    ),
                    run,
                });
            }
        } else {
            first_run = Some((pre_values, final_val));
        }
    }

    None
}

fn run_compare_exchange_pass(backend: &dyn vyre::VyreBackend, n: u32) -> Option<AtomicFinding> {
    if let Some(finding) = invalid_invocation_count_finding(AtomicOp::CompareExchange, n) {
        return Some(finding);
    }
    let output_size = match atomic_output_size(n) {
        Ok(size) => size,
        Err(message) => {
            return Some(AtomicFinding {
                op: AtomicOp::CompareExchange,
                invocations: n,
                message,
                run: 0,
            })
        }
    };
    let expected = 0u32;
    let new_value = 1u32;
    let program = make_compare_exchange_program(expected, new_value, n);
    let input = expected.to_le_bytes().to_vec();

    let mut first_run: Option<(Vec<u32>, u32)> = None;

    for run in 0..5 {
        let output = match dispatch_exact(backend, &program, &[input.clone()], output_size) {
            Ok(bytes) => bytes,
            Err(err) => {
                if n > 64 && n % 64 == 0 {
                    let fallback_program = with_workgroup_size(&program, [64, 1, 1]);
                    match dispatch_exact(backend, &fallback_program, &[input.clone()], output_size)
                    {
                        Ok(bytes) => bytes,
                        Err(err2) => {
                            return Some(AtomicFinding {
                                op: AtomicOp::CompareExchange,
                                invocations: n,
                                message: format!(
                                    "dispatch failed at {n} invocations (run {run}): {err2}. \
                                     Original error: {err}. Fix: ensure backend supports the requested workgroup configuration."
                                ),
                                run,
                            });
                        }
                    }
                } else {
                    return Some(AtomicFinding {
                        op: AtomicOp::CompareExchange,
                        invocations: n,
                        message: format!(
                            "dispatch failed at {n} invocations (run {run}): {err}. Fix: ensure backend can compile and run atomic workloads."
                        ),
                        run,
                    });
                }
            }
        };

        if let Some(finding) =
            output_len_finding(AtomicOp::CompareExchange, n, run, output.len(), output_size)
        {
            return Some(finding);
        }

        let (pre_values, final_val) = match parse_output(&output, n) {
            Ok(parsed) => parsed,
            Err(message) => {
                return Some(AtomicFinding {
                    op: AtomicOp::CompareExchange,
                    invocations: n,
                    message,
                    run,
                })
            }
        };

        if let Some(msg) = check_compare_exchange(&pre_values, final_val, n, expected, new_value) {
            return Some(AtomicFinding {
                op: AtomicOp::CompareExchange,
                invocations: n,
                message: msg,
                run,
            });
        }

        if let Some((ref first_pre, first_final)) = first_run {
            if first_pre != &pre_values || first_final != final_val {
                return Some(AtomicFinding {
                    op: AtomicOp::CompareExchange,
                    invocations: n,
                    message: format!(
                        "atomic CompareExchange results diverged across runs: run 0 had final={first_final} pre-values={first_pre:?}, \
                         run {run} had final={final_val} pre-values={pre_values:?}. \
                         Fix: a conforming backend must produce sequentially-consistent atomic results deterministically."
                    ),
                    run,
                });
            }
        } else {
            first_run = Some((pre_values, final_val));
        }
    }

    None
}

use probe_glue::*;

mod checks {
    use vyre::ir::{AtomicOp, BufferDecl, DataType, Expr, Node, Program};

    pub(super) fn make_atomic_program(
        op: AtomicOp,
        operand: u32,
        n: u32,
    ) -> Result<Program, String> {
        let atomic_expr = match op {
        AtomicOp::Add => Expr::atomic_add("output", Expr::u32(0), Expr::u32(operand)),
        AtomicOp::Or => atomic_expr(AtomicOp::Or, operand),
        AtomicOp::And => atomic_expr(AtomicOp::And, operand),
        AtomicOp::Xor => atomic_expr(AtomicOp::Xor, operand),
        AtomicOp::Min => atomic_expr(AtomicOp::Min, operand),
        AtomicOp::Max => atomic_expr(AtomicOp::Max, operand),
        AtomicOp::Exchange => atomic_expr(AtomicOp::Exchange, operand),
        AtomicOp::CompareExchange => {
            return Err(
                "unsupported atomic op CompareExchange in generic atomic harness. Fix: use the compare-exchange harness."
                    .to_string(),
            )
        }
    };

        Ok(Program::new(
            vec![
                BufferDecl::read("input", 0, DataType::U32),
                BufferDecl::read_write("output", 1, DataType::U32),
            ],
            [n, 1, 1],
            vec![
                Node::let_bind("gid", Expr::gid_x()),
                Node::if_then(
                    Expr::lt(Expr::var("gid"), Expr::u32(n)),
                    vec![
                        Node::let_bind("pre", atomic_expr),
                        Node::store(
                            "output",
                            Expr::add(Expr::var("gid"), Expr::u32(1)),
                            Expr::var("pre"),
                        ),
                    ],
                ),
            ],
        ))
    }

    pub(super) fn make_compare_exchange_program(_expected: u32, new_value: u32, n: u32) -> Program {
        Program::new(
            vec![
                BufferDecl::read("input", 0, DataType::U32),
                BufferDecl::read_write("output", 1, DataType::U32),
            ],
            [n, 1, 1],
            vec![
                Node::let_bind("gid", Expr::gid_x()),
                Node::if_then(
                    Expr::lt(Expr::var("gid"), Expr::u32(n)),
                    vec![
                        Node::let_bind(
                            "pre",
                            Expr::atomic_compare_exchange(
                                "output",
                                Expr::u32(0),
                                Expr::load("input", Expr::u32(0)),
                                Expr::add(Expr::load("input", Expr::u32(0)), Expr::u32(new_value)),
                            ),
                        ),
                        Node::store(
                            "output",
                            Expr::add(Expr::var("gid"), Expr::u32(1)),
                            Expr::var("pre"),
                        ),
                    ],
                ),
            ],
        )
    }

    pub(super) fn parse_output(output: &[u8], n: u32) -> Result<(Vec<u32>, u32), String> {
        let words_len = n
        .checked_add(1)
        .ok_or_else(|| "atomic output word count overflowed u32. Fix: reject unrepresentable invocation counts before parsing.".to_string())?
        as usize;
        let byte_len = words_len
        .checked_mul(4)
        .ok_or_else(|| "atomic output byte count overflowed usize. Fix: reject unrepresentable invocation counts before parsing.".to_string())?;
        if output.len() != byte_len {
            return Err(format!(
            "atomic parser received {} bytes for {n} invocations, expected exactly {byte_len}. Fix: validate output length before parsing.",
            output.len()
        ));
        }
        let mut words = Vec::with_capacity(words_len);
        for i in 0..words_len {
            let start = i * 4;
            words.push(u32::from_le_bytes([
                output[start],
                output[start + 1],
                output[start + 2],
                output[start + 3],
            ]));
        }
        let Some((&final_val, pre_values)) = words.split_first() else {
            return Err(
            "atomic parser received zero words. Fix: reject zero invocation tests before dispatch."
                .to_string(),
        );
        };
        Ok((pre_values.to_vec(), final_val))
    }

    pub(super) fn check_atomic(
        op: AtomicOp,
        pre_values: &[u32],
        final_val: u32,
        n: u32,
        operand: u32,
        initial: u32,
    ) -> Option<String> {
        match op {
        AtomicOp::Add => check_add(pre_values, final_val, n),
        AtomicOp::Or => check_or(pre_values, final_val, n, operand, initial),
        AtomicOp::And => check_and(pre_values, final_val, n, operand, initial),
        AtomicOp::Xor => check_xor(pre_values, final_val, n, operand),
        AtomicOp::Min => check_min(pre_values, final_val, n, operand),
        AtomicOp::Max => check_max(pre_values, final_val, n, operand),
        AtomicOp::Exchange => check_exchange(pre_values, final_val, n, operand),
        AtomicOp::CompareExchange => Some(
            "unsupported atomic op CompareExchange in generic atomic checker. Fix: use the compare-exchange checker."
                .to_string(),
        ),
    }
    }

    pub(super) fn check_compare_exchange(
        pre_values: &[u32],
        final_val: u32,
        n: u32,
        expected: u32,
        new_value: u32,
    ) -> Option<String> {
        let successes = pre_values.iter().filter(|&&v| v == expected).count();
        if successes > 1 {
            return Some(format!(
            "AtomicCompareExchange let {successes} invocations succeed with expected={expected} (limit is 1). \
             Fix: implement sequentially-consistent compare-exchange so that only one invocation \
             succeeds when multiple race on the same expected value.",
        ));
        }
        if successes == 1 && final_val != new_value {
            return Some(format!(
            "AtomicCompareExchange had exactly one success but final counter = {final_val} \
             instead of {new_value}. Fix: the successful compare-exchange must store the new value."
        ));
        }
        if successes == 0 && n > 0 && final_val == expected {
            return Some(format!(
            "AtomicCompareExchange reported zero successes even though final counter stayed at expected={expected}. \
             Fix: when the initial value equals expected, exactly one racing invocation must succeed."
        ));
        }
        None
    }

    fn atomic_expr(op: AtomicOp, operand: u32) -> Expr {
        Expr::Atomic {
            op,
            buffer: "output".into(),
            index: Box::new(Expr::u32(0)),
            expected: None,
            value: Box::new(Expr::u32(operand)),
        }
    }

    fn check_add(pre_values: &[u32], final_val: u32, n: u32) -> Option<String> {
        let mut sorted = pre_values.to_vec();
        sorted.sort_unstable();
        if sorted.len() != n as usize {
            return Some(format!(
            "AtomicAdd returned {} pre-values for {n} invocations. Fix: ensure every invocation writes exactly one pre-value.",
            sorted.len()
        ));
        }
        let x = sorted[0];
        if sorted.windows(2).all(|w| w[1] == w[0].wrapping_add(1)) {
            let expected_final = x.wrapping_add(n);
            if final_val != expected_final {
                return Some(format!(
                "AtomicAdd final counter = {final_val} with {n} invocations, but pre-values imply initial={x} so final should be {expected_final}. Fix: atomicAdd lost or duplicated an increment."
            ));
            }
            return None;
        }
        let derived_initial = final_val.wrapping_sub(n);
        Some(format!(
        "AtomicAdd returned pre-values not a permutation of [{},{}) - final counter = {final_val}. Fix: implement sequentially-consistent atomicAdd with wrapping addition.",
        derived_initial,
        derived_initial.wrapping_add(n)
    ))
    }

    fn check_or(
        pre_values: &[u32],
        final_val: u32,
        n: u32,
        operand: u32,
        initial: u32,
    ) -> Option<String> {
        check_two_state(
            pre_values,
            final_val,
            n,
            initial,
            |v| v | operand,
            "AtomicOr",
            operand,
        )
    }

    fn check_and(
        pre_values: &[u32],
        final_val: u32,
        n: u32,
        operand: u32,
        initial: u32,
    ) -> Option<String> {
        check_two_state(
            pre_values,
            final_val,
            n,
            initial,
            |v| v & operand,
            "AtomicAnd",
            operand,
        )
    }

    fn check_two_state(
        pre_values: &[u32],
        final_val: u32,
        n: u32,
        initial: u32,
        apply: impl Fn(u32) -> u32,
        name: &str,
        operand: u32,
    ) -> Option<String> {
        let count_final = pre_values.iter().filter(|&&v| v == final_val).count();
        if count_final == n as usize {
            if final_val == initial && apply(final_val) == final_val {
                return None;
            }
        } else if count_final == n as usize - 1 {
            let minority: Vec<_> = pre_values.iter().filter(|&&v| v != final_val).collect();
            if minority.len() == 1 && apply(*minority[0]) == final_val {
                return None;
            }
        }
        Some(format!(
        "{name} final counter = {final_val} with {n} invocations (operand={operand:#x}), but returned pre-values do not match any sequential ordering. Fix: implement sequentially-consistent {name}."
    ))
    }

    fn check_xor(pre_values: &[u32], final_val: u32, n: u32, operand: u32) -> Option<String> {
        let other = final_val ^ operand;
        let count_final = pre_values.iter().filter(|&&v| v == final_val).count();
        let count_other = pre_values.iter().filter(|&&v| v == other).count();
        if operand == 0 && count_final == n as usize {
            return None;
        }
        if count_final == n as usize / 2 && count_other == n as usize - (n as usize / 2) {
            return None;
        }
        Some(format!(
        "AtomicXor final counter = {final_val} with {n} invocations (operand={operand:#x}), but returned pre-values do not match any sequential ordering. Fix: implement sequentially-consistent atomicXor."
    ))
    }

    fn check_min(pre_values: &[u32], final_val: u32, n: u32, operand: u32) -> Option<String> {
        check_absorbing(
            pre_values,
            final_val,
            n,
            operand,
            std::cmp::min,
            "AtomicMin",
        )
    }

    fn check_max(pre_values: &[u32], final_val: u32, n: u32, operand: u32) -> Option<String> {
        check_absorbing(
            pre_values,
            final_val,
            n,
            operand,
            std::cmp::max,
            "AtomicMax",
        )
    }

    fn check_absorbing(
        pre_values: &[u32],
        final_val: u32,
        n: u32,
        operand: u32,
        apply: fn(u32, u32) -> u32,
        name: &str,
    ) -> Option<String> {
        let unique: std::collections::HashSet<_> = pre_values.iter().copied().collect();
        if unique.len() == 1 && apply(pre_values[0], operand) == final_val {
            return None;
        }
        if unique.len() == 2 {
            let singleton = unique
                .iter()
                .find(|&&candidate| pre_values.iter().filter(|&&v| v == candidate).count() == 1);
            if let Some(&x) = singleton {
                let y = *unique.iter().find(|&&k| k != x).unwrap_or(&x);
                if y == final_val && y == apply(x, operand) {
                    return None;
                }
            }
        }
        Some(format!(
        "{name} final counter = {final_val} with {n} invocations (operand={operand}), but returned pre-values do not match any sequential ordering. Fix: implement sequentially-consistent {name}."
    ))
    }

    fn check_exchange(pre_values: &[u32], final_val: u32, n: u32, operand: u32) -> Option<String> {
        if final_val != operand {
            return Some(format!(
            "AtomicExchange final counter = {final_val} but operand was {operand}. Fix: atomicExchange must store the operand unconditionally."
        ));
        }
        let count_operand = pre_values.iter().filter(|&&v| v == operand).count();
        if count_operand == n as usize || count_operand == n as usize - 1 {
            None
        } else {
            Some(format!(
            "AtomicExchange with {n} invocations returned pre-values inconsistent with any sequential ordering (operand={operand}). Fix: implement sequentially-consistent atomicExchange."
        ))
        }
    }
}

mod probe_glue {
    /// Helpers for atomic enforcement.
    use crate::enforce::enforcers::atomics::*;

    pub(super) fn with_workgroup_size(program: &Program, workgroup_size: [u32; 3]) -> Program {
        let mut fallback_program = program.clone();
        fallback_program.set_workgroup_size(workgroup_size);
        fallback_program
    }

    pub(super) fn dispatch_exact(
        backend: &dyn vyre::VyreBackend,
        program: &Program,
        inputs: &[Vec<u8>],
        output_size: usize,
    ) -> Result<Vec<u8>, vyre::BackendError> {
        let program = program_with_output_size(program, output_size);
        let mut outputs = backend.dispatch(&program, inputs, &vyre::DispatchConfig::default())?;
        if outputs.is_empty() {
            return Err(vyre::BackendError::new(
                "backend returned zero output buffers. Fix: return the atomic probe output as outputs[0].",
            ));
        }
        let output = outputs.remove(0);
        if output.len() != output_size {
            return Err(vyre::BackendError::new(format!(
                "backend returned {} bytes, expected {output_size}. Fix: size the first output buffer from the atomic probe output declaration.",
                output.len()
            )));
        }
        Ok(output)
    }

    fn program_with_output_size(program: &Program, output_size: usize) -> Program {
        let mut buffers = program.buffers().to_vec();
        for buffer in &mut buffers {
            if buffer.access == vyre::ir::BufferAccess::ReadWrite {
                buffer.is_output = true;
                buffer.count = output_size.div_ceil(4).try_into().unwrap_or(u32::MAX);
                break;
            }
        }
        Program::new(buffers, program.workgroup_size(), program.entry().to_vec())
    }

    pub(super) fn atomic_initial(op: AtomicOp) -> u32 {
        if matches!(op, AtomicOp::And) {
            0xCDCD_CDCD
        } else {
            0
        }
    }

    pub(super) fn output_len_finding(
        op: AtomicOp,
        n: u32,
        run: u32,
        actual: usize,
        expected: usize,
    ) -> Option<AtomicFinding> {
        (actual != expected).then(|| AtomicFinding {
        op: op.clone(),
        invocations: n,
        message: format!(
            "atomic {op:?} returned {actual} bytes for {n} invocations, expected exactly {expected}. \
             Fix: return the final counter plus one pre-value per invocation without truncation or trailing bytes."
        ),
        run,
    })
    }

    pub(super) fn invalid_invocation_count_finding(op: AtomicOp, n: u32) -> Option<AtomicFinding> {
        (n == 0).then(|| AtomicFinding {
        op: op.clone(),
        invocations: n,
        message: format!(
            "atomic {op:?} requested zero invocations. Fix: run atomic conformance with at least one invocation so the final state is checked."
        ),
        run: 0,
    })
    }

    pub(super) fn atomic_output_size(n: u32) -> Result<usize, String> {
        let words = n.checked_add(1).ok_or_else(|| {
        format!(
            "atomic output word count overflowed for {n} invocations. Fix: reject invocation counts whose final counter plus per-invocation pre-values cannot be represented."
        )
    })?;
        (words as usize).checked_mul(4).ok_or_else(|| {
        format!(
            "atomic output byte count overflowed for {n} invocations. Fix: bound atomic output buffers before dispatch."
        )
    })
    }
}

/// Registry entry for `atomics_race` enforcement.
pub struct AtomicsRaceEnforcer;

impl crate::enforce::EnforceGate for AtomicsRaceEnforcer {
    fn id(&self) -> &'static str {
        "atomics_race"
    }

    fn name(&self) -> &'static str {
        "atomics_race"
    }

    fn run(&self, ctx: &crate::enforce::EnforceCtx<'_>) -> Vec<crate::enforce::Finding> {
        let Some(backend) = ctx.backend else {
            return vec![crate::enforce::aggregate_finding(
                self.id(),
                vec![
                    "atomics_race: backend is required. Fix: provide a VyreBackend in EnforceCtx."
                        .to_string(),
                ],
            )];
        };
        let report = enforce_atomics(backend);
        let messages = report
            .findings
            .into_iter()
            .map(|finding| finding.message)
            .collect::<Vec<_>>();
        crate::enforce::finding_result(self.id(), messages)
    }
}

/// Auto-registered `atomics_race` enforcer.
pub const REGISTERED: AtomicsRaceEnforcer = AtomicsRaceEnforcer;