Skip to content

Commit e8a5516

Browse files
authored
[CORE] Invert object/type_traits dependency layering (#638)
## Summary - invert the header dependency so `object.h` includes `type_traits.h`, and move shared type metadata/helpers like `StaticTypeKey`, `TypeIndexToTypeKey`, and `details::type_contains_v` into `type_traits.h` - move object and `Optional` trait specializations into their owning headers, move `DLTensor*` traits into `tensor.h`, and keep the `uint64_t` overflow-checked specialization near the end of `any.h` - add a narrow ASF header checker skip for `.agents/skills/**/SKILL.md` so skill files can keep frontmatter at byte 0
1 parent 102fe0e commit e8a5516

10 files changed

Lines changed: 426 additions & 459 deletions

File tree

.agents/skills/devtools/SKILL.md

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
<!--- Licensed to the Apache Software Foundation (ASF) under one -->
2-
<!--- or more contributor license agreements. See the NOTICE file -->
3-
<!--- distributed with this work for additional information -->
4-
<!--- regarding copyright ownership. The ASF licenses this file -->
5-
<!--- to you under the Apache License, Version 2.0 (the -->
6-
<!--- "License"); you may not use this file except in compliance -->
7-
<!--- with the License. You may obtain a copy of the License at -->
8-
9-
<!--- http://www.apache.org/licenses/LICENSE-2.0 -->
10-
11-
<!--- Unless required by applicable law or agreed to in writing, -->
12-
<!--- software distributed under the License is distributed on an -->
13-
<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
14-
<!--- KIND, either express or implied. See the License for the -->
15-
<!--- specific language governing permissions and limitations -->
16-
<!--- under the License. -->
17-
181
---
192
name: devtools
203
description: Developer reference for Apache TVM-FFI.

include/tvm/ffi/any.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,23 @@ struct AnyEqual {
823823
}
824824
}
825825
};
826+
827+
// Placed near the end because this specialization depends on error handling.
828+
template <>
829+
struct TypeTraits<uint64_t> : public TypeTraitsIntBase<uint64_t> {
830+
TVM_FFI_INLINE static void CopyToAnyView(const uint64_t& src, TVMFFIAny* result) {
831+
if (src > static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) {
832+
TVM_FFI_THROW(OverflowError)
833+
<< "Integer value " << src << " is too large to fit in int64_t. "
834+
<< "Consider explicitly casting to int64_t first if this is intentional.";
835+
}
836+
TypeTraitsIntBase<uint64_t>::CopyInt64ToAnyView(static_cast<int64_t>(src), result);
837+
}
838+
839+
TVM_FFI_INLINE static void MoveToAny(uint64_t src, TVMFFIAny* result) {
840+
CopyToAnyView(src, result);
841+
}
842+
};
826843
} // namespace ffi
827844

828845
// Expose to the tvm namespace for usability

include/tvm/ffi/container/container_details.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -178,20 +178,6 @@ inline constexpr bool all_storage_enabled_v = (storage_enabled_v<T> && ...);
178178
*/
179179
template <typename... T>
180180
inline constexpr bool all_object_ref_v = (std::is_base_of_v<ObjectRef, T> && ...);
181-
/**
182-
* \brief Check if Any storage of Derived can always be directly used as Base.
183-
*
184-
* \tparam Base The base type.
185-
* \tparam Derived The derived type.
186-
* \return True if Derived's storage can be used as Base's storage, false otherwise.
187-
*/
188-
template <typename Base, typename Derived>
189-
inline constexpr bool type_contains_v =
190-
std::is_base_of_v<Base, Derived> || std::is_same_v<Base, Derived>;
191-
// special case for Any
192-
template <typename Derived>
193-
inline constexpr bool type_contains_v<Any, Derived> = true;
194-
195181
/*!
196182
* \brief Create a string of the container type.
197183
* \tparam V The types of the elements in the container.

include/tvm/ffi/container/tensor.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,50 @@ inline size_t GetDataSize(const TensorView& tensor) {
884884
return GetDataSize(tensor.numel(), tensor.dtype());
885885
}
886886

887+
// DLTensor* support lives with Tensor/TensorView because the conversion path
888+
// is tensor-specific and can reuse the FFI error layer.
889+
template <>
890+
struct TypeTraits<DLTensor*> : public TypeTraitsBase {
891+
static constexpr bool storage_enabled = false;
892+
static constexpr int32_t field_static_type_index = TypeIndex::kTVMFFIDLTensorPtr;
893+
894+
TVM_FFI_INLINE static void CopyToAnyView(DLTensor* src, TVMFFIAny* result) {
895+
TVM_FFI_ICHECK_NOTNULL(src);
896+
result->type_index = TypeIndex::kTVMFFIDLTensorPtr;
897+
result->zero_padding = 0;
898+
TVM_FFI_CLEAR_PTR_PADDING_IN_FFI_ANY(result);
899+
result->v_ptr = src;
900+
}
901+
902+
TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
903+
return src->type_index == TypeIndex::kTVMFFIDLTensorPtr;
904+
}
905+
906+
TVM_FFI_INLINE static DLTensor* CopyFromAnyViewAfterCheck(const TVMFFIAny* src) {
907+
TVM_FFI_UNSAFE_ASSUME(src->type_index == TypeIndex::kTVMFFIDLTensorPtr);
908+
return static_cast<DLTensor*>(src->v_ptr);
909+
}
910+
911+
TVM_FFI_INLINE static void MoveToAny(DLTensor*, TVMFFIAny*) {
912+
TVM_FFI_THROW(RuntimeError)
913+
<< "DLTensor* cannot be held in Any as it does not retain ownership, use Tensor instead";
914+
}
915+
916+
TVM_FFI_INLINE static std::optional<DLTensor*> TryCastFromAnyView(const TVMFFIAny* src) {
917+
if (src->type_index == TypeIndex::kTVMFFIDLTensorPtr) {
918+
return static_cast<DLTensor*>(src->v_ptr);
919+
} else if (src->type_index == TypeIndex::kTVMFFITensor) {
920+
return TVMFFITensorGetDLTensorPtr(src->v_obj);
921+
}
922+
return std::nullopt;
923+
}
924+
925+
TVM_FFI_INLINE static std::string TypeStr() { return StaticTypeKey::kTVMFFIDLTensorPtr; }
926+
TVM_FFI_INLINE static std::string TypeSchema() {
927+
return R"({"type":")" + std::string(StaticTypeKey::kTVMFFIDLTensorPtr) + R"("})";
928+
}
929+
};
930+
887931
// TensorView type, allow implicit casting from DLTensor*
888932
// NOTE: we deliberately do not support MoveToAny and MoveFromAny since it does not retain ownership
889933
template <>

0 commit comments

Comments
 (0)