vyre-conform 0.1.0

Conformance suite for vyre backends — proves byte-identical output to CPU reference
Documentation
//! Harness self-test — canary specs that verify the conformance harness
//! itself can detect failures.
//!
//! If ANY canary passes, the harness is broken. This is the meta-test:
//! - We test GPU == CPU (conformance)
//! - We test CPU == truth (golden samples)
//! - We test harness-catches-wrong-GPU (canaries)
//!
//! Without canaries, a bug in compare() that always returns Ok(()) would
//! make every test pass and nobody would notice.

use vyre::VyreBackend;

use crate::pipeline::execution::execute_op;
use crate::spec::primitive;
use crate::spec::types::OpSpec;

/// An intentionally-wrong WGSL that returns the BITWISE NOT of the correct answer.
/// The CPU function computes xor(a, b), but the WGSL computes ~(a ^ b).
/// If the harness can't tell these apart, it's fundamentally broken.
fn wrong_wgsl() -> String {
    r"
fn vyre_op(index: u32, input_len: u32) -> u32 {
    return ~(input.data[0u] ^ input.data[1u]);
}
"
    .to_string()
}

/// An intentionally-wrong CPU reference that returns AND instead of XOR.
/// The WGSL computes xor correctly. If the harness can't detect the
/// CPU/GPU mismatch, it's broken.
fn wrong_cpu(input: &[u8]) -> Vec<u8> {
    if input.len() < 8 {
        return vec![0; 4];
    }
    let left = u32::from_le_bytes([input[0], input[1], input[2], input[3]]);
    let right = u32::from_le_bytes([input[4], input[5], input[6], input[7]]);
    // Intentionally wrong: AND instead of XOR.
    (left & right).to_le_bytes().to_vec()
}

fn correct_xor_wgsl() -> String {
    r"
fn vyre_op(index: u32, input_len: u32) -> u32 {
    return input.data[0u] ^ input.data[1u];
}
"
    .to_string()
}

/// Run all canary checks. Returns errors describing which canaries
/// incorrectly passed (indicating a harness bug).
///
/// A healthy harness returns an empty Vec here.
#[allow(clippy::similar_names)]
#[inline]
pub(crate) fn verify_harness_integrity(backend: &dyn VyreBackend) -> Vec<String> {
    let mut harness_bugs = Vec::new();

    // Canary 1: wrong WGSL, correct CPU → must fail for inputs where a^b != ~(a^b).
    let wrong_gpu_spec = {
        let id = "canary.wrong_gpu";
        OpSpec::builder(id)
            .signature(primitive::binary_u32_sig())
            .cpu_fn(crate::spec::primitive::xor::cpu_fn())
            .wgsl_fn(wrong_wgsl)
            .category(crate::Category::A {
                composition_of: vec![id],
            })
            .laws(vec![crate::spec::law::AlgebraicLaw::Bounded {
                lo: 0,
                hi: u32::MAX,
            }])
            .strictness(crate::spec::types::Strictness::Strict)
            .version(1)
            .alt_wgsl_fns(vec![("category_a_handwritten", wrong_wgsl)])
            .build()
            .expect("registry invariant violated")
    };

    // Use non-zero inputs where xor != ~xor.
    let input = [0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00]; // 1 ^ 2 = 3, ~3 = 0xFFFFFFFC
    if let Ok((gpu, cpu)) = execute_op(backend, &wrong_gpu_spec, &input, 1) {
        if gpu == cpu {
            harness_bugs.push(
                "HARNESS BUG: canary.wrong_gpu passed — GPU returned ~(a^b) but harness \
                 reported it as matching a^b. The compare() function is broken."
                    .to_string(),
            );
        }
        // Mismatch detected → harness is working correctly for this canary.
    } else {
        // Dispatch error is also acceptable — it means the GPU couldn't run the wrong shader.
    }

    // Canary 2: correct WGSL, wrong CPU → must fail.
    let wrong_cpu_spec = {
        let id = "canary.wrong_cpu";
        OpSpec::builder(id)
            .signature(primitive::binary_u32_sig())
            .cpu_fn(wrong_cpu)
            .wgsl_fn(correct_xor_wgsl)
            .category(crate::Category::A {
                composition_of: vec![id],
            })
            .laws(vec![crate::spec::law::AlgebraicLaw::Bounded {
                lo: 0,
                hi: u32::MAX,
            }])
            .strictness(crate::spec::types::Strictness::Strict)
            .version(1)
            .alt_wgsl_fns(vec![("category_a_handwritten", correct_xor_wgsl)])
            .build()
            .expect("registry invariant violated")
    };

    if let Ok((gpu, cpu)) = execute_op(backend, &wrong_cpu_spec, &input, 1) {
        if gpu == cpu {
            harness_bugs.push(
                "HARNESS BUG: canary.wrong_cpu passed — CPU returned AND but GPU returned XOR, \
                 yet harness reported match. The compare() function is broken."
                    .to_string(),
            );
        }
    }

    harness_bugs
}