Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/tvm/ffi/any.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,11 @@ struct Type2Str<const Any&> {
static std::string v() { return "Any"; }
};

template <>
struct Type2Str<Any&&> {
static std::string v() { return "Any"; }
};

template <>
struct Type2Str<AnyView> {
static std::string v() { return "AnyView"; }
Expand All @@ -526,6 +531,11 @@ struct Type2Str<const AnyView&> {
static std::string v() { return "AnyView"; }
};

template <>
struct Type2Str<AnyView&&> {
static std::string v() { return "AnyView"; }
};

template <>
struct Type2Str<void> {
static std::string v() { return "void"; }
Expand Down
2 changes: 2 additions & 0 deletions include/tvm/ffi/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
namespace tvm {
namespace ffi {

class Any;

/*!
* \brief TypeTraits that specifies the conversion behavior from/to FFI Any.
*
Expand Down
56 changes: 56 additions & 0 deletions tests/cpp/test_reflection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,60 @@ TEST(Reflection, AccessPath) {
auto root_parent = root->GetParent();
EXPECT_FALSE(root_parent.has_value());
}

struct TestObjWithAny : public Object {
Any value;
explicit TestObjWithAny(Any value) : value(std::move(value)) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you also try

Suggested change
explicit TestObjWithAny(Any value) : value(std::move(value)) {}
explicit TestObjWithAny(AnyView value) : value(value) {}

and see if it compiles?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code fails to compile with the same missing TypeStr error when using refl::init<AnyView>() so I also add Type2Str<AnyView&&> specialization and related tests.

[[maybe_unused]] static constexpr bool _type_mutable = true;
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("test.TestObjWithAny", TestObjWithAny, Object);
};

TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::ObjectDef<TestObjWithAny>().def(refl::init<Any>()).def_ro("value", &TestObjWithAny::value);
}

struct TestObjWithAnyView : public Object {
Any value;
explicit TestObjWithAnyView(AnyView value) : value(value) {}
[[maybe_unused]] static constexpr bool _type_mutable = true;
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("test.TestObjWithAnyView", TestObjWithAnyView, Object);
};

TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::ObjectDef<TestObjWithAnyView>()
.def(refl::init<AnyView>())
.def_ro("value", &TestObjWithAnyView::value);
}

TEST(Reflection, InitWithAny) {
Function init = reflection::GetMethod("test.TestObjWithAny", "__ffi_init__");
Any obj1 = init(42);
ASSERT_TRUE(obj1.as<TestObjWithAny>() != nullptr);
EXPECT_EQ(obj1.as<TestObjWithAny>()->value.cast<int>(), 42);

Any obj2 = init(3.14);
ASSERT_TRUE(obj2.as<TestObjWithAny>() != nullptr);
EXPECT_EQ(obj2.as<TestObjWithAny>()->value.cast<double>(), 3.14);

Any obj3 = init(String("hello"));
ASSERT_TRUE(obj3.as<TestObjWithAny>() != nullptr);
EXPECT_EQ(obj3.as<TestObjWithAny>()->value.cast<String>(), "hello");
}

TEST(Reflection, InitWithAnyView) {
Function init = reflection::GetMethod("test.TestObjWithAnyView", "__ffi_init__");
Any obj1 = init(42);
ASSERT_TRUE(obj1.as<TestObjWithAnyView>() != nullptr);
EXPECT_EQ(obj1.as<TestObjWithAnyView>()->value.cast<int>(), 42);

Any obj2 = init(3.14);
ASSERT_TRUE(obj2.as<TestObjWithAnyView>() != nullptr);
EXPECT_EQ(obj2.as<TestObjWithAnyView>()->value.cast<double>(), 3.14);

Any obj3 = init(String("hello"));
ASSERT_TRUE(obj3.as<TestObjWithAnyView>() != nullptr);
EXPECT_EQ(obj3.as<TestObjWithAnyView>()->value.cast<String>(), "hello");
}
} // namespace