Skip to content

Commit 4be1af7

Browse files
committed
[FFI][REFACTOR] Move Downcast out of ffi for now (apache#18198)
Downcast was added for backward compact reasons and it have duplicated features as Any.cast. This PR moves it out of ffi to node for now so the ffi part contains minimal set of implementations.
1 parent ed56a5e commit 4be1af7

File tree

3 files changed

+5
-93
lines changed

3 files changed

+5
-93
lines changed

include/tvm/ffi/any.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,6 @@ struct AnyEqual {
635635
}
636636
}
637637
};
638-
639638
} // namespace ffi
640639

641640
// Expose to the tvm namespace for usability

include/tvm/ffi/cast.h

Lines changed: 4 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,18 @@
1818
*/
1919
/*!
2020
* \file tvm/ffi/cast.h
21-
* \brief Value casting support
21+
* \brief Extra value casting helpers
2222
*/
2323
#ifndef TVM_FFI_CAST_H_
2424
#define TVM_FFI_CAST_H_
2525

2626
#include <tvm/ffi/any.h>
27-
#include <tvm/ffi/dtype.h>
28-
#include <tvm/ffi/error.h>
2927
#include <tvm/ffi/object.h>
3028
#include <tvm/ffi/optional.h>
3129

32-
#include <utility>
33-
3430
namespace tvm {
3531
namespace ffi {
32+
3633
/*!
3734
* \brief Get a reference type from a raw object ptr type
3835
*
@@ -46,7 +43,7 @@ namespace ffi {
4643
* \return The corresponding RefType
4744
*/
4845
template <typename RefType, typename ObjectType>
49-
TVM_FFI_INLINE RefType GetRef(const ObjectType* ptr) {
46+
inline RefType GetRef(const ObjectType* ptr) {
5047
static_assert(std::is_base_of_v<typename RefType::ContainerType, ObjectType>,
5148
"Can only cast to the ref of same container type");
5249

@@ -75,92 +72,9 @@ inline ObjectPtr<BaseType> GetObjectPtr(ObjectType* ptr) {
7572
"Can only cast to the ref of same container type");
7673
return details::ObjectUnsafe::ObjectPtrFromUnowned<BaseType>(ptr);
7774
}
78-
79-
/*!
80-
* \brief Downcast a base reference type to a more specific type.
81-
*
82-
* \param ref The input reference
83-
* \return The corresponding SubRef.
84-
* \tparam SubRef The target specific reference type.
85-
* \tparam BaseRef the current reference type.
86-
*/
87-
template <typename SubRef, typename BaseRef,
88-
typename = std::enable_if_t<std::is_base_of_v<ObjectRef, BaseRef>>>
89-
inline SubRef Downcast(BaseRef ref) {
90-
if (ref.defined()) {
91-
if (!ref->template IsInstance<typename SubRef::ContainerType>()) {
92-
TVM_FFI_THROW(TypeError) << "Downcast from " << ref->GetTypeKey() << " to "
93-
<< SubRef::ContainerType::_type_key << " failed.";
94-
}
95-
return SubRef(details::ObjectUnsafe::ObjectPtrFromObjectRef<Object>(std::move(ref)));
96-
} else {
97-
if constexpr (is_optional_type_v<SubRef> || SubRef::_type_is_nullable) {
98-
return SubRef(ObjectPtr<Object>(nullptr));
99-
}
100-
TVM_FFI_THROW(TypeError) << "Downcast from undefined(nullptr) to `"
101-
<< SubRef::ContainerType::_type_key
102-
<< "` is not allowed. Use Downcast<Optional<T>> instead.";
103-
TVM_FFI_UNREACHABLE();
104-
}
105-
}
106-
107-
/*!
108-
* \brief Downcast any to a specific type
109-
*
110-
* \param ref The input reference
111-
* \return The corresponding SubRef.
112-
* \tparam T The target specific reference type.
113-
*/
114-
template <typename T>
115-
inline T Downcast(const Any& ref) {
116-
if constexpr (std::is_same_v<T, Any>) {
117-
return ref;
118-
} else {
119-
return ref.cast<T>();
120-
}
121-
}
122-
123-
/*!
124-
* \brief Downcast any to a specific type
125-
*
126-
* \param ref The input reference
127-
* \return The corresponding SubRef.
128-
* \tparam T The target specific reference type.
129-
*/
130-
template <typename T>
131-
inline T Downcast(Any&& ref) {
132-
if constexpr (std::is_same_v<T, Any>) {
133-
return std::move(ref);
134-
} else {
135-
return std::move(ref).cast<T>();
136-
}
137-
}
138-
139-
/*!
140-
* \brief Downcast std::optional<Any> to std::optional<T>
141-
*
142-
* \param ref The input reference
143-
* \return The corresponding SubRef.
144-
* \tparam OptionalType The target optional type
145-
*/
146-
template <typename OptionalType, typename = std::enable_if_t<is_optional_type_v<OptionalType>>>
147-
inline OptionalType Downcast(const std::optional<Any>& ref) {
148-
if (ref.has_value()) {
149-
if constexpr (std::is_same_v<OptionalType, Any>) {
150-
return *ref;
151-
} else {
152-
return (*ref).cast<OptionalType>();
153-
}
154-
} else {
155-
return OptionalType(std::nullopt);
156-
}
157-
}
158-
15975
} // namespace ffi
16076

161-
// Expose to the tvm namespace
162-
// Rationale: convinience and no ambiguity
163-
using ffi::Downcast;
77+
using ffi::GetObjectPtr;
16478
using ffi::GetRef;
16579
} // namespace tvm
16680
#endif // TVM_FFI_CAST_H_

tests/cpp/test_string.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
#include <gtest/gtest.h>
2020
#include <tvm/ffi/any.h>
21-
#include <tvm/ffi/cast.h>
2221
#include <tvm/ffi/string.h>
2322

2423
namespace {
@@ -266,7 +265,7 @@ TEST(String, Cast) {
266265
string source = "this is a string";
267266
String s{source};
268267
Any r = s;
269-
String s2 = Downcast<String>(r);
268+
String s2 = r.cast<String>();
270269
}
271270

272271
TEST(String, Concat) {

0 commit comments

Comments
 (0)