1use 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;
165macro_rules! binary_new {
169 ($($node:ident: $operands:expr, $doc:literal;)+) => {
170 $(
171 impl $node {
172 #[doc = $doc]
173 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#[allow(missing_docs)]
198#[repr(C)]
199#[derive(tvm_ffi::derive::ObjectRef, Clone)]
200pub struct Cast {
201 data_: ObjectArc<CastObj>,
202}
203
204#[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 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);
250impl Cast {
253 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#[allow(missing_docs)]
280#[repr(C)]
281#[derive(tvm_ffi::derive::ObjectRef, Clone)]
282pub struct LShift {
283 data_: ObjectArc<LShiftObj>,
284}
285
286#[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 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);
334binary_new! {
337 LShift: Operands::Bitwise { allow_bool: false },
338 "Create the node `a << b` of integer operands of one type.";
339}
340
341#[allow(missing_docs)]
344#[repr(C)]
345#[derive(tvm_ffi::derive::ObjectRef, Clone)]
346pub struct RShift {
347 data_: ObjectArc<RShiftObj>,
348}
349
350#[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 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);
398binary_new! {
401 RShift: Operands::Bitwise { allow_bool: false },
402 "Create the node `a >> b` of integer operands of one type.";
403}
404
405#[allow(missing_docs)]
408#[repr(C)]
409#[derive(tvm_ffi::derive::ObjectRef, Clone)]
410pub struct BitwiseAnd {
411 data_: ObjectArc<BitwiseAndObj>,
412}
413
414#[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 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);
463binary_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#[allow(missing_docs)]
473#[repr(C)]
474#[derive(tvm_ffi::derive::ObjectRef, Clone)]
475pub struct BitwiseOr {
476 data_: ObjectArc<BitwiseOrObj>,
477}
478
479#[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 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);
528binary_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#[allow(missing_docs)]
538#[repr(C)]
539#[derive(tvm_ffi::derive::ObjectRef, Clone)]
540pub struct BitwiseXor {
541 data_: ObjectArc<BitwiseXorObj>,
542}
543
544#[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 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);
593binary_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#[allow(missing_docs)]
603#[repr(C)]
604#[derive(tvm_ffi::derive::ObjectRef, Clone)]
605pub struct BitwiseNot {
606 data_: ObjectArc<BitwiseNotObj>,
607}
608
609#[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 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);
655impl BitwiseNot {
658 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#[allow(missing_docs)]
701#[repr(C)]
702#[derive(tvm_ffi::derive::ObjectRef, Clone)]
703pub struct Add {
704 data_: ObjectArc<AddObj>,
705}
706
707#[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 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);
755binary_new! {
758 Add: Operands::Arith,
759 "Create the node `a + b` of operands of one type.";
760}
761
762#[allow(missing_docs)]
765#[repr(C)]
766#[derive(tvm_ffi::derive::ObjectRef, Clone)]
767pub struct Sub {
768 data_: ObjectArc<SubObj>,
769}
770
771#[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 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);
819binary_new! {
822 Sub: Operands::Arith,
823 "Create the node `a - b` of operands of one type.";
824}
825
826#[allow(missing_docs)]
829#[repr(C)]
830#[derive(tvm_ffi::derive::ObjectRef, Clone)]
831pub struct Mul {
832 data_: ObjectArc<MulObj>,
833}
834
835#[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 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);
883binary_new! {
886 Mul: Operands::Arith,
887 "Create the node `a * b` of operands of one type.";
888}
889
890#[allow(missing_docs)]
893#[repr(C)]
894#[derive(tvm_ffi::derive::ObjectRef, Clone)]
895pub struct Div {
896 data_: ObjectArc<DivObj>,
897}
898
899#[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 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);
947binary_new! {
950 Div: Operands::Arith,
951 "Create the node `a / b` of operands of one type.";
952}
953
954#[allow(missing_docs)]
957#[repr(C)]
958#[derive(tvm_ffi::derive::ObjectRef, Clone)]
959pub struct Mod {
960 data_: ObjectArc<ModObj>,
961}
962
963#[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 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);
1011binary_new! {
1014 Mod: Operands::Arith,
1015 "Create the truncated node `a % b` of operands of one type.";
1016}
1017
1018#[allow(missing_docs)]
1021#[repr(C)]
1022#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1023pub struct FloorDiv {
1024 data_: ObjectArc<FloorDivObj>,
1025}
1026
1027#[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 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);
1076binary_new! {
1079 FloorDiv: Operands::Arith,
1080 "Create the node `floor(a / b)` of operands of one type.";
1081}
1082
1083#[allow(missing_docs)]
1086#[repr(C)]
1087#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1088pub struct FloorMod {
1089 data_: ObjectArc<FloorModObj>,
1090}
1091
1092#[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 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);
1141binary_new! {
1144 FloorMod: Operands::Arith,
1145 "Create the node `a - floor(a / b) * b` of operands of one type.";
1146}
1147
1148#[allow(missing_docs)]
1151#[repr(C)]
1152#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1153pub struct Min {
1154 data_: ObjectArc<MinObj>,
1155}
1156
1157#[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 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);
1205binary_new! {
1208 Min: Operands::Arith,
1209 "Create the node `min(a, b)` of operands of one type.";
1210}
1211
1212#[allow(missing_docs)]
1215#[repr(C)]
1216#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1217pub struct Max {
1218 data_: ObjectArc<MaxObj>,
1219}
1220
1221#[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 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);
1269binary_new! {
1272 Max: Operands::Arith,
1273 "Create the node `max(a, b)` of operands of one type.";
1274}
1275
1276#[allow(missing_docs)]
1282#[repr(C)]
1283#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1284pub struct EQ {
1285 data_: ObjectArc<EQObj>,
1286}
1287
1288#[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 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);
1336binary_new! {
1339 EQ: Operands::Compare,
1340 "Create the boolean node `a == b` of operands of one type.";
1341}
1342
1343#[allow(missing_docs)]
1346#[repr(C)]
1347#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1348pub struct NE {
1349 data_: ObjectArc<NEObj>,
1350}
1351
1352#[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 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);
1400binary_new! {
1403 NE: Operands::Compare,
1404 "Create the boolean node `a != b` of operands of one type.";
1405}
1406
1407#[allow(missing_docs)]
1410#[repr(C)]
1411#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1412pub struct LT {
1413 data_: ObjectArc<LTObj>,
1414}
1415
1416#[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 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);
1464binary_new! {
1467 LT: Operands::Compare,
1468 "Create the boolean node `a < b` of operands of one type.";
1469}
1470
1471#[allow(missing_docs)]
1474#[repr(C)]
1475#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1476pub struct LE {
1477 data_: ObjectArc<LEObj>,
1478}
1479
1480#[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 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);
1528binary_new! {
1531 LE: Operands::Compare,
1532 "Create the boolean node `a <= b` of operands of one type.";
1533}
1534
1535#[allow(missing_docs)]
1538#[repr(C)]
1539#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1540pub struct GT {
1541 data_: ObjectArc<GTObj>,
1542}
1543
1544#[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 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);
1592binary_new! {
1595 GT: Operands::Compare,
1596 "Create the boolean node `a > b` of operands of one type.";
1597}
1598
1599#[allow(missing_docs)]
1602#[repr(C)]
1603#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1604pub struct GE {
1605 data_: ObjectArc<GEObj>,
1606}
1607
1608#[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 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);
1656binary_new! {
1659 GE: Operands::Compare,
1660 "Create the boolean node `a >= b` of operands of one type.";
1661}
1662
1663#[allow(missing_docs)]
1669#[repr(C)]
1670#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1671pub struct And {
1672 data_: ObjectArc<AndObj>,
1673}
1674
1675#[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 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);
1723impl And {
1726 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#[allow(missing_docs)]
1746#[repr(C)]
1747#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1748pub struct Or {
1749 data_: ObjectArc<OrObj>,
1750}
1751
1752#[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 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);
1800impl Or {
1803 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#[allow(missing_docs)]
1823#[repr(C)]
1824#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1825pub struct Not {
1826 data_: ObjectArc<NotObj>,
1827}
1828
1829#[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 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);
1875impl Not {
1878 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#[allow(missing_docs)]
1903#[repr(C)]
1904#[derive(tvm_ffi::derive::ObjectRef, Clone)]
1905pub struct Select {
1906 data_: ObjectArc<SelectObj>,
1907}
1908
1909#[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 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);
1971impl Select {
1974 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 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#[allow(missing_docs)]
2030#[repr(C)]
2031#[derive(tvm_ffi::derive::ObjectRef, Clone)]
2032pub struct Let {
2033 data_: ObjectArc<LetObj>,
2034}
2035
2036#[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 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);
2098impl Let {
2101 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#[derive(Clone, Copy)]
2138enum Operands {
2139 Arith,
2141 Bitwise { allow_bool: bool },
2143 Compare,
2145}
2146
2147fn 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
2180fn 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
2186fn 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}