From 67d948bb4fad9547d5194532b4df82f1e2995d4a Mon Sep 17 00:00:00 2001 From: Alan MacDonald Date: Thu, 2 Feb 2023 17:12:42 -0800 Subject: [PATCH 1/6] remove standalone crt special case for windows build --- CMakeLists.txt | 9 ++----- cmake/modules/StandaloneCrt.cmake | 42 +++++++------------------------ 2 files changed, 11 insertions(+), 40 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index abe415957e33..6df6b0822468 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -580,13 +580,8 @@ if(USE_MICRO) add_dependencies(tvm_runtime crt) add_dependencies(tvm_runtime zephyr) add_dependencies(tvm_runtime arduino) - if(MSVC) - target_link_libraries(tvm PRIVATE host_standalone_crt ) - target_link_libraries(tvm_runtime PRIVATE host_standalone_crt) - else() - add_dependencies(tvm host_standalone_crt) - add_dependencies(tvm_runtime host_standalone_crt) - endif() + add_dependencies(tvm host_standalone_crt) + add_dependencies(tvm_runtime host_standalone_crt) endif() if(USE_CPP_RPC) diff --git a/cmake/modules/StandaloneCrt.cmake b/cmake/modules/StandaloneCrt.cmake index 1d4e213ec71a..2f4cb885972d 100644 --- a/cmake/modules/StandaloneCrt.cmake +++ b/cmake/modules/StandaloneCrt.cmake @@ -18,31 +18,6 @@ if(USE_MICRO) -if(MSVC) - - # When building for Windows, use standard CMake for compatibility with - # Visual Studio build tools and not require Make to be on the system. - - # TODO: test building with this MSVC conditional code removed - # when USE_MICRO is enabled - - set(CRT_CONFIG, "src/runtime/micro/crt_config.h") - - add_library(host_standalone_crt - STATIC - 3rdparty/libcrc/src/crcccitt.c - src/runtime/crt/microtvm_rpc_common/frame_buffer.cc - src/runtime/crt/microtvm_rpc_common/framing.cc - src/runtime/crt/microtvm_rpc_common/session.cc - src/runtime/crt/microtvm_rpc_common/write_stream.cc) - - target_include_directories(host_standalone_crt - PRIVATE - 3rdparty/libcrc/include - src/runtime/micro) - -else() - function(create_crt_library CRT_LIBRARY) set(CRT_LIBRARY_NAME host_standalone_crt_${CRT_LIBRARY}) @@ -167,12 +142,15 @@ else() create_crt_library(microtvm_rpc_server ${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_server/rpc_server.cc) - create_crt_library(common - ${RUNTIME_CRT_SOURCE_DIR}/common/crt_backend_api.c - ${RUNTIME_CRT_SOURCE_DIR}/common/crt_runtime_api.c - ${RUNTIME_CRT_SOURCE_DIR}/common/func_registry.c - ${RUNTIME_CRT_SOURCE_DIR}/common/ndarray.c - ${RUNTIME_CRT_SOURCE_DIR}/common/packed_func.c) + if(NOT MSVC) + # TODO: fix remaining windows build issues + create_crt_library(common + ${RUNTIME_CRT_SOURCE_DIR}/common/crt_backend_api.c + ${RUNTIME_CRT_SOURCE_DIR}/common/crt_runtime_api.c + ${RUNTIME_CRT_SOURCE_DIR}/common/func_registry.c + ${RUNTIME_CRT_SOURCE_DIR}/common/ndarray.c + ${RUNTIME_CRT_SOURCE_DIR}/common/packed_func.c) + endif() add_custom_target(host_standalone_crt DEPENDS ${CRT_LIBRARIES} standalone_crt) @@ -198,5 +176,3 @@ else() endif() endif() - -endif() From b840717d6977d332e4034aed2b98493ca9bb9b25 Mon Sep 17 00:00:00 2001 From: Alan MacDonald Date: Thu, 2 Feb 2023 17:47:14 -0800 Subject: [PATCH 2/6] add ssize_t header --- include/tvm/runtime/crt/microtvm_rpc_server.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/tvm/runtime/crt/microtvm_rpc_server.h b/include/tvm/runtime/crt/microtvm_rpc_server.h index 9a7ed54ffe95..d185bfcddb6c 100644 --- a/include/tvm/runtime/crt/microtvm_rpc_server.h +++ b/include/tvm/runtime/crt/microtvm_rpc_server.h @@ -29,6 +29,8 @@ #include #include +#include "../../../../src/support/ssize.h" + #ifdef __cplusplus extern "C" { #endif From 46a18adca34b201685cc77e6ea278798c093ecef Mon Sep 17 00:00:00 2001 From: Alan MacDonald Date: Fri, 3 Feb 2023 10:58:32 -0800 Subject: [PATCH 3/6] enable USE_MICRO by default --- CMakeLists.txt | 18 ++++++++---------- cmake/config.cmake | 3 --- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6df6b0822468..fd05088f6df9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,7 +59,7 @@ tvm_option(USE_OPENMP "Build with OpenMP thread pool implementation" OFF) tvm_option(USE_RELAY_DEBUG "Building Relay in debug mode..." OFF) tvm_option(USE_RTTI "Build with RTTI" ON) tvm_option(USE_MSVC_MT "Build with MT" OFF) -tvm_option(USE_MICRO "Build with Micro TVM support" OFF) +tvm_option(USE_MICRO "Build with Micro TVM support" ON) tvm_option(INSTALL_DEV "Install compiler infrastructure" OFF) tvm_option(HIDE_PRIVATE_SYMBOLS "Compile with -fvisibility=hidden." OFF) tvm_option(USE_TF_TVMDSOOP "Build with TensorFlow TVMDSOOp" OFF) @@ -574,15 +574,13 @@ include(cmake/modules/Logging.cmake) include(cmake/modules/contrib/PAPI.cmake) -if(USE_MICRO) - # NOTE: cmake doesn't track dependencies at the file level across subdirectories. For the - # Unix Makefiles generator, need to add these explicit target-level dependency) - add_dependencies(tvm_runtime crt) - add_dependencies(tvm_runtime zephyr) - add_dependencies(tvm_runtime arduino) - add_dependencies(tvm host_standalone_crt) - add_dependencies(tvm_runtime host_standalone_crt) -endif() +# NOTE: cmake doesn't track dependencies at the file level across subdirectories. For the +# Unix Makefiles generator, need to add these explicit target-level dependency) +add_dependencies(tvm_runtime crt) +add_dependencies(tvm_runtime zephyr) +add_dependencies(tvm_runtime arduino) +add_dependencies(tvm host_standalone_crt) +add_dependencies(tvm_runtime host_standalone_crt) if(USE_CPP_RPC) add_subdirectory("apps/cpp_rpc") diff --git a/cmake/config.cmake b/cmake/config.cmake index e20b358ad837..093d970586a1 100644 --- a/cmake/config.cmake +++ b/cmake/config.cmake @@ -101,9 +101,6 @@ set(USE_SPIRV_KHR_INTEGER_DOT_PRODUCT OFF) # Whether enable OpenGL runtime set(USE_OPENGL OFF) -# Whether enable MicroTVM runtime -set(USE_MICRO OFF) - # Whether enable RPC runtime set(USE_RPC ON) From f46b47a122cac96233553b4ee19f3081565ffb6f Mon Sep 17 00:00:00 2001 From: Alan MacDonald Date: Tue, 7 Feb 2023 17:51:22 -0800 Subject: [PATCH 4/6] disable USE_MICRO for hexagon builds --- CMakeLists.txt | 16 +++++++++++----- cmake/modules/StandaloneCrt.cmake | 3 ++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fd05088f6df9..41357f2ff983 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -264,6 +264,10 @@ else(MSVC) endif() endif(MSVC) +if(USE_HEXAGON) + set(USE_MICRO OFF) +endif() + # Hexagon has dlopen built into QuRT (no need for static library). if(NOT BUILD_FOR_HEXAGON) list(APPEND TVM_RUNTIME_LINKER_LIBS ${CMAKE_DL_LIBS}) @@ -576,11 +580,13 @@ include(cmake/modules/contrib/PAPI.cmake) # NOTE: cmake doesn't track dependencies at the file level across subdirectories. For the # Unix Makefiles generator, need to add these explicit target-level dependency) -add_dependencies(tvm_runtime crt) -add_dependencies(tvm_runtime zephyr) -add_dependencies(tvm_runtime arduino) -add_dependencies(tvm host_standalone_crt) -add_dependencies(tvm_runtime host_standalone_crt) +if(USE_MICRO) + add_dependencies(tvm_runtime crt) + add_dependencies(tvm_runtime zephyr) + add_dependencies(tvm_runtime arduino) + add_dependencies(tvm host_standalone_crt) + add_dependencies(tvm_runtime host_standalone_crt) +endif() if(USE_CPP_RPC) add_subdirectory("apps/cpp_rpc") diff --git a/cmake/modules/StandaloneCrt.cmake b/cmake/modules/StandaloneCrt.cmake index 2f4cb885972d..fc636890277c 100644 --- a/cmake/modules/StandaloneCrt.cmake +++ b/cmake/modules/StandaloneCrt.cmake @@ -143,7 +143,8 @@ if(USE_MICRO) ${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_server/rpc_server.cc) if(NOT MSVC) - # TODO: fix remaining windows build issues + # TODO: if we want to eventually build standalone_crt for windows + # these files would be needed, but for now don't build them create_crt_library(common ${RUNTIME_CRT_SOURCE_DIR}/common/crt_backend_api.c ${RUNTIME_CRT_SOURCE_DIR}/common/crt_runtime_api.c From d5d518489a98b451edc5a990e0a6e7f732a446a4 Mon Sep 17 00:00:00 2001 From: Alan MacDonald Date: Wed, 8 Feb 2023 19:46:55 -0800 Subject: [PATCH 5/6] build microtvm rpc common from sources --- CMakeLists.txt | 13 +++--- cmake/modules/StandaloneCrt.cmake | 56 +++++++++++++----------- include/tvm/runtime/c_runtime_api.h | 7 +++ src/runtime/crt/common/crt_runtime_api.c | 8 ++-- 4 files changed, 48 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 41357f2ff983..928c04d25fff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -264,7 +264,9 @@ else(MSVC) endif() endif(MSVC) -if(USE_HEXAGON) +# USE_MICRO is on by default, but it is not needed for Hexagon builds nor will +# it function properly with USE_RPC disabled. +if(USE_HEXAGON OR NOT USE_RPC) set(USE_MICRO OFF) endif() @@ -578,14 +580,13 @@ include(cmake/modules/Logging.cmake) include(cmake/modules/contrib/PAPI.cmake) -# NOTE: cmake doesn't track dependencies at the file level across subdirectories. For the -# Unix Makefiles generator, need to add these explicit target-level dependency) if(USE_MICRO) - add_dependencies(tvm_runtime crt) - add_dependencies(tvm_runtime zephyr) + # NOTE: cmake doesn't track dependencies at the file level across subdirectories. For the + # Unix Makefiles generator, need to add these explicit target-level dependency) add_dependencies(tvm_runtime arduino) - add_dependencies(tvm host_standalone_crt) + add_dependencies(tvm_runtime crt) add_dependencies(tvm_runtime host_standalone_crt) + add_dependencies(tvm_runtime zephyr) endif() if(USE_CPP_RPC) diff --git a/cmake/modules/StandaloneCrt.cmake b/cmake/modules/StandaloneCrt.cmake index fc636890277c..96fcf5092aa8 100644 --- a/cmake/modules/StandaloneCrt.cmake +++ b/cmake/modules/StandaloneCrt.cmake @@ -15,16 +15,29 @@ # specific language governing permissions and limitations # under the License. - if(USE_MICRO) + message(STATUS "Build microTVM RPC common") + + # add microTVM RPC common files to TVM runtime build + list(APPEND TVM_CRT_SOURCES + 3rdparty/libcrc/src/crcccitt.c + src/runtime/crt/microtvm_rpc_common/frame_buffer.cc + src/runtime/crt/microtvm_rpc_common/framing.cc + src/runtime/crt/microtvm_rpc_common/session.cc + src/runtime/crt/microtvm_rpc_common/write_stream.cc) + + list(APPEND RUNTIME_SRCS ${TVM_CRT_SOURCES}) + include_directories(SYSTEM src/runtime/micro) + + function(create_crt_library CRT_LIBRARY) set(CRT_LIBRARY_NAME host_standalone_crt_${CRT_LIBRARY}) set(CRT_LIBRARY_SOURCES "") foreach(FILE_NAME IN LISTS ARGN) - list(APPEND CRT_LIBRARY_SOURCES ${FILE_NAME}) + list(APPEND CRT_LIBRARY_SOURCES ${FILE_NAME}) endforeach() add_library(${CRT_LIBRARY_NAME} @@ -35,9 +48,9 @@ if(USE_MICRO) set(CRT_LIBRARIES ${CRT_LIBRARIES} ${CRT_LIBRARY_NAME} PARENT_SCOPE) target_include_directories(${CRT_LIBRARY_NAME} - PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/micro/ - ${STANDALONE_CRT_BASE}/include) + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/micro/ + ${STANDALONE_CRT_BASE}/include) set_target_properties(${CRT_LIBRARY_NAME} PROPERTIES @@ -50,7 +63,7 @@ if(USE_MICRO) endfunction() - message(STATUS "Build standalone CRT for microTVM") + message(STATUS "Build microTVM standalone CRT") # Build an isolated build directory, separate from the TVM tree. list(APPEND CRT_FILE_COPY_JOBS @@ -122,25 +135,25 @@ if(USE_MICRO) ${RUNTIME_CRT_SOURCE_DIR}/aot_executor_module/aot_executor_module.c) create_crt_library(graph_executor - ${RUNTIME_CRT_SOURCE_DIR}/graph_executor/graph_executor.c - ${RUNTIME_CRT_SOURCE_DIR}/graph_executor/load_json.c) + ${RUNTIME_CRT_SOURCE_DIR}/graph_executor/graph_executor.c + ${RUNTIME_CRT_SOURCE_DIR}/graph_executor/load_json.c) create_crt_library(graph_executor_module - ${RUNTIME_CRT_SOURCE_DIR}/graph_executor_module/graph_executor_module.c) + ${RUNTIME_CRT_SOURCE_DIR}/graph_executor_module/graph_executor_module.c) create_crt_library(memory - ${RUNTIME_CRT_SOURCE_DIR}/memory/page_allocator.c - ${RUNTIME_CRT_SOURCE_DIR}/memory/stack_allocator.c) + ${RUNTIME_CRT_SOURCE_DIR}/memory/page_allocator.c + ${RUNTIME_CRT_SOURCE_DIR}/memory/stack_allocator.c) create_crt_library(microtvm_rpc_common - ${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/crcccitt.c - ${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/frame_buffer.cc - ${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/framing.cc - ${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/session.cc - ${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/write_stream.cc) + ${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/crcccitt.c + ${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/frame_buffer.cc + ${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/framing.cc + ${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/session.cc + ${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/write_stream.cc) create_crt_library(microtvm_rpc_server - ${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_server/rpc_server.cc) + ${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_server/rpc_server.cc) if(NOT MSVC) # TODO: if we want to eventually build standalone_crt for windows @@ -167,13 +180,4 @@ if(USE_MICRO) gtest_discover_tests(crttest) endif() - set(TVM_CRT_LINKER_LIB host_standalone_crt_microtvm_rpc_common) - if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - list(APPEND TVM_RUNTIME_LINKER_LIBS -Wl,--whole-archive ${TVM_CRT_LINKER_LIB} -Wl,--no-whole-archive) - elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES ".*Clang") - list(APPEND TVM_RUNTIME_LINKER_LIBS -Wl,-force_load ${TVM_CRT_LINKER_LIB}) - else() - list(APPEND TVM_RUNTIME_LINKER_LIBS ${TVM_CRT_LINKER_LIB}) - endif() - endif() diff --git a/include/tvm/runtime/c_runtime_api.h b/include/tvm/runtime/c_runtime_api.h index cd1146697759..485e766ead05 100644 --- a/include/tvm/runtime/c_runtime_api.h +++ b/include/tvm/runtime/c_runtime_api.h @@ -53,6 +53,13 @@ #define TVM_DLL EMSCRIPTEN_KEEPALIVE #endif +// helper macro to suppress unused warning +#if defined(__GNUC__) +#define TVM_ATTRIBUTE_UNUSED __attribute__((unused)) +#else +#define TVM_ATTRIBUTE_UNUSED +#endif + #ifndef TVM_DLL #ifdef _WIN32 #ifdef TVM_EXPORTS diff --git a/src/runtime/crt/common/crt_runtime_api.c b/src/runtime/crt/common/crt_runtime_api.c index 7df610b53c45..f3aa74178415 100644 --- a/src/runtime/crt/common/crt_runtime_api.c +++ b/src/runtime/crt/common/crt_runtime_api.c @@ -112,7 +112,7 @@ int TVMDeviceAllocDataSpaceWithScope(DLDevice dev, int ndim, const int64_t* shap int TVMDeviceFreeDataSpace(DLDevice dev, void* ptr) { return TVMPlatformMemoryFree(ptr, dev); } -static bool IsContiguous(const DLTensor* arr) { +TVM_ATTRIBUTE_UNUSED static bool IsContiguous(const DLTensor* arr) { if (arr->strides == NULL) return true; int64_t expected_stride = 1; for (int32_t i = arr->ndim; i != 0; --i) { @@ -632,12 +632,12 @@ release_and_return : { } // Default implementation, overridden by the platform runtime. -__attribute__((weak)) tvm_crt_error_t TVMPlatformGenerateRandom(uint8_t* buffer, size_t num_bytes) { +TVM_WEAK tvm_crt_error_t TVMPlatformGenerateRandom(uint8_t* buffer, size_t num_bytes) { return kTvmErrorFunctionCallNotImplemented; } // Default implementation, overridden by the platform runtime. -__attribute__((weak)) tvm_crt_error_t TVMPlatformBeforeMeasurement() { return kTvmErrorNoError; } +TVM_WEAK tvm_crt_error_t TVMPlatformBeforeMeasurement() { return kTvmErrorNoError; } // Default implementation, overridden by the platform runtime. -__attribute__((weak)) tvm_crt_error_t TVMPlatformAfterMeasurement() { return kTvmErrorNoError; } +TVM_WEAK tvm_crt_error_t TVMPlatformAfterMeasurement() { return kTvmErrorNoError; } From 5224bdadbf1e906e8ea7cc773a9b3cdf9af8e6af Mon Sep 17 00:00:00 2001 From: Alan MacDonald Date: Mon, 13 Feb 2023 16:34:46 -0800 Subject: [PATCH 6/6] revert to USE_MICRO being off by default --- CMakeLists.txt | 8 +------- cmake/config.cmake | 3 +++ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 928c04d25fff..e48ea780e95d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,7 +59,7 @@ tvm_option(USE_OPENMP "Build with OpenMP thread pool implementation" OFF) tvm_option(USE_RELAY_DEBUG "Building Relay in debug mode..." OFF) tvm_option(USE_RTTI "Build with RTTI" ON) tvm_option(USE_MSVC_MT "Build with MT" OFF) -tvm_option(USE_MICRO "Build with Micro TVM support" ON) +tvm_option(USE_MICRO "Build with Micro TVM support" OFF) tvm_option(INSTALL_DEV "Install compiler infrastructure" OFF) tvm_option(HIDE_PRIVATE_SYMBOLS "Compile with -fvisibility=hidden." OFF) tvm_option(USE_TF_TVMDSOOP "Build with TensorFlow TVMDSOOp" OFF) @@ -264,12 +264,6 @@ else(MSVC) endif() endif(MSVC) -# USE_MICRO is on by default, but it is not needed for Hexagon builds nor will -# it function properly with USE_RPC disabled. -if(USE_HEXAGON OR NOT USE_RPC) - set(USE_MICRO OFF) -endif() - # Hexagon has dlopen built into QuRT (no need for static library). if(NOT BUILD_FOR_HEXAGON) list(APPEND TVM_RUNTIME_LINKER_LIBS ${CMAKE_DL_LIBS}) diff --git a/cmake/config.cmake b/cmake/config.cmake index 093d970586a1..e20b358ad837 100644 --- a/cmake/config.cmake +++ b/cmake/config.cmake @@ -101,6 +101,9 @@ set(USE_SPIRV_KHR_INTEGER_DOT_PRODUCT OFF) # Whether enable OpenGL runtime set(USE_OPENGL OFF) +# Whether enable MicroTVM runtime +set(USE_MICRO OFF) + # Whether enable RPC runtime set(USE_RPC ON)