tvm_tirx/macro/def_expr_op_overloads.rs
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19//! Expression operators for existing reference wrappers.
20
21/// Add the primitive-expression operators to an existing reference wrapper.
22///
23/// The type, a module-scope path to a reference wrapper implementing `Clone` and
24/// `Into<PrimExpr>`, gets the operators `+`, `-`, `*`, `&`, `|`, `^`, `<<`, `>>`, unary `-` and `!`
25/// and [`PrimOps`](crate::ir::prim::PrimOps), all returning an
26/// [`ExprResult`](crate::ir::prim::ExprResult). The operators take
27/// [`PrimExpr`](crate::ir::PrimExpr) operands, so the type must convert into one: its upcast to
28/// `PrimExpr` declares that every value of it is typed by a `PrimType`.
29///
30/// # Examples
31///
32/// ```standalone_crate
33/// use tvm_tirx::ir::{Expr, ExprObj, PrimExpr, PrimType, UNKNOWN_LOC};
34/// use tvm_tirx::sym::Analyzer;
35/// use tvm_tirx::tvm_ffi::{self, rs_object, ObjectRefCore};
36///
37/// #[rs_object(type_key = "my_ext.ThreadIndex", final, immutable)]
38/// pub struct ThreadIndexObj {
39/// base_: ExprObj,
40/// pub axis: i64,
41/// }
42/// #[derive(Clone, tvm_ffi::derive::ObjectRef)]
43/// #[object_ref(parent = Expr)]
44/// pub struct ThreadIndex {
45/// data_: tvm_ffi::ObjectArc<ThreadIndexObj>,
46/// }
47///
48/// impl ThreadIndex {
49/// /// The `int32` index of the thread along `axis`.
50/// pub fn new(axis: i64) -> Self {
51/// let base = ExprObj::new(UNKNOWN_LOC.into(), PrimType::int(32).into());
52/// Self::from_object(ThreadIndexObj::new(base, axis))
53/// }
54/// }
55///
56/// // Every thread index is an `int32`, as `new` makes it.
57/// tvm_ffi::impl_object_ref_upcast!(unsafe ThreadIndex => PrimExpr);
58/// tvm_tirx::def_expr_op_over_loads!(ThreadIndex);
59///
60/// # fn main() -> tvm_tirx::tvm_ffi::Result<()> {
61/// let tx = ThreadIndex::new(0);
62/// let analyzer = Analyzer::new();
63/// assert!(analyzer.can_prove_equal(&tx * 4 + &tx, &tx * 5)?);
64/// # Ok(())
65/// # }
66/// ```
67// `crate::` matches the caller's path and re-emits it, so it names the caller's crate.
68#[allow(clippy::crate_in_macro_def)]
69#[macro_export]
70macro_rules! def_expr_op_over_loads {
71 (crate::$($ty:ident)::+ $(,)?) => {
72 $crate::def_expr_op_over_loads!(@impl crate::$($ty)::+);
73 };
74 (self::$($ty:ident)::+ $(,)?) => {
75 $crate::def_expr_op_over_loads!(@impl self::$($ty)::+);
76 };
77 (super::$($ty:ident)::+ $(,)?) => {
78 $crate::def_expr_op_over_loads!(@impl super::$($ty)::+);
79 };
80 (::$($ty:ident)::+ $(,)?) => {
81 $crate::def_expr_op_over_loads!(@impl ::$($ty)::+);
82 };
83 ($($ty:ident)::+ $(,)?) => {
84 // Anchor caller names before introducing generic operator parameters.
85 $crate::def_expr_op_over_loads!(@impl self::$($ty)::+);
86 };
87 (@impl $ty:ty) => {
88 impl ::core::convert::From<$ty> for $crate::ir::prim::ExprResult {
89 fn from(value: $ty) -> Self {
90 let value = ::core::convert::Into::<$crate::ir::PrimExpr>::into(value);
91 Self::from(::core::result::Result::<_, $crate::tvm_ffi::Error>::Ok(value))
92 }
93 }
94
95 impl ::core::convert::From<&$ty> for $crate::ir::prim::ExprResult {
96 fn from(value: &$ty) -> Self {
97 Self::from(<$ty as ::core::clone::Clone>::clone(value))
98 }
99 }
100
101 impl $crate::ir::prim::PrimOps for $ty {
102 fn to_expr_result(&self) -> $crate::ir::prim::ExprResult {
103 ::core::convert::From::from(self)
104 }
105 }
106
107 $crate::def_expr_op_over_loads!(@binary $ty;
108 Add::add, Sub::sub, Mul::mul, BitAnd::bitand, BitOr::bitor,
109 BitXor::bitxor, Shl::shl, Shr::shr
110 );
111 $crate::def_expr_op_over_loads!(@unary $ty; Neg::neg, Not::not);
112 };
113 (@binary $ty:ty; $($trait:ident::$method:ident),+ $(,)?) => {
114 $(
115 impl<R: $crate::ir::prim::Operand> ::core::ops::$trait<R> for $ty {
116 type Output = $crate::ir::prim::ExprResult;
117 fn $method(self, rhs: R) -> Self::Output {
118 <Self::Output as ::core::ops::$trait<R>>::$method(
119 ::core::convert::From::from(self), rhs,
120 )
121 }
122 }
123
124 impl<R: $crate::ir::prim::Operand> ::core::ops::$trait<R> for &$ty {
125 type Output = $crate::ir::prim::ExprResult;
126 fn $method(self, rhs: R) -> Self::Output {
127 <Self::Output as ::core::ops::$trait<R>>::$method(
128 ::core::convert::From::from(self), rhs,
129 )
130 }
131 }
132
133 $crate::def_expr_op_over_loads!(@literal $ty, $trait::$method;
134 ::core::primitive::i32, ::core::primitive::i64,
135 ::core::primitive::f32, ::core::primitive::f64);
136 )+
137 };
138 (@literal $ty:ty, $trait:ident::$method:ident; $($literal:ty),+ $(,)?) => {
139 $(
140 impl ::core::ops::$trait<$ty> for $literal {
141 type Output = $crate::ir::prim::ExprResult;
142 fn $method(self, rhs: $ty) -> Self::Output {
143 <Self as ::core::ops::$trait<Self::Output>>::$method(
144 self, ::core::convert::From::from(rhs),
145 )
146 }
147 }
148
149 impl ::core::ops::$trait<&$ty> for $literal {
150 type Output = $crate::ir::prim::ExprResult;
151 fn $method(self, rhs: &$ty) -> Self::Output {
152 <Self as ::core::ops::$trait<Self::Output>>::$method(
153 self, ::core::convert::From::from(rhs),
154 )
155 }
156 }
157 )+
158 };
159 (@unary $ty:ty; $($trait:ident::$method:ident),+ $(,)?) => {
160 $(
161 impl ::core::ops::$trait for $ty {
162 type Output = $crate::ir::prim::ExprResult;
163 fn $method(self) -> Self::Output {
164 <Self::Output as ::core::ops::$trait>::$method(
165 ::core::convert::From::from(self),
166 )
167 }
168 }
169
170 impl ::core::ops::$trait for &$ty {
171 type Output = $crate::ir::prim::ExprResult;
172 fn $method(self) -> Self::Output {
173 <Self::Output as ::core::ops::$trait>::$method(
174 ::core::convert::From::from(self),
175 )
176 }
177 }
178 )+
179 };
180}