Skip to content

Commit f6cebb5

Browse files
authored
[microTVM] additional refactoring for enabling USE_MICRO in more builds (#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.
1 parent c6ce283 commit f6cebb5

File tree

5 files changed

+52
-68
lines changed

5 files changed

+52
-68
lines changed

CMakeLists.txt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -577,16 +577,10 @@ include(cmake/modules/contrib/PAPI.cmake)
577577
if(USE_MICRO)
578578
# NOTE: cmake doesn't track dependencies at the file level across subdirectories. For the
579579
# Unix Makefiles generator, need to add these explicit target-level dependency)
580+
add_dependencies(tvm_runtime arduino)
580581
add_dependencies(tvm_runtime crt)
582+
add_dependencies(tvm_runtime host_standalone_crt)
581583
add_dependencies(tvm_runtime zephyr)
582-
add_dependencies(tvm_runtime arduino)
583-
if(MSVC)
584-
target_link_libraries(tvm PRIVATE host_standalone_crt )
585-
target_link_libraries(tvm_runtime PRIVATE host_standalone_crt)
586-
else()
587-
add_dependencies(tvm host_standalone_crt)
588-
add_dependencies(tvm_runtime host_standalone_crt)
589-
endif()
590584
endif()
591585

592586
if(USE_CPP_RPC)

cmake/modules/StandaloneCrt.cmake

Lines changed: 37 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,29 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
1918
if(USE_MICRO)
2019

21-
if(MSVC)
22-
23-
# When building for Windows, use standard CMake for compatibility with
24-
# Visual Studio build tools and not require Make to be on the system.
25-
26-
# TODO: test building with this MSVC conditional code removed
27-
# when USE_MICRO is enabled
20+
message(STATUS "Build microTVM RPC common")
2821

29-
set(CRT_CONFIG, "src/runtime/micro/crt_config.h")
22+
# add microTVM RPC common files to TVM runtime build
23+
list(APPEND TVM_CRT_SOURCES
24+
3rdparty/libcrc/src/crcccitt.c
25+
src/runtime/crt/microtvm_rpc_common/frame_buffer.cc
26+
src/runtime/crt/microtvm_rpc_common/framing.cc
27+
src/runtime/crt/microtvm_rpc_common/session.cc
28+
src/runtime/crt/microtvm_rpc_common/write_stream.cc)
3029

31-
add_library(host_standalone_crt
32-
STATIC
33-
3rdparty/libcrc/src/crcccitt.c
34-
src/runtime/crt/microtvm_rpc_common/frame_buffer.cc
35-
src/runtime/crt/microtvm_rpc_common/framing.cc
36-
src/runtime/crt/microtvm_rpc_common/session.cc
37-
src/runtime/crt/microtvm_rpc_common/write_stream.cc)
30+
list(APPEND RUNTIME_SRCS ${TVM_CRT_SOURCES})
31+
include_directories(SYSTEM src/runtime/micro)
3832

39-
target_include_directories(host_standalone_crt
40-
PRIVATE
41-
3rdparty/libcrc/include
42-
src/runtime/micro)
43-
44-
else()
4533

4634
function(create_crt_library CRT_LIBRARY)
4735

4836
set(CRT_LIBRARY_NAME host_standalone_crt_${CRT_LIBRARY})
4937
set(CRT_LIBRARY_SOURCES "")
5038

5139
foreach(FILE_NAME IN LISTS ARGN)
52-
list(APPEND CRT_LIBRARY_SOURCES ${FILE_NAME})
40+
list(APPEND CRT_LIBRARY_SOURCES ${FILE_NAME})
5341
endforeach()
5442

5543
add_library(${CRT_LIBRARY_NAME}
@@ -60,9 +48,9 @@ else()
6048
set(CRT_LIBRARIES ${CRT_LIBRARIES} ${CRT_LIBRARY_NAME} PARENT_SCOPE)
6149

6250
target_include_directories(${CRT_LIBRARY_NAME}
63-
PUBLIC
64-
${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/micro/
65-
${STANDALONE_CRT_BASE}/include)
51+
PUBLIC
52+
${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/micro/
53+
${STANDALONE_CRT_BASE}/include)
6654

6755
set_target_properties(${CRT_LIBRARY_NAME}
6856
PROPERTIES
@@ -75,7 +63,7 @@ else()
7563

7664
endfunction()
7765

78-
message(STATUS "Build standalone CRT for microTVM")
66+
message(STATUS "Build microTVM standalone CRT")
7967

8068
# Build an isolated build directory, separate from the TVM tree.
8169
list(APPEND CRT_FILE_COPY_JOBS
@@ -147,32 +135,36 @@ else()
147135
${RUNTIME_CRT_SOURCE_DIR}/aot_executor_module/aot_executor_module.c)
148136

149137
create_crt_library(graph_executor
150-
${RUNTIME_CRT_SOURCE_DIR}/graph_executor/graph_executor.c
151-
${RUNTIME_CRT_SOURCE_DIR}/graph_executor/load_json.c)
138+
${RUNTIME_CRT_SOURCE_DIR}/graph_executor/graph_executor.c
139+
${RUNTIME_CRT_SOURCE_DIR}/graph_executor/load_json.c)
152140

153141
create_crt_library(graph_executor_module
154-
${RUNTIME_CRT_SOURCE_DIR}/graph_executor_module/graph_executor_module.c)
142+
${RUNTIME_CRT_SOURCE_DIR}/graph_executor_module/graph_executor_module.c)
155143

156144
create_crt_library(memory
157-
${RUNTIME_CRT_SOURCE_DIR}/memory/page_allocator.c
158-
${RUNTIME_CRT_SOURCE_DIR}/memory/stack_allocator.c)
145+
${RUNTIME_CRT_SOURCE_DIR}/memory/page_allocator.c
146+
${RUNTIME_CRT_SOURCE_DIR}/memory/stack_allocator.c)
159147

160148
create_crt_library(microtvm_rpc_common
161-
${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/crcccitt.c
162-
${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/frame_buffer.cc
163-
${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/framing.cc
164-
${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/session.cc
165-
${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/write_stream.cc)
149+
${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/crcccitt.c
150+
${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/frame_buffer.cc
151+
${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/framing.cc
152+
${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/session.cc
153+
${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_common/write_stream.cc)
166154

167155
create_crt_library(microtvm_rpc_server
168-
${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_server/rpc_server.cc)
169-
170-
create_crt_library(common
171-
${RUNTIME_CRT_SOURCE_DIR}/common/crt_backend_api.c
172-
${RUNTIME_CRT_SOURCE_DIR}/common/crt_runtime_api.c
173-
${RUNTIME_CRT_SOURCE_DIR}/common/func_registry.c
174-
${RUNTIME_CRT_SOURCE_DIR}/common/ndarray.c
175-
${RUNTIME_CRT_SOURCE_DIR}/common/packed_func.c)
156+
${RUNTIME_CRT_SOURCE_DIR}/microtvm_rpc_server/rpc_server.cc)
157+
158+
if(NOT MSVC)
159+
# TODO: if we want to eventually build standalone_crt for windows
160+
# these files would be needed, but for now don't build them
161+
create_crt_library(common
162+
${RUNTIME_CRT_SOURCE_DIR}/common/crt_backend_api.c
163+
${RUNTIME_CRT_SOURCE_DIR}/common/crt_runtime_api.c
164+
${RUNTIME_CRT_SOURCE_DIR}/common/func_registry.c
165+
${RUNTIME_CRT_SOURCE_DIR}/common/ndarray.c
166+
${RUNTIME_CRT_SOURCE_DIR}/common/packed_func.c)
167+
endif()
176168

177169
add_custom_target(host_standalone_crt DEPENDS ${CRT_LIBRARIES} standalone_crt)
178170

@@ -188,15 +180,4 @@ else()
188180
gtest_discover_tests(crttest)
189181
endif()
190182

191-
set(TVM_CRT_LINKER_LIB host_standalone_crt_microtvm_rpc_common)
192-
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
193-
list(APPEND TVM_RUNTIME_LINKER_LIBS -Wl,--whole-archive ${TVM_CRT_LINKER_LIB} -Wl,--no-whole-archive)
194-
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES ".*Clang")
195-
list(APPEND TVM_RUNTIME_LINKER_LIBS -Wl,-force_load ${TVM_CRT_LINKER_LIB})
196-
else()
197-
list(APPEND TVM_RUNTIME_LINKER_LIBS ${TVM_CRT_LINKER_LIB})
198-
endif()
199-
200-
endif()
201-
202183
endif()

include/tvm/runtime/c_runtime_api.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@
5353
#define TVM_DLL EMSCRIPTEN_KEEPALIVE
5454
#endif
5555

56+
// helper macro to suppress unused warning
57+
#if defined(__GNUC__)
58+
#define TVM_ATTRIBUTE_UNUSED __attribute__((unused))
59+
#else
60+
#define TVM_ATTRIBUTE_UNUSED
61+
#endif
62+
5663
#ifndef TVM_DLL
5764
#ifdef _WIN32
5865
#ifdef TVM_EXPORTS

include/tvm/runtime/crt/microtvm_rpc_server.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include <sys/types.h>
3030
#include <tvm/runtime/crt/error_codes.h>
3131

32+
#include "../../../../src/support/ssize.h"
33+
3234
#ifdef __cplusplus
3335
extern "C" {
3436
#endif

src/runtime/crt/common/crt_runtime_api.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ int TVMDeviceAllocDataSpaceWithScope(DLDevice dev, int ndim, const int64_t* shap
112112

113113
int TVMDeviceFreeDataSpace(DLDevice dev, void* ptr) { return TVMPlatformMemoryFree(ptr, dev); }
114114

115-
static bool IsContiguous(const DLTensor* arr) {
115+
TVM_ATTRIBUTE_UNUSED static bool IsContiguous(const DLTensor* arr) {
116116
if (arr->strides == NULL) return true;
117117
int64_t expected_stride = 1;
118118
for (int32_t i = arr->ndim; i != 0; --i) {
@@ -632,12 +632,12 @@ release_and_return : {
632632
}
633633

634634
// Default implementation, overridden by the platform runtime.
635-
__attribute__((weak)) tvm_crt_error_t TVMPlatformGenerateRandom(uint8_t* buffer, size_t num_bytes) {
635+
TVM_WEAK tvm_crt_error_t TVMPlatformGenerateRandom(uint8_t* buffer, size_t num_bytes) {
636636
return kTvmErrorFunctionCallNotImplemented;
637637
}
638638

639639
// Default implementation, overridden by the platform runtime.
640-
__attribute__((weak)) tvm_crt_error_t TVMPlatformBeforeMeasurement() { return kTvmErrorNoError; }
640+
TVM_WEAK tvm_crt_error_t TVMPlatformBeforeMeasurement() { return kTvmErrorNoError; }
641641

642642
// Default implementation, overridden by the platform runtime.
643-
__attribute__((weak)) tvm_crt_error_t TVMPlatformAfterMeasurement() { return kTvmErrorNoError; }
643+
TVM_WEAK tvm_crt_error_t TVMPlatformAfterMeasurement() { return kTvmErrorNoError; }

0 commit comments

Comments
 (0)