tvm
cast.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  */
23 #ifndef TVM_NODE_CAST_H_
24 #define TVM_NODE_CAST_H_
25 
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>
32 
33 #include <utility>
34 
35 namespace tvm {
36 
45 template <typename SubRef, typename BaseRef,
46  typename = std::enable_if_t<std::is_base_of_v<ffi::ObjectRef, BaseRef>>>
47 inline SubRef Downcast(BaseRef ref) {
48  if (ref.defined()) {
49  if (!ref->template IsInstance<typename SubRef::ContainerType>()) {
50  TVM_FFI_THROW(TypeError) << "Downcast from " << ref->GetTypeKey() << " to "
51  << SubRef::ContainerType::_type_key << " failed.";
52  }
53  return SubRef(ffi::details::ObjectUnsafe::ObjectPtrFromObjectRef<ffi::Object>(std::move(ref)));
54  } else {
55  if constexpr (ffi::is_optional_type_v<SubRef> || SubRef::_type_is_nullable) {
56  return SubRef(ffi::ObjectPtr<ffi::Object>(nullptr));
57  }
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();
62  }
63 }
64 
72 template <typename T>
73 inline T Downcast(const ffi::Any& ref) {
74  if constexpr (std::is_same_v<T, Any>) {
75  return ref;
76  } else {
77  return ref.cast<T>();
78  }
79 }
80 
88 template <typename T>
89 inline T Downcast(ffi::Any&& ref) {
90  if constexpr (std::is_same_v<T, Any>) {
91  return std::move(ref);
92  } else {
93  return std::move(ref).cast<T>();
94  }
95 }
96 
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>) {
108  return *ref;
109  } else {
110  return (*ref).cast<OptionalType>();
111  }
112  } else {
113  return OptionalType(std::nullopt);
114  }
115 }
116 } // namespace tvm
117 #endif // TVM_NODE_CAST_H_
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