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/ffi/dtype.h>
28 #include <tvm/ffi/extra/dataclass.h>
29 #include <tvm/ffi/reflection/registry.h>
30 #include <tvm/ffi/string.h>
31 #include <tvm/ir/attrs.h>
32 #include <tvm/ir/base_expr.h>
33 #include <tvm/ir/cow.h>
34 #include <tvm/ir/source_map.h>
35 
36 #include <algorithm>
37 #include <functional>
38 #include <limits>
39 #include <optional>
40 #include <string>
41 #include <type_traits>
42 
43 namespace tvm {
44 
45 // Forward-declare VirtualDevice to avoid circular imports.
46 class VirtualDevice;
47 
58 
69 
79 
90 
101 
112 
123 
134 
145 
156 
167 
178 
189 
199 
209 
218 
229 
240 
251 
261 
262 class GlobalVar;
271 class GlobalVarNode : public ExprNode {
272  public:
274  ffi::String name_hint;
275 
276  static void RegisterReflection() {
277  namespace refl = tvm::ffi::reflection;
278  refl::ObjectDef<GlobalVarNode>().def_ro("name_hint", &GlobalVarNode::name_hint);
279  // A GlobalVar identifies a module-level symbol. Its type is derived from the
280  // corresponding function definition and is not part of the symbol identity.
281  refl::TypeAttrDef<GlobalVarNode>()
282  .def("__s_equal__", &GlobalVarNode::SEqual)
283  .def("__s_hash__", &GlobalVarNode::SHash);
284  }
285 
286  bool SEqual(const GlobalVarNode* other,
287  ffi::TypedFunction<bool(AnyView, AnyView, bool, AnyView)> equal) const {
288  return equal(name_hint, other->name_hint, false, "name_hint");
289  }
290 
291  int64_t SHash(int64_t init_hash, ffi::TypedFunction<int64_t(AnyView, int64_t, bool)> hash) const {
292  return hash(name_hint, init_hash, false);
293  }
294 
295  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindFreeVar;
297 };
298 
303 class GlobalVar : public Expr {
304  public:
305  TVM_DLL explicit GlobalVar(ffi::String name_hint, Span span = {});
306 
309 };
310 
314 class CallNode : public ExprNode {
315  public:
323 
325  ffi::Array<Expr> args;
326 
329 
331  ffi::Array<Type> ty_args;
332 
333  static void RegisterReflection() {
334  namespace refl = tvm::ffi::reflection;
335  refl::ObjectDef<CallNode>()
336  .def_ro("op", &CallNode::op)
337  .def_ro("args", &CallNode::args)
338  .def_ro("attrs", &CallNode::attrs)
339  .def_ro("ty_args", &CallNode::ty_args);
340  }
341 
343 };
344 
348 class Call : public Expr {
349  public:
350  TVM_DLL Call(Type ret_ty, Expr op, ffi::Array<Expr> args, Attrs attrs = Attrs(),
351  ffi::Array<Type> ty_args = ffi::Array<Type>(), Span span = Span());
352 
355 };
356 
361 class IntImmNode : public ExprNode {
362  public:
364  int64_t value;
365 
366  static void RegisterReflection() {
367  namespace refl = tvm::ffi::reflection;
368  refl::ObjectDef<IntImmNode>().def_ro("value", &IntImmNode::value);
369  }
371 };
372 
378 class IntImm : public PrimExpr {
379  public:
386  TVM_DLL IntImm(PrimType value_ty, int64_t value, Span span = Span());
387 
393  static IntImm Bool(bool value, Span span = Span()) {
394  return IntImm(PrimType::Bool(), value, span);
395  }
396 
402  static IntImm Int32(int64_t value, Span span = Span()) {
403  return IntImm(PrimType::Int(32), value, span);
404  }
405 
411  static IntImm Int64(int64_t value, Span span = Span()) {
412  return IntImm(PrimType::Int(64), value, span);
413  }
414 
416  static constexpr bool _type_container_is_exact = true;
418 };
419 
424 class FloatImmNode : public ExprNode {
425  public:
427  double value;
428 
429  static void RegisterReflection() {
430  namespace refl = tvm::ffi::reflection;
431  refl::ObjectDef<FloatImmNode>().def_ro("value", &FloatImmNode::value);
432  }
434 };
435 
441 class FloatImm : public PrimExpr {
442  public:
449  TVM_DLL FloatImm(PrimType value_ty, double value, Span span = Span());
450 
452  static constexpr bool _type_container_is_exact = true;
454 };
455 
457 class RangeNode : public ffi::Object {
458  public:
464  mutable Span span;
468  : min(min), extent(extent), span(span) {}
469 
470  static void RegisterReflection() {
471  namespace refl = tvm::ffi::reflection;
472  refl::ObjectDef<RangeNode>()
473  .def_ro("min", &RangeNode::min)
474  .def_ro("extent", &RangeNode::extent)
475  .def_ro("span", &RangeNode::span, refl::AttachFieldFlag::SEqHashIgnore());
476  }
477 
478  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
479 
480  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.Range", RangeNode, ffi::Object);
481 };
482 
484 class Range : public ffi::ObjectRef {
485  public:
492  TVM_DLL Range(PrimExpr begin, PrimExpr end, Span span = Span());
503  TVM_DLL static Range FromMinExtent(PrimExpr min, PrimExpr extent, Span span = Span());
504  // declare range.
506 };
507 
508 namespace ffi {
509 template <>
510 inline constexpr bool object_ref_contains_v<PrimExpr, IntImmNode> = true;
511 template <>
513 
514 // Type traits to enable automatic conversion into IntImm, Integer, and Bool
515 // when called through the FFI
516 template <>
517 inline constexpr bool use_default_type_traits_v<IntImm> = false;
518 
519 // specialize to enable implicit conversion from const char*
520 template <>
521 struct TypeTraits<IntImm> : public ObjectRefWithFallbackTraitsBase<IntImm, int64_t> {
522  TVM_FFI_INLINE static IntImm ConvertFallbackValue(int64_t value) {
523  auto value_ty =
525  ? PrimType::Int(64)
526  : PrimType::Int(32);
527  return IntImm(value_ty, value);
528  }
529 };
530 
531 template <>
532 inline constexpr bool use_default_type_traits_v<FloatImm> = false;
533 
534 template <>
535 struct TypeTraits<FloatImm> : public ObjectRefWithFallbackTraitsBase<FloatImm, double> {
536  TVM_FFI_INLINE static FloatImm ConvertFallbackValue(double value) {
537  return FloatImm(PrimType::Float(32), value);
538  }
539 };
540 } // namespace ffi
541 } // namespace tvm
542 
543 /* \brief Allow tvm.GLobalVar as key in STL tables
544  *
545  * For most IR expressions, it would be ambiguous whether the
546  * expression should follow reference equality or structural equality.
547  * This is not the case for variables, which do not contain nested
548  * internal structure, and are frequently used as keys in lookup
549  * tables.
550  *
551  * Providing `std::hash` and `std::equal_to` specializations for
552  * `tvm::GlobalVar` allows it to be used as a key in STL tables. For
553  * other IR expressions, the user must specify the type of equality
554  * used (e.g. `std::unordered_set<T, StructuralHash, StructuralEqual>`
555  * or `std::unordered_set<T, ffi::ObjectPtrHash, ffi::ObjectPtrEqual>`).
556  */
557 template <>
558 struct std::hash<tvm::GlobalVar> {
559  std::size_t operator()(const tvm::GlobalVar& var) const { return tvm::ffi::ObjectPtrHash()(var); }
560 };
561 
562 template <>
563 struct std::equal_to<tvm::GlobalVar> {
564  bool operator()(const tvm::GlobalVar& var_a, const tvm::GlobalVar& var_b) const {
565  return tvm::ffi::ObjectPtrEqual()(var_a, var_b);
566  }
567 };
568 #endif // TVM_IR_EXPR_H_
Helpers for attribute objects.
Base expression and primitive type nodes.
Managed reference to AttrsNode.
Definition: attrs.h:59
Call corresponds to callable invocation.
Definition: expr.h:314
Attrs attrs
The additional attributes.
Definition: expr.h:328
ffi::Array< Expr > args
The arguments of the call.
Definition: expr.h:325
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.Call", CallNode, ExprNode)
Expr op
The operator/function being invoked.
Definition: expr.h:322
static void RegisterReflection()
Definition: expr.h:333
ffi::Array< Type > ty_args
The type information arguments passed to the callee.
Definition: expr.h:331
Managed reference to CallNode.
Definition: expr.h:348
Call(Type ret_ty, Expr op, ffi::Array< Expr > args, Attrs attrs=Attrs(), ffi::Array< Type > ty_args=ffi::Array< Type >(), Span span=Span())
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Call, Expr, CallNode)
TVM_DEFINE_OBJECT_REF_COW_METHOD(CallNode)
Base type of all the expressions.
Definition: base_expr.h:276
Managed reference to ExprNode.
Definition: base_expr.h:311
Constant floating point literals in the program.
Definition: expr.h:424
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.FloatImm", FloatImmNode, ExprNode)
double value
The constant value content.
Definition: expr.h:427
static void RegisterReflection()
Definition: expr.h:429
Managed reference class to FloatImmNode.
Definition: expr.h:441
FloatImm(PrimType value_ty, double value, Span span=Span())
Constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(FloatImm, PrimExpr, FloatImmNode)
static constexpr bool _type_container_is_exact
Definition: expr.h:452
TVM_DEFINE_OBJECT_REF_COW_METHOD(FloatImmNode)
Global variable that lives in the top-level module.
Definition: expr.h:271
int64_t SHash(int64_t init_hash, ffi::TypedFunction< int64_t(AnyView, int64_t, bool)> hash) const
Definition: expr.h:291
ffi::String name_hint
The name of the variable, this only acts as a hint.
Definition: expr.h:274
static void RegisterReflection()
Definition: expr.h:276
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.GlobalVar", GlobalVarNode, ExprNode)
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: expr.h:295
bool SEqual(const GlobalVarNode *other, ffi::TypedFunction< bool(AnyView, AnyView, bool, AnyView)> equal) const
Definition: expr.h:286
Managed reference to GlobalVarNode.
Definition: expr.h:303
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(GlobalVar, Expr, GlobalVarNode)
TVM_DEFINE_OBJECT_REF_COW_METHOD(GlobalVarNode)
GlobalVar(ffi::String name_hint, Span span={})
Constant integer literals in the program.
Definition: expr.h:361
static void RegisterReflection()
Definition: expr.h:366
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.IntImm", IntImmNode, ExprNode)
int64_t value
the Internal value.
Definition: expr.h:364
Managed reference class to IntImmNode.
Definition: expr.h:378
IntImm(PrimType value_ty, int64_t value, Span span=Span())
Constructor.
static constexpr bool _type_container_is_exact
Definition: expr.h:416
static IntImm Bool(bool value, Span span=Span())
Construct a scalar boolean constant.
Definition: expr.h:393
TVM_DEFINE_OBJECT_REF_COW_METHOD(IntImmNode)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(IntImm, PrimExpr, IntImmNode)
static IntImm Int32(int64_t value, Span span=Span())
Construct a scalar int32 constant.
Definition: expr.h:402
static IntImm Int64(int64_t value, Span span=Span())
Construct a scalar int64 constant.
Definition: expr.h:411
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition: base_expr.h:354
Definition: base_expr.h:113
static PrimType Float(int bits, int lanes=1)
Construct a floating-point type with fixed lanes.
static PrimType Int(int bits, int lanes=1)
Construct a signed integer type with fixed lanes.
static PrimType Bool(int lanes=1)
Construct a boolean type with fixed lanes.
range over one dimension
Definition: expr.h:457
static void RegisterReflection()
Definition: expr.h:470
PrimExpr min
beginning of the node
Definition: expr.h:460
RangeNode(PrimExpr min, PrimExpr extent, Span span=Span())
Definition: expr.h:467
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.Range", RangeNode, ffi::Object)
RangeNode()
constructor
Definition: expr.h:466
PrimExpr extent
the extend of range
Definition: expr.h:462
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: expr.h:478
Span span
the location of this range in the source
Definition: expr.h:464
Range container
Definition: expr.h:484
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Range, ffi::ObjectRef, RangeNode)
static Range FromMinExtent(PrimExpr min, PrimExpr extent, Span span=Span())
construct a new range with min and extent The corresponding constructor is removed,...
Range(PrimExpr begin, PrimExpr end, Span span=Span())
constructor by begin and end
Definition: source_map.h:111
Managed reference to TypeNode.
Definition: base_expr.h:77
Copy-on-write helper macro for IR ffi::ObjectRef types.
constexpr bool object_ref_contains_v< PrimExpr, IntImmNode >
Definition: expr.h:510
constexpr bool object_ref_contains_v< PrimExpr, FloatImmNode >
Definition: expr.h:512
constexpr bool use_default_type_traits_v< FloatImm >
Definition: expr.h:532
constexpr bool use_default_type_traits_v< IntImm >
Definition: expr.h:517
PrimVar var(std::string name_hint, PrimType t=PrimType::Int(32))
Construct a new Var expression.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
PrimExpr max(PrimExpr a, PrimExpr b, Span span=Span())
take maximum of two values
PrimExpr operator/(PrimExpr a, PrimExpr b)
division operator
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
PrimExpr operator>>(PrimExpr a, PrimExpr b)
right shift operator
PrimExpr operator<(PrimExpr a, PrimExpr b)
less
PrimExpr operator|(PrimExpr a, PrimExpr b)
take bitwise or of two values
PrimExpr operator~(PrimExpr a)
take bitwise negation of two values
PrimExpr operator>=(PrimExpr a, PrimExpr b)
greater_equal
bool operator!=(const PrimType &lhs, const PrimType &rhs)
Definition: base_expr.h:270
PrimExpr operator<=(PrimExpr a, PrimExpr b)
less_equal
PrimExpr operator*(PrimExpr a, PrimExpr b)
multiplication operator
PrimExpr operator&(PrimExpr a, PrimExpr b)
take bitwise and of two values
PrimExpr min(PrimExpr a, PrimExpr b, Span span=Span())
take minimum of two values
PrimExpr operator!(PrimExpr a)
not
PrimExpr operator^(PrimExpr a, PrimExpr b)
take bitwise xor of two values
PrimExpr operator-(PrimExpr a, PrimExpr b)
subtraction operator
PrimExpr operator||(PrimExpr a, PrimExpr b)
or
bool operator==(const PrimType &lhs, const PrimType &rhs)
Definition: base_expr.h:266
PrimExpr operator>(PrimExpr a, PrimExpr b)
greater
PrimExpr operator+(PrimExpr a, PrimExpr b)
add operator
PrimExpr operator<<(PrimExpr a, PrimExpr b)
left shift operator
PrimExpr operator&&(PrimExpr a, PrimExpr b)
and
A map from source names to source code.
static TVM_FFI_INLINE FloatImm ConvertFallbackValue(double value)
Definition: expr.h:536
static TVM_FFI_INLINE IntImm ConvertFallbackValue(int64_t value)
Definition: expr.h:522