vyre 0.4.0

GPU compute intermediate representation with a standard operation library
Documentation
use crate::ir::model::expr::{Expr, Ident};
use crate::ir::model::types::AtomicOp;

impl Expr {
    /// Atomic add and return previous value.
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Expr;
    /// let _ = Expr::atomic_add("out", Expr::u32(0), Expr::u32(1));
    /// ```
    #[must_use]
    #[inline]
    pub fn atomic_add(buffer: &str, index: Self, value: Self) -> Self {
        Self::Atomic {
            op: AtomicOp::Add,
            buffer: Ident::from(buffer),
            index: Box::new(index),
            expected: None,
            value: Box::new(value),
        }
    }

    /// Atomic OR and return previous value.
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Expr;
    /// let _ = Expr::atomic_or("out", Expr::u32(0), Expr::u32(1));
    /// ```
    #[must_use]
    #[inline]
    pub fn atomic_or(buffer: &str, index: Self, value: Self) -> Self {
        Self::Atomic {
            op: AtomicOp::Or,
            buffer: Ident::from(buffer),
            index: Box::new(index),
            expected: None,
            value: Box::new(value),
        }
    }

    /// Atomic AND and return previous value.
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Expr;
    ///
    /// assert!(matches!(Expr::atomic_and("out", Expr::u32(0), Expr::u32(1)), Expr::Atomic { .. }));
    /// ```
    #[must_use]
    #[inline]
    pub fn atomic_and(buffer: &str, index: Self, value: Self) -> Self {
        Self::Atomic {
            op: AtomicOp::And,
            buffer: Ident::from(buffer),
            index: Box::new(index),
            expected: None,
            value: Box::new(value),
        }
    }

    /// Atomic XOR and return previous value.
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Expr;
    ///
    /// assert!(matches!(Expr::atomic_xor("out", Expr::u32(0), Expr::u32(1)), Expr::Atomic { .. }));
    /// ```
    #[must_use]
    #[inline]
    pub fn atomic_xor(buffer: &str, index: Self, value: Self) -> Self {
        Self::Atomic {
            op: AtomicOp::Xor,
            buffer: Ident::from(buffer),
            index: Box::new(index),
            expected: None,
            value: Box::new(value),
        }
    }

    /// Atomic minimum and return previous value.
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Expr;
    ///
    /// assert!(matches!(Expr::atomic_min("out", Expr::u32(0), Expr::u32(1)), Expr::Atomic { .. }));
    /// ```
    #[must_use]
    #[inline]
    pub fn atomic_min(buffer: &str, index: Self, value: Self) -> Self {
        Self::Atomic {
            op: AtomicOp::Min,
            buffer: Ident::from(buffer),
            index: Box::new(index),
            expected: None,
            value: Box::new(value),
        }
    }

    /// Atomic maximum and return previous value.
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Expr;
    ///
    /// assert!(matches!(Expr::atomic_max("out", Expr::u32(0), Expr::u32(1)), Expr::Atomic { .. }));
    /// ```
    #[must_use]
    #[inline]
    pub fn atomic_max(buffer: &str, index: Self, value: Self) -> Self {
        Self::Atomic {
            op: AtomicOp::Max,
            buffer: Ident::from(buffer),
            index: Box::new(index),
            expected: None,
            value: Box::new(value),
        }
    }

    /// Atomic exchange and return previous value.
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Expr;
    ///
    /// assert!(matches!(Expr::atomic_exchange("out", Expr::u32(0), Expr::u32(1)), Expr::Atomic { .. }));
    /// ```
    #[must_use]
    #[inline]
    pub fn atomic_exchange(buffer: &str, index: Self, value: Self) -> Self {
        Self::Atomic {
            op: AtomicOp::Exchange,
            buffer: Ident::from(buffer),
            index: Box::new(index),
            expected: None,
            value: Box::new(value),
        }
    }

    /// Atomic compare-exchange and return previous value.
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Expr;
    /// let _ = Expr::atomic_compare_exchange("out", Expr::u32(0), Expr::u32(1), Expr::u32(2));
    /// ```
    #[must_use]
    #[inline]
    pub fn atomic_compare_exchange(
        buffer: &str,
        index: Self,
        expected: Self,
        new_value: Self,
    ) -> Self {
        Self::Atomic {
            op: AtomicOp::CompareExchange,
            buffer: Ident::from(buffer),
            index: Box::new(index),
            expected: Some(Box::new(expected)),
            value: Box::new(new_value),
        }
    }
}