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/big_int.h>
28#include <tvm/ffi/cast.h>
29#include <tvm/ffi/container/array.h>
30#include <tvm/ffi/container/tuple.h>
31#include <tvm/ffi/container/variant.h>
32#include <tvm/ffi/dtype.h>
33#include <tvm/ffi/error.h>
34#include <tvm/ffi/extra/dataclass.h>
35#include <tvm/ffi/extra/structural_equal.h>
36#include <tvm/ffi/extra/structural_hash.h>
37#include <tvm/ffi/extra/structural_mutate.h>
38#include <tvm/ffi/extra/structural_visit.h>
39#include <tvm/ffi/function.h>
40#include <tvm/ffi/optional.h>
41#include <tvm/ffi/reflection/native_function.h>
42#include <tvm/ffi/reflection/registry.h>
43#include <tvm/ffi/string.h>
44#include <tvm/ir/attrs.h>
45#include <tvm/ir/cow.h>
46#include <tvm/ir/location.h>
47#include <tvm/ir/type.h>
48
49#include <algorithm>
50#include <cstddef>
51#include <cstdint>
52#include <functional>
53#include <limits>
54#include <optional>
55#include <ostream>
56#include <string>
57#include <type_traits>
58#include <utility>
59
60namespace tvm {
61
66class ExprObj : public ffi::Object {
67 public:
72 mutable Location loc = UnknownLoc();
73
80 mutable Type ty = MissingType();
81
82 static void RegisterReflection() {
83 namespace refl = tvm::ffi::reflection;
84 // loc does not participate in structural equal and hash.
85 refl::ObjectDef<ExprObj>()
86 .def_ro("loc", &ExprObj::loc, refl::DefaultValue(UnknownLoc()),
87 refl::AttachFieldFlag::SEqHashIgnore())
88 .def_ro("ty", &ExprObj::ty, refl::DefaultValue(MissingType()));
89 }
90
91 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
92
93 static constexpr const uint32_t _type_child_slots = 64;
94 TVM_FFI_DECLARE_OBJECT_INFO("ir.Expr", ExprObj, ffi::Object);
95};
96
101class Expr : public ffi::ObjectRef {
102 public:
103 // Expressions do not implicitly compare by object identity or address. Callers must name
104 // whether they intend object identity, structural equality, or primitive symbolic comparison.
105 bool operator==(const Expr& other) const = delete;
106 bool operator!=(const Expr& other) const = delete;
107 bool operator<(const Expr& other) const = delete;
108
109 explicit Expr(ffi::ObjectPtr<ExprObj> node) : ffi::ObjectRef(std::move(node)) {
110 TVM_FFI_CHECK(defined(), ValueError) << "Expr expects a defined node";
111 }
112
114};
115
117class StagingExprObj : public ExprObj {
118 public:
119 static void RegisterReflection() { ffi::reflection::ObjectDef<StagingExprObj>(); }
120 static constexpr const uint32_t _type_child_slots = 4;
122};
123
129
136class OpaqueExprObj : public ExprObj {
137 public:
138 static void RegisterReflection() {
139 namespace refl = tvm::ffi::reflection;
140 refl::ObjectDef<OpaqueExprObj>();
141 }
142
143 static constexpr const uint32_t _type_child_slots = 2;
145};
146
148class OpaqueExpr : public Expr {
149 public:
150 explicit OpaqueExpr(ffi::ObjectPtr<OpaqueExprObj> node) : Expr(std::move(node)) {}
151
153};
154
160template <typename ExpectedType>
161class TypedExpr : public Expr {
162 public:
164 ExpectedType ty() const {
165 const auto* node = get();
166 TVM_FFI_DCHECK(node != nullptr);
167 const auto* ty_node = node->ExprObj::ty.template as<typename ExpectedType::ContainerType>();
168 TVM_FFI_DCHECK(ty_node != nullptr);
169 return ffi::GetRef<ExpectedType>(ty_node);
170 }
171
172 explicit TypedExpr(ffi::ObjectPtr<ExprObj> node) : Expr(std::move(node)) {}
173
175 static constexpr bool _type_container_is_exact = false;
176};
177
187class PrimExpr : public TypedExpr<PrimType> {
188 public:
189 using TypedExpr<PrimType>::ty;
190
195 TVM_TIRX_DLL PrimExpr(int32_t value); // NOLINT(*)
200 TVM_TIRX_DLL PrimExpr(float value); // NOLINT(*)
201
202 explicit PrimExpr(ffi::ObjectPtr<ExprObj> node) : TypedExpr<PrimType>(std::move(node)) {}
203
205 static constexpr bool _type_container_is_exact = false;
206};
207
219
220namespace ffi::detail {
221// Array members require storage availability before the expression traits below are complete.
222template <>
223inline constexpr bool storage_enabled_v<Expr> = true;
224template <>
225inline constexpr bool storage_enabled_v<PrimExpr> = true;
226} // namespace ffi::detail
227
229class ConstantObj : public ExprObj {
230 public:
231 static constexpr uint32_t _type_child_slots = 5;
232 static void RegisterReflection() { ffi::reflection::ObjectDef<ConstantObj>(); }
234};
235
237class Constant : public Expr {
238 public:
239 explicit Constant(ffi::ObjectPtr<ConstantObj> node) : Expr(std::move(node)) {}
240
242};
243
246 public:
247 ffi::Any value;
248
249 static void RegisterReflection() {
250 ffi::reflection::ObjectDef<GenericConstObj>().def_ro("value", &GenericConstObj::value);
251 }
253};
254
256class GenericConst : public Constant {
257 public:
258 TVM_TIRX_DLL GenericConst(Type ty, ffi::Any value, Location loc = UnknownLoc());
259
260 explicit GenericConst(ffi::ObjectPtr<GenericConstObj> node) : Constant(std::move(node)) {}
261
264};
265
267class StringImmObj : public ConstantObj {
268 public:
269 ffi::String value;
270
271 static void RegisterReflection() {
272 ffi::reflection::ObjectDef<StringImmObj>().def_ro("value", &StringImmObj::value);
273 }
275};
276
278class StringImm : public Constant {
279 public:
280 TVM_TIRX_DLL explicit StringImm(ffi::String value, Location loc = UnknownLoc());
281
282 explicit StringImm(ffi::ObjectPtr<StringImmObj> node) : Constant(std::move(node)) {}
283
286};
287
290 public:
291 DLDataType value;
292
293 static void RegisterReflection() {
294 ffi::reflection::ObjectDef<DataTypeImmObj>().def_ro("value", &DataTypeImmObj::value);
295 }
297};
298
300class DataTypeImm : public Constant {
301 public:
302 TVM_TIRX_DLL explicit DataTypeImm(DLDataType value, Location loc = UnknownLoc());
303
304 explicit DataTypeImm(ffi::ObjectPtr<DataTypeImmObj> node) : Constant(std::move(node)) {}
305
308};
309
314class IntImmObj : public ConstantObj {
315 public:
317 ffi::BigInt value;
318
319 static void RegisterReflection() {
320 namespace refl = tvm::ffi::reflection;
321 refl::ObjectDef<IntImmObj>().def_ro("value", &IntImmObj::value);
322 }
324};
325
331class IntImm : public PrimExpr {
332 public:
339 TVM_TIRX_DLL IntImm(PrimType value_ty, ffi::BigInt value, Location loc = UnknownLoc());
340
341 template <typename Enum, std::enable_if_t<std::is_enum_v<Enum>, int> = 0>
342 IntImm(PrimType value_ty, Enum value, Location loc = UnknownLoc())
343 : IntImm(std::move(value_ty), ffi::BigInt(static_cast<std::underlying_type_t<Enum>>(value)),
344 std::move(loc)) {}
345
351 static IntImm Bool(bool value, Location loc = UnknownLoc()) {
352 return IntImm(PrimType::Bool(), value, loc);
353 }
354
360 static IntImm Int32(ffi::BigInt value, Location loc = UnknownLoc()) {
361 return IntImm(PrimType::Int(32), std::move(value), loc);
362 }
363
364 template <typename Enum, std::enable_if_t<std::is_enum_v<Enum>, int> = 0>
365 static IntImm Int32(Enum value, Location loc = UnknownLoc()) {
366 return IntImm(PrimType::Int(32), value, std::move(loc));
367 }
368
374 static IntImm Int64(ffi::BigInt value, Location loc = UnknownLoc()) {
375 return IntImm(PrimType::Int(64), std::move(value), loc);
376 }
377
378 template <typename Enum, std::enable_if_t<std::is_enum_v<Enum>, int> = 0>
379 static IntImm Int64(Enum value, Location loc = UnknownLoc()) {
380 return IntImm(PrimType::Int(64), value, std::move(loc));
381 }
382
383 explicit IntImm(ffi::ObjectPtr<IntImmObj> node) : PrimExpr(std::move(node)) {}
384
386 static constexpr bool _type_container_is_exact = true;
388};
389
394class FloatImmObj : public ConstantObj {
395 public:
397 double value;
398
399 static void RegisterReflection() {
400 namespace refl = tvm::ffi::reflection;
401 refl::ObjectDef<FloatImmObj>().def_ro("value", &FloatImmObj::value);
402 }
404};
405
411class FloatImm : public PrimExpr {
412 public:
419 TVM_TIRX_DLL FloatImm(PrimType value_ty, double value, Location loc = UnknownLoc());
420
421 explicit FloatImm(ffi::ObjectPtr<FloatImmObj> node) : PrimExpr(std::move(node)) {}
422
424 static constexpr bool _type_container_is_exact = true;
426};
427
429class TupleObj : public ExprObj {
430 public:
432 ffi::Array<Expr> fields;
433
434 static void RegisterReflection() {
435 namespace refl = tvm::ffi::reflection;
436 refl::ObjectDef<TupleObj>().def_ro("fields", &TupleObj::fields);
437 }
438
440};
441
443class Tuple : public Expr {
444 public:
450 TVM_TIRX_DLL explicit Tuple(ffi::Array<Expr> fields, Location loc = UnknownLoc());
451
452 explicit Tuple(ffi::ObjectPtr<TupleObj> node) : Expr(std::move(node)) {}
453
456};
457
459class TupleGetItemObj : public ExprObj {
460 public:
461 explicit TupleGetItemObj(Expr tuple) : tuple(std::move(tuple)) {}
462 explicit TupleGetItemObj(ffi::UnsafeInit tag) : tuple(tag) {}
463
467 int index;
468
469 static void RegisterReflection() {
470 namespace refl = tvm::ffi::reflection;
471 refl::ObjectDef<TupleGetItemObj>()
472 .def_ro("tuple_value", &TupleGetItemObj::tuple)
473 .def_ro("index", &TupleGetItemObj::index);
474 }
475
477};
478
480class TupleGetItem : public Expr {
481 public:
489
490 explicit TupleGetItem(ffi::ObjectPtr<TupleGetItemObj> node) : Expr(std::move(node)) {}
491
494};
495
497class TensorLoadObj : public ExprObj {
498 public:
499 explicit TensorLoadObj(Expr source) : source(std::move(source)) {}
500 explicit TensorLoadObj(ffi::UnsafeInit tag) : source(tag) {}
501
505 ffi::Array<PrimExpr> indices;
506
507 static void RegisterReflection() {
508 namespace refl = tvm::ffi::reflection;
509 refl::ObjectDef<TensorLoadObj>()
510 .def_ro("source", &TensorLoadObj::source)
511 .def_ro("indices", &TensorLoadObj::indices);
512 }
513
515};
516
517namespace type_attr {
522inline constexpr ffi::reflection::AttrKey<
523 ffi::reflection::NativeFunctionView<PrimType(const TensorLoadObj*)>>
524 kTensorLoadInferType{"__tvm_tensor_load_infer_type__"};
525} // namespace type_attr
526
528class TensorLoad : public PrimExpr {
529 public:
531 TVM_TIRX_DLL TensorLoad(Expr source, ffi::Array<PrimExpr> indices, Location loc = UnknownLoc());
532
534 TVM_TIRX_DLL static TensorLoad Unchecked(PrimType ty, Expr source, ffi::Array<PrimExpr> indices,
535 Location loc = UnknownLoc());
536
538 TVM_TIRX_DLL explicit TensorLoad(ffi::ObjectPtr<TensorLoadObj> node);
539
541 static constexpr bool _type_container_is_exact = true;
543};
544
552class VarObj : public ExprObj {
553 public:
555 ffi::String name;
556
557 static void RegisterReflection() {
558 namespace refl = tvm::ffi::reflection;
559 refl::ObjectDef<VarObj>().def_ro("name", &VarObj::name, refl::AttachFieldFlag::SEqHashIgnore());
560 }
561
562 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindFreeVar;
563 static constexpr const uint32_t _type_child_slots = 1;
564 // VarObj reserves its sole child slot for the final relax::DataflowVarNode subtype, so its
565 // descendant type-index range cannot overflow.
566 static constexpr bool _type_child_slots_can_overflow = false;
568};
569
571class Var : public Expr {
572 public:
573 TVM_TIRX_DLL explicit Var(ffi::String name, Type ty, Location loc = UnknownLoc());
574
576 TVM_TIRX_DLL Var CopyWithName(const ffi::String& name) const;
577
579 TVM_TIRX_DLL Var CopyWithSuffix(const ffi::String& suffix) const;
580
583
586
587 explicit Var(ffi::ObjectPtr<VarObj> node) : Expr(std::move(node)) {}
588
590};
591
598class PrimVar : public PrimExpr {
599 public:
601 explicit PrimVar(ffi::String name, PrimType dtype = PrimType::Int(32),
602 Location loc = UnknownLoc())
603 : PrimExpr(Var(std::move(name), std::move(dtype), std::move(loc)).as_or_throw<PrimExpr>()) {}
604
606 explicit PrimVar(ffi::String name, Type type_annotation, Location loc = UnknownLoc())
607 : PrimExpr(Var(std::move(name), std::move(type_annotation), std::move(loc))
608 .as_or_throw<PrimExpr>()) {}
609
611 operator Var() const { return this->as_or_throw<Var>(); }
612
613 PrimVar CopyWithSuffix(const ffi::String& suffix) const {
614 return this->as_or_throw<Var>().CopyWithSuffix(suffix).as_or_throw<PrimVar>();
615 }
617 return this->as_or_throw<Var>().CopyWithDType(dtype).as_or_throw<PrimVar>();
618 }
619
620 explicit PrimVar(ffi::ObjectPtr<VarObj> node) : PrimExpr(std::move(node)) {}
621
623 static constexpr bool _type_container_is_exact = false;
624};
625
640 public:
642 ffi::Array<Var> vars;
645
646 explicit LambdaExprObj(ffi::UnsafeInit tag) : body(tag) {}
647 explicit LambdaExprObj(Expr body) : body(std::move(body)) {}
649 TVM_TIRX_DLL Expr Apply(const ffi::Array<Expr>& arguments) const;
650
651 static void RegisterReflection() {
652 namespace refl = tvm::ffi::reflection;
653 refl::ObjectDef<LambdaExprObj>()
654 .def_ro("vars", &LambdaExprObj::vars, refl::AttachFieldFlag::SEqHashDefSimple())
655 .def_ro("body", &LambdaExprObj::body);
656 }
657 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
659};
660
667
668class GlobalVar;
677class GlobalVarObj : public ExprObj {
678 public:
680 ffi::String name_hint;
681
682 static void RegisterReflection() {
683 namespace refl = tvm::ffi::reflection;
684 refl::ObjectDef<GlobalVarObj>().def_ro("name_hint", &GlobalVarObj::name_hint);
685 // A GlobalVar identifies a module-level symbol. Its type is derived from the
686 // corresponding function definition and is not part of the symbol identity.
687 refl::TypeAttrDef<GlobalVarObj>()
688 .def(tvm::ffi::type_attr::kSEqual.name, &GlobalVarObj::SEqual)
689 .def(tvm::ffi::type_attr::kSHash.name, &GlobalVarObj::SHash);
690 }
691
692 bool SEqual(const GlobalVarObj* other,
693 ffi::TypedFunction<bool(AnyView, AnyView, bool, AnyView)> equal) const {
694 return equal(name_hint, other->name_hint, false, "name_hint");
695 }
696
697 int64_t SHash(int64_t init_hash, ffi::TypedFunction<int64_t(AnyView, int64_t, bool)> hash) const {
698 return hash(name_hint, init_hash, false);
699 }
700
701 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindFreeVar;
703};
704
709class GlobalVar : public Expr {
710 public:
711 TVM_TIRX_DLL explicit GlobalVar(ffi::String name_hint, Location loc = UnknownLoc());
712
713 explicit GlobalVar(ffi::ObjectPtr<GlobalVarObj> node) : Expr(std::move(node)) {}
714
717};
718
722enum class CallEffectKind : int {
724 kExprAnnotation = 0,
729 kPure = 1,
733 kReadState = 2,
737 kUpdateState = 3,
746 kSpecialCallArg = 4,
750 kEmbedInfo = 5,
754 kControlJump = 6,
755};
756
757inline std::ostream& operator<<(std::ostream& os, CallEffectKind side_effect) {
758 switch (side_effect) {
760 return os << "kExprAnnotation";
761
763 return os << "kPure";
764
766 return os << "kReadState";
767
769 return os << "kUpdateState";
770
772 return os << "kSpecialCallArg";
773
775 return os << "kEmbedInfo";
776
778 return os << "kControlJump";
779
780 default:
781 TVM_FFI_THROW(InternalError) << "Unknown CallEffectKind: " << static_cast<int>(side_effect);
782 }
783}
784
788class CallObj : public ExprObj {
789 public:
790 explicit CallObj(Expr op) : op(std::move(op)) {}
791 explicit CallObj(ffi::UnsafeInit tag) : op(tag) {}
792
800
802 ffi::Array<Expr> args;
803
805 ffi::Optional<Attrs> attrs;
806
808 ffi::Array<Type> ty_args;
809
810 static void RegisterReflection() {
811 namespace refl = tvm::ffi::reflection;
812 refl::ObjectDef<CallObj>()
813 .def_ro("op", &CallObj::op)
814 .def_ro("args", &CallObj::args)
815 .def_ro("attrs", &CallObj::attrs)
816 .def_ro("ty_args", &CallObj::ty_args);
817 }
818
820};
821
825class Call : public Expr {
826 public:
830 TVM_TIRX_DLL Call(ffi::Optional<Type> ret_ty, Expr op, ffi::Array<Expr> args,
831 ffi::Optional<Attrs> attrs = std::nullopt,
832 ffi::Array<Type> ty_args = ffi::Array<Type>(), Location loc = UnknownLoc());
833
835 TVM_TIRX_DLL void Validate() const;
843
844 explicit Call(ffi::ObjectPtr<CallObj> node) : Expr(std::move(node)) {}
845
848};
849
850namespace op_attr {
852inline constexpr ffi::reflection::AttrKey<CallEffectKind> kCallEffectKind{"TCallEffectKind"};
853} // namespace op_attr
854
856class RangeObj : public ffi::Object {
857 public:
865 explicit RangeObj(ffi::UnsafeInit tag) : begin(tag), extent(tag) {}
868
869 static void RegisterReflection() {
870 namespace refl = tvm::ffi::reflection;
871 refl::ObjectDef<RangeObj>()
872 .def_ro("begin", &RangeObj::begin)
873 .def_ro("extent", &RangeObj::extent)
874 .def_ro("loc", &RangeObj::loc, refl::AttachFieldFlag::SEqHashIgnore());
875 }
876
877 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
878
880};
881
883class Range : public ffi::ObjectRef {
884 public:
903 Location loc = UnknownLoc());
911 // declare range.
912 explicit Range(ffi::UnsafeInit tag, ffi::ObjectPtr<RangeObj> n) : ffi::ObjectRef(tag) {
913 data_ = std::move(n);
914 }
915
917};
918
920class TensorRegionObj : public ExprObj {
921 public:
922 explicit TensorRegionObj(Expr source) : source(std::move(source)) {}
923 explicit TensorRegionObj(ffi::UnsafeInit tag) : source(tag) {}
924
928 ffi::Array<Range> region;
929
930 static void RegisterReflection() {
931 namespace refl = tvm::ffi::reflection;
932 // A region's source and symbolic source type form a definition pattern;
933 // ranges then refer to those same identities.
934 refl::ObjectDef<TensorRegionObj>()
935 .def_ro("source", &TensorRegionObj::source, refl::AttachFieldFlag::SEqHashDefPattern())
936 .def_ro("region", &TensorRegionObj::region);
937 }
938
939 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
941};
942
944class TensorRegion : public Expr {
945 public:
946 TVM_TIRX_DLL TensorRegion(Expr source, ffi::Array<Range> region, Type ty,
947 Location loc = UnknownLoc());
948
955
956 explicit TensorRegion(ffi::ObjectPtr<TensorRegionObj> node) : Expr(std::move(node)) {}
957
960};
961
977
978namespace ffi {
979
980template <>
981inline constexpr bool use_default_type_traits_v<tvm::StringImm> = false;
982
983template <>
984struct TypeTraits<tvm::StringImm>
985 : public ObjectRefWithFallbackTraitsBase<tvm::StringImm, ffi::String> {
986 TVM_FFI_INLINE static tvm::StringImm ConvertFallbackValue(ffi::String value) {
987 return tvm::StringImm(value);
988 }
989};
990
991template <>
992inline constexpr bool use_default_type_traits_v<IntImm> = false;
993
994template <>
995struct TypeTraits<IntImm> : public ObjectRefWithFallbackTraitsBase<IntImm, int64_t> {
996 TVM_FFI_INLINE static IntImm ConvertFallbackValue(int64_t value) {
997 auto value_ty =
998 (value > std::numeric_limits<int>::max() || value < std::numeric_limits<int>::min())
999 ? PrimType::Int(64)
1000 : PrimType::Int(32);
1001 return IntImm(value_ty, value);
1002 }
1003};
1004
1005template <>
1007
1008template <>
1009inline constexpr bool use_default_type_traits_v<FloatImm> = false;
1010
1011template <>
1012struct TypeTraits<FloatImm> : public ObjectRefWithFallbackTraitsBase<FloatImm, double> {
1013 TVM_FFI_INLINE static FloatImm ConvertFallbackValue(double value) {
1014 return FloatImm(PrimType::Float(32), value);
1015 }
1016};
1017
1018template <>
1020
1021template <>
1023
1024template <typename ExpectedType>
1025inline constexpr bool use_default_type_traits_v<TypedExpr<ExpectedType>> = false;
1026
1027template <typename ExpectedType>
1028struct TypeTraits<TypedExpr<ExpectedType>>
1029 : public ObjectRefTypeTraitsBase<TypedExpr<ExpectedType>> {
1030 TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
1031 if (src->type_index < TypeIndex::kTVMFFIStaticObjectBegin ||
1032 !detail::IsObjectInstance<ExprObj>(src->type_index)) {
1033 return false;
1034 }
1035 // Non-owning: this only reads `ty`, and the owning form's incref/decref pair costs two
1036 // atomics per check on a path every typed field assignment takes.
1037 const auto* expr = detail::ObjectUnsafe::RawObjectPtrFromUnowned<ExprObj>(src->v_obj);
1038 return detail::AnyUnsafe::CheckAnyViewStrict<ExpectedType>(expr->ty);
1039 }
1040
1041 TVM_FFI_INLINE static std::string GetMismatchTypeInfo(const TVMFFIAny* src) {
1042 if (src->type_index >= TypeIndex::kTVMFFIStaticObjectBegin && src->v_obj != nullptr &&
1043 detail::IsObjectInstance<ExprObj>(src->type_index)) {
1044 const auto* expr = detail::ObjectUnsafe::RawObjectPtrFromUnowned<ExprObj>(src->v_obj);
1045 if (expr->ty.defined()) {
1046 const auto* prim_ty = expr->ty.as<PrimTypeObj>();
1047 std::string ty =
1048 prim_ty ? std::string(ffi::DLDataTypeToString(prim_ty->dtype)) : expr->ty.GetTypeKey();
1049 return TypeIndexToTypeKey(src->type_index) + "[ty=" + ty + "]";
1050 }
1051 }
1052 return TypeTraitsBase::GetMismatchTypeInfo(src);
1053 }
1054};
1055
1056// Primitive expression arguments also accept bool, integer, and floating-point literals.
1057template <>
1058inline constexpr bool use_default_type_traits_v<PrimExpr> = false;
1059
1060template <>
1061struct TypeTraits<PrimExpr>
1062 : public ObjectRefWithFallbackTraitsBase<PrimExpr, StrictBool, int64_t, double> {
1063 TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
1064 return TypeTraits<TypedExpr<PrimType>>::CheckAnyStrict(src);
1065 }
1066
1067 TVM_FFI_INLINE static std::string TypeStr() { return "ir.PrimExpr"; }
1068
1069 TVM_FFI_INLINE static std::string GetMismatchTypeInfo(const TVMFFIAny* src) {
1070 return TypeTraits<TypedExpr<PrimType>>::GetMismatchTypeInfo(src);
1071 }
1072
1073 TVM_FFI_INLINE static PrimExpr ConvertFallbackValue(StrictBool value) {
1074 return IntImm::Bool(value);
1075 }
1076 TVM_FFI_INLINE static PrimExpr ConvertFallbackValue(int64_t value) {
1077 return TypeTraits<IntImm>::ConvertFallbackValue(value);
1078 }
1079 TVM_FFI_INLINE static PrimExpr ConvertFallbackValue(double value) {
1080 return TypeTraits<FloatImm>::ConvertFallbackValue(value);
1081 }
1082};
1083
1084// Generic expression arguments accept primitive/string literals and arrays of expressions.
1085template <>
1086inline constexpr bool use_default_type_traits_v<Expr> = false;
1087
1088template <>
1089struct TypeTraits<Expr> : public ObjectRefWithFallbackTraitsBase<Expr, PrimExpr, ffi::String> {
1090 TVM_FFI_INLINE static Expr ConvertFallbackValue(PrimExpr value) { return value; }
1091 TVM_FFI_INLINE static Expr ConvertFallbackValue(ffi::String value) {
1092 return StringImm(std::move(value));
1093 }
1094 static std::optional<Expr> TryCastFromAnyView(const TVMFFIAny* src) {
1095 if (auto value =
1096 ObjectRefWithFallbackTraitsBase<Expr, PrimExpr, ffi::String>::TryCastFromAnyView(src)) {
1097 return value;
1098 }
1099 if (auto fields = TypeTraits<ffi::Array<Expr>>::TryCastFromAnyView(src)) {
1100 return tvm::Tuple(std::move(*fields));
1101 }
1102 return std::nullopt;
1103 }
1104};
1105
1106template <>
1107inline constexpr bool use_default_type_traits_v<IntExpr> = false;
1108
1109template <>
1110struct TypeTraits<IntExpr> : public ObjectRefWithFallbackTraitsBase<IntExpr, int64_t> {
1111 using Base = ObjectRefWithFallbackTraitsBase<IntExpr, int64_t>;
1112
1113 TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
1114 if (!Base::CheckAnyStrict(src)) return false;
1115 const auto* expr = detail::ObjectUnsafe::RawObjectPtrFromUnowned<ExprObj>(src->v_obj);
1116 const auto* ty = expr->ty.as<PrimTypeObj>();
1117 return ty != nullptr && ty->dtype.lanes == 1 &&
1118 (ty->dtype.code == kDLInt || ty->dtype.code == kDLUInt);
1119 }
1120
1121 TVM_FFI_INLINE static IntExpr ConvertFallbackValue(int64_t value) {
1122 return TypeTraits<PrimExpr>::ConvertFallbackValue(value).as_or_throw<IntExpr>();
1123 }
1124
1125 TVM_FFI_INLINE static std::string TypeStr() { return "ir.IntExpr"; }
1126
1127 TVM_FFI_INLINE static std::string GetMismatchTypeInfo(const TVMFFIAny* src) {
1128 return TypeTraits<TypedExpr<PrimType>>::GetMismatchTypeInfo(src);
1129 }
1130};
1131
1132template <>
1133inline constexpr bool use_default_type_traits_v<PrimVar> = false;
1134
1135template <>
1136struct TypeTraits<PrimVar> : public ObjectRefTypeTraitsBase<PrimVar> {
1137 using Base = ObjectRefTypeTraitsBase<PrimVar>;
1138
1139 TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
1140 if (!Base::CheckAnyStrict(src)) return false;
1141 const auto* var = detail::ObjectUnsafe::RawObjectPtrFromUnowned<VarObj>(src->v_obj);
1142 return var->ty.as<PrimTypeObj>() != nullptr;
1143 }
1144};
1145
1146template <>
1147inline constexpr bool use_default_type_traits_v<TensorRegion> = false;
1148
1149template <>
1150struct TypeTraits<TensorRegion> : public ObjectRefWithFallbackTraitsBase<TensorRegion, TensorLoad> {
1151 TVM_FFI_INLINE static TensorRegion ConvertFallbackValue(TensorLoad value) {
1152 return TensorRegion::FromTensorLoad(std::move(value));
1153 }
1154};
1155
1156} // namespace ffi
1157
1159using SubscriptSlice = ffi::Array<ffi::Variant<
1160 ffi::Tuple<ffi::Optional<PrimExpr>, ffi::Optional<PrimExpr>, ffi::Optional<PrimExpr>>,
1161 PrimExpr>>;
1162
1163namespace type_attr {
1170inline constexpr ffi::reflection::AttrKey<
1171 ffi::TypedFunction<ffi::ObjectRef(Expr, SubscriptSlice, Location)>>
1172 kExprSubscriptRealizeByTy{"__tvm_expr_subscript_realize__"};
1173} // namespace type_attr
1174
1175} // namespace tvm
1176
1177/* \brief Allow tvm.Var and tvm.GlobalVar as keys in STL tables
1178 *
1179 * For most IR expressions, it would be ambiguous whether the
1180 * expression should follow reference equality or structural equality.
1181 * This is not the case for variables, which do not contain nested
1182 * internal structure, and are frequently used as keys in lookup
1183 * tables.
1184 *
1185 * Providing `std::hash` and `std::equal_to` specializations for
1186 * `tvm::Var` and `tvm::GlobalVar` allows them to be used as keys in STL tables. For
1187 * other IR expressions, the user must specify the type of equality
1188 * used (e.g. `std::unordered_set<T, StructuralHash, StructuralEqual>`
1189 * or `std::unordered_set<T, ffi::ObjectPtrHash, ffi::ObjectPtrEqual>`).
1190 */
1191template <>
1192struct std::hash<tvm::Var> {
1193 std::size_t operator()(const tvm::Var& var) const { return tvm::ffi::ObjectPtrHash()(var); }
1194};
1195
1196template <>
1197struct std::equal_to<tvm::Var> {
1198 bool operator()(const tvm::Var& var_a, const tvm::Var& var_b) const {
1199 return tvm::ffi::ObjectPtrEqual()(var_a, var_b);
1200 }
1201};
1202
1203template <>
1204struct std::hash<tvm::GlobalVar> {
1205 std::size_t operator()(const tvm::GlobalVar& var) const { return tvm::ffi::ObjectPtrHash()(var); }
1206};
1207
1208template <>
1209struct std::equal_to<tvm::GlobalVar> {
1210 bool operator()(const tvm::GlobalVar& var_a, const tvm::GlobalVar& var_b) const {
1211 return tvm::ffi::ObjectPtrEqual()(var_a, var_b);
1212 }
1213};
1214#endif // TVM_IR_EXPR_H_
IR/AST nodes for TVM types shared across IR variants.
#define TVM_TIRX_DLL
Definition base.h:55
Helpers for attribute objects.
Call corresponds to callable invocation.
Definition expr.h:788
Expr op
The operator/function being invoked.
Definition expr.h:799
ffi::Array< Type > ty_args
The type information arguments passed to the callee.
Definition expr.h:808
CallObj(Expr op)
Definition expr.h:790
ffi::Optional< Attrs > attrs
The additional attributes.
Definition expr.h:805
ffi::Array< Expr > args
The arguments of the call.
Definition expr.h:802
CallObj(ffi::UnsafeInit tag)
Definition expr.h:791
static void RegisterReflection()
Definition expr.h:810
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.Call", CallObj, ExprObj)
Managed reference to CallObj.
Definition expr.h:825
TVM_TIRX_DLL Call(ffi::Optional< Type > ret_ty, Expr op, ffi::Array< Expr > args, ffi::Optional< Attrs > attrs=std::nullopt, ffi::Array< Type > ty_args=ffi::Array< Type >(), Location loc=UnknownLoc())
Construct a Call, inferring only when ret_ty is absent. Construction preserves provisional IR; invoke...
static TVM_TIRX_DLL Type ReinferType(const CallObj *call)
Recompute a result type from the Call's current explicit inputs.
TVM_DEFINE_OBJECT_REF_COW_METHOD(CallObj)
Call(ffi::ObjectPtr< CallObj > node)
Definition expr.h:844
TVM_TIRX_DLL void Validate() const
Check this Call against its registered operator contract.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Call, Expr, CallObj)
Base node for literal constants.
Definition expr.h:229
static constexpr uint32_t _type_child_slots
Definition expr.h:231
TVM_FFI_DECLARE_OBJECT_INFO("ir.Constant", ConstantObj, ExprObj)
static void RegisterReflection()
Definition expr.h:232
Managed reference to a literal constant.
Definition expr.h:237
Constant(ffi::ObjectPtr< ConstantObj > node)
Definition expr.h:239
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Constant, Expr, ConstantObj)
A data type literal with AnyType as its expression type.
Definition expr.h:289
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.DataTypeImm", DataTypeImmObj, ConstantObj)
DLDataType value
Definition expr.h:291
static void RegisterReflection()
Definition expr.h:293
Managed reference to a data type literal.
Definition expr.h:300
TVM_TIRX_DLL DataTypeImm(DLDataType value, Location loc=UnknownLoc())
DataTypeImm(ffi::ObjectPtr< DataTypeImmObj > node)
Definition expr.h:304
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DataTypeImm, Constant, DataTypeImmObj)
TVM_DEFINE_OBJECT_REF_COW_METHOD(DataTypeImmObj)
Base type of all the expressions.
Definition expr.h:66
Type ty
The deduced or annotated type of the expression.
Definition expr.h:80
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition expr.h:91
static void RegisterReflection()
Definition expr.h:82
TVM_FFI_DECLARE_OBJECT_INFO("ir.Expr", ExprObj, ffi::Object)
Location loc
Location that points to the original source code. Reserved debug information.
Definition expr.h:72
static constexpr const uint32_t _type_child_slots
Definition expr.h:93
Managed reference to ExprObj.
Definition expr.h:101
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Expr, ffi::ObjectRef, ExprObj)
bool operator==(const Expr &other) const =delete
bool operator!=(const Expr &other) const =delete
Expr(ffi::ObjectPtr< ExprObj > node)
Definition expr.h:109
bool operator<(const Expr &other) const =delete
Constant floating point literals in the program.
Definition expr.h:394
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.FloatImm", FloatImmObj, ConstantObj)
static void RegisterReflection()
Definition expr.h:399
double value
The constant value content.
Definition expr.h:397
Managed reference class to FloatImmObj.
Definition expr.h:411
TVM_TIRX_DLL FloatImm(PrimType value_ty, double value, Location loc=UnknownLoc())
Constructor.
static constexpr bool _type_container_is_exact
Definition expr.h:424
FloatImm(ffi::ObjectPtr< FloatImmObj > node)
Definition expr.h:421
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(FloatImm, PrimExpr, FloatImmObj)
TVM_DEFINE_OBJECT_REF_COW_METHOD(FloatImmObj)
A constant whose payload is separate from its expression type.
Definition expr.h:245
ffi::Any value
Definition expr.h:247
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.GenericConst", GenericConstObj, ConstantObj)
static void RegisterReflection()
Definition expr.h:249
Managed reference to a generic constant.
Definition expr.h:256
TVM_TIRX_DLL GenericConst(Type ty, ffi::Any value, Location loc=UnknownLoc())
TVM_DEFINE_OBJECT_REF_COW_METHOD(GenericConstObj)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(GenericConst, Constant, GenericConstObj)
GenericConst(ffi::ObjectPtr< GenericConstObj > node)
Definition expr.h:260
Global variable that lives in the top-level module.
Definition expr.h:677
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.GlobalVar", GlobalVarObj, ExprObj)
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition expr.h:701
static void RegisterReflection()
Definition expr.h:682
bool SEqual(const GlobalVarObj *other, ffi::TypedFunction< bool(AnyView, AnyView, bool, AnyView)> equal) const
Definition expr.h:692
ffi::String name_hint
The name of the variable, this only acts as a hint.
Definition expr.h:680
int64_t SHash(int64_t init_hash, ffi::TypedFunction< int64_t(AnyView, int64_t, bool)> hash) const
Definition expr.h:697
Managed reference to GlobalVarObj.
Definition expr.h:709
GlobalVar(ffi::ObjectPtr< GlobalVarObj > node)
Definition expr.h:713
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(GlobalVar, Expr, GlobalVarObj)
TVM_DEFINE_OBJECT_REF_COW_METHOD(GlobalVarObj)
TVM_TIRX_DLL GlobalVar(ffi::String name_hint, Location loc=UnknownLoc())
View over a scalar signed or unsigned integer expression of any bit width.
Definition expr.h:214
static constexpr bool _type_container_is_exact
Definition expr.h:217
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IntExpr, PrimExpr, ExprObj)
Constant integer literals in the program.
Definition expr.h:314
ffi::BigInt value
the Internal value.
Definition expr.h:317
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.IntImm", IntImmObj, ConstantObj)
static void RegisterReflection()
Definition expr.h:319
Managed reference class to IntImmObj.
Definition expr.h:331
static constexpr bool _type_container_is_exact
Definition expr.h:386
static IntImm Int64(ffi::BigInt value, Location loc=UnknownLoc())
Construct a scalar int64 constant.
Definition expr.h:374
TVM_DEFINE_OBJECT_REF_COW_METHOD(IntImmObj)
IntImm(ffi::ObjectPtr< IntImmObj > node)
Definition expr.h:383
TVM_TIRX_DLL IntImm(PrimType value_ty, ffi::BigInt value, Location loc=UnknownLoc())
Constructor.
static IntImm Bool(bool value, Location loc=UnknownLoc())
Construct a scalar boolean constant.
Definition expr.h:351
IntImm(PrimType value_ty, Enum value, Location loc=UnknownLoc())
Definition expr.h:342
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IntImm, PrimExpr, IntImmObj)
static IntImm Int32(Enum value, Location loc=UnknownLoc())
Definition expr.h:365
static IntImm Int64(Enum value, Location loc=UnknownLoc())
Definition expr.h:379
static IntImm Int32(ffi::BigInt value, Location loc=UnknownLoc())
Construct a scalar int32 constant.
Definition expr.h:360
A typed staging expression representing a lambda computation.
Definition expr.h:639
LambdaExprObj(Expr body)
Definition expr.h:647
TVM_TIRX_DLL Expr Apply(const ffi::Array< Expr > &arguments) const
Simultaneously substitute arguments for the bound parameters.
LambdaExprObj(ffi::UnsafeInit tag)
Definition expr.h:646
static void RegisterReflection()
Definition expr.h:651
ffi::Array< Var > vars
Lambda-local parameter definitions.
Definition expr.h:642
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.LambdaExpr", LambdaExprObj, StagingExprObj)
Expr body
Computation over the parameters and captured expressions.
Definition expr.h:644
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition expr.h:657
Managed reference to a typed staging lambda.
Definition expr.h:662
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(LambdaExpr, StagingExpr, LambdaExprObj)
TVM_TIRX_DLL LambdaExpr(ffi::Array< Var > vars, Expr body, Location loc=UnknownLoc())
Non-nullable source-location metadata.
Definition location.h:91
Managed reference to the MissingTypeObj singleton.
Definition type.h:126
Base node for opaque construction-time expressions.
Definition expr.h:136
static void RegisterReflection()
Definition expr.h:138
TVM_FFI_DECLARE_OBJECT_INFO("ir.OpaqueExpr", OpaqueExprObj, ExprObj)
static constexpr const uint32_t _type_child_slots
Definition expr.h:143
Managed reference to OpaqueExprObj.
Definition expr.h:148
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(OpaqueExpr, Expr, OpaqueExprObj)
OpaqueExpr(ffi::ObjectPtr< OpaqueExprObj > node)
Definition expr.h:150
Typed reference/view over any Expr whose ExprObj::ty is PrimType.
Definition expr.h:187
static constexpr bool _type_container_is_exact
Definition expr.h:205
TVM_TIRX_DLL PrimExpr(int32_t value)
construct from integer.
TVM_TIRX_DLL PrimExpr(float value)
construct from float.
PrimExpr(ffi::ObjectPtr< ExprObj > node)
Definition expr.h:202
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(PrimExpr, TypedExpr< PrimType >, ExprObj)
Primitive data types used in the low-level IR.
Definition type.h:164
DLDataType dtype
The raw DLPack dtype represented by this primitive type.
Definition type.h:169
Definition type.h:182
static TVM_TIRX_DLL PrimType Bool(int lanes=1)
Construct a boolean type with fixed lanes.
static TVM_TIRX_DLL PrimType Int(int bits, int lanes=1)
Construct a signed integer type with fixed lanes.
Checked scalar view over a VarObj.
Definition expr.h:598
static constexpr bool _type_container_is_exact
Definition expr.h:623
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(PrimVar, PrimExpr, VarObj)
PrimVar(ffi::ObjectPtr< VarObj > node)
Definition expr.h:620
PrimVar(ffi::String name, PrimType dtype=PrimType::Int(32), Location loc=UnknownLoc())
Construct a scalar variable directly from a primitive type.
Definition expr.h:601
PrimVar CopyWithDType(PrimType dtype) const
Definition expr.h:616
PrimVar CopyWithSuffix(const ffi::String &suffix) const
Definition expr.h:613
PrimVar(ffi::String name, Type type_annotation, Location loc=UnknownLoc())
Construct a scalar variable directly from a checked type annotation.
Definition expr.h:606
range over one dimension
Definition expr.h:856
RangeObj(PrimExpr begin, PrimExpr extent, Location loc=UnknownLoc())
Definition expr.h:866
PrimExpr extent
Extent of the range.
Definition expr.h:861
RangeObj(ffi::UnsafeInit tag)
constructor
Definition expr.h:865
Location loc
the location of this range in the source
Definition expr.h:863
PrimExpr begin
Inclusive beginning of the range.
Definition expr.h:859
static void RegisterReflection()
Definition expr.h:869
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition expr.h:877
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.Range", RangeObj, ffi::Object)
Range container
Definition expr.h:883
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Range, ffi::ObjectRef, RangeObj)
static TVM_TIRX_DLL PrimExpr GetExtentFromBeginEnd(const PrimExpr &begin, const PrimExpr &end)
Extract the interval width from its endpoints without an analyzer.
Range(ffi::UnsafeInit tag, ffi::ObjectPtr< RangeObj > n)
Definition expr.h:912
static TVM_TIRX_DLL Range FromMinExtent(PrimExpr min, PrimExpr extent, Location loc=UnknownLoc())
construct a new range with min and extent The corresponding constructor is removed,...
TVM_TIRX_DLL Range(PrimExpr begin, PrimExpr end, Location loc=UnknownLoc())
constructor by begin and end
Base node for traversable expressions eliminated during compilation.
Definition expr.h:117
static void RegisterReflection()
Definition expr.h:119
TVM_FFI_DECLARE_OBJECT_INFO("ir.StagingExpr", StagingExprObj, ExprObj)
static constexpr const uint32_t _type_child_slots
Definition expr.h:120
Managed reference to a staging expression.
Definition expr.h:125
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(StagingExpr, Expr, StagingExprObj)
A string literal with shared semantic StringType.
Definition expr.h:267
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.StringImm", StringImmObj, ConstantObj)
ffi::String value
Definition expr.h:269
static void RegisterReflection()
Definition expr.h:271
Managed reference to a string literal.
Definition expr.h:278
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(StringImm, Constant, StringImmObj)
StringImm(ffi::ObjectPtr< StringImmObj > node)
Definition expr.h:282
TVM_DEFINE_OBJECT_REF_COW_METHOD(StringImmObj)
TVM_TIRX_DLL StringImm(ffi::String value, Location loc=UnknownLoc())
Load a value from an indexed expression source.
Definition expr.h:497
TensorLoadObj(ffi::UnsafeInit tag)
Definition expr.h:500
Expr source
The indexed source expression.
Definition expr.h:503
static void RegisterReflection()
Definition expr.h:507
TensorLoadObj(Expr source)
Definition expr.h:499
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.TensorLoad", TensorLoadObj, ExprObj)
ffi::Array< PrimExpr > indices
The indices at which the source is loaded.
Definition expr.h:505
Managed reference to TensorLoadObj.
Definition expr.h:528
TVM_TIRX_DLL TensorLoad(Expr source, ffi::Array< PrimExpr > indices, Location loc=UnknownLoc())
Construct a load using the inference hook on the runtime class of source->ty.
static constexpr bool _type_container_is_exact
Definition expr.h:541
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TensorLoad, PrimExpr, TensorLoadObj)
TVM_DEFINE_OBJECT_REF_COW_METHOD(TensorLoadObj)
TVM_TIRX_DLL TensorLoad(ffi::ObjectPtr< TensorLoadObj > node)
Wrap an existing node, requiring a concrete primitive result type.
static TVM_TIRX_DLL TensorLoad Unchecked(PrimType ty, Expr source, ffi::Array< PrimExpr > indices, Location loc=UnknownLoc())
Construct with an explicit primitive result type, bypassing source-type inference.
A region of an indexed expression source.
Definition expr.h:920
ffi::Array< Range > region
The ranges selected from the source.
Definition expr.h:928
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.TensorRegion", TensorRegionObj, ExprObj)
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition expr.h:939
Expr source
The indexed source expression.
Definition expr.h:926
static void RegisterReflection()
Definition expr.h:930
TensorRegionObj(ffi::UnsafeInit tag)
Definition expr.h:923
TensorRegionObj(Expr source)
Definition expr.h:922
Managed reference to TensorRegionObj.
Definition expr.h:944
TVM_TIRX_DLL TensorRegion(Expr source, ffi::Array< Range > region, Type ty, Location loc=UnknownLoc())
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TensorRegion, Expr, TensorRegionObj)
static TVM_TIRX_DLL TensorRegion FromTensorLoad(TensorLoad value)
Convert a tensor load to a region with unit extent at each index.
TVM_DEFINE_OBJECT_REF_COW_METHOD(TensorRegionObj)
TensorRegion(ffi::ObjectPtr< TensorRegionObj > node)
Definition expr.h:956
Get the index-th field out of a tuple.
Definition expr.h:459
TupleGetItemObj(ffi::UnsafeInit tag)
Definition expr.h:462
int index
The field index.
Definition expr.h:467
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.TupleGetItem", TupleGetItemObj, ExprObj)
Expr tuple
The tuple expression.
Definition expr.h:465
TupleGetItemObj(Expr tuple)
Definition expr.h:461
static void RegisterReflection()
Definition expr.h:469
Managed reference to TupleGetItemObj.
Definition expr.h:480
TVM_TIRX_DLL TupleGetItem(Expr tuple, int index, Location loc=UnknownLoc())
Construct a tuple field projection.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TupleGetItem, Expr, TupleGetItemObj)
TupleGetItem(ffi::ObjectPtr< TupleGetItemObj > node)
Definition expr.h:490
TVM_DEFINE_OBJECT_REF_COW_METHOD(TupleGetItemObj)
Tuple container.
Definition expr.h:429
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.Tuple", TupleObj, ExprObj)
ffi::Array< Expr > fields
The fields of the tuple.
Definition expr.h:432
static void RegisterReflection()
Definition expr.h:434
Managed reference to TupleObj.
Definition expr.h:443
TVM_TIRX_DLL Tuple(ffi::Array< Expr > fields, Location loc=UnknownLoc())
Construct a tuple from its fields.
Tuple(ffi::ObjectPtr< TupleObj > node)
Definition expr.h:452
TVM_DEFINE_OBJECT_REF_COW_METHOD(TupleObj)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Tuple, Expr, TupleObj)
Managed reference to TypeObj.
Definition type.h:75
Typed reference/view over an expression whose result type is a specific Type subtype.
Definition expr.h:161
static constexpr bool _type_container_is_exact
Definition expr.h:175
ExpectedType ty() const
Definition expr.h:164
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TypedExpr, Expr, ExprObj)
TypedExpr(ffi::ObjectPtr< ExprObj > node)
Definition expr.h:172
Reference to the shared immutable unknown location.
Definition location.h:105
A local variable in the IR.
Definition expr.h:552
static void RegisterReflection()
Definition expr.h:557
static constexpr bool _type_child_slots_can_overflow
Definition expr.h:566
static constexpr const uint32_t _type_child_slots
Definition expr.h:563
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition expr.h:562
ffi::String name
The variable name.
Definition expr.h:555
TVM_FFI_DECLARE_OBJECT_INFO("ir.Var", VarObj, ExprObj)
Managed reference to VarObj.
Definition expr.h:571
TVM_TIRX_DLL Var CopyWithDType(PrimType dtype) const
Return a fresh ordinary Var with a new primitive type.
TVM_TIRX_DLL Var CopyWithType(Type type) const
Return a fresh ordinary Var with a new type, retaining its metadata.
TVM_TIRX_DLL Var CopyWithSuffix(const ffi::String &suffix) const
Return a fresh ordinary Var with a suffix appended to its name.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Var, Expr, VarObj)
TVM_TIRX_DLL Var(ffi::String name, Type ty, Location loc=UnknownLoc())
Var(ffi::ObjectPtr< VarObj > node)
Definition expr.h:587
TVM_TIRX_DLL Var CopyWithName(const ffi::String &name) const
Return a fresh ordinary Var with the same type and a new name.
Copy-on-write helper macro for IR ffi::ObjectRef types.
Source locations and source-text lookup.
constexpr bool storage_enabled_v< PrimExpr >
Definition expr.h:225
constexpr bool storage_enabled_v< Expr >
Definition expr.h:223
constexpr bool use_default_type_traits_v< PrimVar >
Definition expr.h:1133
constexpr bool use_default_type_traits_v< Expr >
Definition expr.h:1086
constexpr bool use_default_type_traits_v< PrimExpr >
Definition expr.h:1058
constexpr bool use_default_type_traits_v< TensorRegion >
Definition expr.h:1147
constexpr bool object_ref_contains_v< PrimExpr, IntImmObj >
Definition expr.h:1006
constexpr bool object_ref_contains_v< PrimExpr, FloatImmObj >
Definition expr.h:1019
constexpr bool use_default_type_traits_v< IntExpr >
Definition expr.h:1107
constexpr bool use_default_type_traits_v< FloatImm >
Definition expr.h:1009
constexpr bool object_ref_contains_v< PrimExpr, TensorLoadObj >
Definition expr.h:1022
constexpr bool use_default_type_traits_v< IntImm >
Definition expr.h:992
constexpr ffi::reflection::AttrKey< CallEffectKind > kCallEffectKind
The runtime effect of an operator call.
Definition expr.h:852
PrimVar var(std::string name_hint, PrimType t=PrimType::Int(32))
Construct a new Var expression.
constexpr ffi::reflection::AttrKey< ffi::TypedFunction< ffi::ObjectRef(Expr, SubscriptSlice, Location)> > kExprSubscriptRealizeByTy
Realize a subscript of an expression, dispatched on the type of the expression.
Definition expr.h:1172
constexpr ffi::reflection::AttrKey< ffi::reflection::NativeFunctionView< PrimType(const TensorLoadObj *)> > kTensorLoadInferType
Infer a load's primitive result type from its populated source, indices and location.
Definition expr.h:524
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
TVM_TIRX_DLL CallEffectKind SideEffect(const Expr &expr)
Analyze the runtime effects of an expression.
std::ostream & operator<<(std::ostream &os, CallEffectKind side_effect)
Definition expr.h:757
TVM_TIRX_DLL PrimExpr equal(PrimExpr a, PrimExpr b, Location loc=UnknownLoc())
equal
CallEffectKind
The effect type of the call.
Definition expr.h:722
@ kControlJump
Function that changes control flow.
@ kOpaque
Opaque function, cannot make any assumption.
@ kExprAnnotation
Function corresponds to an annotation(e.g. likely) and can translate to identity.
@ kPure
Pure function that do not interacts with any external state.
@ kReadState
Function's that may read from states(e.g. RAM)
@ kEmbedInfo
Embed opaque information in the Expr, cannot be codegen.
@ kSpecialCallArg
Special intrinsic to annotate call arguments info only valid as a direct argument to a call.
@ kUpdateState
Function that may read/write from states(e.g. RAM).
ffi::Array< ffi::Variant< ffi::Tuple< ffi::Optional< PrimExpr >, ffi::Optional< PrimExpr >, ffi::Optional< PrimExpr > >, PrimExpr > > SubscriptSlice
Expression subscripts: point indices or (start, stop, step) slices.
Definition expr.h:1161
PrimExpr min(PrimExpr source, ffi::Array< s_tir::IterVar > axis, ffi::Array< PrimExpr > init={}, Location loc=UnknownLoc())
max of source expression over axis
static TVM_FFI_INLINE Expr ConvertFallbackValue(PrimExpr value)
Definition expr.h:1090
static std::optional< Expr > TryCastFromAnyView(const TVMFFIAny *src)
Definition expr.h:1094
static TVM_FFI_INLINE Expr ConvertFallbackValue(ffi::String value)
Definition expr.h:1091
static TVM_FFI_INLINE FloatImm ConvertFallbackValue(double value)
Definition expr.h:1013
static TVM_FFI_INLINE IntExpr ConvertFallbackValue(int64_t value)
Definition expr.h:1121
static TVM_FFI_INLINE std::string GetMismatchTypeInfo(const TVMFFIAny *src)
Definition expr.h:1127
static TVM_FFI_INLINE std::string TypeStr()
Definition expr.h:1125
ObjectRefWithFallbackTraitsBase< IntExpr, int64_t > Base
Definition expr.h:1111
static TVM_FFI_INLINE bool CheckAnyStrict(const TVMFFIAny *src)
Definition expr.h:1113
static TVM_FFI_INLINE IntImm ConvertFallbackValue(int64_t value)
Definition expr.h:996
static TVM_FFI_INLINE PrimExpr ConvertFallbackValue(int64_t value)
Definition expr.h:1076
static TVM_FFI_INLINE std::string GetMismatchTypeInfo(const TVMFFIAny *src)
Definition expr.h:1069
static TVM_FFI_INLINE PrimExpr ConvertFallbackValue(double value)
Definition expr.h:1079
static TVM_FFI_INLINE bool CheckAnyStrict(const TVMFFIAny *src)
Definition expr.h:1063
static TVM_FFI_INLINE PrimExpr ConvertFallbackValue(StrictBool value)
Definition expr.h:1073
static TVM_FFI_INLINE std::string TypeStr()
Definition expr.h:1067
static TVM_FFI_INLINE bool CheckAnyStrict(const TVMFFIAny *src)
Definition expr.h:1139
ObjectRefTypeTraitsBase< PrimVar > Base
Definition expr.h:1137
static TVM_FFI_INLINE TensorRegion ConvertFallbackValue(TensorLoad value)
Definition expr.h:1151
static TVM_FFI_INLINE bool CheckAnyStrict(const TVMFFIAny *src)
Definition expr.h:1030
static TVM_FFI_INLINE std::string GetMismatchTypeInfo(const TVMFFIAny *src)
Definition expr.h:1041
static TVM_FFI_INLINE tvm::StringImm ConvertFallbackValue(ffi::String value)
Definition expr.h:986