vyre 0.4.0

GPU compute intermediate representation with a standard operation library
Documentation
use crate::ir::{BufferDecl, DataType, Expr, Node, Program};
use crate::ops::{AlgebraicLaw, OpSpec, U32_U32_INPUTS, U32_OUTPUTS};

// Greatest common divisor of two u32 values via the Euclidean algorithm.
//
// The loop is bounded to 64 iterations — sufficient for all 32-bit inputs
// (worst-case Euclidean steps on u32 is 48 for consecutive Fibonacci numbers).


const LAWS: &[AlgebraicLaw] = &[
    AlgebraicLaw::Commutative,
    AlgebraicLaw::Associative,
    AlgebraicLaw::Identity { element: 0 },
    AlgebraicLaw::Idempotent,
];

/// Greatest common divisor operation.
#[derive(Debug, Clone, Copy, Default)]
pub struct Gcd;

impl Gcd {
    /// Declarative operation specification.
    ///
    /// Laws are declared as explicit `AlgebraicLaw` values on `SPEC`.
    pub const SPEC: OpSpec = OpSpec::composition_inlinable(
        "primitive.math.gcd",
        U32_U32_INPUTS,
        U32_OUTPUTS,
        LAWS,
        Self::program,
    );

    /// Build the canonical IR program.
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ops::primitive::gcd::Gcd;
    ///
    /// let program = Gcd::program();
    /// assert!(!program.entry().is_empty());
    /// ```
    #[must_use]
    pub fn program() -> Program {
        let idx = Expr::var("idx");
        Program::new(
            vec![
                BufferDecl::read("a", 0, DataType::U32),
                BufferDecl::read("b", 1, DataType::U32),
                BufferDecl::output("out", 2, DataType::U32),
            ],
            crate::ops::primitive::WORKGROUP_SIZE,
            vec![
                Node::let_bind("idx", Expr::gid_x()),
                Node::if_then(
                    Expr::lt(idx.clone(), Expr::buf_len("out")),
                    vec![
                        Node::let_bind("x", Expr::load("a", idx.clone())),
                        Node::let_bind("y", Expr::load("b", idx.clone())),
                        Node::loop_for(
                            "i",
                            Expr::u32(0),
                            Expr::u32(64),
                            vec![Node::if_then(
                                Expr::ne(Expr::var("y"), Expr::u32(0)),
                                vec![
                                    Node::let_bind("t", Expr::var("y")),
                                    Node::assign(
                                        "y",
                                        Expr::rem(Expr::var("x"), Expr::var("y")),
                                    ),
                                    Node::assign("x", Expr::var("t")),
                                ],
                            )],
                        ),
                        Node::store("out", idx, Expr::var("x")),
                    ],
                ),
            ],
        )
    }
}

// Greatest common divisor operation module.

// WGSL lowering marker for `primitive.math.gcd`.
//
// Not a stub: this is a zero-overhead Category A marker. `Gcd::program`
// builds concrete IR through a custom composition in `kernel.rs`;
// `core/src/lower/wgsl/expr.rs` and `core/src/lower/wgsl/node.rs` emit WGSL.
// `core/tests/conformance.rs::conformance_all_primitives` verifies
// lowered GPU bytes are bit-exact against the conform CPU reference.
//
// ```wgsl
// var x = _vyre_load_a(idx);
// var y = _vyre_load_b(idx);
// for (var i = 0u; i < 64u; i = i + 1u) {
//     if (y != 0u) {
//         let t = y;
//         y = _vyre_safe_mod_u32(x, y);
//         x = t;
//     }
// }
// _vyre_store_out(idx, x);
// ```