This is an automated email from the ASF dual-hosted git repository. tqchen pushed a commit to branch refactor-s0 in repository https://gitbox.apache.org/repos/asf/tvm.git
commit dce37a83b363fbd6e9afdfa0a0f1b8a0857ceaa9 Author: tqchen <[email protected]> AuthorDate: Sun Mar 9 09:27:02 2025 -0400 CMake links ok --- CMakeLists.txt | 16 ++-- cmake/modules/contrib/CUTLASS.cmake | 1 + ffi/CMakeLists.txt | 97 ++++++++++++------------ ffi/scripts/run_tests.sh | 1 - ffi/tests/cpp/CMakeLists.txt | 6 +- include/tvm/runtime/object.h | 1 + src/ffi/registry.cc | 144 ------------------------------------ 7 files changed, 63 insertions(+), 203 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5509408d95..b630f12b06 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -507,6 +507,9 @@ list(REMOVE_ITEM COMPILER_SRCS ${LIBINFO_FILE}) add_library(tvm_objs OBJECT ${COMPILER_SRCS}) add_library(tvm_runtime_objs OBJECT ${RUNTIME_SRCS}) add_library(tvm_libinfo_objs OBJECT ${LIBINFO_FILE}) +target_link_libraries(tvm_objs PUBLIC tvm_ffi_header) +target_link_libraries(tvm_runtime_objs PUBLIC tvm_ffi_header) +target_link_libraries(tvm_libinfo_objs PUBLIC tvm_ffi_header) include(GNUInstallDirs) if(NOT BUILD_DUMMY_LIBTVM) @@ -612,8 +615,10 @@ endif() target_link_libraries(tvm PRIVATE ${TVM_RUNTIME_LINKER_LIBS}) target_link_libraries(tvm_runtime PRIVATE ${TVM_RUNTIME_LINKER_LIBS}) -target_link_directories(tvm PRIVATE tvm_ffi) -target_link_directories(tvm_runtime PRIVATE tvm_ffi) + +target_link_libraries(tvm PUBLIC tvm_ffi_objs) +target_link_libraries(tvm_runtime PUBLIC tvm_ffi_objs) + if(BUILD_FOR_HEXAGON AND DEFINED USE_HEXAGON_GTEST AND EXISTS ${USE_HEXAGON_GTEST}) include(FetchContent) @@ -652,6 +657,7 @@ if (HIDE_PRIVATE_SYMBOLS AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin") target_link_libraries(tvm_runtime PRIVATE ${HIDE_SYMBOLS_LINKER_FLAGS}) target_compile_definitions(tvm_allvisible PUBLIC $<TARGET_PROPERTY:tvm,INTERFACE_COMPILE_DEFINITONS>) target_compile_definitions(tvm_allvisible PRIVATE $<TARGET_PROPERTY:tvm,COMPILE_DEFINITONS>) + target_link_libraries(tvm_allvisible PUBLIC tvm_ffi_objs) endif() # Create the `cpptest` target if we can find GTest. If not, we create dummy @@ -709,10 +715,10 @@ if(NOT DEFINED ENV{CONDA_BUILD}) endif() # Installation rules -install(TARGETS tvm EXPORT ${PROJECT_NAME}Targets DESTINATION lib${LIB_SUFFIX}) -install(TARGETS tvm_runtime EXPORT ${PROJECT_NAME}Targets DESTINATION lib${LIB_SUFFIX}) +install(TARGETS tvm DESTINATION lib${LIB_SUFFIX}) +install(TARGETS tvm_runtime DESTINATION lib${LIB_SUFFIX}) if(BUILD_FOR_HEXAGON AND DEFINED USE_HEXAGON_GTEST AND EXISTS ${USE_HEXAGON_GTEST}) - install(TARGETS gtest EXPORT ${PROJECT_NAME}Targets DESTINATION lib${LIB_SUFFIX}) + install(TARGETS gtest DESTINATION lib${LIB_SUFFIX}) endif() if (INSTALL_DEV) diff --git a/cmake/modules/contrib/CUTLASS.cmake b/cmake/modules/contrib/CUTLASS.cmake index b302622cbc..75e7f68c88 100644 --- a/cmake/modules/contrib/CUTLASS.cmake +++ b/cmake/modules/contrib/CUTLASS.cmake @@ -36,6 +36,7 @@ if(USE_CUDA AND USE_CUTLASS) set(CUTLASS_FPA_INTB_RUNTIME_SRCS "") list(APPEND CUTLASS_FPA_INTB_RUNTIME_SRCS src/runtime/contrib/cutlass/weight_preprocess.cc) add_library(fpA_intB_cutlass_objs OBJECT ${CUTLASS_FPA_INTB_RUNTIME_SRCS}) + target_link_libraries(fpA_intB_cutlass_objs PRIVATE tvm_ffi_header) target_compile_definitions(fpA_intB_cutlass_objs PRIVATE DMLC_USE_LOGGING_LIBRARY=<tvm/runtime/logging.h>) target_include_directories(fpA_intB_cutlass_objs PRIVATE ${PROJECT_SOURCE_DIR}/3rdparty/cutlass_fpA_intB_gemm diff --git a/ffi/CMakeLists.txt b/ffi/CMakeLists.txt index 1aa05110a7..154b13ef05 100644 --- a/ffi/CMakeLists.txt +++ b/ffi/CMakeLists.txt @@ -31,60 +31,61 @@ include(cmake/Utils/AddLibbacktrace.cmake) add_library(dlpack_header INTERFACE) target_include_directories(dlpack_header INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/dlpack/include") -########## Target: `tvm_ffi` ########## +########## Target: `tvm_ffi_header` ########## -add_library(tvm_ffi INTERFACE) -target_link_libraries(tvm_ffi INTERFACE dlpack_header) -target_compile_features(tvm_ffi INTERFACE cxx_std_17) -target_include_directories(tvm_ffi INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include") +add_library(tvm_ffi_header INTERFACE) +target_include_directories(tvm_ffi_header INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include") +target_link_libraries(tvm_ffi_header INTERFACE dlpack_header) ########## Target: `tvm_ffi` ########## +add_library(tvm_ffi_objs OBJECT + "${CMAKE_CURRENT_SOURCE_DIR}/src/ffi/traceback.cc" + "${CMAKE_CURRENT_SOURCE_DIR}/src/ffi/object.cc" + "${CMAKE_CURRENT_SOURCE_DIR}/src/ffi/function.cc" +) +set_target_properties( + tvm_ffi_objs PROPERTIES + POSITION_INDEPENDENT_CODE ON + CXX_STANDARD 17 + CXX_EXTENSIONS OFF + CXX_STANDARD_REQUIRED ON + CXX_VISIBILITY_PRESET hidden + VISIBILITY_INLINES_HIDDEN ON + PREFIX "lib" +) +add_cxx_warning(tvm_ffi_objs) +target_link_libraries(tvm_ffi_objs PRIVATE dlpack_header) +target_include_directories(tvm_ffi_objs PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include") -if (TVM_FFI_BUILD_REGISTRY) - add_library(tvm_ffi_objs OBJECT - "${CMAKE_CURRENT_SOURCE_DIR}/src/ffi/traceback.cc" - "${CMAKE_CURRENT_SOURCE_DIR}/src/ffi/object.cc" - "${CMAKE_CURRENT_SOURCE_DIR}/src/ffi/function.cc" - ) - set_target_properties( - tvm_ffi_objs PROPERTIES - POSITION_INDEPENDENT_CODE ON - CXX_STANDARD 17 - CXX_EXTENSIONS OFF - CXX_STANDARD_REQUIRED ON - CXX_VISIBILITY_PRESET hidden - VISIBILITY_INLINES_HIDDEN ON - PREFIX "lib" - ) - add_cxx_warning(tvm_ffi_objs) - target_link_libraries(tvm_ffi_objs PRIVATE dlpack_header) - target_include_directories(tvm_ffi_objs PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include") - - if (TVM_FFI_USE_LIBBRACKTRACE) - message(STATUS "Setting C++ macro TVM_FFI_USE_LIBBRACKTRACE - 1") - target_compile_definitions(tvm_ffi_objs PRIVATE TVM_FFI_USE_LIBBRACKTRACE=1) - else() - message(STATUS "Setting C++ macro TVM_FFI_USE_LIBBRACKTRACE - 0") - target_compile_definitions(tvm_ffi_objs PRIVATE TVM_FFI_USE_LIBBRACKTRACE=0) - endif() +if (TVM_FFI_USE_LIBBRACKTRACE) + message(STATUS "Setting C++ macro TVM_FFI_USE_LIBBRACKTRACE - 1") + target_compile_definitions(tvm_ffi_objs PRIVATE TVM_FFI_USE_LIBBRACKTRACE=1) +else() + message(STATUS "Setting C++ macro TVM_FFI_USE_LIBBRACKTRACE - 0") + target_compile_definitions(tvm_ffi_objs PRIVATE TVM_FFI_USE_LIBBRACKTRACE=0) +endif() - if (TVM_FFI_BACKTRACE_ON_SEGFAULT) - message(STATUS "Setting C++ macro TVM_FFI_BACKTRACE_ON_SEGFAULT - 1") - target_compile_definitions(tvm_ffi_objs PRIVATE TVM_FFI_BACKTRACE_ON_SEGFAULT=1) - else() - message(STATUS "Setting C++ macro TVM_FFI_BACKTRACE_ON_SEGFAULT - 0") - target_compile_definitions(tvm_ffi_objs PRIVATE TVM_FFI_BACKTRACE_ON_SEGFAULT=0) - endif() +if (TVM_FFI_BACKTRACE_ON_SEGFAULT) + message(STATUS "Setting C++ macro TVM_FFI_BACKTRACE_ON_SEGFAULT - 1") + target_compile_definitions(tvm_ffi_objs PRIVATE TVM_FFI_BACKTRACE_ON_SEGFAULT=1) +else() + message(STATUS "Setting C++ macro TVM_FFI_BACKTRACE_ON_SEGFAULT - 0") + target_compile_definitions(tvm_ffi_objs PRIVATE TVM_FFI_BACKTRACE_ON_SEGFAULT=0) +endif() + +add_target_from_obj(tvm_ffi tvm_ffi_objs) + +if (TARGET libbacktrace) + target_link_libraries(tvm_ffi_objs PRIVATE libbacktrace) + target_link_libraries(tvm_ffi_shared PRIVATE libbacktrace) + target_link_libraries(tvm_ffi_static PRIVATE libbacktrace) +endif () +target_link_libraries(tvm_ffi_objs PUBLIC tvm_ffi_header) +target_link_libraries(tvm_ffi_shared PUBLIC tvm_ffi_header) +target_link_libraries(tvm_ffi_static PUBLIC tvm_ffi_header) - add_target_from_obj(tvm_ffi tvm_ffi_objs) - if (TARGET libbacktrace) - target_link_libraries(tvm_ffi_objs PRIVATE libbacktrace) - target_link_libraries(tvm_ffi_shared PRIVATE libbacktrace) - target_link_libraries(tvm_ffi_static PRIVATE libbacktrace) - endif () - install(TARGETS tvm_ffi_static DESTINATION "lib/") - install(TARGETS tvm_ffi_shared DESTINATION "lib/") -endif (TVM_FFI_BUILD_REGISTRY) +install(TARGETS tvm_ffi_static DESTINATION lib${LIB_SUFFIX}) +install(TARGETS tvm_ffi_shared DESTINATION lib${LIB_SUFFIX}) ########## Adding tests ########## diff --git a/ffi/scripts/run_tests.sh b/ffi/scripts/run_tests.sh index dc7e6cdc71..bfe36745b6 100755 --- a/ffi/scripts/run_tests.sh +++ b/ffi/scripts/run_tests.sh @@ -5,7 +5,6 @@ BUILD_TYPE=RelWithDebugInfo rm -rf build/CMakeFiles build/CMakeCache.txt cmake -G Ninja -S . -B build -DTVM_FFI_BUILD_TESTS=ON -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \ - -DTVM_FFI_BUILD_REGISTRY=ON \ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_CXX_COMPILER_LAUNCHER=ccache cmake --build build --parallel 16 --clean-first --config ${BUILD_TYPE} --target tvm_ffi_tests GTEST_COLOR=1 ctest -V -C ${BUILD_TYPE} --test-dir build --output-on-failure diff --git a/ffi/tests/cpp/CMakeLists.txt b/ffi/tests/cpp/CMakeLists.txt index 54dae7177d..542a60e658 100644 --- a/ffi/tests/cpp/CMakeLists.txt +++ b/ffi/tests/cpp/CMakeLists.txt @@ -16,10 +16,6 @@ set_target_properties( ) add_cxx_warning(tvm_ffi_tests) add_sanitizer_address(tvm_ffi_tests) -target_link_libraries(tvm_ffi_tests PRIVATE tvm_ffi) - -if (TVM_FFI_BUILD_REGISTRY) - target_link_libraries(tvm_ffi_tests PRIVATE tvm_ffi_shared) -endif() +target_link_libraries(tvm_ffi_tests PRIVATE tvm_ffi_shared) add_googletest(tvm_ffi_tests) diff --git a/include/tvm/runtime/object.h b/include/tvm/runtime/object.h index 892294bd82..527d663c02 100644 --- a/include/tvm/runtime/object.h +++ b/include/tvm/runtime/object.h @@ -25,6 +25,7 @@ #include <tvm/runtime/c_runtime_api.h> #include <tvm/runtime/logging.h> +#include <tvm/ffi/object.h> #include <string> #include <type_traits> diff --git a/src/ffi/registry.cc b/src/ffi/registry.cc deleted file mode 100644 index 05c0280796..0000000000 --- a/src/ffi/registry.cc +++ /dev/null @@ -1,144 +0,0 @@ -#include <memory> -#include <string> -#include <tvm/ffi/ffi.hpp> -#include <unordered_map> -#include <vector> - -namespace { - -using tvm::ffi::Any; -using tvm::ffi::AnyView; -using tvm::ffi::Func; -using tvm::ffi::Ref; -using tvm::ffi::TypeIndexTraits; -using tvm::ffi::TypeTraits; -using tvm::ffi::details::InitTypeTable; - -struct TypeInfoImpl : public TVMFFITypeInfo { - std::string type_key_data; - std::vector<int32_t> type_ancestors_data; -}; - -struct TypeTable { - int32_t num_types; - std::vector<std::shared_ptr<TypeInfoImpl>> type_table; - std::unordered_map<std::string, std::shared_ptr<TypeInfoImpl>> type_key_to_info; - std::unordered_map<std::string, std::vector<Any>> type_attrs; - - static TypeTable *New(); - static TypeTable *Global() { - static std::unique_ptr<TypeTable> instance(TypeTable::New()); - return instance.get(); - } -}; - -int32_t TypeDef(TypeTable *self, int32_t type_index, const char *type_key, int32_t type_depth, - const int32_t *type_ancestors) { - if (self == nullptr) { - TVM_FFI_THROW(ValueError) << "TypeTable is provided as nullptr."; - } - if (self->type_key_to_info.count(type_key) != 0) { - TVM_FFI_THROW(ValueError) << "Type key is already registered."; - } - if (type_index == -1) { - type_index = self->num_types++; - } - std::shared_ptr<TypeInfoImpl> info = std::make_shared<TypeInfoImpl>(); - { - info->type_key_data = std::string(type_key); - info->type_ancestors_data = std::vector<int32_t>(type_ancestors, type_ancestors + type_depth); - info->type_index = type_index; - info->type_key = info->type_key_data.c_str(); - info->type_depth = type_depth; - info->type_ancestors = info->type_ancestors_data.data(); - } - if (type_index >= static_cast<int32_t>(self->type_table.size())) { - self->type_table.resize((type_index / 1024 + 1) * 1024); - } - self->type_table.at(type_index) = info; - self->type_key_to_info[type_key] = info; - return type_index; -} - -struct InitFunctor { - template <enum TVMFFITypeIndex type_index, typename ObjectType> - void RegisterType(TypeTable *self) { - TypeDef(self, static_cast<int32_t>(type_index), TypeIndexTraits<type_index>::type_key, - std::max<int32_t>(ObjectType::_type_depth, 0), ObjectType::_type_ancestors.data()); - } - - template <enum TVMFFITypeIndex type_index, typename ObjectType> - void RegisterStr(TypeTable *self) { - Any value = Any(Ref<Func>::New(TypeTraits<ObjectType>::__str__)); - TVMFFIDynTypeSetAttr(self, static_cast<int32_t>(type_index), "__str__", &value); - } -}; - -TypeTable *TypeTable::New() { - TypeTable *self = new TypeTable(); - self->type_table.resize(1024); - self->type_key_to_info.reserve(1024); - self->num_types = static_cast<int32_t>(TVMFFITypeIndex::kTVMFFIDynObjectBegin); - InitTypeTable(InitFunctor(), self); - return self; -} - -} // namespace - -TVM_FFI_API void TVMFFIDynTypeDef(TVMFFITypeTableHandle _self, const char *type_key, - int32_t type_depth, const int32_t *type_ancestors, - int32_t *type_index) { - TypeTable *self = static_cast<TypeTable *>(_self); - if (self == nullptr) { - self = TypeTable::Global(); - } - *type_index = TypeDef(self, -1, type_key, type_depth, type_ancestors); -} - -TVM_FFI_API void TVMFFIDynTypeIndex2Info(TVMFFITypeTableHandle _self, int32_t type_index, - TVMFFITypeInfoHandle *ret) { - TypeTable *self = static_cast<TypeTable *>(_self); - if (self == nullptr) { - self = TypeTable::Global(); - } - if (type_index < 0 || type_index >= static_cast<int32_t>(self->type_table.size())) { - *ret = nullptr; - } else { - *ret = self->type_table.at(type_index).get(); - } -} - -TVM_FFI_API void TVMFFIDynTypeSetAttr(TVMFFITypeTableHandle _self, int32_t type_index, - const char *key, TVMFFIAnyHandle value) { - TypeTable *self = static_cast<TypeTable *>(_self); - if (self == nullptr) { - self = TypeTable::Global(); - } - std::vector<Any> &attrs = self->type_attrs[key]; - if (type_index >= static_cast<int32_t>(attrs.size())) { - attrs.resize((type_index / 1024 + 1) * 1024); - } - attrs.at(type_index) = Any(*static_cast<AnyView *>(value)); -} - -TVM_FFI_API void TVMFFIDynTypeGetAttr(TVMFFITypeTableHandle _self, int32_t type_index, - const char *key, TVMFFIAnyHandle *ret) { - TypeTable *self = static_cast<TypeTable *>(_self); - if (self == nullptr) { - self = TypeTable::Global(); - } - std::vector<Any> &attrs = self->type_attrs[key]; - if (type_index >= static_cast<int32_t>(attrs.size())) { - *ret = nullptr; - } else { - *ret = &attrs.at(type_index); - } -} - -TVM_FFI_API void TVMFFIDynTypeTypeTableCreate(TVMFFITypeTableHandle *ret) { - *ret = TypeTable::New(); -} - -TVM_FFI_API void TVMFFIDynTypeTypeTableDestroy(TVMFFITypeTableHandle handle) { - delete static_cast<TypeTable *>(handle); -}
