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};

// Sign function over i32 values.



pub const INPUTS: &[DataType] = &[DataType::I32];

pub const OUTPUTS: &[DataType] = &[DataType::I32];

pub const LAWS: &[AlgebraicLaw] = &[AlgebraicLaw::Idempotent];

/// Signed i32 sign operation returning -1, 0, or 1.
#[derive(Debug, Clone, Copy, Default)]
pub struct Sign;

impl Sign {
    /// Declarative operation specification.
    pub const SPEC: OpSpec =
        OpSpec::composition_inlinable("primitive.math.sign", INPUTS, OUTPUTS, LAWS, Self::program);

    /// Build the canonical IR program.
    #[must_use]
    pub fn program() -> Program {
        let idx = Expr::var("idx");
        Program::new(
            vec![
                BufferDecl::read("a", 0, DataType::I32),
                BufferDecl::output("out", 1, DataType::I32),
            ],
            [64, 1, 1],
            vec![
                Node::let_bind("idx", Expr::gid_x()),
                Node::if_then(
                    Expr::lt(idx.clone(), Expr::buf_len("out")),
                    vec![Node::store(
                        "out",
                        idx.clone(),
                        sign_expr(Expr::load("a", idx)),
                    )],
                ),
            ],
        )
    }
}

pub fn sign_expr(value: Expr) -> Expr {
    Expr::select(
        Expr::lt(value.clone(), Expr::i32(0)),
        Expr::i32(-1),
        Expr::select(Expr::gt(value, Expr::i32(0)), Expr::i32(1), Expr::i32(0)),
    )
}

// Backend-specific lowering tripwires for signed sign.

// WGSL lowering marker for `primitive.math.sign`.
//
// Not a stub: this is a zero-overhead Category A marker. `Sign::program`
// builds concrete IR through `core/src/ops/primitive/math/sign/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(select(0i, 1i, (x > 0i)), -1i, (x < 0i)));
// ```