vyre 0.4.0

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

// Rotate bits right by amount & 31.



pub const LAWS: &[AlgebraicLaw] = &[
    AlgebraicLaw::Bounded {
        lo: 0,
        hi: u32::MAX,
    },
    AlgebraicLaw::InverseOf {
        op: "primitive.bitwise.rotl",
    },
];

/// Rotate right operation.
#[derive(Debug, Clone, Copy, Default)]
pub struct Rotr;

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

    /// Build the canonical IR program.
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Expr;
    /// use vyre::ops::primitive::rotr::Rotr;
    ///
    /// let _expr = Expr::bitor(Expr::shr(Expr::u32(8), Expr::u32(3)), Expr::shl(Expr::u32(8), Expr::u32(29)));
    /// let program = Rotr::program();
    /// assert!(!program.entry().is_empty());
    /// ```
    #[must_use]
    pub fn program() -> Program {
        primitive::binary_u32_program(|a, b| {
            let r = Expr::bitand(b, Expr::u32(31));
            Expr::select(
                Expr::eq(r.clone(), Expr::u32(0)),
                a.clone(),
                Expr::bitor(
                    Expr::shr(a.clone(), r.clone()),
                    Expr::shl(a, Expr::sub(Expr::u32(32), r)),
                ),
            )
        })
    }
}

// Backend-specific lowering tripwires for rotr.

// WGSL lowering marker for `primitive.bitwise.rotr`.
//
// Not a stub: this is a zero-overhead Category A marker. `Rotr::program`
// builds concrete IR through `core/src/ops/primitive/bitwise/rotr/kernel.rs`; `core/src/lower/wgsl/expr.rs` emits WGSL.
// `core/tests/conformance.rs::conformance_all_primitives` verifies
// lowered GPU bytes are bit-exact against the conform CPU reference.
//
// ```wgsl
// _vyre_store_out(idx, select(((x >> r) | (x << (32u - r))), x, (r == 0u)));
// ```