Skip to main content

tvm_tirx/ir/prim/
expr.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//! Primitive operations. Each node, such as [`Add`], [`LT`] or [`Select`], applies one operation
20//! to [`PrimExpr`] operands, lane by lane for vectors; its `new` checks the operand types and
21//! derives the node's type, without the promotion and folding of [`op`](super::op).
22
23// tvm-ffi-stubgen(file): mod(missing_docs = allow)
24// `custom_new` reserves `new` for a hand-written constructor (operand type checks and the derived
25// result type); the generated `from_fields` allocates the fields as given.
26// tvm-ffi-stubgen(object): prim.Cast(custom_new, upcast = PrimExpr)
27// tvm-ffi-stubgen(object): prim.LShift(custom_new, upcast = PrimExpr)
28// tvm-ffi-stubgen(object): prim.RShift(custom_new, upcast = PrimExpr)
29// tvm-ffi-stubgen(object): prim.BitwiseAnd(custom_new, upcast = PrimExpr)
30// tvm-ffi-stubgen(object): prim.BitwiseOr(custom_new, upcast = PrimExpr)
31// tvm-ffi-stubgen(object): prim.BitwiseXor(custom_new, upcast = PrimExpr)
32// tvm-ffi-stubgen(object): prim.BitwiseNot(custom_new, upcast = PrimExpr)
33// tvm-ffi-stubgen(object): prim.Add(custom_new, upcast = PrimExpr)
34// tvm-ffi-stubgen(object): prim.Sub(custom_new, upcast = PrimExpr)
35// tvm-ffi-stubgen(object): prim.Mul(custom_new, upcast = PrimExpr)
36// tvm-ffi-stubgen(object): prim.Div(custom_new, upcast = PrimExpr)
37// tvm-ffi-stubgen(object): prim.Mod(custom_new, upcast = PrimExpr)
38// tvm-ffi-stubgen(object): prim.FloorDiv(custom_new, upcast = PrimExpr)
39// tvm-ffi-stubgen(object): prim.FloorMod(custom_new, upcast = PrimExpr)
40// tvm-ffi-stubgen(object): prim.Min(custom_new, upcast = PrimExpr)
41// tvm-ffi-stubgen(object): prim.Max(custom_new, upcast = PrimExpr)
42// tvm-ffi-stubgen(object): prim.EQ(custom_new, upcast = PrimExpr)
43// tvm-ffi-stubgen(object): prim.NE(custom_new, upcast = PrimExpr)
44// tvm-ffi-stubgen(object): prim.LT(custom_new, upcast = PrimExpr)
45// tvm-ffi-stubgen(object): prim.LE(custom_new, upcast = PrimExpr)
46// tvm-ffi-stubgen(object): prim.GT(custom_new, upcast = PrimExpr)
47// tvm-ffi-stubgen(object): prim.GE(custom_new, upcast = PrimExpr)
48// tvm-ffi-stubgen(object): prim.And(custom_new, upcast = PrimExpr)
49// tvm-ffi-stubgen(object): prim.Or(custom_new, upcast = PrimExpr)
50// tvm-ffi-stubgen(object): prim.Not(custom_new, upcast = PrimExpr)
51// tvm-ffi-stubgen(object): prim.Select(custom_new, upcast = PrimExpr)
52// tvm-ffi-stubgen(object): prim.Let(custom_new, upcast = PrimExpr)
53// Every node is typed by a `PrimType` and holds `PrimExpr` operands, as C++ `Add` derives from
54// `PrimExpr` and `AddObj` holds `PrimExpr a, b`: its allocator takes a `PrimType`, and it converts
55// into a `PrimExpr` without a check.
56// tvm-ffi-stubgen(field): prim.Cast.ty(type = PrimType)
57// tvm-ffi-stubgen(field): prim.Cast.value(type = PrimExpr)
58// tvm-ffi-stubgen(field): prim.LShift.ty(type = PrimType)
59// tvm-ffi-stubgen(field): prim.LShift.a(type = PrimExpr)
60// tvm-ffi-stubgen(field): prim.LShift.b(type = PrimExpr)
61// tvm-ffi-stubgen(field): prim.RShift.ty(type = PrimType)
62// tvm-ffi-stubgen(field): prim.RShift.a(type = PrimExpr)
63// tvm-ffi-stubgen(field): prim.RShift.b(type = PrimExpr)
64// tvm-ffi-stubgen(field): prim.BitwiseAnd.ty(type = PrimType)
65// tvm-ffi-stubgen(field): prim.BitwiseAnd.a(type = PrimExpr)
66// tvm-ffi-stubgen(field): prim.BitwiseAnd.b(type = PrimExpr)
67// tvm-ffi-stubgen(field): prim.BitwiseOr.ty(type = PrimType)
68// tvm-ffi-stubgen(field): prim.BitwiseOr.a(type = PrimExpr)
69// tvm-ffi-stubgen(field): prim.BitwiseOr.b(type = PrimExpr)
70// tvm-ffi-stubgen(field): prim.BitwiseXor.ty(type = PrimType)
71// tvm-ffi-stubgen(field): prim.BitwiseXor.a(type = PrimExpr)
72// tvm-ffi-stubgen(field): prim.BitwiseXor.b(type = PrimExpr)
73// tvm-ffi-stubgen(field): prim.BitwiseNot.ty(type = PrimType)
74// tvm-ffi-stubgen(field): prim.BitwiseNot.a(type = PrimExpr)
75// tvm-ffi-stubgen(field): prim.Add.ty(type = PrimType)
76// tvm-ffi-stubgen(field): prim.Add.a(type = PrimExpr)
77// tvm-ffi-stubgen(field): prim.Add.b(type = PrimExpr)
78// tvm-ffi-stubgen(field): prim.Sub.ty(type = PrimType)
79// tvm-ffi-stubgen(field): prim.Sub.a(type = PrimExpr)
80// tvm-ffi-stubgen(field): prim.Sub.b(type = PrimExpr)
81// tvm-ffi-stubgen(field): prim.Mul.ty(type = PrimType)
82// tvm-ffi-stubgen(field): prim.Mul.a(type = PrimExpr)
83// tvm-ffi-stubgen(field): prim.Mul.b(type = PrimExpr)
84// tvm-ffi-stubgen(field): prim.Div.ty(type = PrimType)
85// tvm-ffi-stubgen(field): prim.Div.a(type = PrimExpr)
86// tvm-ffi-stubgen(field): prim.Div.b(type = PrimExpr)
87// tvm-ffi-stubgen(field): prim.Mod.ty(type = PrimType)
88// tvm-ffi-stubgen(field): prim.Mod.a(type = PrimExpr)
89// tvm-ffi-stubgen(field): prim.Mod.b(type = PrimExpr)
90// tvm-ffi-stubgen(field): prim.FloorDiv.ty(type = PrimType)
91// tvm-ffi-stubgen(field): prim.FloorDiv.a(type = PrimExpr)
92// tvm-ffi-stubgen(field): prim.FloorDiv.b(type = PrimExpr)
93// tvm-ffi-stubgen(field): prim.FloorMod.ty(type = PrimType)
94// tvm-ffi-stubgen(field): prim.FloorMod.a(type = PrimExpr)
95// tvm-ffi-stubgen(field): prim.FloorMod.b(type = PrimExpr)
96// tvm-ffi-stubgen(field): prim.Min.ty(type = PrimType)
97// tvm-ffi-stubgen(field): prim.Min.a(type = PrimExpr)
98// tvm-ffi-stubgen(field): prim.Min.b(type = PrimExpr)
99// tvm-ffi-stubgen(field): prim.Max.ty(type = PrimType)
100// tvm-ffi-stubgen(field): prim.Max.a(type = PrimExpr)
101// tvm-ffi-stubgen(field): prim.Max.b(type = PrimExpr)
102// tvm-ffi-stubgen(field): prim.EQ.ty(type = PrimType)
103// tvm-ffi-stubgen(field): prim.EQ.a(type = PrimExpr)
104// tvm-ffi-stubgen(field): prim.EQ.b(type = PrimExpr)
105// tvm-ffi-stubgen(field): prim.NE.ty(type = PrimType)
106// tvm-ffi-stubgen(field): prim.NE.a(type = PrimExpr)
107// tvm-ffi-stubgen(field): prim.NE.b(type = PrimExpr)
108// tvm-ffi-stubgen(field): prim.LT.ty(type = PrimType)
109// tvm-ffi-stubgen(field): prim.LT.a(type = PrimExpr)
110// tvm-ffi-stubgen(field): prim.LT.b(type = PrimExpr)
111// tvm-ffi-stubgen(field): prim.LE.ty(type = PrimType)
112// tvm-ffi-stubgen(field): prim.LE.a(type = PrimExpr)
113// tvm-ffi-stubgen(field): prim.LE.b(type = PrimExpr)
114// tvm-ffi-stubgen(field): prim.GT.ty(type = PrimType)
115// tvm-ffi-stubgen(field): prim.GT.a(type = PrimExpr)
116// tvm-ffi-stubgen(field): prim.GT.b(type = PrimExpr)
117// tvm-ffi-stubgen(field): prim.GE.ty(type = PrimType)
118// tvm-ffi-stubgen(field): prim.GE.a(type = PrimExpr)
119// tvm-ffi-stubgen(field): prim.GE.b(type = PrimExpr)
120// tvm-ffi-stubgen(field): prim.And.ty(type = PrimType)
121// tvm-ffi-stubgen(field): prim.And.a(type = PrimExpr)
122// tvm-ffi-stubgen(field): prim.And.b(type = PrimExpr)
123// tvm-ffi-stubgen(field): prim.Or.ty(type = PrimType)
124// tvm-ffi-stubgen(field): prim.Or.a(type = PrimExpr)
125// tvm-ffi-stubgen(field): prim.Or.b(type = PrimExpr)
126// tvm-ffi-stubgen(field): prim.Not.ty(type = PrimType)
127// tvm-ffi-stubgen(field): prim.Not.a(type = PrimExpr)
128// tvm-ffi-stubgen(field): prim.Select.ty(type = PrimType)
129// tvm-ffi-stubgen(field): prim.Select.condition(type = PrimExpr)
130// tvm-ffi-stubgen(field): prim.Select.true_value(type = PrimExpr)
131// tvm-ffi-stubgen(field): prim.Select.false_value(type = PrimExpr)
132// tvm-ffi-stubgen(field): prim.Let.ty(type = PrimType)
133// tvm-ffi-stubgen(field): prim.Let.value(type = PrimExpr)
134// tvm-ffi-stubgen(field): prim.Let.body(type = PrimExpr)
135// Names the hand-written wrappers use: the import section keeps them even when no generated block
136// does.
137// tvm-ffi-stubgen(import): tvm_ffi::AnyView
138// tvm-ffi-stubgen(import): tvm_ffi::DLDataTypeCode
139// tvm-ffi-stubgen(import): tvm_ffi::ObjectRefCore
140// tvm-ffi-stubgen(import): tvm_ffi::Result
141// tvm-ffi-stubgen(import): crate::ir::r#type::PrimType
142// tvm-ffi-stubgen(import): crate::ir::expr::PrimExpr
143// tvm-ffi-stubgen(import): crate::ir::r#type::PrimTypeObj
144// tvm-ffi-stubgen(import): crate::ir::support
145// tvm-ffi-stubgen(import): crate::ir::expr::IntoExpr
146// tvm-ffi-stubgen(import): tvm_ffi::IntoArg
147
148// tvm-ffi-stubgen(begin): import-section
149use crate::ir::expr::Expr;
150use crate::ir::expr::ExprObj;
151use crate::ir::expr::IntoExpr;
152use crate::ir::expr::PrimExpr;
153use crate::ir::expr::Var;
154use crate::ir::location::Location;
155use crate::ir::r#type::PrimType;
156use crate::ir::r#type::PrimTypeObj;
157use crate::ir::support;
158use std::ops::Deref;
159use tvm_ffi::AnyView;
160use tvm_ffi::DLDataTypeCode;
161use tvm_ffi::IntoArg;
162use tvm_ffi::ObjectArc;
163use tvm_ffi::ObjectRefCore;
164use tvm_ffi::Result;
165// tvm-ffi-stubgen(end)
166
167/// `impl $node { pub fn new(a, b, loc) }` for each binary node, which checks the operand types.
168macro_rules! binary_new {
169    ($($node:ident: $operands:expr, $doc:literal;)+) => {
170        $(
171            impl $node {
172                #[doc = $doc]
173                ///
174                /// # Errors
175                ///
176                /// Returns a `TypeError` if the operand types differ, or do not suit the node, such
177                /// as a float for a bitwise node.
178                pub fn new(
179                    a: impl IntoExpr<PrimExpr>,
180                    b: impl IntoExpr<PrimExpr>,
181                    loc: impl IntoArg<Location>,
182                ) -> Result<Self> {
183                    let (a, b) = (a.into_owned_expr()?, b.into_owned_expr()?);
184                    let ty = binary_type(stringify!($node), $operands, &a, &b)?;
185                    Ok(Self::from_fields(loc.into_owned(), ty, a, b))
186                }
187            }
188        )+
189    };
190}
191
192// ---------------------------------------------------------------------------------------------
193// Casts
194
195/// The conversion of `value` to the node's type, lane by lane: both have as many lanes.
196// tvm-ffi-stubgen(begin): object/prim.Cast
197#[allow(missing_docs)]
198#[repr(C)]
199#[derive(tvm_ffi::derive::ObjectRef, Clone)]
200pub struct Cast {
201    data_: ObjectArc<CastObj>,
202}
203
204/// The object behind [`Cast`].
205// Complete: reflected fields fill [40, 48) exactly.
206#[allow(missing_docs)]
207#[repr(C)]
208#[derive(tvm_ffi::derive::Object)]
209#[object(type_key = "prim.Cast", final, immutable)]
210pub struct CastObj {
211    base_: ExprObj,
212    pub value: PrimExpr,
213}
214
215const _: () = {
216    assert!(::core::mem::size_of::<CastObj>() == 48);
217    assert!(::core::mem::align_of::<CastObj>() == 8);
218};
219
220impl Deref for CastObj {
221    type Target = ExprObj;
222    fn deref(&self) -> &ExprObj {
223        &self.base_
224    }
225}
226
227#[allow(missing_docs)]
228impl CastObj {
229    pub(crate) fn new(loc: Location, ty: PrimType, value: PrimExpr) -> Self {
230        let base_ = ExprObj::new(loc, ty.into());
231        Self { base_, value }
232    }
233}
234
235#[allow(missing_docs)]
236impl Cast {
237    /// Create a `Cast` from its field values.
238    pub fn from_fields(
239        loc: impl IntoArg<Location>,
240        ty: impl IntoArg<PrimType>,
241        value: impl IntoArg<PrimExpr>,
242    ) -> Self {
243        let obj = CastObj::new(loc.into_owned(), ty.into_owned(), value.into_owned());
244        Self { data_: ObjectArc::new(obj) }
245    }
246}
247
248tvm_ffi::impl_object_ref_upcast!(Cast => Expr);
249tvm_ffi::impl_object_ref_upcast!(unsafe Cast => PrimExpr);
250// tvm-ffi-stubgen(end)
251
252impl Cast {
253    /// Create the node converting `value` to `ty`, which has as many lanes as `value`.
254    ///
255    /// # Errors
256    ///
257    /// Returns an `InternalError` if `ty` and the type of `value` have different numbers of lanes.
258    pub fn new(
259        ty: impl IntoArg<PrimType>,
260        value: impl IntoExpr<PrimExpr>,
261        loc: impl IntoArg<Location>,
262    ) -> Result<Self> {
263        let (ty, value) = (ty.into_owned(), value.into_owned_expr()?);
264        let value_dtype = value.prim_ty().dtype;
265        if ty.dtype.lanes != value_dtype.lanes {
266            let condition = "ty->dtype.lanes == value_expr_ty->dtype.lanes";
267            let (lhs, rhs) = (ty.dtype.lanes, value_dtype.lanes);
268            return Err(support::compare_failed(tvm_ffi::INTERNAL_ERROR, condition, lhs, rhs, ""));
269        }
270        Ok(Self::from_fields(loc.into_owned(), ty, value))
271    }
272}
273
274// ---------------------------------------------------------------------------------------------
275// Bitwise operations
276
277/// The left shift `a << b` of integer operands of one type.
278// tvm-ffi-stubgen(begin): object/prim.LShift
279#[allow(missing_docs)]
280#[repr(C)]
281#[derive(tvm_ffi::derive::ObjectRef, Clone)]
282pub struct LShift {
283    data_: ObjectArc<LShiftObj>,
284}
285
286/// The object behind [`LShift`].
287// Complete: reflected fields fill [40, 56) exactly.
288#[allow(missing_docs)]
289#[repr(C)]
290#[derive(tvm_ffi::derive::Object)]
291#[object(type_key = "prim.LShift", final, immutable)]
292pub struct LShiftObj {
293    base_: ExprObj,
294    pub a: PrimExpr,
295    pub b: PrimExpr,
296}
297
298const _: () = {
299    assert!(::core::mem::size_of::<LShiftObj>() == 56);
300    assert!(::core::mem::align_of::<LShiftObj>() == 8);
301};
302
303impl Deref for LShiftObj {
304    type Target = ExprObj;
305    fn deref(&self) -> &ExprObj {
306        &self.base_
307    }
308}
309
310#[allow(missing_docs)]
311impl LShiftObj {
312    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
313        let base_ = ExprObj::new(loc, ty.into());
314        Self { base_, a, b }
315    }
316}
317
318#[allow(missing_docs)]
319impl LShift {
320    /// Create a `LShift` from its field values.
321    pub fn from_fields(
322        loc: impl IntoArg<Location>,
323        ty: impl IntoArg<PrimType>,
324        a: impl IntoArg<PrimExpr>,
325        b: impl IntoArg<PrimExpr>,
326    ) -> Self {
327        let obj = LShiftObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
328        Self { data_: ObjectArc::new(obj) }
329    }
330}
331
332tvm_ffi::impl_object_ref_upcast!(LShift => Expr);
333tvm_ffi::impl_object_ref_upcast!(unsafe LShift => PrimExpr);
334// tvm-ffi-stubgen(end)
335
336binary_new! {
337    LShift: Operands::Bitwise { allow_bool: false },
338        "Create the node `a << b` of integer operands of one type.";
339}
340
341/// The right shift `a >> b` of integer operands of one type.
342// tvm-ffi-stubgen(begin): object/prim.RShift
343#[allow(missing_docs)]
344#[repr(C)]
345#[derive(tvm_ffi::derive::ObjectRef, Clone)]
346pub struct RShift {
347    data_: ObjectArc<RShiftObj>,
348}
349
350/// The object behind [`RShift`].
351// Complete: reflected fields fill [40, 56) exactly.
352#[allow(missing_docs)]
353#[repr(C)]
354#[derive(tvm_ffi::derive::Object)]
355#[object(type_key = "prim.RShift", final, immutable)]
356pub struct RShiftObj {
357    base_: ExprObj,
358    pub a: PrimExpr,
359    pub b: PrimExpr,
360}
361
362const _: () = {
363    assert!(::core::mem::size_of::<RShiftObj>() == 56);
364    assert!(::core::mem::align_of::<RShiftObj>() == 8);
365};
366
367impl Deref for RShiftObj {
368    type Target = ExprObj;
369    fn deref(&self) -> &ExprObj {
370        &self.base_
371    }
372}
373
374#[allow(missing_docs)]
375impl RShiftObj {
376    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
377        let base_ = ExprObj::new(loc, ty.into());
378        Self { base_, a, b }
379    }
380}
381
382#[allow(missing_docs)]
383impl RShift {
384    /// Create a `RShift` from its field values.
385    pub fn from_fields(
386        loc: impl IntoArg<Location>,
387        ty: impl IntoArg<PrimType>,
388        a: impl IntoArg<PrimExpr>,
389        b: impl IntoArg<PrimExpr>,
390    ) -> Self {
391        let obj = RShiftObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
392        Self { data_: ObjectArc::new(obj) }
393    }
394}
395
396tvm_ffi::impl_object_ref_upcast!(RShift => Expr);
397tvm_ffi::impl_object_ref_upcast!(unsafe RShift => PrimExpr);
398// tvm-ffi-stubgen(end)
399
400binary_new! {
401    RShift: Operands::Bitwise { allow_bool: false },
402        "Create the node `a >> b` of integer operands of one type.";
403}
404
405/// The bitwise and `a & b` of integer or boolean operands of one type.
406// tvm-ffi-stubgen(begin): object/prim.BitwiseAnd
407#[allow(missing_docs)]
408#[repr(C)]
409#[derive(tvm_ffi::derive::ObjectRef, Clone)]
410pub struct BitwiseAnd {
411    data_: ObjectArc<BitwiseAndObj>,
412}
413
414/// The object behind [`BitwiseAnd`].
415// Complete: reflected fields fill [40, 56) exactly.
416#[allow(missing_docs)]
417#[repr(C)]
418#[derive(tvm_ffi::derive::Object)]
419#[object(type_key = "prim.BitwiseAnd", final, immutable)]
420pub struct BitwiseAndObj {
421    base_: ExprObj,
422    pub a: PrimExpr,
423    pub b: PrimExpr,
424}
425
426const _: () = {
427    assert!(::core::mem::size_of::<BitwiseAndObj>() == 56);
428    assert!(::core::mem::align_of::<BitwiseAndObj>() == 8);
429};
430
431impl Deref for BitwiseAndObj {
432    type Target = ExprObj;
433    fn deref(&self) -> &ExprObj {
434        &self.base_
435    }
436}
437
438#[allow(missing_docs)]
439impl BitwiseAndObj {
440    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
441        let base_ = ExprObj::new(loc, ty.into());
442        Self { base_, a, b }
443    }
444}
445
446#[allow(missing_docs)]
447impl BitwiseAnd {
448    /// Create a `BitwiseAnd` from its field values.
449    pub fn from_fields(
450        loc: impl IntoArg<Location>,
451        ty: impl IntoArg<PrimType>,
452        a: impl IntoArg<PrimExpr>,
453        b: impl IntoArg<PrimExpr>,
454    ) -> Self {
455        let obj =
456            BitwiseAndObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
457        Self { data_: ObjectArc::new(obj) }
458    }
459}
460
461tvm_ffi::impl_object_ref_upcast!(BitwiseAnd => Expr);
462tvm_ffi::impl_object_ref_upcast!(unsafe BitwiseAnd => PrimExpr);
463// tvm-ffi-stubgen(end)
464
465binary_new! {
466    BitwiseAnd: Operands::Bitwise { allow_bool: true },
467        "Create the node `a & b` of integer or boolean operands of one type.";
468}
469
470/// The bitwise or `a | b` of integer or boolean operands of one type.
471// tvm-ffi-stubgen(begin): object/prim.BitwiseOr
472#[allow(missing_docs)]
473#[repr(C)]
474#[derive(tvm_ffi::derive::ObjectRef, Clone)]
475pub struct BitwiseOr {
476    data_: ObjectArc<BitwiseOrObj>,
477}
478
479/// The object behind [`BitwiseOr`].
480// Complete: reflected fields fill [40, 56) exactly.
481#[allow(missing_docs)]
482#[repr(C)]
483#[derive(tvm_ffi::derive::Object)]
484#[object(type_key = "prim.BitwiseOr", final, immutable)]
485pub struct BitwiseOrObj {
486    base_: ExprObj,
487    pub a: PrimExpr,
488    pub b: PrimExpr,
489}
490
491const _: () = {
492    assert!(::core::mem::size_of::<BitwiseOrObj>() == 56);
493    assert!(::core::mem::align_of::<BitwiseOrObj>() == 8);
494};
495
496impl Deref for BitwiseOrObj {
497    type Target = ExprObj;
498    fn deref(&self) -> &ExprObj {
499        &self.base_
500    }
501}
502
503#[allow(missing_docs)]
504impl BitwiseOrObj {
505    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
506        let base_ = ExprObj::new(loc, ty.into());
507        Self { base_, a, b }
508    }
509}
510
511#[allow(missing_docs)]
512impl BitwiseOr {
513    /// Create a `BitwiseOr` from its field values.
514    pub fn from_fields(
515        loc: impl IntoArg<Location>,
516        ty: impl IntoArg<PrimType>,
517        a: impl IntoArg<PrimExpr>,
518        b: impl IntoArg<PrimExpr>,
519    ) -> Self {
520        let obj =
521            BitwiseOrObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
522        Self { data_: ObjectArc::new(obj) }
523    }
524}
525
526tvm_ffi::impl_object_ref_upcast!(BitwiseOr => Expr);
527tvm_ffi::impl_object_ref_upcast!(unsafe BitwiseOr => PrimExpr);
528// tvm-ffi-stubgen(end)
529
530binary_new! {
531    BitwiseOr: Operands::Bitwise { allow_bool: true },
532        "Create the node `a | b` of integer or boolean operands of one type.";
533}
534
535/// The bitwise exclusive or `a ^ b` of integer or boolean operands of one type.
536// tvm-ffi-stubgen(begin): object/prim.BitwiseXor
537#[allow(missing_docs)]
538#[repr(C)]
539#[derive(tvm_ffi::derive::ObjectRef, Clone)]
540pub struct BitwiseXor {
541    data_: ObjectArc<BitwiseXorObj>,
542}
543
544/// The object behind [`BitwiseXor`].
545// Complete: reflected fields fill [40, 56) exactly.
546#[allow(missing_docs)]
547#[repr(C)]
548#[derive(tvm_ffi::derive::Object)]
549#[object(type_key = "prim.BitwiseXor", final, immutable)]
550pub struct BitwiseXorObj {
551    base_: ExprObj,
552    pub a: PrimExpr,
553    pub b: PrimExpr,
554}
555
556const _: () = {
557    assert!(::core::mem::size_of::<BitwiseXorObj>() == 56);
558    assert!(::core::mem::align_of::<BitwiseXorObj>() == 8);
559};
560
561impl Deref for BitwiseXorObj {
562    type Target = ExprObj;
563    fn deref(&self) -> &ExprObj {
564        &self.base_
565    }
566}
567
568#[allow(missing_docs)]
569impl BitwiseXorObj {
570    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
571        let base_ = ExprObj::new(loc, ty.into());
572        Self { base_, a, b }
573    }
574}
575
576#[allow(missing_docs)]
577impl BitwiseXor {
578    /// Create a `BitwiseXor` from its field values.
579    pub fn from_fields(
580        loc: impl IntoArg<Location>,
581        ty: impl IntoArg<PrimType>,
582        a: impl IntoArg<PrimExpr>,
583        b: impl IntoArg<PrimExpr>,
584    ) -> Self {
585        let obj =
586            BitwiseXorObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
587        Self { data_: ObjectArc::new(obj) }
588    }
589}
590
591tvm_ffi::impl_object_ref_upcast!(BitwiseXor => Expr);
592tvm_ffi::impl_object_ref_upcast!(unsafe BitwiseXor => PrimExpr);
593// tvm-ffi-stubgen(end)
594
595binary_new! {
596    BitwiseXor: Operands::Bitwise { allow_bool: true },
597        "Create the node `a ^ b` of integer or boolean operands of one type.";
598}
599
600/// The bitwise complement `~a` of an integer or boolean operand.
601// tvm-ffi-stubgen(begin): object/prim.BitwiseNot
602#[allow(missing_docs)]
603#[repr(C)]
604#[derive(tvm_ffi::derive::ObjectRef, Clone)]
605pub struct BitwiseNot {
606    data_: ObjectArc<BitwiseNotObj>,
607}
608
609/// The object behind [`BitwiseNot`].
610// Complete: reflected fields fill [40, 48) exactly.
611#[allow(missing_docs)]
612#[repr(C)]
613#[derive(tvm_ffi::derive::Object)]
614#[object(type_key = "prim.BitwiseNot", final, immutable)]
615pub struct BitwiseNotObj {
616    base_: ExprObj,
617    pub a: PrimExpr,
618}
619
620const _: () = {
621    assert!(::core::mem::size_of::<BitwiseNotObj>() == 48);
622    assert!(::core::mem::align_of::<BitwiseNotObj>() == 8);
623};
624
625impl Deref for BitwiseNotObj {
626    type Target = ExprObj;
627    fn deref(&self) -> &ExprObj {
628        &self.base_
629    }
630}
631
632#[allow(missing_docs)]
633impl BitwiseNotObj {
634    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr) -> Self {
635        let base_ = ExprObj::new(loc, ty.into());
636        Self { base_, a }
637    }
638}
639
640#[allow(missing_docs)]
641impl BitwiseNot {
642    /// Create a `BitwiseNot` from its field values.
643    pub fn from_fields(
644        loc: impl IntoArg<Location>,
645        ty: impl IntoArg<PrimType>,
646        a: impl IntoArg<PrimExpr>,
647    ) -> Self {
648        let obj = BitwiseNotObj::new(loc.into_owned(), ty.into_owned(), a.into_owned());
649        Self { data_: ObjectArc::new(obj) }
650    }
651}
652
653tvm_ffi::impl_object_ref_upcast!(BitwiseNot => Expr);
654tvm_ffi::impl_object_ref_upcast!(unsafe BitwiseNot => PrimExpr);
655// tvm-ffi-stubgen(end)
656
657impl BitwiseNot {
658    /// Create the node `~a` over an integer or boolean operand.
659    ///
660    /// # Errors
661    ///
662    /// Returns a `TypeError` if `a` is not of an integer or boolean type.
663    pub fn new(a: impl IntoExpr<PrimExpr>, loc: impl IntoArg<Location>) -> Result<Self> {
664        let a = a.into_owned_expr()?;
665        let integer_or_bool =
666            [DLDataTypeCode::kDLInt, DLDataTypeCode::kDLUInt, DLDataTypeCode::kDLBool];
667        if !a.prim_ty().matches_code(&integer_or_bool) {
668            let condition = "a_ty.MatchesCode(DLDataTypeCode::kDLInt, DLDataTypeCode::kDLUInt) || \
669                             a_ty.MatchesCode(DLDataTypeCode::kDLBool)";
670            let message = "BitwiseNot requires an integer or boolean operand";
671            return Err(support::check_failed(tvm_ffi::TYPE_ERROR, condition, message));
672        }
673        Ok(Self::from_fields(loc.into_owned(), a.prim_ty().clone(), a))
674    }
675}
676
677// ---------------------------------------------------------------------------------------------
678// Arithmetic
679
680/// The sum `a + b` of operands of one type, elementwise for vectors.
681///
682/// Like every node, [`Add::new`] checks the operand types and neither promotes them nor folds
683/// constants; [`op::add`](super::op::add) and the `+` operator do both.
684///
685/// # Examples
686///
687/// ```
688/// use tvm_tirx::ir::prim::Add;
689/// use tvm_tirx::ir::{PrimType, PrimVar, UNKNOWN_LOC};
690/// use tvm_tirx::script::script;
691///
692/// let x = PrimVar::new("x", PrimType::int(32), UNKNOWN_LOC);
693/// assert_eq!(script(&Add::new(&x, 1, UNKNOWN_LOC)?)?, "x + 1");
694///
695/// let f = PrimVar::new("f", PrimType::float(32), UNKNOWN_LOC);
696/// assert!(Add::new(&x, &f, UNKNOWN_LOC).is_err());
697/// # Ok::<(), tvm_tirx::tvm_ffi::Error>(())
698/// ```
699// tvm-ffi-stubgen(begin): object/prim.Add
700#[allow(missing_docs)]
701#[repr(C)]
702#[derive(tvm_ffi::derive::ObjectRef, Clone)]
703pub struct Add {
704    data_: ObjectArc<AddObj>,
705}
706
707/// The object behind [`Add`].
708// Complete: reflected fields fill [40, 56) exactly.
709#[allow(missing_docs)]
710#[repr(C)]
711#[derive(tvm_ffi::derive::Object)]
712#[object(type_key = "prim.Add", final, immutable)]
713pub struct AddObj {
714    base_: ExprObj,
715    pub a: PrimExpr,
716    pub b: PrimExpr,
717}
718
719const _: () = {
720    assert!(::core::mem::size_of::<AddObj>() == 56);
721    assert!(::core::mem::align_of::<AddObj>() == 8);
722};
723
724impl Deref for AddObj {
725    type Target = ExprObj;
726    fn deref(&self) -> &ExprObj {
727        &self.base_
728    }
729}
730
731#[allow(missing_docs)]
732impl AddObj {
733    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
734        let base_ = ExprObj::new(loc, ty.into());
735        Self { base_, a, b }
736    }
737}
738
739#[allow(missing_docs)]
740impl Add {
741    /// Create a `Add` from its field values.
742    pub fn from_fields(
743        loc: impl IntoArg<Location>,
744        ty: impl IntoArg<PrimType>,
745        a: impl IntoArg<PrimExpr>,
746        b: impl IntoArg<PrimExpr>,
747    ) -> Self {
748        let obj = AddObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
749        Self { data_: ObjectArc::new(obj) }
750    }
751}
752
753tvm_ffi::impl_object_ref_upcast!(Add => Expr);
754tvm_ffi::impl_object_ref_upcast!(unsafe Add => PrimExpr);
755// tvm-ffi-stubgen(end)
756
757binary_new! {
758    Add: Operands::Arith,
759        "Create the node `a + b` of operands of one type.";
760}
761
762/// The difference `a - b` of operands of one type, elementwise for vectors.
763// tvm-ffi-stubgen(begin): object/prim.Sub
764#[allow(missing_docs)]
765#[repr(C)]
766#[derive(tvm_ffi::derive::ObjectRef, Clone)]
767pub struct Sub {
768    data_: ObjectArc<SubObj>,
769}
770
771/// The object behind [`Sub`].
772// Complete: reflected fields fill [40, 56) exactly.
773#[allow(missing_docs)]
774#[repr(C)]
775#[derive(tvm_ffi::derive::Object)]
776#[object(type_key = "prim.Sub", final, immutable)]
777pub struct SubObj {
778    base_: ExprObj,
779    pub a: PrimExpr,
780    pub b: PrimExpr,
781}
782
783const _: () = {
784    assert!(::core::mem::size_of::<SubObj>() == 56);
785    assert!(::core::mem::align_of::<SubObj>() == 8);
786};
787
788impl Deref for SubObj {
789    type Target = ExprObj;
790    fn deref(&self) -> &ExprObj {
791        &self.base_
792    }
793}
794
795#[allow(missing_docs)]
796impl SubObj {
797    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
798        let base_ = ExprObj::new(loc, ty.into());
799        Self { base_, a, b }
800    }
801}
802
803#[allow(missing_docs)]
804impl Sub {
805    /// Create a `Sub` from its field values.
806    pub fn from_fields(
807        loc: impl IntoArg<Location>,
808        ty: impl IntoArg<PrimType>,
809        a: impl IntoArg<PrimExpr>,
810        b: impl IntoArg<PrimExpr>,
811    ) -> Self {
812        let obj = SubObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
813        Self { data_: ObjectArc::new(obj) }
814    }
815}
816
817tvm_ffi::impl_object_ref_upcast!(Sub => Expr);
818tvm_ffi::impl_object_ref_upcast!(unsafe Sub => PrimExpr);
819// tvm-ffi-stubgen(end)
820
821binary_new! {
822    Sub: Operands::Arith,
823        "Create the node `a - b` of operands of one type.";
824}
825
826/// The product `a * b` of operands of one type, elementwise for vectors.
827// tvm-ffi-stubgen(begin): object/prim.Mul
828#[allow(missing_docs)]
829#[repr(C)]
830#[derive(tvm_ffi::derive::ObjectRef, Clone)]
831pub struct Mul {
832    data_: ObjectArc<MulObj>,
833}
834
835/// The object behind [`Mul`].
836// Complete: reflected fields fill [40, 56) exactly.
837#[allow(missing_docs)]
838#[repr(C)]
839#[derive(tvm_ffi::derive::Object)]
840#[object(type_key = "prim.Mul", final, immutable)]
841pub struct MulObj {
842    base_: ExprObj,
843    pub a: PrimExpr,
844    pub b: PrimExpr,
845}
846
847const _: () = {
848    assert!(::core::mem::size_of::<MulObj>() == 56);
849    assert!(::core::mem::align_of::<MulObj>() == 8);
850};
851
852impl Deref for MulObj {
853    type Target = ExprObj;
854    fn deref(&self) -> &ExprObj {
855        &self.base_
856    }
857}
858
859#[allow(missing_docs)]
860impl MulObj {
861    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
862        let base_ = ExprObj::new(loc, ty.into());
863        Self { base_, a, b }
864    }
865}
866
867#[allow(missing_docs)]
868impl Mul {
869    /// Create a `Mul` from its field values.
870    pub fn from_fields(
871        loc: impl IntoArg<Location>,
872        ty: impl IntoArg<PrimType>,
873        a: impl IntoArg<PrimExpr>,
874        b: impl IntoArg<PrimExpr>,
875    ) -> Self {
876        let obj = MulObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
877        Self { data_: ObjectArc::new(obj) }
878    }
879}
880
881tvm_ffi::impl_object_ref_upcast!(Mul => Expr);
882tvm_ffi::impl_object_ref_upcast!(unsafe Mul => PrimExpr);
883// tvm-ffi-stubgen(end)
884
885binary_new! {
886    Mul: Operands::Arith,
887        "Create the node `a * b` of operands of one type.";
888}
889
890/// The quotient `a / b` of operands of one type; integer division truncates toward zero.
891// tvm-ffi-stubgen(begin): object/prim.Div
892#[allow(missing_docs)]
893#[repr(C)]
894#[derive(tvm_ffi::derive::ObjectRef, Clone)]
895pub struct Div {
896    data_: ObjectArc<DivObj>,
897}
898
899/// The object behind [`Div`].
900// Complete: reflected fields fill [40, 56) exactly.
901#[allow(missing_docs)]
902#[repr(C)]
903#[derive(tvm_ffi::derive::Object)]
904#[object(type_key = "prim.Div", final, immutable)]
905pub struct DivObj {
906    base_: ExprObj,
907    pub a: PrimExpr,
908    pub b: PrimExpr,
909}
910
911const _: () = {
912    assert!(::core::mem::size_of::<DivObj>() == 56);
913    assert!(::core::mem::align_of::<DivObj>() == 8);
914};
915
916impl Deref for DivObj {
917    type Target = ExprObj;
918    fn deref(&self) -> &ExprObj {
919        &self.base_
920    }
921}
922
923#[allow(missing_docs)]
924impl DivObj {
925    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
926        let base_ = ExprObj::new(loc, ty.into());
927        Self { base_, a, b }
928    }
929}
930
931#[allow(missing_docs)]
932impl Div {
933    /// Create a `Div` from its field values.
934    pub fn from_fields(
935        loc: impl IntoArg<Location>,
936        ty: impl IntoArg<PrimType>,
937        a: impl IntoArg<PrimExpr>,
938        b: impl IntoArg<PrimExpr>,
939    ) -> Self {
940        let obj = DivObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
941        Self { data_: ObjectArc::new(obj) }
942    }
943}
944
945tvm_ffi::impl_object_ref_upcast!(Div => Expr);
946tvm_ffi::impl_object_ref_upcast!(unsafe Div => PrimExpr);
947// tvm-ffi-stubgen(end)
948
949binary_new! {
950    Div: Operands::Arith,
951        "Create the node `a / b` of operands of one type.";
952}
953
954/// The remainder `a % b` of division truncated toward zero, of operands of one type.
955// tvm-ffi-stubgen(begin): object/prim.Mod
956#[allow(missing_docs)]
957#[repr(C)]
958#[derive(tvm_ffi::derive::ObjectRef, Clone)]
959pub struct Mod {
960    data_: ObjectArc<ModObj>,
961}
962
963/// The object behind [`Mod`].
964// Complete: reflected fields fill [40, 56) exactly.
965#[allow(missing_docs)]
966#[repr(C)]
967#[derive(tvm_ffi::derive::Object)]
968#[object(type_key = "prim.Mod", final, immutable)]
969pub struct ModObj {
970    base_: ExprObj,
971    pub a: PrimExpr,
972    pub b: PrimExpr,
973}
974
975const _: () = {
976    assert!(::core::mem::size_of::<ModObj>() == 56);
977    assert!(::core::mem::align_of::<ModObj>() == 8);
978};
979
980impl Deref for ModObj {
981    type Target = ExprObj;
982    fn deref(&self) -> &ExprObj {
983        &self.base_
984    }
985}
986
987#[allow(missing_docs)]
988impl ModObj {
989    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
990        let base_ = ExprObj::new(loc, ty.into());
991        Self { base_, a, b }
992    }
993}
994
995#[allow(missing_docs)]
996impl Mod {
997    /// Create a `Mod` from its field values.
998    pub fn from_fields(
999        loc: impl IntoArg<Location>,
1000        ty: impl IntoArg<PrimType>,
1001        a: impl IntoArg<PrimExpr>,
1002        b: impl IntoArg<PrimExpr>,
1003    ) -> Self {
1004        let obj = ModObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
1005        Self { data_: ObjectArc::new(obj) }
1006    }
1007}
1008
1009tvm_ffi::impl_object_ref_upcast!(Mod => Expr);
1010tvm_ffi::impl_object_ref_upcast!(unsafe Mod => PrimExpr);
1011// tvm-ffi-stubgen(end)
1012
1013binary_new! {
1014    Mod: Operands::Arith,
1015        "Create the truncated node `a % b` of operands of one type.";
1016}
1017
1018/// The quotient `floor(a / b)` of operands of one type, rounded toward negative infinity.
1019// tvm-ffi-stubgen(begin): object/prim.FloorDiv
1020#[allow(missing_docs)]
1021#[repr(C)]
1022#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1023pub struct FloorDiv {
1024    data_: ObjectArc<FloorDivObj>,
1025}
1026
1027/// The object behind [`FloorDiv`].
1028// Complete: reflected fields fill [40, 56) exactly.
1029#[allow(missing_docs)]
1030#[repr(C)]
1031#[derive(tvm_ffi::derive::Object)]
1032#[object(type_key = "prim.FloorDiv", final, immutable)]
1033pub struct FloorDivObj {
1034    base_: ExprObj,
1035    pub a: PrimExpr,
1036    pub b: PrimExpr,
1037}
1038
1039const _: () = {
1040    assert!(::core::mem::size_of::<FloorDivObj>() == 56);
1041    assert!(::core::mem::align_of::<FloorDivObj>() == 8);
1042};
1043
1044impl Deref for FloorDivObj {
1045    type Target = ExprObj;
1046    fn deref(&self) -> &ExprObj {
1047        &self.base_
1048    }
1049}
1050
1051#[allow(missing_docs)]
1052impl FloorDivObj {
1053    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
1054        let base_ = ExprObj::new(loc, ty.into());
1055        Self { base_, a, b }
1056    }
1057}
1058
1059#[allow(missing_docs)]
1060impl FloorDiv {
1061    /// Create a `FloorDiv` from its field values.
1062    pub fn from_fields(
1063        loc: impl IntoArg<Location>,
1064        ty: impl IntoArg<PrimType>,
1065        a: impl IntoArg<PrimExpr>,
1066        b: impl IntoArg<PrimExpr>,
1067    ) -> Self {
1068        let obj =
1069            FloorDivObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
1070        Self { data_: ObjectArc::new(obj) }
1071    }
1072}
1073
1074tvm_ffi::impl_object_ref_upcast!(FloorDiv => Expr);
1075tvm_ffi::impl_object_ref_upcast!(unsafe FloorDiv => PrimExpr);
1076// tvm-ffi-stubgen(end)
1077
1078binary_new! {
1079    FloorDiv: Operands::Arith,
1080        "Create the node `floor(a / b)` of operands of one type.";
1081}
1082
1083/// The remainder `a - floor(a / b) * b` of floor division, of operands of one type.
1084// tvm-ffi-stubgen(begin): object/prim.FloorMod
1085#[allow(missing_docs)]
1086#[repr(C)]
1087#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1088pub struct FloorMod {
1089    data_: ObjectArc<FloorModObj>,
1090}
1091
1092/// The object behind [`FloorMod`].
1093// Complete: reflected fields fill [40, 56) exactly.
1094#[allow(missing_docs)]
1095#[repr(C)]
1096#[derive(tvm_ffi::derive::Object)]
1097#[object(type_key = "prim.FloorMod", final, immutable)]
1098pub struct FloorModObj {
1099    base_: ExprObj,
1100    pub a: PrimExpr,
1101    pub b: PrimExpr,
1102}
1103
1104const _: () = {
1105    assert!(::core::mem::size_of::<FloorModObj>() == 56);
1106    assert!(::core::mem::align_of::<FloorModObj>() == 8);
1107};
1108
1109impl Deref for FloorModObj {
1110    type Target = ExprObj;
1111    fn deref(&self) -> &ExprObj {
1112        &self.base_
1113    }
1114}
1115
1116#[allow(missing_docs)]
1117impl FloorModObj {
1118    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
1119        let base_ = ExprObj::new(loc, ty.into());
1120        Self { base_, a, b }
1121    }
1122}
1123
1124#[allow(missing_docs)]
1125impl FloorMod {
1126    /// Create a `FloorMod` from its field values.
1127    pub fn from_fields(
1128        loc: impl IntoArg<Location>,
1129        ty: impl IntoArg<PrimType>,
1130        a: impl IntoArg<PrimExpr>,
1131        b: impl IntoArg<PrimExpr>,
1132    ) -> Self {
1133        let obj =
1134            FloorModObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
1135        Self { data_: ObjectArc::new(obj) }
1136    }
1137}
1138
1139tvm_ffi::impl_object_ref_upcast!(FloorMod => Expr);
1140tvm_ffi::impl_object_ref_upcast!(unsafe FloorMod => PrimExpr);
1141// tvm-ffi-stubgen(end)
1142
1143binary_new! {
1144    FloorMod: Operands::Arith,
1145        "Create the node `a - floor(a / b) * b` of operands of one type.";
1146}
1147
1148/// The smaller of `a` and `b`, of one type, elementwise for vectors.
1149// tvm-ffi-stubgen(begin): object/prim.Min
1150#[allow(missing_docs)]
1151#[repr(C)]
1152#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1153pub struct Min {
1154    data_: ObjectArc<MinObj>,
1155}
1156
1157/// The object behind [`Min`].
1158// Complete: reflected fields fill [40, 56) exactly.
1159#[allow(missing_docs)]
1160#[repr(C)]
1161#[derive(tvm_ffi::derive::Object)]
1162#[object(type_key = "prim.Min", final, immutable)]
1163pub struct MinObj {
1164    base_: ExprObj,
1165    pub a: PrimExpr,
1166    pub b: PrimExpr,
1167}
1168
1169const _: () = {
1170    assert!(::core::mem::size_of::<MinObj>() == 56);
1171    assert!(::core::mem::align_of::<MinObj>() == 8);
1172};
1173
1174impl Deref for MinObj {
1175    type Target = ExprObj;
1176    fn deref(&self) -> &ExprObj {
1177        &self.base_
1178    }
1179}
1180
1181#[allow(missing_docs)]
1182impl MinObj {
1183    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
1184        let base_ = ExprObj::new(loc, ty.into());
1185        Self { base_, a, b }
1186    }
1187}
1188
1189#[allow(missing_docs)]
1190impl Min {
1191    /// Create a `Min` from its field values.
1192    pub fn from_fields(
1193        loc: impl IntoArg<Location>,
1194        ty: impl IntoArg<PrimType>,
1195        a: impl IntoArg<PrimExpr>,
1196        b: impl IntoArg<PrimExpr>,
1197    ) -> Self {
1198        let obj = MinObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
1199        Self { data_: ObjectArc::new(obj) }
1200    }
1201}
1202
1203tvm_ffi::impl_object_ref_upcast!(Min => Expr);
1204tvm_ffi::impl_object_ref_upcast!(unsafe Min => PrimExpr);
1205// tvm-ffi-stubgen(end)
1206
1207binary_new! {
1208    Min: Operands::Arith,
1209        "Create the node `min(a, b)` of operands of one type.";
1210}
1211
1212/// The larger of `a` and `b`, of one type, elementwise for vectors.
1213// tvm-ffi-stubgen(begin): object/prim.Max
1214#[allow(missing_docs)]
1215#[repr(C)]
1216#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1217pub struct Max {
1218    data_: ObjectArc<MaxObj>,
1219}
1220
1221/// The object behind [`Max`].
1222// Complete: reflected fields fill [40, 56) exactly.
1223#[allow(missing_docs)]
1224#[repr(C)]
1225#[derive(tvm_ffi::derive::Object)]
1226#[object(type_key = "prim.Max", final, immutable)]
1227pub struct MaxObj {
1228    base_: ExprObj,
1229    pub a: PrimExpr,
1230    pub b: PrimExpr,
1231}
1232
1233const _: () = {
1234    assert!(::core::mem::size_of::<MaxObj>() == 56);
1235    assert!(::core::mem::align_of::<MaxObj>() == 8);
1236};
1237
1238impl Deref for MaxObj {
1239    type Target = ExprObj;
1240    fn deref(&self) -> &ExprObj {
1241        &self.base_
1242    }
1243}
1244
1245#[allow(missing_docs)]
1246impl MaxObj {
1247    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
1248        let base_ = ExprObj::new(loc, ty.into());
1249        Self { base_, a, b }
1250    }
1251}
1252
1253#[allow(missing_docs)]
1254impl Max {
1255    /// Create a `Max` from its field values.
1256    pub fn from_fields(
1257        loc: impl IntoArg<Location>,
1258        ty: impl IntoArg<PrimType>,
1259        a: impl IntoArg<PrimExpr>,
1260        b: impl IntoArg<PrimExpr>,
1261    ) -> Self {
1262        let obj = MaxObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
1263        Self { data_: ObjectArc::new(obj) }
1264    }
1265}
1266
1267tvm_ffi::impl_object_ref_upcast!(Max => Expr);
1268tvm_ffi::impl_object_ref_upcast!(unsafe Max => PrimExpr);
1269// tvm-ffi-stubgen(end)
1270
1271binary_new! {
1272    Max: Operands::Arith,
1273        "Create the node `max(a, b)` of operands of one type.";
1274}
1275
1276// ---------------------------------------------------------------------------------------------
1277// Comparisons
1278
1279/// The comparison `a == b` of operands of one type: a boolean with their lanes.
1280// tvm-ffi-stubgen(begin): object/prim.EQ
1281#[allow(missing_docs)]
1282#[repr(C)]
1283#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1284pub struct EQ {
1285    data_: ObjectArc<EQObj>,
1286}
1287
1288/// The object behind [`EQ`].
1289// Complete: reflected fields fill [40, 56) exactly.
1290#[allow(missing_docs)]
1291#[repr(C)]
1292#[derive(tvm_ffi::derive::Object)]
1293#[object(type_key = "prim.EQ", final, immutable)]
1294pub struct EQObj {
1295    base_: ExprObj,
1296    pub a: PrimExpr,
1297    pub b: PrimExpr,
1298}
1299
1300const _: () = {
1301    assert!(::core::mem::size_of::<EQObj>() == 56);
1302    assert!(::core::mem::align_of::<EQObj>() == 8);
1303};
1304
1305impl Deref for EQObj {
1306    type Target = ExprObj;
1307    fn deref(&self) -> &ExprObj {
1308        &self.base_
1309    }
1310}
1311
1312#[allow(missing_docs)]
1313impl EQObj {
1314    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
1315        let base_ = ExprObj::new(loc, ty.into());
1316        Self { base_, a, b }
1317    }
1318}
1319
1320#[allow(missing_docs)]
1321impl EQ {
1322    /// Create a `EQ` from its field values.
1323    pub fn from_fields(
1324        loc: impl IntoArg<Location>,
1325        ty: impl IntoArg<PrimType>,
1326        a: impl IntoArg<PrimExpr>,
1327        b: impl IntoArg<PrimExpr>,
1328    ) -> Self {
1329        let obj = EQObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
1330        Self { data_: ObjectArc::new(obj) }
1331    }
1332}
1333
1334tvm_ffi::impl_object_ref_upcast!(EQ => Expr);
1335tvm_ffi::impl_object_ref_upcast!(unsafe EQ => PrimExpr);
1336// tvm-ffi-stubgen(end)
1337
1338binary_new! {
1339    EQ: Operands::Compare,
1340        "Create the boolean node `a == b` of operands of one type.";
1341}
1342
1343/// The comparison `a != b` of operands of one type: a boolean with their lanes.
1344// tvm-ffi-stubgen(begin): object/prim.NE
1345#[allow(missing_docs)]
1346#[repr(C)]
1347#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1348pub struct NE {
1349    data_: ObjectArc<NEObj>,
1350}
1351
1352/// The object behind [`NE`].
1353// Complete: reflected fields fill [40, 56) exactly.
1354#[allow(missing_docs)]
1355#[repr(C)]
1356#[derive(tvm_ffi::derive::Object)]
1357#[object(type_key = "prim.NE", final, immutable)]
1358pub struct NEObj {
1359    base_: ExprObj,
1360    pub a: PrimExpr,
1361    pub b: PrimExpr,
1362}
1363
1364const _: () = {
1365    assert!(::core::mem::size_of::<NEObj>() == 56);
1366    assert!(::core::mem::align_of::<NEObj>() == 8);
1367};
1368
1369impl Deref for NEObj {
1370    type Target = ExprObj;
1371    fn deref(&self) -> &ExprObj {
1372        &self.base_
1373    }
1374}
1375
1376#[allow(missing_docs)]
1377impl NEObj {
1378    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
1379        let base_ = ExprObj::new(loc, ty.into());
1380        Self { base_, a, b }
1381    }
1382}
1383
1384#[allow(missing_docs)]
1385impl NE {
1386    /// Create a `NE` from its field values.
1387    pub fn from_fields(
1388        loc: impl IntoArg<Location>,
1389        ty: impl IntoArg<PrimType>,
1390        a: impl IntoArg<PrimExpr>,
1391        b: impl IntoArg<PrimExpr>,
1392    ) -> Self {
1393        let obj = NEObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
1394        Self { data_: ObjectArc::new(obj) }
1395    }
1396}
1397
1398tvm_ffi::impl_object_ref_upcast!(NE => Expr);
1399tvm_ffi::impl_object_ref_upcast!(unsafe NE => PrimExpr);
1400// tvm-ffi-stubgen(end)
1401
1402binary_new! {
1403    NE: Operands::Compare,
1404        "Create the boolean node `a != b` of operands of one type.";
1405}
1406
1407/// The comparison `a < b` of operands of one type: a boolean with their lanes.
1408// tvm-ffi-stubgen(begin): object/prim.LT
1409#[allow(missing_docs)]
1410#[repr(C)]
1411#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1412pub struct LT {
1413    data_: ObjectArc<LTObj>,
1414}
1415
1416/// The object behind [`LT`].
1417// Complete: reflected fields fill [40, 56) exactly.
1418#[allow(missing_docs)]
1419#[repr(C)]
1420#[derive(tvm_ffi::derive::Object)]
1421#[object(type_key = "prim.LT", final, immutable)]
1422pub struct LTObj {
1423    base_: ExprObj,
1424    pub a: PrimExpr,
1425    pub b: PrimExpr,
1426}
1427
1428const _: () = {
1429    assert!(::core::mem::size_of::<LTObj>() == 56);
1430    assert!(::core::mem::align_of::<LTObj>() == 8);
1431};
1432
1433impl Deref for LTObj {
1434    type Target = ExprObj;
1435    fn deref(&self) -> &ExprObj {
1436        &self.base_
1437    }
1438}
1439
1440#[allow(missing_docs)]
1441impl LTObj {
1442    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
1443        let base_ = ExprObj::new(loc, ty.into());
1444        Self { base_, a, b }
1445    }
1446}
1447
1448#[allow(missing_docs)]
1449impl LT {
1450    /// Create a `LT` from its field values.
1451    pub fn from_fields(
1452        loc: impl IntoArg<Location>,
1453        ty: impl IntoArg<PrimType>,
1454        a: impl IntoArg<PrimExpr>,
1455        b: impl IntoArg<PrimExpr>,
1456    ) -> Self {
1457        let obj = LTObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
1458        Self { data_: ObjectArc::new(obj) }
1459    }
1460}
1461
1462tvm_ffi::impl_object_ref_upcast!(LT => Expr);
1463tvm_ffi::impl_object_ref_upcast!(unsafe LT => PrimExpr);
1464// tvm-ffi-stubgen(end)
1465
1466binary_new! {
1467    LT: Operands::Compare,
1468        "Create the boolean node `a < b` of operands of one type.";
1469}
1470
1471/// The comparison `a <= b` of operands of one type: a boolean with their lanes.
1472// tvm-ffi-stubgen(begin): object/prim.LE
1473#[allow(missing_docs)]
1474#[repr(C)]
1475#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1476pub struct LE {
1477    data_: ObjectArc<LEObj>,
1478}
1479
1480/// The object behind [`LE`].
1481// Complete: reflected fields fill [40, 56) exactly.
1482#[allow(missing_docs)]
1483#[repr(C)]
1484#[derive(tvm_ffi::derive::Object)]
1485#[object(type_key = "prim.LE", final, immutable)]
1486pub struct LEObj {
1487    base_: ExprObj,
1488    pub a: PrimExpr,
1489    pub b: PrimExpr,
1490}
1491
1492const _: () = {
1493    assert!(::core::mem::size_of::<LEObj>() == 56);
1494    assert!(::core::mem::align_of::<LEObj>() == 8);
1495};
1496
1497impl Deref for LEObj {
1498    type Target = ExprObj;
1499    fn deref(&self) -> &ExprObj {
1500        &self.base_
1501    }
1502}
1503
1504#[allow(missing_docs)]
1505impl LEObj {
1506    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
1507        let base_ = ExprObj::new(loc, ty.into());
1508        Self { base_, a, b }
1509    }
1510}
1511
1512#[allow(missing_docs)]
1513impl LE {
1514    /// Create a `LE` from its field values.
1515    pub fn from_fields(
1516        loc: impl IntoArg<Location>,
1517        ty: impl IntoArg<PrimType>,
1518        a: impl IntoArg<PrimExpr>,
1519        b: impl IntoArg<PrimExpr>,
1520    ) -> Self {
1521        let obj = LEObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
1522        Self { data_: ObjectArc::new(obj) }
1523    }
1524}
1525
1526tvm_ffi::impl_object_ref_upcast!(LE => Expr);
1527tvm_ffi::impl_object_ref_upcast!(unsafe LE => PrimExpr);
1528// tvm-ffi-stubgen(end)
1529
1530binary_new! {
1531    LE: Operands::Compare,
1532        "Create the boolean node `a <= b` of operands of one type.";
1533}
1534
1535/// The comparison `a > b` of operands of one type: a boolean with their lanes.
1536// tvm-ffi-stubgen(begin): object/prim.GT
1537#[allow(missing_docs)]
1538#[repr(C)]
1539#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1540pub struct GT {
1541    data_: ObjectArc<GTObj>,
1542}
1543
1544/// The object behind [`GT`].
1545// Complete: reflected fields fill [40, 56) exactly.
1546#[allow(missing_docs)]
1547#[repr(C)]
1548#[derive(tvm_ffi::derive::Object)]
1549#[object(type_key = "prim.GT", final, immutable)]
1550pub struct GTObj {
1551    base_: ExprObj,
1552    pub a: PrimExpr,
1553    pub b: PrimExpr,
1554}
1555
1556const _: () = {
1557    assert!(::core::mem::size_of::<GTObj>() == 56);
1558    assert!(::core::mem::align_of::<GTObj>() == 8);
1559};
1560
1561impl Deref for GTObj {
1562    type Target = ExprObj;
1563    fn deref(&self) -> &ExprObj {
1564        &self.base_
1565    }
1566}
1567
1568#[allow(missing_docs)]
1569impl GTObj {
1570    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
1571        let base_ = ExprObj::new(loc, ty.into());
1572        Self { base_, a, b }
1573    }
1574}
1575
1576#[allow(missing_docs)]
1577impl GT {
1578    /// Create a `GT` from its field values.
1579    pub fn from_fields(
1580        loc: impl IntoArg<Location>,
1581        ty: impl IntoArg<PrimType>,
1582        a: impl IntoArg<PrimExpr>,
1583        b: impl IntoArg<PrimExpr>,
1584    ) -> Self {
1585        let obj = GTObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
1586        Self { data_: ObjectArc::new(obj) }
1587    }
1588}
1589
1590tvm_ffi::impl_object_ref_upcast!(GT => Expr);
1591tvm_ffi::impl_object_ref_upcast!(unsafe GT => PrimExpr);
1592// tvm-ffi-stubgen(end)
1593
1594binary_new! {
1595    GT: Operands::Compare,
1596        "Create the boolean node `a > b` of operands of one type.";
1597}
1598
1599/// The comparison `a >= b` of operands of one type: a boolean with their lanes.
1600// tvm-ffi-stubgen(begin): object/prim.GE
1601#[allow(missing_docs)]
1602#[repr(C)]
1603#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1604pub struct GE {
1605    data_: ObjectArc<GEObj>,
1606}
1607
1608/// The object behind [`GE`].
1609// Complete: reflected fields fill [40, 56) exactly.
1610#[allow(missing_docs)]
1611#[repr(C)]
1612#[derive(tvm_ffi::derive::Object)]
1613#[object(type_key = "prim.GE", final, immutable)]
1614pub struct GEObj {
1615    base_: ExprObj,
1616    pub a: PrimExpr,
1617    pub b: PrimExpr,
1618}
1619
1620const _: () = {
1621    assert!(::core::mem::size_of::<GEObj>() == 56);
1622    assert!(::core::mem::align_of::<GEObj>() == 8);
1623};
1624
1625impl Deref for GEObj {
1626    type Target = ExprObj;
1627    fn deref(&self) -> &ExprObj {
1628        &self.base_
1629    }
1630}
1631
1632#[allow(missing_docs)]
1633impl GEObj {
1634    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
1635        let base_ = ExprObj::new(loc, ty.into());
1636        Self { base_, a, b }
1637    }
1638}
1639
1640#[allow(missing_docs)]
1641impl GE {
1642    /// Create a `GE` from its field values.
1643    pub fn from_fields(
1644        loc: impl IntoArg<Location>,
1645        ty: impl IntoArg<PrimType>,
1646        a: impl IntoArg<PrimExpr>,
1647        b: impl IntoArg<PrimExpr>,
1648    ) -> Self {
1649        let obj = GEObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
1650        Self { data_: ObjectArc::new(obj) }
1651    }
1652}
1653
1654tvm_ffi::impl_object_ref_upcast!(GE => Expr);
1655tvm_ffi::impl_object_ref_upcast!(unsafe GE => PrimExpr);
1656// tvm-ffi-stubgen(end)
1657
1658binary_new! {
1659    GE: Operands::Compare,
1660        "Create the boolean node `a >= b` of operands of one type.";
1661}
1662
1663// ---------------------------------------------------------------------------------------------
1664// Logical operations
1665
1666/// The conjunction `a && b` of boolean operands of one type.
1667// tvm-ffi-stubgen(begin): object/prim.And
1668#[allow(missing_docs)]
1669#[repr(C)]
1670#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1671pub struct And {
1672    data_: ObjectArc<AndObj>,
1673}
1674
1675/// The object behind [`And`].
1676// Complete: reflected fields fill [40, 56) exactly.
1677#[allow(missing_docs)]
1678#[repr(C)]
1679#[derive(tvm_ffi::derive::Object)]
1680#[object(type_key = "prim.And", final, immutable)]
1681pub struct AndObj {
1682    base_: ExprObj,
1683    pub a: PrimExpr,
1684    pub b: PrimExpr,
1685}
1686
1687const _: () = {
1688    assert!(::core::mem::size_of::<AndObj>() == 56);
1689    assert!(::core::mem::align_of::<AndObj>() == 8);
1690};
1691
1692impl Deref for AndObj {
1693    type Target = ExprObj;
1694    fn deref(&self) -> &ExprObj {
1695        &self.base_
1696    }
1697}
1698
1699#[allow(missing_docs)]
1700impl AndObj {
1701    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
1702        let base_ = ExprObj::new(loc, ty.into());
1703        Self { base_, a, b }
1704    }
1705}
1706
1707#[allow(missing_docs)]
1708impl And {
1709    /// Create a `And` from its field values.
1710    pub fn from_fields(
1711        loc: impl IntoArg<Location>,
1712        ty: impl IntoArg<PrimType>,
1713        a: impl IntoArg<PrimExpr>,
1714        b: impl IntoArg<PrimExpr>,
1715    ) -> Self {
1716        let obj = AndObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
1717        Self { data_: ObjectArc::new(obj) }
1718    }
1719}
1720
1721tvm_ffi::impl_object_ref_upcast!(And => Expr);
1722tvm_ffi::impl_object_ref_upcast!(unsafe And => PrimExpr);
1723// tvm-ffi-stubgen(end)
1724
1725impl And {
1726    /// Create the boolean node `a && b` over boolean operands.
1727    ///
1728    /// # Errors
1729    ///
1730    /// Returns an `InternalError` if an operand is not boolean, or a `TypeError` if the operand
1731    /// types differ.
1732    pub fn new(
1733        a: impl IntoExpr<PrimExpr>,
1734        b: impl IntoExpr<PrimExpr>,
1735        loc: impl IntoArg<Location>,
1736    ) -> Result<Self> {
1737        let (a, b) = (a.into_owned_expr()?, b.into_owned_expr()?);
1738        let ty = logical_type(&a, &b)?;
1739        Ok(Self::from_fields(loc.into_owned(), ty, a, b))
1740    }
1741}
1742
1743/// The disjunction `a || b` of boolean operands of one type.
1744// tvm-ffi-stubgen(begin): object/prim.Or
1745#[allow(missing_docs)]
1746#[repr(C)]
1747#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1748pub struct Or {
1749    data_: ObjectArc<OrObj>,
1750}
1751
1752/// The object behind [`Or`].
1753// Complete: reflected fields fill [40, 56) exactly.
1754#[allow(missing_docs)]
1755#[repr(C)]
1756#[derive(tvm_ffi::derive::Object)]
1757#[object(type_key = "prim.Or", final, immutable)]
1758pub struct OrObj {
1759    base_: ExprObj,
1760    pub a: PrimExpr,
1761    pub b: PrimExpr,
1762}
1763
1764const _: () = {
1765    assert!(::core::mem::size_of::<OrObj>() == 56);
1766    assert!(::core::mem::align_of::<OrObj>() == 8);
1767};
1768
1769impl Deref for OrObj {
1770    type Target = ExprObj;
1771    fn deref(&self) -> &ExprObj {
1772        &self.base_
1773    }
1774}
1775
1776#[allow(missing_docs)]
1777impl OrObj {
1778    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr, b: PrimExpr) -> Self {
1779        let base_ = ExprObj::new(loc, ty.into());
1780        Self { base_, a, b }
1781    }
1782}
1783
1784#[allow(missing_docs)]
1785impl Or {
1786    /// Create a `Or` from its field values.
1787    pub fn from_fields(
1788        loc: impl IntoArg<Location>,
1789        ty: impl IntoArg<PrimType>,
1790        a: impl IntoArg<PrimExpr>,
1791        b: impl IntoArg<PrimExpr>,
1792    ) -> Self {
1793        let obj = OrObj::new(loc.into_owned(), ty.into_owned(), a.into_owned(), b.into_owned());
1794        Self { data_: ObjectArc::new(obj) }
1795    }
1796}
1797
1798tvm_ffi::impl_object_ref_upcast!(Or => Expr);
1799tvm_ffi::impl_object_ref_upcast!(unsafe Or => PrimExpr);
1800// tvm-ffi-stubgen(end)
1801
1802impl Or {
1803    /// Create the boolean node `a || b` over boolean operands.
1804    ///
1805    /// # Errors
1806    ///
1807    /// Returns an `InternalError` if an operand is not boolean, or a `TypeError` if the operand
1808    /// types differ.
1809    pub fn new(
1810        a: impl IntoExpr<PrimExpr>,
1811        b: impl IntoExpr<PrimExpr>,
1812        loc: impl IntoArg<Location>,
1813    ) -> Result<Self> {
1814        let (a, b) = (a.into_owned_expr()?, b.into_owned_expr()?);
1815        let ty = logical_type(&a, &b)?;
1816        Ok(Self::from_fields(loc.into_owned(), ty, a, b))
1817    }
1818}
1819
1820/// The negation `!a` of a boolean operand.
1821// tvm-ffi-stubgen(begin): object/prim.Not
1822#[allow(missing_docs)]
1823#[repr(C)]
1824#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1825pub struct Not {
1826    data_: ObjectArc<NotObj>,
1827}
1828
1829/// The object behind [`Not`].
1830// Complete: reflected fields fill [40, 48) exactly.
1831#[allow(missing_docs)]
1832#[repr(C)]
1833#[derive(tvm_ffi::derive::Object)]
1834#[object(type_key = "prim.Not", final, immutable)]
1835pub struct NotObj {
1836    base_: ExprObj,
1837    pub a: PrimExpr,
1838}
1839
1840const _: () = {
1841    assert!(::core::mem::size_of::<NotObj>() == 48);
1842    assert!(::core::mem::align_of::<NotObj>() == 8);
1843};
1844
1845impl Deref for NotObj {
1846    type Target = ExprObj;
1847    fn deref(&self) -> &ExprObj {
1848        &self.base_
1849    }
1850}
1851
1852#[allow(missing_docs)]
1853impl NotObj {
1854    pub(crate) fn new(loc: Location, ty: PrimType, a: PrimExpr) -> Self {
1855        let base_ = ExprObj::new(loc, ty.into());
1856        Self { base_, a }
1857    }
1858}
1859
1860#[allow(missing_docs)]
1861impl Not {
1862    /// Create a `Not` from its field values.
1863    pub fn from_fields(
1864        loc: impl IntoArg<Location>,
1865        ty: impl IntoArg<PrimType>,
1866        a: impl IntoArg<PrimExpr>,
1867    ) -> Self {
1868        let obj = NotObj::new(loc.into_owned(), ty.into_owned(), a.into_owned());
1869        Self { data_: ObjectArc::new(obj) }
1870    }
1871}
1872
1873tvm_ffi::impl_object_ref_upcast!(Not => Expr);
1874tvm_ffi::impl_object_ref_upcast!(unsafe Not => PrimExpr);
1875// tvm-ffi-stubgen(end)
1876
1877impl Not {
1878    /// Create the boolean node `!a` over a boolean operand.
1879    ///
1880    /// # Errors
1881    ///
1882    /// Returns an `InternalError` if `a` is not boolean.
1883    pub fn new(a: impl IntoExpr<PrimExpr>, loc: impl IntoArg<Location>) -> Result<Self> {
1884        let a = a.into_owned_expr()?;
1885        if !a.prim_ty().matches_code(&[DLDataTypeCode::kDLBool]) {
1886            let condition = "a_ty.MatchesCode(DLDataTypeCode::kDLBool)";
1887            return Err(support::check_failed(tvm_ffi::INTERNAL_ERROR, condition, ""));
1888        }
1889        Ok(Self::from_fields(loc.into_owned(), bool_type(a.prim_ty())?, a))
1890    }
1891}
1892
1893// ---------------------------------------------------------------------------------------------
1894// `Select` and `Let`
1895
1896/// The choice of `true_value` where `condition` holds and `false_value` elsewhere, lane by lane
1897/// for a vector condition.
1898///
1899/// Both values may be evaluated; [`op::if_then_else`](super::op::if_then_else) evaluates only the
1900/// chosen one.
1901// tvm-ffi-stubgen(begin): object/prim.Select
1902#[allow(missing_docs)]
1903#[repr(C)]
1904#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1905pub struct Select {
1906    data_: ObjectArc<SelectObj>,
1907}
1908
1909/// The object behind [`Select`].
1910// Complete: reflected fields fill [40, 64) exactly.
1911#[allow(missing_docs)]
1912#[repr(C)]
1913#[derive(tvm_ffi::derive::Object)]
1914#[object(type_key = "prim.Select", final, immutable)]
1915pub struct SelectObj {
1916    base_: ExprObj,
1917    pub condition: PrimExpr,
1918    pub true_value: PrimExpr,
1919    pub false_value: PrimExpr,
1920}
1921
1922const _: () = {
1923    assert!(::core::mem::size_of::<SelectObj>() == 64);
1924    assert!(::core::mem::align_of::<SelectObj>() == 8);
1925};
1926
1927impl Deref for SelectObj {
1928    type Target = ExprObj;
1929    fn deref(&self) -> &ExprObj {
1930        &self.base_
1931    }
1932}
1933
1934#[allow(missing_docs)]
1935impl SelectObj {
1936    pub(crate) fn new(
1937        loc: Location,
1938        ty: PrimType,
1939        condition: PrimExpr,
1940        true_value: PrimExpr,
1941        false_value: PrimExpr,
1942    ) -> Self {
1943        let base_ = ExprObj::new(loc, ty.into());
1944        Self { base_, condition, true_value, false_value }
1945    }
1946}
1947
1948#[allow(missing_docs)]
1949impl Select {
1950    /// Create a `Select` from its field values.
1951    pub fn from_fields(
1952        loc: impl IntoArg<Location>,
1953        ty: impl IntoArg<PrimType>,
1954        condition: impl IntoArg<PrimExpr>,
1955        true_value: impl IntoArg<PrimExpr>,
1956        false_value: impl IntoArg<PrimExpr>,
1957    ) -> Self {
1958        let obj = SelectObj::new(
1959            loc.into_owned(),
1960            ty.into_owned(),
1961            condition.into_owned(),
1962            true_value.into_owned(),
1963            false_value.into_owned(),
1964        );
1965        Self { data_: ObjectArc::new(obj) }
1966    }
1967}
1968
1969tvm_ffi::impl_object_ref_upcast!(Select => Expr);
1970tvm_ffi::impl_object_ref_upcast!(unsafe Select => PrimExpr);
1971// tvm-ffi-stubgen(end)
1972
1973impl Select {
1974    /// Create the node choosing `true_value` where `condition` holds and `false_value` elsewhere,
1975    /// evaluating both, unlike [`op::if_then_else`](super::op::if_then_else).
1976    ///
1977    /// # Errors
1978    ///
1979    /// Returns an `InternalError` if `condition` is not boolean or has other lanes than the values,
1980    /// or a `TypeError` if the values are of different types.
1981    pub fn new(
1982        condition: impl IntoExpr<PrimExpr>,
1983        true_value: impl IntoExpr<PrimExpr>,
1984        false_value: impl IntoExpr<PrimExpr>,
1985        loc: impl IntoArg<Location>,
1986    ) -> Result<Self> {
1987        let (condition, true_value, false_value) = (
1988            condition.into_owned_expr()?,
1989            true_value.into_owned_expr()?,
1990            false_value.into_owned_expr()?,
1991        );
1992        let (condition_ty, true_ty) = (condition.prim_ty(), true_value.prim_ty());
1993        let (true_dtype, false_dtype) = (true_ty.dtype, false_value.prim_ty().dtype);
1994        if !condition_ty.matches_code(&[DLDataTypeCode::kDLBool]) {
1995            let condition = "condition_ty.MatchesCode(DLDataTypeCode::kDLBool)";
1996            return Err(support::check_failed(tvm_ffi::INTERNAL_ERROR, condition, ""));
1997        }
1998        // C++ `GetLanesOrVScaleFactor(ty)`.
1999        let lanes_or_vscale_factor = |ty: &PrimType| match ty.vscale_factor() {
2000            Some(factor) => Ok(i32::from(factor)),
2001            None => support::lanes(ty),
2002        };
2003        if lanes_or_vscale_factor(condition_ty)? != lanes_or_vscale_factor(true_ty)?
2004            && !condition_ty.is_scalar()
2005        {
2006            let condition = "GetLanesOrVScaleFactor(condition_ty) == \
2007                             GetLanesOrVScaleFactor(true_ty) || condition_ty.IsScalar()";
2008            return Err(support::check_failed(tvm_ffi::INTERNAL_ERROR, condition, ""));
2009        }
2010        if false_dtype != true_dtype {
2011            let message = format!(
2012                "mismatched types. False type: {}; True type: {}",
2013                support::dtype_name(false_dtype),
2014                support::dtype_name(true_dtype)
2015            );
2016            return Err(support::check_failed(
2017                tvm_ffi::TYPE_ERROR,
2018                "false_ty == true_ty",
2019                &message,
2020            ));
2021        }
2022        let ty = true_ty.clone();
2023        Ok(Self::from_fields(loc.into_owned(), ty, condition, true_value, false_value))
2024    }
2025}
2026
2027/// The expression `body` with `var` bound to `value`, of the type of `body`.
2028// tvm-ffi-stubgen(begin): object/prim.Let
2029#[allow(missing_docs)]
2030#[repr(C)]
2031#[derive(tvm_ffi::derive::ObjectRef, Clone)]
2032pub struct Let {
2033    data_: ObjectArc<LetObj>,
2034}
2035
2036/// The object behind [`Let`].
2037// Complete: reflected fields fill [40, 64) exactly.
2038#[allow(missing_docs)]
2039#[repr(C)]
2040#[derive(tvm_ffi::derive::Object)]
2041#[object(type_key = "prim.Let", final, immutable)]
2042pub struct LetObj {
2043    base_: ExprObj,
2044    pub var: Var,
2045    pub value: PrimExpr,
2046    pub body: PrimExpr,
2047}
2048
2049const _: () = {
2050    assert!(::core::mem::size_of::<LetObj>() == 64);
2051    assert!(::core::mem::align_of::<LetObj>() == 8);
2052};
2053
2054impl Deref for LetObj {
2055    type Target = ExprObj;
2056    fn deref(&self) -> &ExprObj {
2057        &self.base_
2058    }
2059}
2060
2061#[allow(missing_docs)]
2062impl LetObj {
2063    pub(crate) fn new(
2064        loc: Location,
2065        ty: PrimType,
2066        var: Var,
2067        value: PrimExpr,
2068        body: PrimExpr,
2069    ) -> Self {
2070        let base_ = ExprObj::new(loc, ty.into());
2071        Self { base_, var, value, body }
2072    }
2073}
2074
2075#[allow(missing_docs)]
2076impl Let {
2077    /// Create a `Let` from its field values.
2078    pub fn from_fields(
2079        loc: impl IntoArg<Location>,
2080        ty: impl IntoArg<PrimType>,
2081        var: impl IntoArg<Var>,
2082        value: impl IntoArg<PrimExpr>,
2083        body: impl IntoArg<PrimExpr>,
2084    ) -> Self {
2085        let obj = LetObj::new(
2086            loc.into_owned(),
2087            ty.into_owned(),
2088            var.into_owned(),
2089            value.into_owned(),
2090            body.into_owned(),
2091        );
2092        Self { data_: ObjectArc::new(obj) }
2093    }
2094}
2095
2096tvm_ffi::impl_object_ref_upcast!(Let => Expr);
2097tvm_ffi::impl_object_ref_upcast!(unsafe Let => PrimExpr);
2098// tvm-ffi-stubgen(end)
2099
2100impl Let {
2101    /// Create the node binding `var` to `value` in `body`, of the type of `body`.
2102    ///
2103    /// # Errors
2104    ///
2105    /// Returns an `InternalError` if `value` is not of the type of `var`, or a `TypeError` if the
2106    /// type of `var` is not a `PrimType`.
2107    pub fn new(
2108        var: impl IntoArg<Var>,
2109        value: impl IntoExpr<PrimExpr>,
2110        body: impl IntoExpr<PrimExpr>,
2111        loc: impl IntoArg<Location>,
2112    ) -> Result<Self> {
2113        let (var, value, body) =
2114            (var.into_owned(), value.into_owned_expr()?, body.into_owned_expr()?);
2115        let value_dtype = value.prim_ty().dtype;
2116        let Some(var_ty) = var.ty.downcast_ref::<PrimTypeObj>() else {
2117            tvm_ffi::bail!(
2118                tvm_ffi::TYPE_ERROR,
2119                "Cannot treat type `{}` as type `ir.PrimType`",
2120                tvm_ffi::reflection::type_key(AnyView::from(&var.ty).type_index())
2121                    .unwrap_or_default()
2122            );
2123        };
2124        if value_dtype != var_ty.dtype {
2125            let condition = "value.ty() == var->ty.as_or_throw<PrimType>()";
2126            return Err(support::check_failed(tvm_ffi::INTERNAL_ERROR, condition, ""));
2127        }
2128        let ty = body.prim_ty().clone();
2129        Ok(Self::from_fields(loc.into_owned(), ty, var, value, body))
2130    }
2131}
2132
2133// ---------------------------------------------------------------------------------------------
2134// What the node constructors check
2135
2136/// What the constructor of a binary node checks of its operands, and the type it gives the node.
2137#[derive(Clone, Copy)]
2138enum Operands {
2139    /// `Add`, `Min`, ...: operands of one type, which is the node's.
2140    Arith,
2141    /// `LShift`, `BitwiseAnd`, ...: as `Arith`, of an integer type, or boolean if allowed.
2142    Bitwise { allow_bool: bool },
2143    /// `EQ`, `LT`, ...: operands of one type; the node is boolean.
2144    Compare,
2145}
2146
2147/// The type of the binary node `node` of `a` and `b`, after checking the operands.
2148fn binary_type(node: &str, operands: Operands, a: &PrimExpr, b: &PrimExpr) -> Result<PrimType> {
2149    let (a_ty, b_dtype) = (a.prim_ty(), b.prim_ty().dtype);
2150    let a_dtype = a_ty.dtype;
2151    if a_dtype != b_dtype {
2152        let message = format!(
2153            "mismatched types. {} vs. {}\n",
2154            support::dtype_name(a_dtype),
2155            support::dtype_name(b_dtype)
2156        );
2157        let condition = "a_ty->dtype == b_ty->dtype";
2158        return Err(support::check_failed(tvm_ffi::TYPE_ERROR, condition, &message));
2159    }
2160    match operands {
2161        Operands::Arith => Ok(a_ty.clone()),
2162        Operands::Bitwise { allow_bool } => {
2163            if a_ty.matches_code(&[DLDataTypeCode::kDLInt, DLDataTypeCode::kDLUInt])
2164                || (allow_bool && a_ty.matches_code(&[DLDataTypeCode::kDLBool]))
2165            {
2166                return Ok(a_ty.clone());
2167            }
2168            let condition = format!(
2169                "a.ty().MatchesCode(DLDataTypeCode::kDLInt, DLDataTypeCode::kDLUInt) || \
2170                 ({allow_bool} && a.ty().MatchesCode(DLDataTypeCode::kDLBool))"
2171            );
2172            let operands = if allow_bool { "integer or boolean" } else { "integer" };
2173            let message = format!("{node} requires {operands} operands");
2174            Err(support::check_failed(tvm_ffi::TYPE_ERROR, &condition, &message))
2175        }
2176        Operands::Compare => bool_type(a_ty),
2177    }
2178}
2179
2180/// The boolean type with the lanes of `operand`.
2181fn bool_type(operand: &PrimType) -> Result<PrimType> {
2182    use crate::ir::dtype;
2183    PrimType::try_from(dtype::with_lanes(dtype::bool(), operand.dtype.lanes))
2184}
2185
2186/// The type of the logical node of `a` and `b`, after checking that the operands are booleans of
2187/// one type.
2188fn logical_type(a: &PrimExpr, b: &PrimExpr) -> Result<PrimType> {
2189    for (ty, operand) in [(a.prim_ty(), "a_ty"), (b.prim_ty(), "b_ty")] {
2190        if !ty.matches_code(&[DLDataTypeCode::kDLBool]) {
2191            let condition = format!("{operand}.MatchesCode(DLDataTypeCode::kDLBool)");
2192            return Err(support::check_failed(tvm_ffi::INTERNAL_ERROR, &condition, ""));
2193        }
2194    }
2195    if a.prim_ty().dtype != b.prim_ty().dtype {
2196        return Err(support::check_failed(tvm_ffi::TYPE_ERROR, "a_ty == b_ty", "mismatched types"));
2197    }
2198    bool_type(a.prim_ty())
2199}