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>>>
49 if (!ref->template IsInstance<typename SubRef::ContainerType>()) {
50 TVM_FFI_THROW(TypeError) <<
"Downcast from " << ref->GetTypeKey() <<
" to "
51 << SubRef::ContainerType::_type_key <<
" failed.";
53 return SubRef(ffi::details::ObjectUnsafe::ObjectPtrFromObjectRef<ffi::Object>(std::move(ref)));
55 if constexpr (ffi::is_optional_type_v<SubRef> || SubRef::_type_is_nullable) {
56 return SubRef(ffi::ObjectPtr<ffi::Object>(
nullptr));
58 TVM_FFI_THROW(TypeError) <<
"Downcast from undefined(nullptr) to `"
59 << SubRef::ContainerType::_type_key
60 <<
"` is not allowed. Use Downcast<Optional<T>> instead.";
61 TVM_FFI_UNREACHABLE();
74 if constexpr (std::is_same_v<T, Any>) {
90 if constexpr (std::is_same_v<T, Any>) {
91 return std::move(ref);
93 return std::move(ref).cast<T>();
104 template <
typename OptionalType,
typename = std::enable_if_t<ffi::is_optional_type_v<OptionalType>>>
105 inline OptionalType
Downcast(
const std::optional<ffi::Any>& ref) {
106 if (ref.has_value()) {
107 if constexpr (std::is_same_v<OptionalType, ffi::Any>) {
110 return (*ref).cast<OptionalType>();
113 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