23 #ifndef TVM_NODE_CAST_H_
24 #define TVM_NODE_CAST_H_
26 #include <tvm/ffi/any.h>
27 #include <tvm/ffi/cast.h>
28 #include <tvm/ffi/dtype.h>
29 #include <tvm/ffi/error.h>
30 #include <tvm/ffi/object.h>
31 #include <tvm/ffi/optional.h>
45 template <
typename SubRef,
typename BaseRef,
46 typename = std::enable_if_t<std::is_base_of_v<ffi::ObjectRef, BaseRef>>>
48 using ContainerType =
typename SubRef::ContainerType;
50 if (!ref->template IsInstance<ContainerType>()) {
51 TVM_FFI_THROW(TypeError) <<
"Downcast from " << ref->GetTypeKey() <<
" to "
52 << SubRef::ContainerType::_type_key <<
" failed.";
54 return ffi::details::ObjectUnsafe::ObjectRefFromObjectPtr<SubRef>(
55 ffi::details::ObjectUnsafe::ObjectPtrFromObjectRef<ffi::Object>(std::move(ref)));
57 if constexpr (ffi::is_optional_type_v<SubRef> || SubRef::_type_is_nullable) {
58 return ffi::details::ObjectUnsafe::ObjectRefFromObjectPtr<SubRef>(
nullptr);
60 TVM_FFI_THROW(TypeError) <<
"Downcast from undefined(nullptr) to `" << ContainerType::_type_key
61 <<
"` is not allowed. Use Downcast<ffi::Optional<T>> instead.";
62 TVM_FFI_UNREACHABLE();
75 if constexpr (std::is_same_v<T, Any>) {
91 if constexpr (std::is_same_v<T, Any>) {
92 return std::move(ref);
94 return std::move(ref).cast<T>();
105 template <
typename OptionalType,
typename = std::enable_if_t<ffi::is_optional_type_v<OptionalType>>>
106 inline OptionalType
Downcast(
const std::optional<ffi::Any>& ref) {
107 if (ref.has_value()) {
108 if constexpr (std::is_same_v<OptionalType, ffi::Any>) {
111 return (*ref).cast<OptionalType>();
114 return OptionalType(std::nullopt);
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
SubRef Downcast(BaseRef ref)
Downcast a base reference type to a more specific type.
Definition: cast.h:47