vyre 0.4.0

GPU compute intermediate representation with a standard operation library
Documentation
//! Core type definitions for the vyre IR.
//!
//! These public types are defined by `vyre-spec` so that backend
//! conformance can be proved without depending on `vyre`. The extension
//! trait `DataTypeSizeBytes` adds conservative size queries needed by
//! lowering and validation code.
//!
//! # Examples
//!
//! ```
//! use vyre::ir::{DataType, BufferAccess, BinOp};
//!
//! // Element type for a U32 buffer
//! let elem = DataType::U32;
//!
//! // Read-write access for an output buffer
//! let access = BufferAccess::ReadWrite;
//!
//! // The arithmetic operator used inside an Expr::BinOp
//! let op = BinOp::Add;
//! ```

/// Re-export of frozen IR types from `vyre-spec`.
///
/// These types are the vocabulary of every vyre program: data types,
/// buffer access modes, binary and unary operators, function calling
/// conventions, and operation signatures. Because they live in the spec
/// crate, frontends and backends can depend on them without pulling in
/// the full compiler.
pub use vyre_spec::{AtomicOp, BinOp, BufferAccess, Convention, DataType, OpSignature, UnOp};

/// Conservative byte-size query for IR data types.
///
/// This extension trait mirrors [`DataType::min_bytes`] under a name used by
/// lowering and validation code that needs a fixed per-value size. `Bytes`
/// returns `0` because it represents variable-length byte buffers rather than a
/// scalar element.
///
/// # Examples
///
/// ```
/// use vyre::ir::{DataType, DataTypeSizeBytes};
///
/// assert_eq!(DataType::U32.size_bytes(), 4);
/// assert_eq!(DataType::Bytes.size_bytes(), 0);
/// ```
pub trait DataTypeSizeBytes {
    /// Return the conservative byte count for one value of this type.
    ///
    /// Scalar types return their natural width (`U32` → 4, `Vec4U32` → 16).
    /// `Bytes` returns `0` because it denotes a variable-length buffer, not a
    /// fixed-size element.
    fn size_bytes(self) -> usize;
}

impl DataTypeSizeBytes for DataType {
    fn size_bytes(self) -> usize {
        match self {
            Self::Bool | Self::U32 | Self::I32 | Self::F32 => 4,
            Self::F16 | Self::BF16 => 2,
            Self::F64 => 8,
            Self::U64 | Self::Vec2U32 => 8,
            Self::Vec4U32 => 16,
            Self::Bytes | Self::Array { .. } | Self::Tensor => 0,
        }
    }
}