Skip to content

Commit 2a840a3

Browse files
author
Krzysztof Parzyszek
authored
[Hexagon] Refactor Hexagon.cmake (#10227)
This file is included every time TVM is build, regardless of whether any support for Hexagon is enabled or not. This refactoring is meant to remove underlying assumptions about what features are enabled and what the compilation targets are. Now, when there is nothing needed from Hexagon, the script exits early (although it doesn't need to), and the rest of it is (and should remain) safe to execute regardless of build configuration. Disable "runtime.module.loadfile_hexagon" from the offload runtime, since it conflicts with device_api.hexagon.v2. It was only used with offload on Android, which is being deprecated.
1 parent ce45f26 commit 2a840a3

File tree

2 files changed

+160
-113
lines changed

2 files changed

+160
-113
lines changed

cmake/modules/Hexagon.cmake

Lines changed: 154 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -47,153 +47,197 @@ function(find_hexagon_toolchain)
4747
endif()
4848
endfunction()
4949

50+
macro(file_glob_append _output_list)
51+
tvm_file_glob(GLOB _tmp0 ${ARGN})
52+
set(_tmp1 ${${_output_list}})
53+
list(APPEND _tmp1 ${_tmp0})
54+
set(${_output_list} ${_tmp1})
55+
endmacro()
56+
57+
set(TVMRT_SOURCE_DIR "${CMAKE_SOURCE_DIR}/src/runtime")
58+
59+
# First, verify that USE_HEXAGON_DEVICE has a valid value.
60+
if(DEFINED USE_HEXAGON_DEVICE)
61+
if(NOT USE_HEXAGON_DEVICE STREQUAL "${PICK_SIM}" AND
62+
NOT USE_HEXAGON_DEVICE STREQUAL "${PICK_HW}" AND
63+
NOT USE_HEXAGON_DEVICE STREQUAL "${PICK_NONE}")
64+
message(SEND_ERROR "USE_HEXAGON_DEVICE must be one of "
65+
"[${PICK_NONE}|${PICK_SIM}|${PICK_HW}]")
66+
set(USE_HEXAGON_DEVICE OFF)
67+
endif()
68+
endif()
69+
5070
if(BUILD_FOR_HEXAGON)
5171
find_hexagon_sdk_root("${USE_HEXAGON_SDK}" "${USE_HEXAGON_ARCH}")
5272
# Add SDK and QuRT includes when building for Hexagon.
5373
include_directories(SYSTEM ${HEXAGON_SDK_INCLUDES} ${HEXAGON_QURT_INCLUDES})
5474
endif()
5575

56-
if (NOT USE_HEXAGON_SDK STREQUAL "" AND
57-
NOT USE_HEXAGON_SDK STREQUAL "/path/to/sdk")
58-
set(HEXAGON_SDK_PATH_DEFINED ${USE_HEXAGON_SDK})
76+
# This .cmake file is included when building any part of TVM for any
77+
# architecture. It shouldn't require any Hexagon-specific parameters
78+
# (like the path to the SDK), unless it's needed.
79+
# Two flags can enable some Hexagon-related functionality:
80+
# - USE_HEXAGON_DEVICE
81+
# - USE_HEXAGON_RPC
82+
#
83+
# USE_HEXAGON_RPC:
84+
# - When building for Hexagon, this will build the Hexagon endpoint of the
85+
# RPC server: the FastRPC skel library (with TVM runtime built into it).
86+
# - When building for Android, this will build the (intermediary) RPC server,
87+
# including the "stub" code for the FastRPC implementation of the RPC
88+
# channel.
89+
90+
if(NOT BUILD_FOR_HEXAGON AND NOT BUILD_FOR_ANDROID)
91+
set(BUILD_FOR_HOST TRUE)
5992
endif()
6093

61-
if (BUILD_FOR_ANDROID AND HEXAGON_SDK_PATH_DEFINED)
62-
find_hexagon_sdk_root("${USE_HEXAGON_SDK}" "${USE_HEXAGON_ARCH}")
94+
95+
if(NOT USE_HEXAGON_DEVICE AND NOT USE_HEXAGON_RPC)
96+
# If nothing related to Hexagon is enabled, add phony Hexagon codegen,
97+
# and some stuff needed by cpptests (this part is a temporary workaround
98+
# until e2e support for Hexagon is enabled).
99+
if(BUILD_FOR_HOST)
100+
list(APPEND COMPILER_SRCS src/target/opt/build_hexagon_off.cc)
101+
endif()
102+
list(APPEND RUNTIME_SRCS src/runtime/hexagon/hexagon/hexagon_buffer.cc)
103+
list(APPEND RUNTIME_SRCS src/runtime/hexagon/hexagon/hexagon_common.cc)
104+
list(APPEND RUNTIME_SRCS src/runtime/hexagon/hexagon/hexagon_user_dma.cc)
105+
return()
106+
endif()
107+
108+
109+
function(add_android_paths)
110+
if(NOT DEFINED HEXAGON_SDK_INCLUDES OR
111+
NOT DEFINED HEXAGON_RPCMEM_ROOT OR
112+
NOT DEFINED HEXAGON_REMOTE_ROOT)
113+
message(FATAL_ERROR "This function must be called after find_hexagon_sdk_root")
114+
endif()
63115
include_directories(SYSTEM
64116
${HEXAGON_SDK_INCLUDES}
65117
${HEXAGON_RPCMEM_ROOT}/inc
66-
${HEXAGON_REMOTE_ROOT})
118+
${HEXAGON_REMOTE_ROOT}
119+
)
67120
link_directories(${HEXAGON_REMOTE_ROOT})
68-
list(APPEND TVM_RUNTIME_LINKER_LIBS cdsprpc)
69-
endif()
121+
endfunction()
70122

71-
# Don't run these checks when compiling Hexagon device code,
72-
# e.g. when compiling the TVM runtime for Hexagon.
73-
if (NOT BUILD_FOR_HEXAGON AND NOT BUILD_FOR_ANDROID)
74-
if(USE_HEXAGON_DEVICE STREQUAL "OFF")
75-
list(APPEND COMPILER_SRCS src/target/opt/build_hexagon_off.cc)
76-
# append select runtime sources for unit testing
77-
list(APPEND RUNTIME_SRCS src/runtime/hexagon/hexagon/hexagon_buffer.cc)
78-
list(APPEND RUNTIME_SRCS src/runtime/hexagon/hexagon/hexagon_common.cc)
79-
list(APPEND RUNTIME_SRCS src/runtime/hexagon/hexagon/hexagon_user_dma.cc)
80-
return()
81-
elseif(NOT USE_HEXAGON_DEVICE STREQUAL "${PICK_SIM}" AND
82-
NOT USE_HEXAGON_DEVICE STREQUAL "${PICK_HW}")
83-
message(SEND_ERROR "USE_HEXAGON_DEVICE must be one of "
84-
"[${PICK_NONE}|${PICK_SIM}|${PICK_HW}]")
85-
return()
86-
endif()
87-
endif()
88123

89-
# If no Hexagon support is enabled (other than some stub code), cmake
90-
# execution should stop before reaching this point.
124+
# Common sources for TVM runtime with Hexagon support
125+
file_glob_append(RUNTIME_HEXAGON_COMMON_SRCS
126+
"${TVMRT_SOURCE_DIR}/hexagon/hexagon_module.cc"
127+
"${TVMRT_SOURCE_DIR}/hexagon/hexagon/*.cc"
128+
)
91129

92-
if(NOT USE_HEXAGON_SDK OR NOT USE_HEXAGON_ARCH)
93-
message(SEND_ERROR "Please set USE_HEXAGON_SDK to the Hexagon SDK root, "
94-
"and USE_HEXAGON_ARCH to the Hexagon architecture version")
95-
return()
96-
endif()
97130

98-
if(USE_HEXAGON_LAUNCHER STREQUAL "ON")
99-
message(SEND_ERROR "USE_HEXAGON_LAUNCHER is deprecated, please build apps separately")
100-
endif()
131+
if(USE_HEXAGON_DEVICE)
132+
function(invalid_device_value_for BUILD_TARGET)
133+
message(SEND_ERROR
134+
"USE_HEXAGON_DEVICE=${USE_HEXAGON_DEVICE} is not supported when "
135+
"building for ${BUILD_TARGET}"
136+
)
137+
endfunction()
138+
139+
list(APPEND RUNTIME_HEXAGON_SRCS ${RUNTIME_HEXAGON_COMMON_SRCS})
140+
141+
if(BUILD_FOR_HOST)
142+
if(NOT USE_HEXAGON_DEVICE STREQUAL "${PICK_SIM}")
143+
invalid_device_value_for("host")
144+
endif()
145+
find_hexagon_toolchain()
146+
file_glob_append(RUNTIME_HEXAGON_SRCS
147+
"${TVMRT_SOURCE_DIR}/hexagon/android/*.cc"
148+
"${TVMRT_SOURCE_DIR}/hexagon/android/sim/*.cc"
149+
)
150+
include_directories(SYSTEM "${HEXAGON_TOOLCHAIN}/include/iss")
151+
link_directories("${HEXAGON_TOOLCHAIN}/lib/iss")
152+
list(APPEND TVM_RUNTIME_LINKER_LIBS "-lwrapper")
153+
154+
ExternalProject_Add(sim_dev
155+
SOURCE_DIR "${TVMRT_SOURCE_DIR}/hexagon/android/sim/driver"
156+
CMAKE_ARGS
157+
"-DCMAKE_C_COMPILER=${HEXAGON_TOOLCHAIN}/bin/hexagon-clang"
158+
"-DCMAKE_CXX_COMPILER=${HEXAGON_TOOLCHAIN}/bin/hexagon-clang++"
159+
"-DHEXAGON_ARCH=${USE_HEXAGON_ARCH}"
160+
INSTALL_COMMAND "true"
161+
)
162+
163+
elseif(BUILD_FOR_ANDROID)
164+
if(NOT USE_HEXAGON_DEVICE STREQUAL "${PICK_HW}")
165+
invalid_device_value_for("Android")
166+
endif()
167+
find_hexagon_sdk_root("${USE_HEXAGON_SDK}" "${USE_HEXAGON_ARCH}")
168+
find_hexagon_toolchain()
169+
add_android_paths()
170+
file_glob_append(RUNTIME_HEXAGON_SRCS
171+
"${TVMRT_SOURCE_DIR}/hexagon/android/*.cc"
172+
"${TVMRT_SOURCE_DIR}/hexagon/android/target/*.cc"
173+
)
174+
# Hexagon runtime uses __android_log_print, which is in liblog.
175+
list(APPEND TVM_RUNTIME_LINKER_LIBS dl log cdsprpc)
176+
177+
elseif(BUILD_FOR_HEXAGON)
178+
invalid_device_value_for("Hexagon")
179+
endif()
180+
endif() # USE_HEXAGON_DEVICE
101181

102-
# find_hexagon_sdk_root has been called at this point.
103182

104183
if(USE_HEXAGON_RPC)
105-
set(TVMRT_SOURCE_DIR "${CMAKE_SOURCE_DIR}/src/runtime")
106-
set(QAIC_EXE "${HEXAGON_QAIC_EXE}")
107-
foreach(INCDIR IN LISTS HEXAGON_SDK_INCLUDES HEXAGON_REMOTE_ROOT)
108-
list(APPEND QAIC_FLAGS "-I${INCDIR}")
109-
endforeach()
110-
111-
add_custom_command(
112-
OUTPUT
113-
"${TVMRT_SOURCE_DIR}/hexagon/rpc/hexagon_rpc.h"
114-
"${TVMRT_SOURCE_DIR}/hexagon/rpc/hexagon_rpc_skel.c"
115-
"${TVMRT_SOURCE_DIR}/hexagon/rpc/hexagon_rpc_stub.c"
116-
COMMAND
117-
${QAIC_EXE} ${QAIC_FLAGS} "${TVMRT_SOURCE_DIR}/hexagon/rpc/hexagon_rpc.idl"
118-
-o "${TVMRT_SOURCE_DIR}/hexagon/rpc"
119-
MAIN_DEPENDENCY "${TVMRT_SOURCE_DIR}/hexagon/rpc/hexagon_rpc.idl"
120-
)
184+
function(build_rpc_idl)
185+
set(QAIC_EXE "${HEXAGON_QAIC_EXE}")
186+
foreach(INCDIR IN LISTS HEXAGON_SDK_INCLUDES HEXAGON_REMOTE_ROOT)
187+
list(APPEND QAIC_FLAGS "-I${INCDIR}")
188+
endforeach()
189+
190+
add_custom_command(
191+
OUTPUT
192+
"${TVMRT_SOURCE_DIR}/hexagon/rpc/hexagon_rpc.h"
193+
"${TVMRT_SOURCE_DIR}/hexagon/rpc/hexagon_rpc_skel.c"
194+
"${TVMRT_SOURCE_DIR}/hexagon/rpc/hexagon_rpc_stub.c"
195+
COMMAND
196+
${QAIC_EXE} ${QAIC_FLAGS} "${TVMRT_SOURCE_DIR}/hexagon/rpc/hexagon_rpc.idl"
197+
-o "${TVMRT_SOURCE_DIR}/hexagon/rpc"
198+
MAIN_DEPENDENCY "${TVMRT_SOURCE_DIR}/hexagon/rpc/hexagon_rpc.idl"
199+
)
200+
endfunction()
201+
202+
list(APPEND RUNTIME_HEXAGON_SRCS ${RUNTIME_HEXAGON_COMMON_SRCS})
121203

122204
if(BUILD_FOR_ANDROID)
123205
# Android part
124-
tvm_file_glob(GLOB RUNTIME_HEXAGON_SRCS src/runtime/hexagon/host/*.cc)
125-
tvm_file_glob(GLOB RUNTIME_HEXAGON_SRCS "${TVMRT_SOURCE_DIR}/hexagon/rpc/android/*.cc")
126-
list(APPEND RUNTIME_HEXAGON_SRCS "${TVMRT_SOURCE_DIR}/hexagon/rpc/hexagon_rpc_stub.c")
206+
find_hexagon_sdk_root("${USE_HEXAGON_SDK}" "${USE_HEXAGON_ARCH}")
207+
add_android_paths()
208+
build_rpc_idl()
209+
file_glob_append(RUNTIME_HEXAGON_SRCS
210+
"${TVMRT_SOURCE_DIR}/hexagon/host/*.cc"
211+
"${TVMRT_SOURCE_DIR}/hexagon/rpc/android/*.cc"
212+
"${TVMRT_SOURCE_DIR}/hexagon/rpc/hexagon_rpc_stub.c"
213+
)
214+
list(APPEND TVM_RUNTIME_LINKER_LIBS cdsprpc)
127215

128216
elseif(BUILD_FOR_HEXAGON)
129217
# Hexagon part
218+
find_hexagon_sdk_root("${USE_HEXAGON_SDK}" "${USE_HEXAGON_ARCH}")
130219
find_hexagon_toolchain()
131-
message(STATUS "HEXAGON_TOOLCHAIN: ${HEXAGON_TOOLCHAIN}")
220+
build_rpc_idl()
132221

133-
add_library(hexagon_rpc_skel SHARED
134-
"${TVMRT_SOURCE_DIR}/hexagon/rpc/hexagon_rpc_skel.c"
135-
"${TVMRT_SOURCE_DIR}/hexagon/rpc/hexagon/rpc_server.cc"
222+
# Include the generic RPC code into the TVM runtime.
223+
list(APPEND RUNTIME_HEXAGON_SRCS
136224
"${TVMRT_SOURCE_DIR}/minrpc/minrpc_server.h"
137225
"${TVMRT_SOURCE_DIR}/minrpc/rpc_reference.h"
138226
"${TVMRT_SOURCE_DIR}/rpc/rpc_module.cc"
139227
"${TVMRT_SOURCE_DIR}/rpc/rpc_endpoint.cc"
140228
"${TVMRT_SOURCE_DIR}/rpc/rpc_session.cc"
141229
"${TVMRT_SOURCE_DIR}/rpc/rpc_local_session.cc"
142230
)
231+
# Add the hardware-specific RPC code into the skel library.
232+
add_library(hexagon_rpc_skel SHARED
233+
"${TVMRT_SOURCE_DIR}/hexagon/rpc/hexagon/rpc_server.cc"
234+
"${TVMRT_SOURCE_DIR}/hexagon/rpc/hexagon_rpc_skel.c"
235+
)
143236
target_include_directories(hexagon_rpc_skel
144237
SYSTEM PRIVATE "${TVMRT_SOURCE_DIR}/hexagon/rpc"
145238
)
146239
endif()
147-
endif()
240+
endif() # USE_HEXAGON_RPC
148241

149-
if(USE_HEXAGON_DEVICE STREQUAL "${PICK_SIM}")
150-
find_hexagon_toolchain()
151-
message(STATUS "Hexagon toolchain: ${HEXAGON_TOOLCHAIN}")
152-
tvm_file_glob(GLOB RUNTIME_HEXAGON_SIM_SRCS src/runtime/hexagon/android/sim/*.cc)
153-
include_directories(SYSTEM "${HEXAGON_TOOLCHAIN}/include/iss")
154-
link_directories("${HEXAGON_TOOLCHAIN}/lib/iss")
155-
list(APPEND TVM_RUNTIME_LINKER_LIBS "-lwrapper")
156-
ExternalProject_Add(sim_dev
157-
SOURCE_DIR "${CMAKE_SOURCE_DIR}/src/runtime/hexagon/android/sim/driver"
158-
CMAKE_ARGS
159-
"-DCMAKE_C_COMPILER=${HEXAGON_TOOLCHAIN}/bin/hexagon-clang"
160-
"-DCMAKE_CXX_COMPILER=${HEXAGON_TOOLCHAIN}/bin/hexagon-clang++"
161-
"-DHEXAGON_ARCH=${USE_HEXAGON_ARCH}"
162-
INSTALL_COMMAND "true"
163-
)
164-
elseif(USE_HEXAGON_DEVICE STREQUAL "${PICK_HW}")
165-
find_hexagon_sdk_root("${USE_HEXAGON_SDK}" "${USE_HEXAGON_ARCH}")
166-
find_hexagon_toolchain()
167-
tvm_file_glob(GLOB RUNTIME_HEXAGON_DEVICE_SRCS src/runtime/hexagon/android/target/*.cc)
168-
169-
include_directories(SYSTEM
170-
${HEXAGON_SDK_INCLUDES}
171-
${HEXAGON_RPCMEM_ROOT}/inc
172-
${HEXAGON_REMOTE_ROOT}
173-
)
174242

175-
list(APPEND TVM_RUNTIME_LINKER_LIBS "dl")
176-
if(BUILD_FOR_ANDROID)
177-
# Hexagon runtime uses __android_log_print, which is in liblog.
178-
list(APPEND TVM_RUNTIME_LINKER_LIBS "log")
179-
endif()
180-
endif()
181-
182-
set(RUNTIME_HEXAGON_COMMON_SRCS src/runtime/hexagon/hexagon_module.cc)
183-
if (USE_HEXAGON_DEVICE STREQUAL "${PICK_NONE}")
184-
if(BUILD_FOR_HEXAGON)
185-
tvm_file_glob(GLOB RUNTIME_HEXAGON_SRCS src/runtime/hexagon/hexagon/*.cc)
186-
elseif(BUILD_FOR_ANDROID AND HEXAGON_SDK_PATH_DEFINED)
187-
else()
188-
tvm_file_glob(GLOB RUNTIME_HEXAGON_SRCS src/runtime/hexagon/host/*.cc)
189-
endif()
190-
else()
191-
tvm_file_glob(GLOB RUNTIME_HEXAGON_SRCS src/runtime/hexagon/android/*.cc)
192-
endif()
193-
194-
list(APPEND RUNTIME_SRCS
195-
${RUNTIME_HEXAGON_SRCS}
196-
${RUNTIME_HEXAGON_SIM_SRCS}
197-
${RUNTIME_HEXAGON_DEVICE_SRCS}
198-
${RUNTIME_HEXAGON_COMMON_SRCS}
199-
)
243+
list(APPEND RUNTIME_SRCS ${RUNTIME_HEXAGON_SRCS})

src/runtime/hexagon/android/hexagon_module.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,12 @@ std::shared_ptr<Device> Device::Global() {
510510

511511
} // namespace hexagon
512512

513-
TVM_REGISTER_GLOBAL("runtime.module.loadfile_hexagon").set_body([](TVMArgs args, TVMRetValue* rv) {
514-
*rv = HexagonModuleLoadFile(args[0], args[1]);
515-
});
513+
// Disable this: it conflicts with loadfile_hexagon from hexagon_common.cc
514+
// This was only used with offload on Android, which is being deprecated.
515+
// TVM_REGISTER_GLOBAL("runtime.module.loadfile_hexagon").set_body([](TVMArgs args, TVMRetValue* rv)
516+
// {
517+
// *rv = HexagonModuleLoadFile(args[0], args[1]);
518+
// });
516519

517520
} // namespace runtime
518521
} // namespace tvm

0 commit comments

Comments
 (0)