Skip to main content

def_expr_op_over_loads

Macro def_expr_op_over_loads 

Source
macro_rules! def_expr_op_over_loads {
    (crate::$($ty:ident)::+ $(,)?) => { ... };
    (self::$($ty:ident)::+ $(,)?) => { ... };
    (super::$($ty:ident)::+ $(,)?) => { ... };
    (::$($ty:ident)::+ $(,)?) => { ... };
    ($($ty:ident)::+ $(,)?) => { ... };
    (@impl $ty:ty) => { ... };
    (@binary $ty:ty; $($trait:ident::$method:ident),+ $(,)?) => { ... };
    (@literal $ty:ty, $trait:ident::$method:ident; $($literal:ty),+ $(,)?) => { ... };
    (@unary $ty:ty; $($trait:ident::$method:ident),+ $(,)?) => { ... };
}
Expand description

Add the primitive-expression operators to an existing reference wrapper.

The type, a module-scope path to a reference wrapper implementing Clone and Into<PrimExpr>, gets the operators +, -, *, &, |, ^, <<, >>, unary - and ! and PrimOps, all returning an ExprResult. The operators take PrimExpr operands, so the type must convert into one: its upcast to PrimExpr declares that every value of it is typed by a PrimType.

ยงExamples

use tvm_tirx::ir::{Expr, ExprObj, PrimExpr, PrimType, UNKNOWN_LOC};
use tvm_tirx::sym::Analyzer;
use tvm_tirx::tvm_ffi::{self, rs_object, ObjectRefCore};

#[rs_object(type_key = "my_ext.ThreadIndex", final, immutable)]
pub struct ThreadIndexObj {
    base_: ExprObj,
    pub axis: i64,
}
#[derive(Clone, tvm_ffi::derive::ObjectRef)]
#[object_ref(parent = Expr)]
pub struct ThreadIndex {
    data_: tvm_ffi::ObjectArc<ThreadIndexObj>,
}

impl ThreadIndex {
    /// The `int32` index of the thread along `axis`.
    pub fn new(axis: i64) -> Self {
        let base = ExprObj::new(UNKNOWN_LOC.into(), PrimType::int(32).into());
        Self::from_object(ThreadIndexObj::new(base, axis))
    }
}

// Every thread index is an `int32`, as `new` makes it.
tvm_ffi::impl_object_ref_upcast!(unsafe ThreadIndex => PrimExpr);
tvm_tirx::def_expr_op_over_loads!(ThreadIndex);

let tx = ThreadIndex::new(0);
let analyzer = Analyzer::new();
assert!(analyzer.can_prove_equal(&tx * 4 + &tx, &tx * 5)?);