Skip to content

Commit

Permalink
[microTVM] additional refactoring for enabling USE_MICRO in more buil…
Browse files Browse the repository at this point in the history
…ds (#13909)

Additional changes that make it possible to enable USE_MICRO across nearly all builds except for hexagon or when USE_RPC is disabled. This mainly standardizes how the microTVM RPC common files are built across all platforms. There will be a follow-up PR that will enable USE_MICRO for nearly all build types once the best mechanism and behaviors for doing so is determined.
  • Loading branch information
alanmacd authored Feb 14, 2023
1 parent c6ce283 commit f6cebb5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 68 deletions.
10 changes: 2 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -577,16 +577,10 @@ 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 arduino)
add_dependencies(tvm_runtime crt)
add_dependencies(tvm_runtime host_standalone_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()
endif()

if(USE_CPP_RPC)
Expand Down
93 changes: 37 additions & 56 deletions cmake/modules/StandaloneCrt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,29 @@
# specific language governing permissions and limitations
# under the License.


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
message(STATUS "Build microTVM RPC common")

set(CRT_CONFIG, "src/runtime/micro/crt_config.h")
# 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)

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)
list(APPEND RUNTIME_SRCS ${TVM_CRT_SOURCES})
include_directories(SYSTEM src/runtime/micro)

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})
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}
Expand All @@ -60,9 +48,9 @@ else()
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
Expand All @@ -75,7 +63,7 @@ else()

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
Expand Down Expand Up @@ -147,32 +135,36 @@ else()
${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)

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)
${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_server/rpc_server.cc)

if(NOT MSVC)
# 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
${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)

Expand All @@ -188,15 +180,4 @@ else()
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()

endif()
7 changes: 7 additions & 0 deletions include/tvm/runtime/c_runtime_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions include/tvm/runtime/crt/microtvm_rpc_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <sys/types.h>
#include <tvm/runtime/crt/error_codes.h>

#include "../../../../src/support/ssize.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/crt/common/crt_runtime_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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; }

0 comments on commit f6cebb5

Please sign in to comment.