tvm
Loading...
Searching...
No Matches
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
42namespace tvm {
43
44// Forward-declare VirtualDevice to avoid circular imports.
45class VirtualDevice;
46
48class TupleNode : public ExprNode {
49 public:
51 ffi::Array<Expr> fields;
52
53 static void RegisterReflection() {
54 namespace refl = tvm::ffi::reflection;
55 refl::ObjectDef<TupleNode>().def_ro("fields", &TupleNode::fields);
56 }
57
59};
60
62class Tuple : public Expr {
63 public:
69 TVM_DLL explicit Tuple(ffi::Array<Expr> fields, Span span = Span());
70
73};
74
76class TupleGetItemNode : public ExprNode {
77 public:
81 int index;
82
83 static void RegisterReflection() {
84 namespace refl = tvm::ffi::reflection;
85 refl::ObjectDef<TupleGetItemNode>()
86 .def_ro("tuple_value", &TupleGetItemNode::tuple)
87 .def_ro("index", &TupleGetItemNode::index);
88 }
89
91};
92
107
109class TensorLoadNode : public ExprNode {
110 public:
114 ffi::Array<PrimExpr> indices;
115
116 static void RegisterReflection() {
117 namespace refl = tvm::ffi::reflection;
118 refl::ObjectDef<TensorLoadNode>()
119 .def_ro("source", &TensorLoadNode::source, refl::AttachFieldFlag::SEqHashDefRecursive())
120 .def_ro("indices", &TensorLoadNode::indices);
121 }
122
124};
125
133
144
155
165
176
187
198
209
220
231
242
253
264
275
285
295
304
315
326
337
347
355class VarNode : public ExprNode {
356 public:
358 ffi::String name;
359
360 static void RegisterReflection() {
361 namespace refl = tvm::ffi::reflection;
362 refl::ObjectDef<VarNode>().def_ro("name", &VarNode::name,
363 refl::AttachFieldFlag::SEqHashIgnore());
364 }
365
367 static constexpr const uint32_t _type_child_slots = 1;
369};
370
372class Var : public Expr {
373 public:
374 TVM_DLL explicit Var(ffi::String name, ffi::Optional<Type> ty_annotation, Span span = Span());
375
377 TVM_DLL Var CopyWithName(const ffi::String& name) const;
378
380 TVM_DLL Var CopyWithSuffix(const ffi::String& suffix) const;
381
384
386};
387
388class GlobalVar;
397class GlobalVarNode : public ExprNode {
398 public:
400 ffi::String name_hint;
401
402 static void RegisterReflection() {
403 namespace refl = tvm::ffi::reflection;
404 refl::ObjectDef<GlobalVarNode>().def_ro("name_hint", &GlobalVarNode::name_hint);
405 // A GlobalVar identifies a module-level symbol. Its type is derived from the
406 // corresponding function definition and is not part of the symbol identity.
407 refl::TypeAttrDef<GlobalVarNode>()
408 .def("__s_equal__", &GlobalVarNode::SEqual)
409 .def("__s_hash__", &GlobalVarNode::SHash);
410 }
411
413 ffi::TypedFunction<bool(AnyView, AnyView, bool, AnyView)> equal) const {
414 return equal(name_hint, other->name_hint, false, "name_hint");
415 }
416
417 int64_t SHash(int64_t init_hash, ffi::TypedFunction<int64_t(AnyView, int64_t, bool)> hash) const {
418 return hash(name_hint, init_hash, false);
419 }
420
423};
424
436
440class CallNode : public ExprNode {
441 public:
449
451 ffi::Array<Expr> args;
452
455
457 ffi::Array<Type> ty_args;
458
459 static void RegisterReflection() {
460 namespace refl = tvm::ffi::reflection;
461 refl::ObjectDef<CallNode>()
462 .def_ro("op", &CallNode::op)
463 .def_ro("args", &CallNode::args)
464 .def_ro("attrs", &CallNode::attrs)
465 .def_ro("ty_args", &CallNode::ty_args);
466 }
467
469};
470
474class Call : public Expr {
475 public:
476 TVM_DLL Call(Type ret_ty, Expr op, ffi::Array<Expr> args, Attrs attrs = Attrs(),
477 ffi::Array<Type> ty_args = ffi::Array<Type>(), Span span = Span());
478
481};
482
487class IntImmNode : public ExprNode {
488 public:
491
492 static void RegisterReflection() {
493 namespace refl = tvm::ffi::reflection;
494 refl::ObjectDef<IntImmNode>().def_ro("value", &IntImmNode::value);
495 }
497};
498
504class IntImm : public PrimExpr {
505 public:
513
519 static IntImm Bool(bool value, Span span = Span()) {
520 return IntImm(PrimType::Bool(), value, span);
521 }
522
528 static IntImm Int32(int64_t value, Span span = Span()) {
529 return IntImm(PrimType::Int(32), value, span);
530 }
531
537 static IntImm Int64(int64_t value, Span span = Span()) {
538 return IntImm(PrimType::Int(64), value, span);
539 }
540
542 static constexpr bool _type_container_is_exact = true;
544};
545
550class FloatImmNode : public ExprNode {
551 public:
553 double value;
554
555 static void RegisterReflection() {
556 namespace refl = tvm::ffi::reflection;
557 refl::ObjectDef<FloatImmNode>().def_ro("value", &FloatImmNode::value);
558 }
560};
561
581
583class RangeNode : public ffi::Object {
584 public:
590 mutable Span span;
595
596 static void RegisterReflection() {
597 namespace refl = tvm::ffi::reflection;
598 refl::ObjectDef<RangeNode>()
599 .def_ro("min", &RangeNode::min)
600 .def_ro("extent", &RangeNode::extent)
601 .def_ro("span", &RangeNode::span, refl::AttachFieldFlag::SEqHashIgnore());
602 }
603
605
607};
608
610class Range : public ffi::ObjectRef {
611 public:
630 // declare range.
632};
633
634namespace ffi {
635template <>
637template <>
639template <>
641
642// Type traits to enable automatic conversion into IntImm, Integer, and Bool
643// when called through the FFI
644template <>
645inline constexpr bool use_default_type_traits_v<IntImm> = false;
646
647// specialize to enable implicit conversion from const char*
648template <>
649struct TypeTraits<IntImm> : public ObjectRefWithFallbackTraitsBase<IntImm, int64_t> {
651 auto value_ty =
652 (value > std::numeric_limits<int>::max() || value < std::numeric_limits<int>::min())
653 ? PrimType::Int(64)
654 : PrimType::Int(32);
655 return IntImm(value_ty, value);
656 }
657};
658
659template <>
660inline constexpr bool use_default_type_traits_v<FloatImm> = false;
661
662template <>
663struct TypeTraits<FloatImm> : public ObjectRefWithFallbackTraitsBase<FloatImm, double> {
665 return FloatImm(PrimType::Float(32), value);
666 }
667};
668} // namespace ffi
669} // namespace tvm
670
671/* \brief Allow tvm.Var and tvm.GlobalVar as keys in STL tables
672 *
673 * For most IR expressions, it would be ambiguous whether the
674 * expression should follow reference equality or structural equality.
675 * This is not the case for variables, which do not contain nested
676 * internal structure, and are frequently used as keys in lookup
677 * tables.
678 *
679 * Providing `std::hash` and `std::equal_to` specializations for
680 * `tvm::Var` and `tvm::GlobalVar` allows them to be used as keys in STL tables. For
681 * other IR expressions, the user must specify the type of equality
682 * used (e.g. `std::unordered_set<T, StructuralHash, StructuralEqual>`
683 * or `std::unordered_set<T, ffi::ObjectPtrHash, ffi::ObjectPtrEqual>`).
684 */
685template <>
686struct std::hash<tvm::Var> {
687 std::size_t operator()(const tvm::Var& var) const { return tvm::ffi::ObjectPtrHash()(var); }
688};
689
690template <>
691struct std::equal_to<tvm::Var> {
692 bool operator()(const tvm::Var& var_a, const tvm::Var& var_b) const {
693 return tvm::ffi::ObjectPtrEqual()(var_a, var_b);
694 }
695};
696
697template <>
698struct std::hash<tvm::GlobalVar> {
699 std::size_t operator()(const tvm::GlobalVar& var) const { return tvm::ffi::ObjectPtrHash()(var); }
700};
701
702template <>
703struct std::equal_to<tvm::GlobalVar> {
704 bool operator()(const tvm::GlobalVar& var_a, const tvm::GlobalVar& var_b) const {
705 return tvm::ffi::ObjectPtrEqual()(var_a, var_b);
706 }
707};
708#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:440
Attrs attrs
The additional attributes.
Definition expr.h:454
ffi::Array< Expr > args
The arguments of the call.
Definition expr.h:451
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.Call", CallNode, ExprNode)
Expr op
The operator/function being invoked.
Definition expr.h:448
static void RegisterReflection()
Definition expr.h:459
ffi::Array< Type > ty_args
The type information arguments passed to the callee.
Definition expr.h:457
Managed reference to CallNode.
Definition expr.h:474
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:300
Managed reference to ExprNode.
Definition base_expr.h:335
Constant floating point literals in the program.
Definition expr.h:550
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.FloatImm", FloatImmNode, ExprNode)
double value
The constant value content.
Definition expr.h:553
static void RegisterReflection()
Definition expr.h:555
Managed reference class to FloatImmNode.
Definition expr.h:567
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:578
TVM_DEFINE_OBJECT_REF_COW_METHOD(FloatImmNode)
Global variable that lives in the top-level module.
Definition expr.h:397
int64_t SHash(int64_t init_hash, ffi::TypedFunction< int64_t(AnyView, int64_t, bool)> hash) const
Definition expr.h:417
ffi::String name_hint
The name of the variable, this only acts as a hint.
Definition expr.h:400
static void RegisterReflection()
Definition expr.h:402
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.GlobalVar", GlobalVarNode, ExprNode)
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition expr.h:421
bool SEqual(const GlobalVarNode *other, ffi::TypedFunction< bool(AnyView, AnyView, bool, AnyView)> equal) const
Definition expr.h:412
Managed reference to GlobalVarNode.
Definition expr.h:429
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:487
static void RegisterReflection()
Definition expr.h:492
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.IntImm", IntImmNode, ExprNode)
int64_t value
the Internal value.
Definition expr.h:490
Managed reference class to IntImmNode.
Definition expr.h:504
IntImm(PrimType value_ty, int64_t value, Span span=Span())
Constructor.
static constexpr bool _type_container_is_exact
Definition expr.h:542
static IntImm Bool(bool value, Span span=Span())
Construct a scalar boolean constant.
Definition expr.h:519
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:528
static IntImm Int64(int64_t value, Span span=Span())
Construct a scalar int64 constant.
Definition expr.h:537
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:401
Definition base_expr.h:137
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:583
static void RegisterReflection()
Definition expr.h:596
PrimExpr min
beginning of the node
Definition expr.h:586
RangeNode(PrimExpr min, PrimExpr extent, Span span=Span())
Definition expr.h:593
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.Range", RangeNode, ffi::Object)
RangeNode()
constructor
Definition expr.h:592
PrimExpr extent
the extend of range
Definition expr.h:588
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition expr.h:604
Span span
the location of this range in the source
Definition expr.h:590
Range container
Definition expr.h:610
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
Load a value from an indexed expression source.
Definition expr.h:109
ffi::Array< PrimExpr > indices
The indices at which the source is loaded.
Definition expr.h:114
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.TensorLoad", TensorLoadNode, ExprNode)
Expr source
The indexed source expression.
Definition expr.h:112
static void RegisterReflection()
Definition expr.h:116
Managed reference to TensorLoadNode.
Definition expr.h:127
static constexpr bool _type_container_is_exact
Definition expr.h:130
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(TensorLoad, PrimExpr, TensorLoadNode)
TVM_DEFINE_OBJECT_REF_COW_METHOD(TensorLoadNode)
Get the index-th field out of a tuple.
Definition expr.h:76
Expr tuple
The tuple expression.
Definition expr.h:79
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.TupleGetItem", TupleGetItemNode, ExprNode)
static void RegisterReflection()
Definition expr.h:83
int index
The field index.
Definition expr.h:81
Managed reference to TupleGetItemNode.
Definition expr.h:94
TVM_DEFINE_OBJECT_REF_COW_METHOD(TupleGetItemNode)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(TupleGetItem, Expr, TupleGetItemNode)
TupleGetItem(Expr tuple, int index, Span span=Span())
Construct a tuple field projection.
Tuple container.
Definition expr.h:48
ffi::Array< Expr > fields
The fields of the tuple.
Definition expr.h:51
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.Tuple", TupleNode, ExprNode)
static void RegisterReflection()
Definition expr.h:53
Managed reference to TupleNode.
Definition expr.h:62
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Tuple, Expr, TupleNode)
TVM_DEFINE_OBJECT_REF_COW_METHOD(TupleNode)
Tuple(ffi::Array< Expr > fields, Span span=Span())
Construct a tuple from its fields.
Managed reference to TypeNode.
Definition base_expr.h:77
A local variable in the IR.
Definition expr.h:355
TVM_FFI_DECLARE_OBJECT_INFO("ir.Var", VarNode, ExprNode)
static constexpr const uint32_t _type_child_slots
Definition expr.h:367
ffi::String name
The variable name.
Definition expr.h:358
static void RegisterReflection()
Definition expr.h:360
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition expr.h:366
Managed reference to VarNode.
Definition expr.h:372
Var CopyWithName(const ffi::String &name) const
Return a fresh ordinary Var with the same type and a new name.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Var, Expr, VarNode)
Var CopyWithDType(PrimType dtype) const
Return a fresh ordinary Var with a new primitive type.
Var CopyWithSuffix(const ffi::String &suffix) const
Return a fresh ordinary Var with a suffix appended to its name.
Var(ffi::String name, ffi::Optional< Type > ty_annotation, Span span=Span())
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Copy-on-write helper macro for IR ffi::ObjectRef types.
PrimVar var(std::string name_hint, PrimType t=PrimType::Int(32))
Construct a new Var expression.
tvm::Var Var
Definition var.h:38
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
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:294
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:290
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:664
static TVM_FFI_INLINE IntImm ConvertFallbackValue(int64_t value)
Definition expr.h:650