This is an automated email from the ASF dual-hosted git repository. tqchen pushed a commit to branch refactor-s2 in repository https://gitbox.apache.org/repos/asf/tvm.git
commit a8ba7dd47aa14a4b787ea611b35d14eac8512943 Author: tqchen <[email protected]> AuthorDate: Thu Apr 24 09:43:46 2025 -0400 [FFI] Rename ArrayNode to ArrayObj, MapNode to MapObj --- ffi/include/tvm/ffi/container/array.h | 164 ++++++++-------- ffi/include/tvm/ffi/container/container_details.h | 2 +- ffi/include/tvm/ffi/container/map.h | 224 +++++++++++----------- ffi/include/tvm/ffi/container/tuple.h | 32 ++-- ffi/tests/cpp/test_map.cc | 2 +- 5 files changed, 212 insertions(+), 212 deletions(-) diff --git a/ffi/include/tvm/ffi/container/array.h b/ffi/include/tvm/ffi/container/array.h index 66d71d2421..91d0f297c8 100644 --- a/ffi/include/tvm/ffi/container/array.h +++ b/ffi/include/tvm/ffi/container/array.h @@ -42,7 +42,7 @@ namespace tvm { namespace ffi { /*! \brief array node content in array */ -class ArrayNode : public Object, public details::InplaceArrayBase<ArrayNode, Any> { +class ArrayObj : public Object, public details::InplaceArrayBase<ArrayObj, Any> { public: /*! \return The size of the array */ size_t size() const { return this->size_; } @@ -74,14 +74,14 @@ class ArrayNode : public Object, public details::InplaceArrayBase<ArrayNode, Any * \brief Constructs a container and copy from another * \param cap The capacity of the container * \param from Source of the copy - * \return Ref-counted ArrayNode requested + * \return Ref-counted ArrayObj requested */ - static ObjectPtr<ArrayNode> CopyFrom(int64_t cap, ArrayNode* from) { + static ObjectPtr<ArrayObj> CopyFrom(int64_t cap, ArrayObj* from) { int64_t size = from->size_; if (size > cap) { TVM_FFI_THROW(ValueError) << "not enough capacity"; } - ObjectPtr<ArrayNode> p = ArrayNode::Empty(cap); + ObjectPtr<ArrayObj> p = ArrayObj::Empty(cap); Any* write = p->MutableBegin(); Any* read = from->MutableBegin(); // To ensure exception safety, size is only incremented after the initialization succeeds @@ -95,14 +95,14 @@ class ArrayNode : public Object, public details::InplaceArrayBase<ArrayNode, Any * \brief Constructs a container and move from another * \param cap The capacity of the container * \param from Source of the move - * \return Ref-counted ArrayNode requested + * \return Ref-counted ArrayObj requested */ - static ObjectPtr<ArrayNode> MoveFrom(int64_t cap, ArrayNode* from) { + static ObjectPtr<ArrayObj> MoveFrom(int64_t cap, ArrayObj* from) { int64_t size = from->size_; if (size > cap) { TVM_FFI_THROW(RuntimeError) << "not enough capacity"; } - ObjectPtr<ArrayNode> p = ArrayNode::Empty(cap); + ObjectPtr<ArrayObj> p = ArrayObj::Empty(cap); Any* write = p->MutableBegin(); Any* read = from->MutableBegin(); // To ensure exception safety, size is only incremented after the initialization succeeds @@ -117,10 +117,10 @@ class ArrayNode : public Object, public details::InplaceArrayBase<ArrayNode, Any * \brief Constructs a container with n elements. Each element is a copy of val * \param n The size of the container * \param val The init value - * \return Ref-counted ArrayNode requested + * \return Ref-counted ArrayObj requested */ - static ObjectPtr<ArrayNode> CreateRepeated(int64_t n, const Any& val) { - ObjectPtr<ArrayNode> p = ArrayNode::Empty(n); + static ObjectPtr<ArrayObj> CreateRepeated(int64_t n, const Any& val) { + ObjectPtr<ArrayObj> p = ArrayObj::Empty(n); Any* itr = p->MutableBegin(); for (int64_t& i = p->size_ = 0; i < n; ++i) { new (itr++) Any(val); @@ -131,7 +131,7 @@ class ArrayNode : public Object, public details::InplaceArrayBase<ArrayNode, Any static constexpr const int32_t _type_index = TypeIndex::kTVMFFIArray; static constexpr const char* _type_key = "object.Array"; static const constexpr bool _type_final = true; - TVM_FFI_DECLARE_STATIC_OBJECT_INFO(ArrayNode, Object); + TVM_FFI_DECLARE_STATIC_OBJECT_INFO(ArrayObj, Object); private: /*! \return Size of initialized memory, used by InplaceArrayBase. */ @@ -144,13 +144,13 @@ class ArrayNode : public Object, public details::InplaceArrayBase<ArrayNode, Any Any* MutableEnd() const { return MutableBegin() + size_; } /*! - * \brief Create an ArrayNode with the given capacity. + * \brief Create an ArrayObj with the given capacity. * \param n Required capacity - * \return Ref-counted ArrayNode requested + * \return Ref-counted ArrayObj requested */ - static ObjectPtr<ArrayNode> Empty(int64_t n = kInitSize) { + static ObjectPtr<ArrayObj> Empty(int64_t n = kInitSize) { TVM_FFI_ICHECK_GE(n, 0); - ObjectPtr<ArrayNode> p = make_inplace_array_object<ArrayNode, Any>(n); + ObjectPtr<ArrayObj> p = make_inplace_array_object<ArrayObj, Any>(n); p->capacity_ = n; p->size_ = 0; return p; @@ -165,7 +165,7 @@ class ArrayNode : public Object, public details::InplaceArrayBase<ArrayNode, Any * \return Self */ template <typename IterType> - ArrayNode* InitRange(int64_t idx, IterType first, IterType last) { + ArrayObj* InitRange(int64_t idx, IterType first, IterType last) { Any* itr = MutableBegin() + idx; for (; first != last; ++first) { Any ref = *first; @@ -181,7 +181,7 @@ class ArrayNode : public Object, public details::InplaceArrayBase<ArrayNode, Any * \param src_end The end point of copy (exclusive) * \return Self */ - ArrayNode* MoveElementsLeft(int64_t dst, int64_t src_begin, int64_t src_end) { + ArrayObj* MoveElementsLeft(int64_t dst, int64_t src_begin, int64_t src_end) { Any* from = MutableBegin() + src_begin; Any* to = MutableBegin() + dst; while (src_begin++ != src_end) { @@ -197,7 +197,7 @@ class ArrayNode : public Object, public details::InplaceArrayBase<ArrayNode, Any * \param src_end The end point of move (exclusive) * \return Self */ - ArrayNode* MoveElementsRight(int64_t dst, int64_t src_begin, int64_t src_end) { + ArrayObj* MoveElementsRight(int64_t dst, int64_t src_begin, int64_t src_end) { Any* from = MutableBegin() + src_end; Any* to = MutableBegin() + (src_end - src_begin + dst); while (src_begin++ != src_end) { @@ -212,7 +212,7 @@ class ArrayNode : public Object, public details::InplaceArrayBase<ArrayNode, Any * \param val Default value * \return Self */ - ArrayNode* EnlargeBy(int64_t delta, const Any& val = Any()) { + ArrayObj* EnlargeBy(int64_t delta, const Any& val = Any()) { Any* itr = MutableEnd(); while (delta-- > 0) { new (itr++) Any(val); @@ -226,7 +226,7 @@ class ArrayNode : public Object, public details::InplaceArrayBase<ArrayNode, Any * \param delta Size shrinked, should be positive * \return Self */ - ArrayNode* ShrinkBy(int64_t delta) { + ArrayObj* ShrinkBy(int64_t delta) { Any* itr = MutableEnd(); while (delta-- > 0) { (--itr)->Any::~Any(); @@ -241,14 +241,14 @@ class ArrayNode : public Object, public details::InplaceArrayBase<ArrayNode, Any /*! \brief Number of elements allocated */ int64_t capacity_; - /*! \brief Initial size of ArrayNode */ + /*! \brief Initial size of ArrayObj */ static constexpr int64_t kInitSize = 4; /*! \brief Expansion factor of the Array */ static constexpr int64_t kIncFactor = 2; // CRTP parent class - friend InplaceArrayBase<ArrayNode, Any>; + friend InplaceArrayBase<ArrayObj, Any>; // Reference class template <typename, typename> @@ -260,8 +260,8 @@ class ArrayNode : public Object, public details::InplaceArrayBase<ArrayNode, Any template <typename, typename> friend struct TypeTraits; - // To specialize make_object<ArrayNode> - friend ObjectPtr<ArrayNode> make_object<>(); + // To specialize make_object<ArrayObj> + friend ObjectPtr<ArrayObj> make_object<>(); }; /*! \brief Helper struct for type-checking @@ -310,7 +310,7 @@ class Array : public ObjectRef { /*! * \brief default constructor */ - Array() { data_ = ArrayNode::Empty(); } + Array() { data_ = ArrayObj::Empty(); } Array(Array<T>&& other) : ObjectRef(std::move(other.data_)) {} Array(const Array<T>& other) : ObjectRef(other.data_) {} template <typename U, typename = std::enable_if_t<details::type_contains_v<T, U>>> @@ -377,7 +377,7 @@ class Array : public ObjectRef { * \param n The size of the container * \param val The init value */ - explicit Array(const size_t n, const T& val) { data_ = ArrayNode::CreateRepeated(n, val); } + explicit Array(const size_t n, const T& val) { data_ = ArrayObj::CreateRepeated(n, val); } public: // iterators @@ -392,21 +392,21 @@ class Array : public ObjectRef { using reverse_iterator = details::ReverseIterAdapter<ValueConverter, const Any*>; /*! \return begin iterator */ - iterator begin() const { return iterator(GetArrayNode()->begin()); } + iterator begin() const { return iterator(GetArrayObj()->begin()); } /*! \return end iterator */ - iterator end() const { return iterator(GetArrayNode()->end()); } + iterator end() const { return iterator(GetArrayObj()->end()); } /*! \return rbegin iterator */ reverse_iterator rbegin() const { - // ArrayNode::end() is never nullptr - return reverse_iterator(GetArrayNode()->end() - 1); + // ArrayObj::end() is never nullptr + return reverse_iterator(GetArrayObj()->end() - 1); } /*! \return rend iterator */ reverse_iterator rend() const { - // ArrayNode::begin() is never nullptr - return reverse_iterator(GetArrayNode()->begin() - 1); + // ArrayObj::begin() is never nullptr + return reverse_iterator(GetArrayObj()->begin() - 1); } public: @@ -417,7 +417,7 @@ class Array : public ObjectRef { * \return the i-th element. */ const T operator[](int64_t i) const { - ArrayNode* p = GetArrayNode(); + ArrayObj* p = GetArrayObj(); if (p == nullptr) { TVM_FFI_THROW(IndexError) << "cannot index a null array"; } @@ -429,14 +429,14 @@ class Array : public ObjectRef { /*! \return The size of the array */ size_t size() const { - ArrayNode* p = GetArrayNode(); - return p == nullptr ? 0 : GetArrayNode()->size_; + ArrayObj* p = GetArrayObj(); + return p == nullptr ? 0 : GetArrayObj()->size_; } /*! \return The capacity of the array */ size_t capacity() const { - ArrayNode* p = GetArrayNode(); - return p == nullptr ? 0 : GetArrayNode()->capacity_; + ArrayObj* p = GetArrayObj(); + return p == nullptr ? 0 : GetArrayObj()->capacity_; } /*! \return Whether array is empty */ @@ -444,7 +444,7 @@ class Array : public ObjectRef { /*! \return The first element of the array */ const T front() const { - ArrayNode* p = GetArrayNode(); + ArrayObj* p = GetArrayObj(); if (p == nullptr || p->size_ == 0) { TVM_FFI_THROW(IndexError) << "cannot index a empty array"; } @@ -453,7 +453,7 @@ class Array : public ObjectRef { /*! \return The last element of the array */ const T back() const { - ArrayNode* p = GetArrayNode(); + ArrayObj* p = GetArrayObj(); if (p == nullptr || p->size_ == 0) { TVM_FFI_THROW(IndexError) << "cannot index a empty array"; } @@ -467,7 +467,7 @@ class Array : public ObjectRef { * \param item The item to be pushed. */ void push_back(const T& item) { - ArrayNode* p = CopyOnWrite(1); + ArrayObj* p = CopyOnWrite(1); p->EmplaceInit(p->size_++, item); } @@ -481,7 +481,7 @@ class Array : public ObjectRef { TVM_FFI_THROW(RuntimeError) << "cannot insert a null array"; } int64_t idx = std::distance(begin(), position); - int64_t size = GetArrayNode()->size_; + int64_t size = GetArrayObj()->size_; auto addr = CopyOnWrite(1) // ->EnlargeBy(1) // ->MoveElementsRight(idx + 1, idx, size) // @@ -507,7 +507,7 @@ class Array : public ObjectRef { TVM_FFI_THROW(RuntimeError) << "cannot insert a null array"; } int64_t idx = std::distance(begin(), position); - int64_t size = GetArrayNode()->size_; + int64_t size = GetArrayObj()->size_; int64_t numel = std::distance(first, last); CopyOnWrite(numel) ->EnlargeBy(numel) @@ -520,7 +520,7 @@ class Array : public ObjectRef { if (data_ == nullptr) { TVM_FFI_THROW(RuntimeError) << "cannot pop_back a null array"; } - int64_t size = GetArrayNode()->size_; + int64_t size = GetArrayObj()->size_; if (size == 0) { TVM_FFI_THROW(RuntimeError) << "cannot pop_back an empty array"; } @@ -536,7 +536,7 @@ class Array : public ObjectRef { TVM_FFI_THROW(RuntimeError) << "cannot erase a null array"; } int64_t st = std::distance(begin(), position); - int64_t size = GetArrayNode()->size_; + int64_t size = GetArrayObj()->size_; if (st < 0 || st >= size) { TVM_FFI_THROW(RuntimeError) << "cannot erase at index " << st << ", because Array size is " << size; @@ -558,7 +558,7 @@ class Array : public ObjectRef { if (data_ == nullptr) { TVM_FFI_THROW(RuntimeError) << "cannot erase a null array"; } - int64_t size = GetArrayNode()->size_; + int64_t size = GetArrayObj()->size_; int64_t st = std::distance(begin(), first); int64_t ed = std::distance(begin(), last); if (st >= ed) { @@ -585,7 +585,7 @@ class Array : public ObjectRef { SwitchContainer(n); return; } - int64_t size = GetArrayNode()->size_; + int64_t size = GetArrayObj()->size_; if (size < n) { CopyOnWrite(n - size)->EnlargeBy(n - size); } else if (size > n) { @@ -598,7 +598,7 @@ class Array : public ObjectRef { * \param n lower bound of the capacity */ void reserve(int64_t n) { - if (data_ == nullptr || n > GetArrayNode()->capacity_) { + if (data_ == nullptr || n > GetArrayObj()->capacity_) { SwitchContainer(n); } } @@ -606,7 +606,7 @@ class Array : public ObjectRef { /*! \brief Release reference to all the elements */ void clear() { if (data_ != nullptr) { - ArrayNode* p = CopyOnWrite(); + ArrayObj* p = CopyOnWrite(); p->clear(); } } @@ -650,15 +650,15 @@ class Array : public ObjectRef { * \param value The value to be setted. */ void Set(int64_t i, T value) { - ArrayNode* p = this->CopyOnWrite(); + ArrayObj* p = this->CopyOnWrite(); if (i < 0 || i >= p->size_) { TVM_FFI_THROW(IndexError) << "indexing " << i << " on an array of size " << p->size_; } *(p->MutableBegin() + i) = std::move(value); } - /*! \return The underlying ArrayNode */ - ArrayNode* GetArrayNode() const { return static_cast<ArrayNode*>(data_.get()); } + /*! \return The underlying ArrayObj */ + ArrayObj* GetArrayObj() const { return static_cast<ArrayObj*>(data_.get()); } /*! * \brief Helper function to apply a map function onto the array. @@ -707,14 +707,14 @@ class Array : public ObjectRef { if (cap < 0) { TVM_FFI_THROW(ValueError) << "cannot construct an Array of negative size"; } - ArrayNode* p = GetArrayNode(); + ArrayObj* p = GetArrayObj(); if (p != nullptr && data_.unique() && p->capacity_ >= cap) { // do not have to make new space p->clear(); } else { // create new space - data_ = ArrayNode::Empty(cap); - p = GetArrayNode(); + data_ = ArrayObj::Empty(cap); + p = GetArrayObj(); } // To ensure exception safety, size is only incremented after the initialization succeeds Any* itr = p->MutableBegin(); @@ -731,18 +731,18 @@ class Array : public ObjectRef { * * \return Handle to the internal node container(which ganrantees to be unique) */ - ArrayNode* CopyOnWrite() { + ArrayObj* CopyOnWrite() { if (data_ == nullptr) { - return SwitchContainer(ArrayNode::kInitSize); + return SwitchContainer(ArrayObj::kInitSize); } if (!data_.unique()) { return SwitchContainer(capacity()); } - return static_cast<ArrayNode*>(data_.get()); + return static_cast<ArrayObj*>(data_.get()); } /*! \brief specify container node */ - using ContainerType = ArrayNode; + using ContainerType = ArrayObj; /*! * \brief Agregate arguments into a single Array<T> @@ -761,36 +761,36 @@ class Array : public ObjectRef { /*! * \brief Implement copy-on-write semantics, and ensures capacity is enough for extra elements. * \param reserve_extra Number of extra slots needed - * \return ArrayNode pointer to the unique copy + * \return ArrayObj pointer to the unique copy */ - ArrayNode* CopyOnWrite(int64_t reserve_extra) { - ArrayNode* p = GetArrayNode(); + ArrayObj* CopyOnWrite(int64_t reserve_extra) { + ArrayObj* p = GetArrayObj(); if (p == nullptr) { // necessary to get around the constexpr address issue before c++17 - const int64_t kInitSize = ArrayNode::kInitSize; + const int64_t kInitSize = ArrayObj::kInitSize; return SwitchContainer(std::max(kInitSize, reserve_extra)); } if (p->capacity_ >= p->size_ + reserve_extra) { return CopyOnWrite(); } - int64_t cap = p->capacity_ * ArrayNode::kIncFactor; + int64_t cap = p->capacity_ * ArrayObj::kIncFactor; cap = std::max(cap, p->size_ + reserve_extra); return SwitchContainer(cap); } /*! - * \brief Move or copy the ArrayNode to new address with the given capacity + * \brief Move or copy the ArrayObj to new address with the given capacity * \param capacity The capacity requirement of the new address */ - ArrayNode* SwitchContainer(int64_t capacity) { + ArrayObj* SwitchContainer(int64_t capacity) { if (data_ == nullptr) { - data_ = ArrayNode::Empty(capacity); + data_ = ArrayObj::Empty(capacity); } else if (data_.unique()) { - data_ = ArrayNode::MoveFrom(capacity, GetArrayNode()); + data_ = ArrayObj::MoveFrom(capacity, GetArrayObj()); } else { - data_ = ArrayNode::CopyFrom(capacity, GetArrayNode()); + data_ = ArrayObj::CopyFrom(capacity, GetArrayObj()); } - return static_cast<ArrayNode*>(data_.get()); + return static_cast<ArrayObj*>(data_.get()); } /*! \brief Helper method for mutate/map @@ -801,7 +801,7 @@ class Array : public ObjectRef { * Applies both mutate-in-place and copy-on-write optimizations, if * possible. * - * \param data A pointer to the ArrayNode containing input data. + * \param data A pointer to the ArrayObj containing input data. * Passed by value to allow for mutate-in-place optimizations. * * \param fmap The mapping function @@ -821,7 +821,7 @@ class Array : public ObjectRef { return nullptr; } - TVM_FFI_ICHECK(data->IsInstance<ArrayNode>()); + TVM_FFI_ICHECK(data->IsInstance<ArrayObj>()); constexpr bool is_same_output_type = std::is_same_v<T, U>; @@ -830,7 +830,7 @@ class Array : public ObjectRef { // Mutate-in-place path. Only allowed if the output type U is // the same as type T, we have a mutable this*, and there are // no other shared copies of the array. - auto arr = static_cast<ArrayNode*>(data.get()); + auto arr = static_cast<ArrayObj*>(data.get()); for (auto it = arr->MutableBegin(); it != arr->MutableEnd(); it++) { T value = details::AnyUnsafe::CopyFromAnyStorageAfterCheck<T>(*it); // reset the original value to nullptr, to ensure unique ownership @@ -844,8 +844,8 @@ class Array : public ObjectRef { constexpr bool compatible_types = is_valid_iterator_v<T, U*> || is_valid_iterator_v<U, T*>; - ObjectPtr<ArrayNode> output = nullptr; - auto arr = static_cast<ArrayNode*>(data.get()); + ObjectPtr<ArrayObj> output = nullptr; + auto arr = static_cast<ArrayObj*>(data.get()); auto it = arr->begin(); if constexpr (compatible_types) { @@ -870,7 +870,7 @@ class Array : public ObjectRef { // will be overwritten before returning, all objects will be // of type `U` for the calling scope. all_identical = false; - output = ArrayNode::CreateRepeated(arr->size(), Any()); + output = ArrayObj::CreateRepeated(arr->size(), Any()); output->InitRange(0, arr->begin(), it); output->SetItem(it - arr->begin(), std::move(mapped)); it++; @@ -890,7 +890,7 @@ class Array : public ObjectRef { // non-nullable type. Since the default `Any()` will be // overwritten before returning, all objects will be of type `U` // for the calling scope. - output = ArrayNode::CreateRepeated(arr->size(), Any()); + output = ArrayObj::CreateRepeated(arr->size(), Any()); } // Normal path for incompatible types, or post-copy path for @@ -936,10 +936,10 @@ inline Array<T> Concat(Array<T> lhs, const Array<T>& rhs) { return std::move(lhs); } -// Specialize make_object<ArrayNode> to make sure it is correct. +// Specialize make_object<ArrayObj> to make sure it is correct. template <> -inline ObjectPtr<ArrayNode> make_object() { - return ArrayNode::Empty(); +inline ObjectPtr<ArrayObj> make_object() { + return ArrayObj::Empty(); } // Traits for Array @@ -956,7 +956,7 @@ struct TypeTraits<Array<T>> : public ObjectRefTypeTraitsBase<Array<T>> { return TypeTraitsBase::GetMismatchTypeInfo(src); } if constexpr (!std::is_same_v<T, Any>) { - const ArrayNode* n = reinterpret_cast<const ArrayNode*>(src->v_obj); + const ArrayObj* n = reinterpret_cast<const ArrayObj*>(src->v_obj); for (size_t i = 0; i < n->size(); i++) { const Any& any_v = (*n)[i]; // CheckAnyStorage is cheaper than as<T> @@ -977,7 +977,7 @@ struct TypeTraits<Array<T>> : public ObjectRefTypeTraitsBase<Array<T>> { if constexpr (std::is_same_v<T, Any>) { return true; } else { - const ArrayNode* n = reinterpret_cast<const ArrayNode*>(src->v_obj); + const ArrayObj* n = reinterpret_cast<const ArrayObj*>(src->v_obj); for (size_t i = 0; i < n->size(); i++) { const Any& any_v = (*n)[i]; if (!details::AnyUnsafe::CheckAnyStorage<T>(any_v)) return false; @@ -990,7 +990,7 @@ struct TypeTraits<Array<T>> : public ObjectRefTypeTraitsBase<Array<T>> { // try to run conversion. if (src->type_index != TypeIndex::kTVMFFIArray) return std::nullopt; if constexpr (!std::is_same_v<T, Any>) { - const ArrayNode* n = reinterpret_cast<const ArrayNode*>(src->v_obj); + const ArrayObj* n = reinterpret_cast<const ArrayObj*>(src->v_obj); bool storage_check = [&]() { for (size_t i = 0; i < n->size(); i++) { const Any& any_v = (*n)[i]; diff --git a/ffi/include/tvm/ffi/container/container_details.h b/ffi/include/tvm/ffi/container/container_details.h index bcdaa18ae7..9f54b32ecb 100644 --- a/ffi/include/tvm/ffi/container/container_details.h +++ b/ffi/include/tvm/ffi/container/container_details.h @@ -48,7 +48,7 @@ namespace details { * * \code * // Example usage of the template to define a simple array wrapper - * class ArrayNode : public tvm::ffi::details::InplaceArrayBase<ArrayNode, Elem> { + * class ArrayObj : public tvm::ffi::details::InplaceArrayBase<ArrayObj, Elem> { * public: * // Wrap EmplaceInit to initialize the elements * template <typename Iterator> diff --git a/ffi/include/tvm/ffi/container/map.h b/ffi/include/tvm/ffi/container/map.h index b9d59d89ae..31b630f9f8 100644 --- a/ffi/include/tvm/ffi/container/map.h +++ b/ffi/include/tvm/ffi/container/map.h @@ -47,7 +47,7 @@ namespace ffi { #endif // TVM_FFI_DEBUG_WITH_ABI_CHANGE /*! \brief Shared content of all specializations of hash map */ -class MapNode : public Object { +class MapObj : public Object { public: /*! \brief Type of the keys in the hash map */ using key_type = Any; @@ -64,10 +64,10 @@ class MapNode : public Object { static constexpr const int32_t _type_index = TypeIndex::kTVMFFIMap; static constexpr const char* _type_key = "object.Map"; static const constexpr bool _type_final = true; - TVM_FFI_DECLARE_STATIC_OBJECT_INFO(MapNode, Object); + TVM_FFI_DECLARE_STATIC_OBJECT_INFO(MapObj, Object); /*! - * \brief Number of elements in the SmallMapNode + * \brief Number of elements in the SmallMapObj * \return The result */ size_t size() const { return size_; } @@ -160,25 +160,25 @@ class MapNode : public Object { #if TVM_FFI_DEBUG_WITH_ABI_CHANGE uint64_t state_marker; /*! \brief Construct by value */ - iterator(uint64_t index, const MapNode* self) + iterator(uint64_t index, const MapObj* self) : state_marker(self->state_marker), index(index), self(self) {} #else - iterator(uint64_t index, const MapNode* self) : index(index), self(self) {} + iterator(uint64_t index, const MapObj* self) : index(index), self(self) {} #endif // TVM_FFI_DEBUG_WITH_ABI_CHANGE /*! \brief The position on the array */ uint64_t index; /*! \brief The container it points to */ - const MapNode* self; + const MapObj* self; - friend class DenseMapNode; - friend class SmallMapNode; + friend class DenseMapObj; + friend class SmallMapObj; }; /*! * \brief Create an empty container * \return The object created */ - static inline ObjectPtr<MapNode> Empty(); + static inline ObjectPtr<MapObj> Empty(); protected: #if TVM_FFI_DEBUG_WITH_ABI_CHANGE @@ -200,11 +200,11 @@ class MapNode : public Object { */ static inline void InsertMaybeReHash(const KVType& kv, ObjectPtr<Object>* map); /*! - * \brief Create an empty container with elements copying from another SmallMapNode + * \brief Create an empty container with elements copying from another SmallMapObj * \param from The source container * \return The object created */ - static inline ObjectPtr<MapNode> CopyFrom(MapNode* from); + static inline ObjectPtr<MapObj> CopyFrom(MapObj* from); /*! \brief number of slots minus 1 */ uint64_t slots_; /*! \brief number of entries in the container */ @@ -215,20 +215,20 @@ class MapNode : public Object { }; /*! \brief A specialization of small-sized hash map */ -class SmallMapNode : public MapNode, - public details::InplaceArrayBase<SmallMapNode, MapNode::KVType> { +class SmallMapObj : public MapObj, + public details::InplaceArrayBase<SmallMapObj, MapObj::KVType> { private: static constexpr uint64_t kInitSize = 2; static constexpr uint64_t kMaxSize = 4; public: - using MapNode::iterator; - using MapNode::KVType; + using MapObj::iterator; + using MapObj::KVType; /*! \brief Defaults to the destructor of InplaceArrayBase */ - ~SmallMapNode() = default; + ~SmallMapObj() = default; /*! - * \brief Count the number of times a key exists in the SmallMapNode + * \brief Count the number of times a key exists in the SmallMapObj * \param key The indexing key * \return The result, 0 or 1 */ @@ -283,7 +283,7 @@ class SmallMapNode : public MapNode, private: /*! - * \brief Remove a position in SmallMapNode + * \brief Remove a position in SmallMapObj * \param index The position to be removed */ void Erase(const uint64_t index) { @@ -305,9 +305,9 @@ class SmallMapNode : public MapNode, * \param n Number of empty slots * \return The object created */ - static ObjectPtr<SmallMapNode> Empty(uint64_t n = kInitSize) { + static ObjectPtr<SmallMapObj> Empty(uint64_t n = kInitSize) { using ::tvm::ffi::make_inplace_array_object; - ObjectPtr<SmallMapNode> p = make_inplace_array_object<SmallMapNode, KVType>(n); + ObjectPtr<SmallMapObj> p = make_inplace_array_object<SmallMapObj, KVType>(n); p->size_ = 0; p->slots_ = n; return p; @@ -321,8 +321,8 @@ class SmallMapNode : public MapNode, * \return The object created */ template <typename IterType> - static ObjectPtr<SmallMapNode> CreateFromRange(uint64_t n, IterType first, IterType last) { - ObjectPtr<SmallMapNode> p = Empty(n); + static ObjectPtr<SmallMapObj> CreateFromRange(uint64_t n, IterType first, IterType last) { + ObjectPtr<SmallMapObj> p = Empty(n); KVType* ptr = static_cast<KVType*>(p->AddressOf(0)); for (; first != last; ++first, ++p->size_) { new (ptr++) KVType(*first); @@ -330,11 +330,11 @@ class SmallMapNode : public MapNode, return p; } /*! - * \brief Create an empty container with elements copying from another SmallMapNode + * \brief Create an empty container with elements copying from another SmallMapObj * \param from The source container * \return The object created */ - static ObjectPtr<SmallMapNode> CopyFrom(SmallMapNode* from) { + static ObjectPtr<SmallMapObj> CopyFrom(SmallMapObj* from) { KVType* first = static_cast<KVType*>(from->AddressOf(0)); KVType* last = first + from->size_; return CreateFromRange(from->size_, first, last); @@ -345,7 +345,7 @@ class SmallMapNode : public MapNode, * \param map The pointer to the map, can be changed if re-hashing happens */ static void InsertMaybeReHash(const KVType& kv, ObjectPtr<Object>* map) { - SmallMapNode* map_node = static_cast<SmallMapNode*>(map->get()); + SmallMapObj* map_node = static_cast<SmallMapObj*>(map->get()); iterator itr = map_node->find(kv.first); if (itr.index < map_node->size_) { itr->second = kv.second; @@ -386,9 +386,9 @@ class SmallMapNode : public MapNode, uint64_t GetSize() const { return size_; } protected: - friend class MapNode; - friend class DenseMapNode; - friend class details::InplaceArrayBase<SmallMapNode, MapNode::KVType>; + friend class MapObj; + friend class DenseMapObj; + friend class details::InplaceArrayBase<SmallMapObj, MapObj::KVType>; }; /*! \brief A specialization of hash map that implements the idea of array-based hash map. @@ -396,7 +396,7 @@ class SmallMapNode : public MapNode, * * A. Overview * - * DenseMapNode did several improvements over traditional separate chaining hash, + * DenseMapObj did several improvements over traditional separate chaining hash, * in terms of cache locality, memory footprints and data organization. * * A1. Implicit linked list. For better cache locality, instead of using linked list @@ -449,7 +449,7 @@ class SmallMapNode : public MapNode, * [2] https://programmingpraxis.com/2018/06/19/fibonacci-hash/ * [3] https://fgiesen.wordpress.com/2015/02/22/triangular-numbers-mod-2n/ */ -class DenseMapNode : public MapNode { +class DenseMapObj : public MapObj { private: /*! \brief The number of elements in a memory block */ static constexpr int kBlockCap = 16; @@ -471,12 +471,12 @@ class DenseMapNode : public MapNode { static_assert(std::is_standard_layout<Block>::value, "Block is not standard layout"); public: - using MapNode::iterator; + using MapObj::iterator; /*! - * \brief Destroy the DenseMapNode + * \brief Destroy the DenseMapObj */ - ~DenseMapNode() { this->Reset(); } + ~DenseMapObj() { this->Reset(); } /*! \return The number of elements of the key */ size_t count(const key_type& key) const { return !Search(key).IsNone(); } /*! @@ -717,9 +717,9 @@ class DenseMapNode : public MapNode { * \param n_slots Number of slots required, should be power-of-two * \return The object created */ - static ObjectPtr<DenseMapNode> Empty(uint32_t fib_shift, uint64_t n_slots) { - TVM_FFI_ICHECK_GT(n_slots, uint64_t(SmallMapNode::kMaxSize)); - ObjectPtr<DenseMapNode> p = make_object<DenseMapNode>(); + static ObjectPtr<DenseMapObj> Empty(uint32_t fib_shift, uint64_t n_slots) { + TVM_FFI_ICHECK_GT(n_slots, uint64_t(SmallMapObj::kMaxSize)); + ObjectPtr<DenseMapObj> p = make_object<DenseMapObj>(); uint64_t n_blocks = CalcNumBlocks(n_slots - 1); Block* block = p->data_ = new Block[n_blocks]; p->slots_ = n_slots - 1; @@ -731,12 +731,12 @@ class DenseMapNode : public MapNode { return p; } /*! - * \brief Create an empty container with elements copying from another DenseMapNode + * \brief Create an empty container with elements copying from another DenseMapObj * \param from The source container * \return The object created */ - static ObjectPtr<DenseMapNode> CopyFrom(DenseMapNode* from) { - ObjectPtr<DenseMapNode> p = make_object<DenseMapNode>(); + static ObjectPtr<DenseMapObj> CopyFrom(DenseMapObj* from) { + ObjectPtr<DenseMapObj> p = make_object<DenseMapObj>(); uint64_t n_blocks = CalcNumBlocks(from->slots_); p->data_ = new Block[n_blocks]; p->slots_ = from->slots_; @@ -764,14 +764,14 @@ class DenseMapNode : public MapNode { * \param map The pointer to the map, can be changed if re-hashing happens */ static void InsertMaybeReHash(const KVType& kv, ObjectPtr<Object>* map) { - DenseMapNode* map_node = static_cast<DenseMapNode*>(map->get()); + DenseMapObj* map_node = static_cast<DenseMapObj*>(map->get()); ListNode iter; // Try to insert. If succeed, we simply return if (map_node->TryInsert(kv.first, &iter)) { iter.Val() = kv.second; return; } - TVM_FFI_ICHECK_GT(map_node->slots_, uint64_t(SmallMapNode::kMaxSize)); + TVM_FFI_ICHECK_GT(map_node->slots_, uint64_t(SmallMapObj::kMaxSize)); // Otherwise, start rehash ObjectPtr<Object> p = Empty(map_node->fib_shift_ - 1, map_node->slots_ * 2 + 2); // Insert the given `kv` into the new hash map @@ -883,7 +883,7 @@ class DenseMapNode : public MapNode { /*! \brief Construct None */ ListNode() : index(0), block(nullptr) {} /*! \brief Construct from position */ - ListNode(uint64_t index, const DenseMapNode* self) + ListNode(uint64_t index, const DenseMapObj* self) : index(index), block(self->data_ + (index / kBlockCap)) {} /*! \brief Metadata on the entry */ uint8_t& Meta() const { return *(block->bytes + index % kBlockCap); } @@ -923,7 +923,7 @@ class DenseMapNode : public MapNode { /*! \brief If the entry has next entry on the linked list */ bool HasNext() const { return NextProbeLocation(Meta() & 0b01111111) != 0; } /*! \brief Move the entry to the next entry on the linked list */ - bool MoveToNext(const DenseMapNode* self, uint8_t meta) { + bool MoveToNext(const DenseMapObj* self, uint8_t meta) { uint64_t offset = NextProbeLocation(meta & 0b01111111); if (offset == 0) { index = 0; @@ -936,9 +936,9 @@ class DenseMapNode : public MapNode { return true; } /*! \brief Move the entry to the next entry on the linked list */ - bool MoveToNext(const DenseMapNode* self) { return MoveToNext(self, Meta()); } + bool MoveToNext(const DenseMapObj* self) { return MoveToNext(self, Meta()); } /*! \brief Get the previous entry on the linked list */ - ListNode FindPrev(const DenseMapNode* self) const { + ListNode FindPrev(const DenseMapObj* self) const { // start from the head of the linked list, which must exist ListNode next = self->IndexFromHash(AnyHash()(Key())); // `prev` is always the previous item of `next` @@ -948,7 +948,7 @@ class DenseMapNode : public MapNode { return prev; } /*! \brief Get the next empty jump */ - bool GetNextEmpty(const DenseMapNode* self, uint8_t* jump, ListNode* result) const { + bool GetNextEmpty(const DenseMapObj* self, uint8_t* jump, ListNode* result) const { for (uint8_t idx = 1; idx < kNumJumpDists; ++idx) { ListNode candidate((index + NextProbeLocation(idx)) & (self->slots_), self); if (candidate.IsEmpty()) { @@ -1000,15 +1000,15 @@ class DenseMapNode : public MapNode { /* clang-format on */ return kNextProbeLocation[index]; } - friend class MapNode; + friend class MapObj; }; #define TVM_DISPATCH_MAP(base, var, body) \ { \ - using TSmall = SmallMapNode*; \ - using TDense = DenseMapNode*; \ + using TSmall = SmallMapObj*; \ + using TDense = DenseMapObj*; \ uint64_t slots = base->slots_; \ - if (slots <= SmallMapNode::kMaxSize) { \ + if (slots <= SmallMapObj::kMaxSize) { \ TSmall var = static_cast<TSmall>(base); \ body; \ } else { \ @@ -1019,10 +1019,10 @@ class DenseMapNode : public MapNode { #define TVM_DISPATCH_MAP_CONST(base, var, body) \ { \ - using TSmall = const SmallMapNode*; \ - using TDense = const DenseMapNode*; \ + using TSmall = const SmallMapObj*; \ + using TDense = const DenseMapObj*; \ uint64_t slots = base->slots_; \ - if (slots <= SmallMapNode::kMaxSize) { \ + if (slots <= SmallMapObj::kMaxSize) { \ TSmall var = static_cast<TSmall>(base); \ body; \ } else { \ @@ -1031,12 +1031,12 @@ class DenseMapNode : public MapNode { } \ } -inline MapNode::iterator::pointer MapNode::iterator::operator->() const { +inline MapObj::iterator::pointer MapObj::iterator::operator->() const { TVM_FFI_MAP_FAIL_IF_CHANGED() TVM_DISPATCH_MAP_CONST(self, p, { return p->DeRefItr(index); }); } -inline MapNode::iterator& MapNode::iterator::operator++() { +inline MapObj::iterator& MapObj::iterator::operator++() { TVM_FFI_MAP_FAIL_IF_CHANGED() TVM_DISPATCH_MAP_CONST(self, p, { index = p->IncItr(index); @@ -1044,7 +1044,7 @@ inline MapNode::iterator& MapNode::iterator::operator++() { }); } -inline MapNode::iterator& MapNode::iterator::operator--() { +inline MapObj::iterator& MapObj::iterator::operator--() { TVM_FFI_MAP_FAIL_IF_CHANGED() TVM_DISPATCH_MAP_CONST(self, p, { index = p->DecItr(index); @@ -1052,91 +1052,91 @@ inline MapNode::iterator& MapNode::iterator::operator--() { }); } -inline size_t MapNode::count(const key_type& key) const { +inline size_t MapObj::count(const key_type& key) const { TVM_DISPATCH_MAP_CONST(this, p, { return p->count(key); }); } -inline const MapNode::mapped_type& MapNode::at(const MapNode::key_type& key) const { +inline const MapObj::mapped_type& MapObj::at(const MapObj::key_type& key) const { TVM_DISPATCH_MAP_CONST(this, p, { return p->at(key); }); } -inline MapNode::mapped_type& MapNode::at(const MapNode::key_type& key) { +inline MapObj::mapped_type& MapObj::at(const MapObj::key_type& key) { TVM_DISPATCH_MAP(this, p, { return p->at(key); }); } -inline MapNode::iterator MapNode::begin() const { +inline MapObj::iterator MapObj::begin() const { TVM_DISPATCH_MAP_CONST(this, p, { return p->begin(); }); } -inline MapNode::iterator MapNode::end() const { +inline MapObj::iterator MapObj::end() const { TVM_DISPATCH_MAP_CONST(this, p, { return p->end(); }); } -inline MapNode::iterator MapNode::find(const MapNode::key_type& key) const { +inline MapObj::iterator MapObj::find(const MapObj::key_type& key) const { TVM_DISPATCH_MAP_CONST(this, p, { return p->find(key); }); } -inline void MapNode::erase(const MapNode::iterator& position) { +inline void MapObj::erase(const MapObj::iterator& position) { TVM_DISPATCH_MAP(this, p, { return p->erase(position); }); } #undef TVM_DISPATCH_MAP #undef TVM_DISPATCH_MAP_CONST -inline ObjectPtr<MapNode> MapNode::Empty() { return SmallMapNode::Empty(); } +inline ObjectPtr<MapObj> MapObj::Empty() { return SmallMapObj::Empty(); } -inline ObjectPtr<MapNode> MapNode::CopyFrom(MapNode* from) { - if (from->slots_ <= SmallMapNode::kMaxSize) { - return SmallMapNode::CopyFrom(static_cast<SmallMapNode*>(from)); +inline ObjectPtr<MapObj> MapObj::CopyFrom(MapObj* from) { + if (from->slots_ <= SmallMapObj::kMaxSize) { + return SmallMapObj::CopyFrom(static_cast<SmallMapObj*>(from)); } else { - return DenseMapNode::CopyFrom(static_cast<DenseMapNode*>(from)); + return DenseMapObj::CopyFrom(static_cast<DenseMapObj*>(from)); } } template <typename IterType> -inline ObjectPtr<Object> MapNode::CreateFromRange(IterType first, IterType last) { +inline ObjectPtr<Object> MapObj::CreateFromRange(IterType first, IterType last) { int64_t _cap = std::distance(first, last); if (_cap < 0) { - return SmallMapNode::Empty(); + return SmallMapObj::Empty(); } uint64_t cap = static_cast<uint64_t>(_cap); - if (cap < SmallMapNode::kMaxSize) { - return SmallMapNode::CreateFromRange(cap, first, last); + if (cap < SmallMapObj::kMaxSize) { + return SmallMapObj::CreateFromRange(cap, first, last); } uint32_t fib_shift; uint64_t n_slots; - DenseMapNode::CalcTableSize(cap, &fib_shift, &n_slots); - ObjectPtr<Object> obj = DenseMapNode::Empty(fib_shift, n_slots); + DenseMapObj::CalcTableSize(cap, &fib_shift, &n_slots); + ObjectPtr<Object> obj = DenseMapObj::Empty(fib_shift, n_slots); for (; first != last; ++first) { KVType kv(*first); - DenseMapNode::InsertMaybeReHash(kv, &obj); + DenseMapObj::InsertMaybeReHash(kv, &obj); } return obj; } -inline void MapNode::InsertMaybeReHash(const KVType& kv, ObjectPtr<Object>* map) { - constexpr uint64_t kSmallMapMaxSize = SmallMapNode::kMaxSize; - MapNode* base = static_cast<MapNode*>(map->get()); +inline void MapObj::InsertMaybeReHash(const KVType& kv, ObjectPtr<Object>* map) { + constexpr uint64_t kSmallMapMaxSize = SmallMapObj::kMaxSize; + MapObj* base = static_cast<MapObj*>(map->get()); #if TVM_FFI_DEBUG_WITH_ABI_CHANGE base->state_marker++; #endif // TVM_FFI_DEBUG_WITH_ABI_CHANGE if (base->slots_ < kSmallMapMaxSize) { - SmallMapNode::InsertMaybeReHash(kv, map); + SmallMapObj::InsertMaybeReHash(kv, map); } else if (base->slots_ == kSmallMapMaxSize) { if (base->size_ < base->slots_) { - SmallMapNode::InsertMaybeReHash(kv, map); + SmallMapObj::InsertMaybeReHash(kv, map); } else { - ObjectPtr<Object> new_map = MapNode::CreateFromRange(base->begin(), base->end()); - DenseMapNode::InsertMaybeReHash(kv, &new_map); + ObjectPtr<Object> new_map = MapObj::CreateFromRange(base->begin(), base->end()); + DenseMapObj::InsertMaybeReHash(kv, &new_map); *map = std::move(new_map); } } else { - DenseMapNode::InsertMaybeReHash(kv, map); + DenseMapObj::InsertMaybeReHash(kv, map); } } template <> -inline ObjectPtr<MapNode> make_object<>() = delete; +inline ObjectPtr<MapObj> make_object<>() = delete; /*! * \brief Map container of NodeRef->NodeRef in DSL graph. @@ -1158,7 +1158,7 @@ class Map : public ObjectRef { /*! * \brief default constructor */ - Map() { data_ = MapNode::Empty(); } + Map() { data_ = MapObj::Empty(); } /*! * \brief move constructor * \param other source @@ -1216,14 +1216,14 @@ class Map : public ObjectRef { */ template <typename IterType> Map(IterType begin, IterType end) { - data_ = MapNode::CreateFromRange(begin, end); + data_ = MapObj::CreateFromRange(begin, end); } /*! * \brief constructor from initializer list * \param init The initalizer list */ Map(std::initializer_list<std::pair<K, V>> init) { - data_ = MapNode::CreateFromRange(init.begin(), init.end()); + data_ = MapObj::CreateFromRange(init.begin(), init.end()); } /*! * \brief constructor from unordered_map @@ -1231,7 +1231,7 @@ class Map : public ObjectRef { */ template <typename Hash, typename Equal> Map(const std::unordered_map<K, V, Hash, Equal>& init) { // NOLINT(*) - data_ = MapNode::CreateFromRange(init.begin(), init.end()); + data_ = MapObj::CreateFromRange(init.begin(), init.end()); } /*! * \brief Read element from map. @@ -1239,7 +1239,7 @@ class Map : public ObjectRef { * \return the corresonding element. */ const V at(const K& key) const { - return details::AnyUnsafe::CopyFromAnyStorageAfterCheck<V>(GetMapNode()->at(key)); + return details::AnyUnsafe::CopyFromAnyStorageAfterCheck<V>(GetMapObj()->at(key)); } /*! * \brief Read element from map. @@ -1249,21 +1249,21 @@ class Map : public ObjectRef { const V operator[](const K& key) const { return this->at(key); } /*! \return The size of the array */ size_t size() const { - MapNode* n = GetMapNode(); + MapObj* n = GetMapObj(); return n == nullptr ? 0 : n->size(); } /*! \return The number of elements of the key */ size_t count(const K& key) const { - MapNode* n = GetMapNode(); - return n == nullptr ? 0 : GetMapNode()->count(key); + MapObj* n = GetMapObj(); + return n == nullptr ? 0 : GetMapObj()->count(key); } /*! \return whether array is empty */ bool empty() const { return size() == 0; } /*! \brief Release reference to all the elements */ void clear() { - MapNode* n = GetMapNode(); + MapObj* n = GetMapObj(); if (n != nullptr) { - data_ = MapNode::Empty(); + data_ = MapObj::Empty(); } } /*! @@ -1273,18 +1273,18 @@ class Map : public ObjectRef { */ void Set(const K& key, const V& value) { CopyOnWrite(); - MapNode::InsertMaybeReHash(MapNode::KVType(key, value), &data_); + MapObj::InsertMaybeReHash(MapObj::KVType(key, value), &data_); } /*! \return begin iterator */ - iterator begin() const { return iterator(GetMapNode()->begin()); } + iterator begin() const { return iterator(GetMapObj()->begin()); } /*! \return end iterator */ - iterator end() const { return iterator(GetMapNode()->end()); } + iterator end() const { return iterator(GetMapObj()->end()); } /*! \return find the key and returns the associated iterator */ - iterator find(const K& key) const { return iterator(GetMapNode()->find(key)); } + iterator find(const K& key) const { return iterator(GetMapObj()->find(key)); } /*! \return The value associated with the key, NullOpt if not found */ std::optional<V> Get(const K& key) const { - MapNode::iterator iter = GetMapNode()->find(key); - if (iter == GetMapNode()->end()) { + MapObj::iterator iter = GetMapObj()->find(key); + if (iter == GetMapObj()->end()) { return std::nullopt; } return details::AnyUnsafe::CopyFromAnyStorageAfterCheck<V>(iter->second); @@ -1299,16 +1299,16 @@ class Map : public ObjectRef { * * \return Handle to the internal node container(which guarantees to be unique) */ - MapNode* CopyOnWrite() { + MapObj* CopyOnWrite() { if (data_.get() == nullptr) { - data_ = MapNode::Empty(); + data_ = MapObj::Empty(); } else if (!data_.unique()) { - data_ = MapNode::CopyFrom(GetMapNode()); + data_ = MapObj::CopyFrom(GetMapObj()); } - return GetMapNode(); + return GetMapObj(); } /*! \brief specify container node */ - using ContainerType = MapNode; + using ContainerType = MapObj; /*! \brief Iterator of the hash map */ class iterator { @@ -1346,18 +1346,18 @@ class Map : public ObjectRef { } private: - iterator(const MapNode::iterator& itr) // NOLINT(*) + iterator(const MapObj::iterator& itr) // NOLINT(*) : itr(itr) {} template <typename, typename, typename> friend class Map; - MapNode::iterator itr; + MapObj::iterator itr; }; private: - /*! \brief Return data_ as type of pointer of MapNode */ - MapNode* GetMapNode() const { return static_cast<MapNode*>(data_.get()); } + /*! \brief Return data_ as type of pointer of MapObj */ + MapObj* GetMapObj() const { return static_cast<MapObj*>(data_.get()); } template <typename, typename, typename> friend class Map; @@ -1393,7 +1393,7 @@ struct TypeTraits<Map<K, V>> : public ObjectRefTypeTraitsBase<Map<K, V>> { return TypeTraitsBase::GetMismatchTypeInfo(src); } if constexpr (!std::is_same_v<K, Any> || !std::is_same_v<V, Any>) { - const MapNode* n = reinterpret_cast<const MapNode*>(src->v_obj); + const MapObj* n = reinterpret_cast<const MapObj*>(src->v_obj); for (const auto& kv : *n) { if constexpr (!std::is_same_v<K, Any>) { if (!details::AnyUnsafe::CheckAnyStorage<K>(kv.first) && !kv.first.as<K>().has_value()) { @@ -1419,7 +1419,7 @@ struct TypeTraits<Map<K, V>> : public ObjectRefTypeTraitsBase<Map<K, V>> { if constexpr (std::is_same_v<K, Any> && std::is_same_v<V, Any>) { return true; } else { - const MapNode* n = reinterpret_cast<const MapNode*>(src->v_obj); + const MapObj* n = reinterpret_cast<const MapObj*>(src->v_obj); for (const auto& kv : *n) { if constexpr (!std::is_same_v<K, Any>) { if (!details::AnyUnsafe::CheckAnyStorage<K>(kv.first)) return false; @@ -1435,7 +1435,7 @@ struct TypeTraits<Map<K, V>> : public ObjectRefTypeTraitsBase<Map<K, V>> { static TVM_FFI_INLINE std::optional<Map<K, V>> TryConvertFromAnyView(const TVMFFIAny* src) { if (src->type_index != TypeIndex::kTVMFFIMap) return std::nullopt; if constexpr (!std::is_same_v<K, Any> || !std::is_same_v<V, Any>) { - const MapNode* n = reinterpret_cast<const MapNode*>(src->v_obj); + const MapObj* n = reinterpret_cast<const MapObj*>(src->v_obj); bool storage_check = [&]() { for (const auto& kv : *n) { if constexpr (!std::is_same_v<K, Any>) { diff --git a/ffi/include/tvm/ffi/container/tuple.h b/ffi/include/tvm/ffi/container/tuple.h index 085c193ad3..8cc17eb302 100644 --- a/ffi/include/tvm/ffi/container/tuple.h +++ b/ffi/include/tvm/ffi/container/tuple.h @@ -19,7 +19,7 @@ /*! * \file tvm/ffi/container/tuple.h - * \brief Typed tuple like std::tuple backed by ArrayNode container. + * \brief Typed tuple like std::tuple backed by ArrayObj container. */ #ifndef TVM_FFI_CONTAINER_TUPLE_H_ #define TVM_FFI_CONTAINER_TUPLE_H_ @@ -34,7 +34,7 @@ namespace tvm { namespace ffi { /*! - * \brief Typed tuple like std::tuple backed by ArrayNode container. + * \brief Typed tuple like std::tuple backed by ArrayObj container. * * Tuple implements in-place copy-on-write semantics. * @@ -96,7 +96,7 @@ class Tuple : public ObjectRef { auto Get() const { static_assert(I < sizeof...(Types), "Tuple index out of bounds"); using ReturnType = std::tuple_element_t<I, std::tuple<Types...>>; - const Any* ptr = GetArrayNode()->begin() + I; + const Any* ptr = GetArrayObj()->begin() + I; return details::AnyUnsafe::CopyFromAnyStorageAfterCheck<ReturnType>(*ptr); } @@ -115,16 +115,16 @@ class Tuple : public ObjectRef { static_assert(I < sizeof...(Types), "Tuple index out of bounds"); using T = std::tuple_element_t<I, std::tuple<Types...>>; this->CopyIfNotUnique(); - Any* ptr = GetArrayNode()->MutableBegin() + I; + Any* ptr = GetArrayObj()->MutableBegin() + I; *ptr = T(std::forward<U>(item)); } /*! \brief specify container node */ - using ContainerType = ArrayNode; + using ContainerType = ArrayObj; private: - static ObjectPtr<ArrayNode> MakeDefaultTupleNode() { - ObjectPtr<ArrayNode> p = make_inplace_array_object<ArrayNode, Any>(sizeof...(Types)); + static ObjectPtr<ArrayObj> MakeDefaultTupleNode() { + ObjectPtr<ArrayObj> p = make_inplace_array_object<ArrayObj, Any>(sizeof...(Types)); p->capacity_ = sizeof...(Types); // immeidate set size to 0, to ensure exception safety p->size_ = 0; @@ -135,8 +135,8 @@ class Tuple : public ObjectRef { } template <typename... UTypes> - static ObjectPtr<ArrayNode> MakeTupleNode(UTypes&&... args) { - ObjectPtr<ArrayNode> p = make_inplace_array_object<ArrayNode, Any>(sizeof...(Types)); + static ObjectPtr<ArrayObj> MakeTupleNode(UTypes&&... args) { + ObjectPtr<ArrayObj> p = make_inplace_array_object<ArrayObj, Any>(sizeof...(Types)); p->capacity_ = sizeof...(Types); // immeidate set size to 0, to ensure exception safety p->size_ = 0; @@ -149,12 +149,12 @@ class Tuple : public ObjectRef { /*! \brief Copy on write */ void CopyIfNotUnique() { if (!data_.unique()) { - ObjectPtr<ArrayNode> p = make_inplace_array_object<ArrayNode, Any>(sizeof...(Types)); + ObjectPtr<ArrayObj> p = make_inplace_array_object<ArrayObj, Any>(sizeof...(Types)); p->capacity_ = sizeof...(Types); // immeidate set size to 0, to ensure exception safety p->size_ = 0; Any* itr = p->MutableBegin(); - const Any* read = GetArrayNode()->begin(); + const Any* read = GetArrayObj()->begin(); // increase size after each new to ensure exception safety for (size_t i = 0; i < sizeof...(Types); ++i) { new (itr++) Any(*read++); @@ -164,8 +164,8 @@ class Tuple : public ObjectRef { } } - /*! \return The underlying ArrayNode */ - ArrayNode* GetArrayNode() const { return static_cast<ArrayNode*>(data_.get()); } + /*! \return The underlying ArrayObj */ + ArrayObj* GetArrayObj() const { return static_cast<ArrayObj*>(data_.get()); } template <typename... UTypes> friend class Tuple; @@ -182,7 +182,7 @@ struct TypeTraits<Tuple<Types...>> : public ObjectRefTypeTraitsBase<Tuple<Types. if (src->type_index != TypeIndex::kTVMFFIArray) { return TypeTraitsBase::GetMismatchTypeInfo(src); } - const ArrayNode* n = reinterpret_cast<const ArrayNode*>(src->v_obj); + const ArrayObj* n = reinterpret_cast<const ArrayObj*>(src->v_obj); if (n->size() != sizeof...(Types)) { return "Array[size=" + std::to_string(n->size()) + "]"; } @@ -208,7 +208,7 @@ struct TypeTraits<Tuple<Types...>> : public ObjectRefTypeTraitsBase<Tuple<Types. static TVM_FFI_INLINE bool CheckAnyStorage(const TVMFFIAny* src) { if (src->type_index != TypeIndex::kTVMFFIArray) return false; - const ArrayNode* n = reinterpret_cast<const ArrayNode*>(src->v_obj); + const ArrayObj* n = reinterpret_cast<const ArrayObj*>(src->v_obj); if (n->size() != sizeof...(Types)) return false; const TVMFFIAny* ffi_any_arr = reinterpret_cast<const TVMFFIAny*>(n->begin()); return CheckAnyStorageHelper<0, Types...>(ffi_any_arr); @@ -231,7 +231,7 @@ struct TypeTraits<Tuple<Types...>> : public ObjectRefTypeTraitsBase<Tuple<Types. const TVMFFIAny* src // ) { if (src->type_index != TypeIndex::kTVMFFIArray) return std::nullopt; - const ArrayNode* n = reinterpret_cast<const ArrayNode*>(src->v_obj); + const ArrayObj* n = reinterpret_cast<const ArrayObj*>(src->v_obj); if (n->size() != sizeof...(Types)) return std::nullopt; // fast path, storage is already in the right type if (CheckAnyStorage(src)) { diff --git a/ffi/tests/cpp/test_map.cc b/ffi/tests/cpp/test_map.cc index 3e295fa3d9..de4beb3150 100644 --- a/ffi/tests/cpp/test_map.cc +++ b/ffi/tests/cpp/test_map.cc @@ -245,7 +245,7 @@ TEST(Map, AnyConvertCheck) { TEST(Map, PackedFuncGetItem) { Function f = Function::FromUnpacked( - [](const MapNode* n, const Any& k) -> Any { return n->at(k); }, "map_get_item"); + [](const MapObj* n, const Any& k) -> Any { return n->at(k); }, "map_get_item"); Map<String, int64_t> map{{"x", 1}, {"y", 2}}; Any k("x"); Any v = f(map, k);
