tvm
expr.h
Go to the documentation of this file.
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 
24 #ifndef TVM_IR_EXPR_H_
25 #define TVM_IR_EXPR_H_
26 
27 #include <tvm/ir/span.h>
28 #include <tvm/ir/type.h>
29 #include <tvm/node/node.h>
31 #include <tvm/runtime/object.h>
32 
33 #include <algorithm>
34 #include <limits>
35 #include <string>
36 #include <type_traits>
37 
38 namespace tvm {
39 
41 
42 // Forward-declare VirtualDevice to avoid circular imports.
43 class VirtualDevice;
44 
49 class BaseExprNode : public Object {
50  public:
55  mutable Span span;
56 
57  static constexpr const char* _type_key = "BaseExpr";
58  static constexpr const bool _type_has_method_sequal_reduce = true;
59  static constexpr const bool _type_has_method_shash_reduce = true;
60  static constexpr const uint32_t _type_child_slots = 62;
62 };
63 
68 class BaseExpr : public ObjectRef {
69  public:
71 };
72 
85 class PrimExprNode : public BaseExprNode {
86  public:
102 
103  static constexpr const char* _type_key = "PrimExpr";
104  static constexpr const uint32_t _type_child_slots = 38;
106 };
107 
112 class PrimExpr : public BaseExpr {
113  public:
118  TVM_DLL PrimExpr(int32_t value); // NOLINT(*)
123  TVM_DLL PrimExpr(float value); // NOLINT(*)
124 
126  DataType dtype() const { return static_cast<const PrimExprNode*>(get())->dtype; }
127 
129 
130  private:
131  // Internal function for conversion.
132  friend struct runtime::PackedFuncValueConverter<PrimExpr>;
133  TVM_DLL static PrimExpr FromObject_(ObjectRef ref);
134 };
135 
145 TVM_DLL PrimExpr operator+(PrimExpr a, PrimExpr b);
146 
156 TVM_DLL PrimExpr operator-(PrimExpr a, PrimExpr b);
157 
166 TVM_DLL PrimExpr operator-(PrimExpr a);
167 
177 TVM_DLL PrimExpr operator*(PrimExpr a, PrimExpr b);
178 
188 TVM_DLL PrimExpr operator/(PrimExpr a, PrimExpr b);
189 
199 TVM_DLL PrimExpr operator<<(PrimExpr a, PrimExpr b);
200 
210 TVM_DLL PrimExpr operator>>(PrimExpr a, PrimExpr b);
211 
221 TVM_DLL PrimExpr operator>(PrimExpr a, PrimExpr b);
222 
232 TVM_DLL PrimExpr operator>=(PrimExpr a, PrimExpr b);
233 
243 TVM_DLL PrimExpr operator<(PrimExpr a, PrimExpr b);
244 
254 TVM_DLL PrimExpr operator<=(PrimExpr a, PrimExpr b);
255 
265 TVM_DLL PrimExpr operator==(PrimExpr a, PrimExpr b);
266 
276 TVM_DLL PrimExpr operator!=(PrimExpr a, PrimExpr b);
277 
286 TVM_DLL PrimExpr operator&&(PrimExpr a, PrimExpr b);
287 
296 TVM_DLL PrimExpr operator||(PrimExpr a, PrimExpr b);
297 
305 TVM_DLL PrimExpr operator!(PrimExpr a);
306 
316 TVM_DLL PrimExpr operator&(PrimExpr a, PrimExpr b);
317 
327 TVM_DLL PrimExpr operator|(PrimExpr a, PrimExpr b);
328 
338 TVM_DLL PrimExpr operator^(PrimExpr a, PrimExpr b);
339 
348 TVM_DLL PrimExpr operator~(PrimExpr a);
349 
359 class RelayExprNode : public BaseExprNode {
360  public:
367  mutable Type checked_type_ = Type(nullptr);
371  inline const Type& checked_type() const;
382  template <typename TTypeNode>
383  inline const TTypeNode* type_as() const;
384 
408 
420  VirtualDevice virtual_device() const;
421 
422  static constexpr const char* _type_key = "RelayExpr";
423  static constexpr const uint32_t _type_child_slots = 22;
425 };
426 
431 class RelayExpr : public BaseExpr {
432  public:
434 };
435 
436 class GlobalVar;
445 class GlobalVarNode : public RelayExprNode {
446  public:
449 
451  v->Visit("name_hint", &name_hint);
452  v->Visit("virtual_device_", &virtual_device_);
453  v->Visit("span", &span);
454  v->Visit("_checked_type_", &checked_type_);
455  }
456 
457  bool SEqualReduce(const GlobalVarNode* other, SEqualReducer equal) const {
458  // name matters for global var.
459  return equal(name_hint, other->name_hint) && equal.FreeVarEqualImpl(this, other);
460  }
461 
462  void SHashReduce(SHashReducer hash_reduce) const {
463  hash_reduce(name_hint);
464  hash_reduce.FreeVarHashImpl(this);
465  }
466 
467  static constexpr const char* _type_key = "GlobalVar";
469 };
470 
475 class GlobalVar : public RelayExpr {
476  public:
477  TVM_DLL explicit GlobalVar(String name_hint, Type type = {}, Span span = {});
478 
481 };
482 
483 // PrimExprs that are useful as runtime containers.
484 //
489 class IntImmNode : public PrimExprNode {
490  public:
492  int64_t value;
493 
495  v->Visit("dtype", &dtype);
496  v->Visit("value", &value);
497  v->Visit("span", &span);
498  }
499 
500  bool SEqualReduce(const IntImmNode* other, SEqualReducer equal) const {
501  return equal(dtype, other->dtype) && equal(value, other->value);
502  }
503 
504  void SHashReduce(SHashReducer hash_reduce) const {
505  hash_reduce(dtype);
506  hash_reduce(value);
507  }
508 
509  static constexpr const char* _type_key = "IntImm";
511 };
512 
518 class IntImm : public PrimExpr {
519  public:
526  TVM_DLL IntImm(DataType dtype, int64_t value, Span span = Span());
527 
529 };
530 
535 class FloatImmNode : public PrimExprNode {
536  public:
538  double value;
539 
541  v->Visit("dtype", &dtype);
542  v->Visit("value", &value);
543  v->Visit("span", &span);
544  }
545 
546  bool SEqualReduce(const FloatImmNode* other, SEqualReducer equal) const {
547  return equal(dtype, other->dtype) && equal(value, other->value);
548  }
549 
550  void SHashReduce(SHashReducer hash_reduce) const {
551  hash_reduce(dtype);
552  hash_reduce(value);
553  }
554 
555  static constexpr const char* _type_key = "FloatImm";
557 };
558 
564 class FloatImm : public PrimExpr {
565  public:
572  TVM_DLL FloatImm(DataType dtype, double value, Span span = Span());
573 
575 };
576 
583 class Bool : public IntImm {
584  public:
585  explicit Bool(bool value, Span span = Span()) : IntImm(DataType::Bool(), value, span) {}
586  Bool operator!() const { return Bool((*this)->value == 0); }
587  operator bool() const { return (*this)->value != 0; }
588 
590 };
591 
592 // Overload operators to make sure we have the most fine grained types.
593 inline Bool operator||(const Bool& a, bool b) { return Bool(a.operator bool() || b); }
594 inline Bool operator||(bool a, const Bool& b) { return Bool(a || b.operator bool()); }
595 inline Bool operator||(const Bool& a, const Bool& b) {
596  return Bool(a.operator bool() || b.operator bool());
597 }
598 inline Bool operator&&(const Bool& a, bool b) { return Bool(a.operator bool() && b); }
599 inline Bool operator&&(bool a, const Bool& b) { return Bool(a && b.operator bool()); }
600 inline Bool operator&&(const Bool& a, const Bool& b) {
601  return Bool(a.operator bool() && b.operator bool());
602 }
603 
604 inline bool operator==(const Bool& a, bool b) { return a.operator bool() == b; }
605 inline bool operator==(bool a, const Bool& b) { return a == b.operator bool(); }
606 inline bool operator==(const Bool& a, const Bool& b) {
607  return a.operator bool() == b.operator bool();
608 }
609 
618 class Integer : public IntImm {
619  public:
620  Integer() {}
624  explicit Integer(ObjectPtr<Object> node) : IntImm(node) {}
628  Integer(int value, Span span = Span()) : IntImm(DataType::Int(32), value, span) {} // NOLINT(*)
633  Integer(IntImm other) : IntImm(std::move(other)) {} // NOLINT(*)
639  template <typename Enum, typename = typename std::enable_if<std::is_enum<Enum>::value>::type>
640  explicit Integer(Enum value) : Integer(static_cast<int>(value)) {
641  static_assert(std::is_same<int, typename std::underlying_type<Enum>::type>::value,
642  "declare enum to be enum int to use visitor");
643  }
648  Integer& operator=(const IntImm& other) {
649  data_ = ObjectRef::GetDataPtr<Object>(other);
650  return *this;
651  }
655  int64_t IntValue() const {
656  ICHECK(data_ != nullptr) << " Trying to reference a null Integer";
657  return (*this)->value;
658  }
659  // comparators
660  Bool operator==(int other) const {
661  if (data_ == nullptr) return Bool(false);
662  return Bool((*this)->value == other);
663  }
664  Bool operator!=(int other) const { return !(*this == other); }
665  template <typename Enum, typename = typename std::enable_if<std::is_enum<Enum>::value>::type>
666  Bool operator==(Enum other) const {
667  return *this == static_cast<int>(other);
668  }
669  template <typename Enum, typename = typename std::enable_if<std::is_enum<Enum>::value>::type>
670  Bool operator!=(Enum other) const {
671  return *this != static_cast<int>(other);
672  }
673 };
674 
676 class RangeNode : public Object {
677  public:
683  mutable Span span;
686  RangeNode(PrimExpr min, PrimExpr extent, Span span = Span())
687  : min(min), extent(extent), span(span) {}
688 
690  v->Visit("min", &min);
691  v->Visit("extent", &extent);
692  v->Visit("span", &span);
693  }
694 
695  bool SEqualReduce(const RangeNode* other, SEqualReducer equal) const {
696  return equal(min, other->min) && equal(extent, other->extent);
697  }
698 
699  void SHashReduce(SHashReducer hash_reduce) const {
700  hash_reduce(min);
701  hash_reduce(extent);
702  }
703 
704  static constexpr const char* _type_key = "Range";
705  static constexpr const bool _type_has_method_sequal_reduce = true;
706  static constexpr const bool _type_has_method_shash_reduce = true;
708 };
709 
711 class Range : public ObjectRef {
712  public:
719  TVM_DLL Range(PrimExpr begin, PrimExpr end, Span span = Span());
730  static Range FromMinExtent(PrimExpr min, PrimExpr extent, Span span = Span());
731  // declare range.
733 };
734 
735 // implementataions
736 inline const Type& RelayExprNode::checked_type() const {
737  ICHECK(checked_type_.defined()) << "internal error: the type checker has "
738  << "not populated the checked_type "
739  << "field for " << GetRef<RelayExpr>(this);
740  return this->checked_type_;
741 }
742 
743 template <typename TTypeNode>
744 inline const TTypeNode* RelayExprNode::type_as() const {
745  static_assert(std::is_base_of<TypeNode, TTypeNode>::value,
746  "TType must be a special case of type");
747  ICHECK(checked_type_.defined())
748  << "Type inference for this Expr has not completed. Try to call infer_type pass.";
749  const TTypeNode* node = checked_type_.as<TTypeNode>();
750  ICHECK(node != nullptr) << "Expected type to be " << TTypeNode::_type_key << ", but get "
751  << checked_type_->GetTypeKey();
752  return node;
753 }
754 
755 } // namespace tvm
756 
757 namespace tvm {
758 namespace runtime {
759 // common rule for RetValue and ArgValue
760 template <>
762  static PrimExpr From(const TVMPODValue_& val) {
763  if (val.type_code() == kTVMNullptr) {
764  return PrimExpr(ObjectPtr<Object>(nullptr));
765  }
766  if (val.type_code() == kDLInt) {
767  return IntImm(runtime::DataType::Int(32), val.operator int());
768  }
769  if (val.type_code() == kDLFloat) {
770  return FloatImm(runtime::DataType::Float(32), val.operator double());
771  }
772 
773  return PrimExpr::FromObject_(val.AsObjectRef<ObjectRef>());
774  }
775 };
776 
777 template <>
779  static tvm::Integer From(const TVMPODValue_& val) {
780  if (val.type_code() == kTVMNullptr) {
781  return Integer(ObjectPtr<Object>(nullptr));
782  }
783  if (val.type_code() == kTVMArgInt) {
784  return Integer(val.operator int());
785  }
786  return val.AsObjectRef<tvm::Integer>();
787  }
788 };
789 
790 template <>
792  static tvm::Bool From(const TVMPODValue_& val) {
793  if (val.type_code() == kTVMNullptr) {
794  return Bool(ObjectPtr<Object>(nullptr));
795  }
796  if (val.type_code() == kTVMArgInt) {
797  int v = val.operator int();
798  ICHECK(v == 0 || v == 1) << "ValueError: boolean value can only be 0 or 1, but get " << v;
799  return Bool(static_cast<bool>(v));
800  }
801  return val.AsObjectRef<tvm::Bool>();
802  }
803 };
804 
805 } // namespace runtime
806 } // namespace tvm
807 #endif // TVM_IR_EXPR_H_
Integer(Enum value)
Constructor from enum.
Definition: expr.h:640
PrimExpr operator!=(PrimExpr a, PrimExpr b)
not_equal
tvm::Span Span
Definition: base.h:65
static constexpr const char * _type_key
Definition: expr.h:57
void FreeVarHashImpl(const runtime::Object *var) const
Implementation for hash for a free var.
Definition: structural_hash.h:185
PrimExpr operator<(PrimExpr a, PrimExpr b)
less
double value
The constant value content.
Definition: expr.h:538
PrimExpr min
beginning of the node
Definition: expr.h:679
const Type & checked_type() const
Definition: expr.h:736
A custom smart pointer for Object.
Definition: object.h:358
PrimExpr operator||(PrimExpr a, PrimExpr b)
or
Boolean constant.
Definition: expr.h:583
Definitions and helper macros for IR/AST nodes.
Runtime String container types.
Internal base class to handle conversion to POD values.
Definition: packed_func.h:541
void SHashReduce(SHashReducer hash_reduce) const
Definition: expr.h:699
bool FreeVarEqualImpl(const runtime::Object *lhs, const runtime::Object *rhs) const
Implementation for equality rule of var type objects(e.g. TypeVar, tir::Var).
Definition: structural_equal.h:277
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:124
static constexpr const bool _type_has_method_shash_reduce
Definition: expr.h:59
Integer()
Definition: expr.h:620
String name_hint
The name of the variable, this only acts as a hint.
Definition: expr.h:448
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Span span
the location of this range in the source
Definition: expr.h:683
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:102
Definition: c_runtime_api.h:111
Bool operator==(int other) const
Definition: expr.h:660
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
Constant floating point literals in the program.
Definition: expr.h:535
Definition: loop_state.h:456
bool SEqualReduce(const IntImmNode *other, SEqualReducer equal) const
Definition: expr.h:500
DataType dtype() const
Definition: expr.h:126
Integer(int value, Span span=Span())
Construct integer from int value.
Definition: expr.h:628
base class of all object containers.
Definition: object.h:167
PrimExpr operator-(PrimExpr a, PrimExpr b)
subtraction operator
Integer & operator=(const IntImm &other)
Assign an expression to integer.
Definition: expr.h:648
Integer(IntImm other)
Construct integer from int imm.
Definition: expr.h:633
Managed reference to BaseExprNode.
Definition: expr.h:68
Constant integer literals in the program.
Definition: expr.h:489
PrimExpr extent
the extend of range
Definition: expr.h:681
PrimExpr operator &&(PrimExpr a, PrimExpr b)
and
Span information for debugging purposes.
Integer(ObjectPtr< Object > node)
constructor from node.
Definition: expr.h:624
Managed reference class to FloatImmNode.
Definition: expr.h:564
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
PrimExpr operator &(PrimExpr a, PrimExpr b)
take bitwise and of two values
static tvm::Bool From(const TVMPODValue_ &val)
Definition: expr.h:792
bool SEqualReduce(const FloatImmNode *other, SEqualReducer equal) const
Definition: expr.h:546
Managed reference class to VirtualDeviceNode.
Definition: virtual_device.h:261
Range constainer.
Definition: expr.h:711
Definition: span.h:115
PrimExpr operator!(PrimExpr a)
not
Span span
Span that points to the original source code. Reserved debug information.
Definition: expr.h:55
int64_t IntValue() const
convert to int64_t
Definition: expr.h:655
static constexpr const uint32_t _type_child_slots
Definition: expr.h:60
IR/AST nodes for the unified type system in TVM.
void SHashReduce(SHashReducer hash_reduce) const
Definition: expr.h:462
bool SEqualReduce(const RangeNode *other, SEqualReducer equal) const
Definition: expr.h:695
Runtime primitive data type.
Definition: data_type.h:41
Base type of all the expressions.
Definition: expr.h:49
tvm::GlobalVar GlobalVar
Definition: expr.h:58
static DataType Float(int bits, int lanes=1)
Construct an float type.
Definition: data_type.h:168
static PrimExpr From(const TVMPODValue_ &val)
Definition: expr.h:762
static constexpr const bool _type_has_method_sequal_reduce
Definition: expr.h:58
void VisitAttrs(AttrVisitor *v)
Definition: expr.h:540
Bool operator!() const
Definition: expr.h:586
Managed reference class to IntImmNode.
Definition: expr.h:518
PrimExpr operator<<(PrimExpr a, PrimExpr b)
left shift operator
Managed reference to GlobalVarNode.
Definition: expr.h:475
PrimExpr operator^(PrimExpr a, PrimExpr b)
take bitwise xor of two values
RangeNode(PrimExpr min, PrimExpr extent, Span span=Span())
Definition: expr.h:686
ObjectRef virtual_device_
The virtual device (VirtualDevice) for this node (the result of device planning). For first-order exp...
Definition: expr.h:407
TObjectRef AsObjectRef() const
Definition: packed_func.h:1823
int64_t value
the Internal value.
Definition: expr.h:492
Reference to string objects.
Definition: string.h:97
Managed reference to RelayExprNode.
Definition: expr.h:431
#define TVM_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:713
void VisitAttrs(AttrVisitor *v)
Definition: expr.h:494
PrimExpr operator>>(PrimExpr a, PrimExpr b)
right shift operator
bool SEqualReduce(const GlobalVarNode *other, SEqualReducer equal) const
Definition: expr.h:457
PrimExpr operator==(PrimExpr a, PrimExpr b)
equal
Bool operator!=(Enum other) const
Definition: expr.h:670
tvm::Type Type
Definition: type.h:47
void SHashReduce(SHashReducer hash_reduce) const
Definition: expr.h:504
PrimExpr operator>=(PrimExpr a, PrimExpr b)
greater_equal
Bool operator!=(int other) const
Definition: expr.h:664
Base class of all object reference.
Definition: object.h:511
#define TVM_DEFINE_OBJECT_REF_COW_METHOD(ObjectName)
Define CopyOnWrite function in an ObjectRef.
Definition: object.h:785
void SHashReduce(SHashReducer hash_reduce) const
Definition: expr.h:550
PrimExpr operator*(PrimExpr a, PrimExpr b)
multiplication operator
A managed object in the TVM runtime.
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:671
RangeNode()
constructor
Definition: expr.h:685
static tvm::Integer From(const TVMPODValue_ &val)
Definition: expr.h:779
void VisitAttrs(AttrVisitor *v)
Definition: expr.h:689
void VisitAttrs(AttrVisitor *v)
Definition: expr.h:450
const TTypeNode * type_as() const
Check if the inferred(checked) type of the Expr is backed by a TTypeNode and return it...
Definition: expr.h:744
DataType dtype
The runtime data type of the primitive expression.
Definition: expr.h:101
Definition: c_runtime_api.h:114
PrimExpr operator/(PrimExpr a, PrimExpr b)
division operator
int type_code() const
Definition: packed_func.h:610
Bool(bool value, Span span=Span())
Definition: expr.h:585
PrimExpr operator<=(PrimExpr a, PrimExpr b)
less_equal
Managed reference to TypeNode.
Definition: type.h:93
TVM_DECLARE_BASE_OBJECT_INFO(BaseExprNode, Object)
PrimExpr operator~(PrimExpr a)
take bitwise negation of two values
Reference to PrimExprNode.
Definition: expr.h:112
Global variable that lives in the top-level module.
Definition: expr.h:445
PrimExpr operator|(PrimExpr a, PrimExpr b)
take bitwise or of two values
Base node of all non-primitive expressions.
Definition: expr.h:359
#define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:728
PrimExpr operator+(PrimExpr a, PrimExpr b)
add operator
Bool operator==(Enum other) const
Definition: expr.h:666
Type trait to specify special value conversion rules from TVMArgValue and TVMRetValue.
Definition: packed_func.h:1096
static DataType Int(int bits, int lanes=1)
Construct an int type.
Definition: data_type.h:154
Base node of all primitive expressions.
Definition: expr.h:85
Container of constant int that adds more constructors.
Definition: expr.h:618
PrimExpr operator>(PrimExpr a, PrimExpr b)
greater
range over one dimension
Definition: expr.h:676