28#ifndef TVM_RELAX_NESTED_MSG_H_
29#define TVM_RELAX_NESTED_MSG_H_
31#include <tvm/ffi/container/array.h>
32#include <tvm/ffi/optional.h>
138 : data_(std::move(other)) {}
140 data_ = std::move(other);
148 data_ = std::move(other);
165 bool operator==(std::nullptr_t)
const {
return data_ ==
nullptr; }
166 bool operator!=(std::nullptr_t)
const {
return data_ !=
nullptr; }
170 return data_.type_index() != ffi::TypeIndex::kTVMFFINone &&
171 data_.type_index() != ffi::TypeIndex::kTVMFFIArray;
175 bool IsNull()
const {
return data_.type_index() == ffi::TypeIndex::kTVMFFINone; }
178 bool IsNested()
const {
return data_.type_index() == ffi::TypeIndex::kTVMFFIArray; }
186 return ffi::detail::AnyUnsafe::CopyFromAnyViewAfterCheck<T>(data_);
194 return ffi::detail::AnyUnsafe::CopyFromAnyViewAfterCheck<ffi::Array<NestedMsg<T>,
void>>(data_);
200 explicit NestedMsg(ffi::Any data) : data_(data) {}
201 template <
typename,
typename>
212template <
typename T,
typename FType>
214 if (msg ==
nullptr)
return;
233template <
typename T,
typename FType>
241 ffi::Array<NestedMsg<T>> arr_lhs = lhs.
NestedArray();
242 ffi::Array<NestedMsg<T>> arr_rhs = rhs.
NestedArray();
243 if (arr_lhs.size() != arr_rhs.size())
return false;
244 for (
size_t i = 0; i < arr_lhs.size(); ++i) {
245 if (!
Equal(arr_lhs[i], arr_rhs[i], fequal))
return false;
264template <
typename T,
typename FType>
266 if (
auto* tuple = expr.as<
TupleObj>()) {
267 ffi::Array<NestedMsg<T>> res;
268 res.reserve(tuple->fields.size());
269 for (
Expr x : tuple->fields) {
270 res.push_back(MapToNestedMsg<T, FType>(x, fmapleaf));
274 return fmapleaf(expr);
291template <
typename T,
typename FType>
294 ffi::Array<NestedMsg<T>> res;
295 res.reserve(tuple->fields.size());
296 for (
Type x : tuple->fields) {
297 res.push_back(MapToNestedMsg<T, FType>(x, fmapleaf));
319template <
typename T,
typename FType>
323 ffi::Array<NestedMsg<T>> res;
324 res.reserve(tuple->fields.size());
325 for (
size_t i = 0; i < tuple->fields.size(); ++i) {
327 if (
const auto* expr_tuple = expr.as<
TupleObj>()) {
328 return expr_tuple->fields[i];
332 res.push_back(MapToNestedMsgByType<T, FType>(field, fmapleaf));
336 return fmapleaf(expr);
357template <
typename TargetType,
typename T,
typename FMapLeaf,
typename FCombine>
360 return fmapleaf(std::nullopt);
361 }
else if (msg.
IsLeaf()) {
366 ffi::Array<TargetType> subexpr;
367 subexpr.reserve(arr.size());
368 for (
size_t i = 0; i < arr.size(); ++i) {
369 subexpr.push_back(NestedMsgTo<TargetType>(arr[i], fmapleaf, fcombine));
371 return fcombine(subexpr);
387template <
typename T,
typename FType>
389 return NestedMsgTo<Expr>(msg, fmapleaf, [](ffi::Array<Expr> arr) {
390 ffi::Optional<Expr> simplified_tuple;
391 bool simplified_flag =
false;
392 if (arr.size() >= 1) {
393 simplified_flag = true;
394 for (size_t i = 0; i < arr.size() && simplified_flag; ++i) {
395 auto* node = arr[i].as<TupleGetItemObj>();
396 if (node == nullptr || node->index != static_cast<int>(i)) {
397 simplified_flag = false;
399 if (simplified_tuple.has_value()) {
400 simplified_flag &= simplified_tuple.value().same_as(node->tuple);
402 simplified_tuple = node->tuple;
403 TVM_FFI_ICHECK(simplified_tuple.has_value());
408 return simplified_flag ? simplified_tuple.value() :
Tuple(arr);
428template <
typename T,
typename FType>
430 if (lhs.
IsNull())
return rhs;
431 if (rhs.
IsNull())
return lhs;
434 TVM_FFI_ICHECK(rhs.
IsLeaf()) <<
"Cannot combine leaf with nested";
438 TVM_FFI_ICHECK(rhs.
IsNested()) <<
"Cannot combine leaf with nested";
439 ffi::Array<NestedMsg<T>> arr_lhs = lhs.
NestedArray();
440 ffi::Array<NestedMsg<T>> arr_rhs = rhs.
NestedArray();
441 TVM_FFI_ICHECK_EQ(arr_lhs.size(), arr_rhs.size())
442 <<
"Cannot combine two nested array with different sizes";
443 ffi::Array<NestedMsg<T>> res;
444 res.reserve(arr_lhs.size());
445 for (
size_t i = 0; i < arr_lhs.size(); ++i) {
446 res.push_back(CombineNestedMsg<T, FType>(arr_lhs[i], arr_rhs[i], fcombine));
460template <
typename T,
typename FType>
464 }
else if (msg.
IsLeaf()) {
469 ffi::Array<NestedMsg<T>> res;
470 res.reserve(arr.size());
471 for (
int i = 0; i < static_cast<int>(arr.size()); ++i) {
491template <
typename T,
typename FType>
493 if (
auto* tuple = expr.as<
TupleObj>()) {
494 TVM_FFI_ICHECK(msg.
IsNested()) <<
"Expected nested to match tuple";
496 TVM_FFI_ICHECK_EQ(arr.size(), tuple->fields.size())
497 <<
"Expected nested array size to match tuple size";
498 for (
size_t i = 0; i < arr.size(); ++i) {
502 fvisitleaf(expr, msg);
520template <
typename T, std::
size_t N,
typename FType>
524 std::array<ffi::Array<NestedMsg<T>>, N> msg_arrays;
525 for (
size_t i = 0; i < N; ++i) {
526 TVM_FFI_ICHECK(msgs[i].IsNested()) <<
"Expected nested to match tuple";
527 msg_arrays[i] = msgs[i].NestedArray();
530 ffi::Array<Expr> fields;
531 fields.reserve(tuple->fields.size());
532 for (
size_t i = 0; i < tuple->fields.size(); ++i) {
534 if (
const auto* expr_tuple = expr.as<
TupleObj>()) {
535 return expr_tuple->fields[i];
539 std::array<NestedMsg<T>, N> sub_msgs;
540 for (
size_t j = 0; j < N; ++j) {
541 sub_msgs[j] = msg_arrays[j][i];
544 same &= (fields.back().same_as(field));
546 return same ? expr :
Tuple(fields);
548 for (
const auto& msg : msgs) {
549 TVM_FFI_ICHECK(msg.IsLeaf()) <<
"Expected leaf to match non-tuple";
551 return ftransleaf(expr, msgs);
569template <
typename T, std::
size_t N,
typename FType>
572 std::array<ffi::Array<NestedMsg<T>>, N> msg_arrays;
573 for (
size_t i = 0; i < N; ++i) {
574 TVM_FFI_ICHECK(msgs[i].IsNested()) <<
"Expected nested to match tuple";
575 msg_arrays[i] = msgs[i].NestedArray();
578 ffi::Array<Type> fields;
579 fields.reserve(tuple->fields.size());
580 for (
size_t i = 0; i < tuple->fields.size(); ++i) {
581 Type field = tuple->fields[i];
582 std::array<NestedMsg<T>, N> sub_msgs;
583 for (
size_t j = 0; j < N; ++j) {
584 sub_msgs[j] = msg_arrays[j][i];
587 same &= (fields.back().same_as(field));
591 for (
const auto& msg : msgs) {
592 TVM_FFI_ICHECK(msg.IsLeaf()) <<
"Expected leaf to match non-tuple";
594 return ftransleaf(ty, msgs);
603inline constexpr bool use_default_type_traits_v<relax::NestedMsg<T>> =
false;
606struct TypeTraits<relax::NestedMsg<T>> :
public TypeTraitsBase {
607 TVM_FFI_INLINE
static void CopyToAnyView(
const relax::NestedMsg<T>& src, TVMFFIAny* result) {
608 *result = ffi::AnyView(src.data_).CopyToTVMFFIAny();
612 *result = details::AnyUnsafe::MoveAnyToTVMFFIAny(std::move(src.data_));
615 TVM_FFI_INLINE
static std::string GetMismatchTypeInfo(
const TVMFFIAny* src) {
616 return TypeTraitsBase::GetMismatchTypeInfo(src);
619 static bool CheckAnyStrict(
const TVMFFIAny* src) {
620 if (src->type_index == TypeIndex::kTVMFFINone) {
623 if (TypeTraits<T>::CheckAnyStrict(src)) {
626 if (src->type_index == TypeIndex::kTVMFFIArray) {
627 const ffi::ArrayObj* array =
reinterpret_cast<const ffi::ArrayObj*
>(src->v_obj);
628 for (
size_t i = 0; i < array->size(); ++i) {
629 const Any& any_v = (*array)[i];
636 TVM_FFI_INLINE
static relax::NestedMsg<T> CopyFromAnyViewAfterCheck(
const TVMFFIAny* src) {
644 static std::optional<relax::NestedMsg<T>> TryCastFromAnyView(
const TVMFFIAny* src) {
645 if (CheckAnyStrict(src)) {
646 return CopyFromAnyViewAfterCheck(src);
649 if (src->type_index == TypeIndex::kTVMFFINone) {
652 if (
auto opt_value = TypeTraits<T>::TryCastFromAnyView(src)) {
655 if (src->type_index == TypeIndex::kTVMFFIArray) {
656 const ArrayObj* n =
reinterpret_cast<const ArrayObj*
>(src->v_obj);
657 ffi::Array<relax::NestedMsg<T>> result;
658 result.reserve(n->size());
659 for (
size_t i = 0; i < n->size(); i++) {
660 const Any& any_v = (*n)[i];
662 result.push_back(*std::move(opt_v));
672 TVM_FFI_INLINE
static std::string TypeStr() {
673 return "NestedMsg<" + details::Type2Str<T>::v() +
">";
676 TVM_FFI_INLINE
static std::string TypeSchema() {
677 std::ostringstream oss;
678 oss << R
"({"type":"NestedMsg","args":[)";
679 oss << details::TypeSchema<T>::v();
Managed reference to ExprObj.
Definition expr.h:101
Managed reference to TupleGetItemObj.
Definition expr.h:480
Tuple container.
Definition expr.h:429
The type of tuple values.
Definition type.h:475
Managed reference to TupleTypeObj.
Definition type.h:493
Managed reference to TupleObj.
Definition expr.h:443
Managed reference to TypeObj.
Definition type.h:75
Container that stores possibly nested message with leaf message type T.
Definition nested_msg.h:119
NestedMsg< T > & operator=(ffi::Array< NestedMsg< T >, void > other)
Definition nested_msg.h:147
NestedMsg(T other)
Definition nested_msg.h:137
friend struct ffi::TypeTraits
Definition nested_msg.h:202
NestedMsg(std::nullptr_t)
Definition nested_msg.h:131
NestedMsg(std::initializer_list< NestedMsg< T > > other)
Definition nested_msg.h:153
NestedMsg(const NestedMsg< T > &)=default
ffi::Array< NestedMsg< T >, void > NestedArray() const
Definition nested_msg.h:193
NestedMsg< T > & operator=(const NestedMsg< T > &)=default
NestedMsg(ffi::Array< NestedMsg< T >, void > other)
Definition nested_msg.h:144
NestedMsg< T > & operator=(int val)=delete
T LeafValue() const
Definition nested_msg.h:184
NestedMsg(int val)=delete
NestedMsg< T > & operator=(std::initializer_list< NestedMsg< T > > other)
Definition nested_msg.h:155
bool IsNull() const
Definition nested_msg.h:175
NestedMsg< T > & operator=(NestedMsg< T > &&)=default
bool IsNested() const
Definition nested_msg.h:178
bool operator==(std::nullptr_t) const
Definition nested_msg.h:165
NestedMsg< T > & operator=(T other)
Definition nested_msg.h:139
NestedMsg(NestedMsg< T > &&)=default
NestedMsg< T > & operator=(std::nullptr_t)
Definition nested_msg.h:132
NestedMsg(std::nullopt_t)
Nullopt handling.
Definition nested_msg.h:128
bool IsLeaf() const
Definition nested_msg.h:169
bool operator!=(std::nullptr_t) const
Definition nested_msg.h:166
Relax types, including the richer dependent Relax type nodes.
NestedMsg< T > CombineNestedMsg(NestedMsg< T > lhs, NestedMsg< T > rhs, FType fcombine)
Recursively combine two nested message into one.
Definition nested_msg.h:429
NestedMsg< T > MapNestedMsg(NestedMsg< T > msg, FType fmapleaf)
Recursively map a nested message to another one, with leaf mapped by the input fmapleaf.
Definition nested_msg.h:461
Expr TransformTupleLeaf(Expr expr, std::array< NestedMsg< T >, N > msgs, FType ftransleaf)
Recursively transform the tuple structure in expr and msgs along with it.
Definition nested_msg.h:521
TargetType NestedMsgTo(NestedMsg< T > msg, FMapLeaf fmapleaf, FCombine fcombine)
Map nested message back to TargetType.
Definition nested_msg.h:358
Type GetType(const Expr &expr)
Get the underlying Relax type of expr.
Definition type.h:383
NestedMsg< T > MapToNestedMsgByType(Expr expr, FType fmapleaf)
Map expr with possible nested-tuple to nested message.
Definition nested_msg.h:320
NestedMsg< T > MapToNestedMsg(Expr expr, FType fmapleaf)
Map expr with possible nested-tuple to nested message.
Definition nested_msg.h:265
void ForEachLeaf(const NestedMsg< T > &msg, FType fvisit)
Apply fvisit for each leaf elements in the nested message.
Definition nested_msg.h:213
void DecomposeNestedMsg(Expr expr, NestedMsg< T > msg, FType fvisitleaf)
Recursively decompose the tuple structure in expr and msg along with it.
Definition nested_msg.h:492
Expr NestedMsgToExpr(NestedMsg< T > msg, FType fmapleaf)
Map nested message back to the expr.
Definition nested_msg.h:388
bool Equal(const NestedMsg< T > &lhs, const NestedMsg< T > &rhs, FType fequal)
Recursively compare two nested messages.
Definition nested_msg.h:234
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41