Skip to content

Commit 73bbfbb

Browse files
author
Krzysztof Parzyszek
authored
[Hexagon] Do not auto-build apps when building TVM (#9970)
* [Hexagon] Do not auto-build apps when building TVM The Hexagon cmakes have recently become unwieldy due to a complex network of dependencies between various automatically built components. This was in large part because of trying to automatically build some apps, which then tried to build TVM runtimes again, but with their own configurations. This patch removes the ability to automatically build any Hexagon- -related apps from the main TVM build. The following cmake options are now deprecated: - `USE_HEXAGON_LAUNCHER` - `USE_HEXAGON_PROXY_RPC` In order to build the binaries needed for HexagonLauncher from tvm.contrib.hexagon: - Build TVM+runtime for x86, with codegen for Hexagon enabled. This can be done via `USE_HEXAGON_DEVICE=sim` or `target`. - Build Android runtime and tvm_rpc with `-DUSE_RPC=ON`, `-DUSE_CPP_RPC=ON`, and `-DUSE_HEXAGON_RPC=ON`. - Build Hexagon runtime with `-DUSE_HEXAGON_RPC=ON`, and `-DBUILD_STATIC_RUNTIME=ON`. * Add README.md * Restart CI * Add optional variable to set output directory
1 parent 6f2b35f commit 73bbfbb

6 files changed

Lines changed: 232 additions & 442 deletions

File tree

CMakeLists.txt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ tvm_option(ROCM_PATH "The path to rocm" /opt/rocm)
3232
tvm_option(USE_HEXAGON_DEVICE "Build with Hexagon device support in TVM runtime" OFF)
3333
tvm_option(USE_HEXAGON_SDK "Path to the Hexagon SDK root (required for Hexagon support in TVM runtime or for building TVM runtime for Hexagon)" /path/to/sdk)
3434
tvm_option(USE_HEXAGON_RPC "Enable Hexagon RPC using minRPC implementation over Android." OFF)
35-
tvm_option(USE_HEXAGON_LAUNCHER "Build the Hexagon graph launcher application" OFF)
36-
tvm_option(USE_HEXAGON_PROXY_RPC "Build the Hexagon Proxy RPC server application" OFF)
3735
tvm_option(USE_RPC "Build with RPC" ON)
3836
tvm_option(USE_THREADS "Build with thread support" ON)
3937
tvm_option(USE_LLVM "Build with LLVM, can be set to specific llvm-config path" OFF)
@@ -323,7 +321,7 @@ if(BUILD_FOR_HEXAGON)
323321
# runtime, since it would cause multiple definition errors with the
324322
# static one.
325323
if(NOT BUILD_STATIC_RUNTIME)
326-
list(APPEND RUNTIME_SRCS src/runtime/hexagon/hexagon_posix.cc)
324+
list(APPEND RUNTIME_SRCS src/runtime/hexagon/android/hexagon_posix.cc)
327325
# Allow undefined symbols (there will be some from libc).
328326
set(TVM_NO_UNDEFINED_SYMBOLS "")
329327
endif()
@@ -559,12 +557,6 @@ else()
559557
target_compile_definitions(tvm_libinfo_objs PRIVATE "USE_FALLBACK_STL_MAP=0")
560558
endif(USE_FALLBACK_STL_MAP)
561559

562-
if(BUILD_FOR_HEXAGON)
563-
# Wrap pthread_create to allow setting custom stack size.
564-
set_property(TARGET tvm_runtime APPEND PROPERTY LINK_FLAGS
565-
"-Wl,--wrap=pthread_create")
566-
endif()
567-
568560
if(USE_THREADS AND NOT BUILD_FOR_HEXAGON)
569561
message(STATUS "Build with thread support...")
570562
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
@@ -699,6 +691,18 @@ if(APPLE AND TVM_IS_DEBUG_BUILD)
699691
)
700692
endif()
701693

694+
if(BUILD_FOR_HEXAGON)
695+
# Wrap pthread_create to allow setting custom stack size.
696+
set_property(TARGET tvm_runtime APPEND PROPERTY LINK_FLAGS
697+
"-Wl,--wrap=pthread_create")
698+
# Link tvm_runtime into the RPC skel library. Make sure it's built
699+
# as a part of the "runtime" target.
700+
if(USE_HEXAGON_RPC)
701+
target_link_libraries(hexagon_rpc_skel -Wl,--whole-archive tvm_runtime -Wl,--no-whole-archive)
702+
add_dependencies(runtime hexagon_rpc_skel)
703+
endif()
704+
endif()
705+
702706
#Caches the build.
703707
#Note that ccache-3.x doesn't support nvcc well, so CUDA kernels may never hit the cache and still
704708
#need to be re-compiled every time. Using ccache 4.0+ can resolve this issue.

apps/hexagon_api/CMakeLists.txt

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
cmake_minimum_required(VERSION 3.2)
2+
3+
project(hexagon_api)
4+
5+
include(ExternalProject)
6+
7+
# Required variables:
8+
# ANDROID_ABI
9+
# ANDROID_PLATFORM
10+
# USE_ANDROID_TOOLCHAIN (Android toolchain .cmake file)
11+
# USE_HEXAGON_ARCH
12+
# USE_HEXAGON_SDK
13+
# USE_HEXAGON_TOOLCHAIN (Path to Hexagon toolchain ending with "Tools")
14+
# Optional variable:
15+
# USE_OUTPUT_BINARY_DIR (Path to copy the output binaries to)
16+
17+
set(TVM_SOURCE_DIR "${CMAKE_SOURCE_DIR}/../..")
18+
19+
if(DEFINED USE_OUTPUT_BINARY_DIR)
20+
set(HEXAGON_API_BINARY_DIR "${USE_OUTPUT_BINARY_DIR}")
21+
else()
22+
set(HEXAGON_API_BINARY_DIR "${CMAKE_BINARY_DIR}/hexagon_rpc")
23+
endif()
24+
file(MAKE_DIRECTORY ${HEXAGON_API_BINARY_DIR})
25+
26+
# Build Android binaries:
27+
# - libtvm_runtime.so
28+
# - tvm_rpc_android
29+
30+
ExternalProject_Add(android_tvm_runtime_rpc
31+
SOURCE_DIR "${TVM_SOURCE_DIR}"
32+
BUILD_COMMAND $(MAKE) runtime tvm_rpc
33+
CMAKE_ARGS
34+
"-DCMAKE_TOOLCHAIN_FILE=${USE_ANDROID_TOOLCHAIN}"
35+
"-DANDROID_PLATFORM=${ANDROID_PLATFORM}"
36+
"-DANDROID_ABI=${ANDROID_ABI}"
37+
"-DUSE_HEXAGON_SDK=${USE_HEXAGON_SDK}"
38+
"-DUSE_HEXAGON_ARCH=${USE_HEXAGON_ARCH}"
39+
"-DCMAKE_CXX_STANDARD=14"
40+
"-DUSE_LIBBACKTRACE=OFF"
41+
"-DUSE_LLVM=OFF"
42+
"-DUSE_RPC=ON"
43+
"-DUSE_CPP_RPC=ON"
44+
"-DUSE_HEXAGON_RPC=ON"
45+
INSTALL_COMMAND ""
46+
BUILD_ALWAYS ON
47+
)
48+
ExternalProject_Get_Property(android_tvm_runtime_rpc BINARY_DIR)
49+
ExternalProject_Add_Step(android_tvm_runtime_rpc copy_runtime
50+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
51+
${BINARY_DIR}/libtvm_runtime.so
52+
${HEXAGON_API_BINARY_DIR}
53+
DEPENDEES install
54+
)
55+
ExternalProject_Add_Step(android_tvm_runtime_rpc copy_rpc_server
56+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
57+
${BINARY_DIR}/tvm_rpc
58+
${HEXAGON_API_BINARY_DIR}/tvm_rpc_android
59+
DEPENDEES install
60+
)
61+
62+
63+
# Build Hexagon binaries:
64+
# - libhexagon_rpc_skel.so
65+
# - libtvm_runtime.a
66+
67+
ExternalProject_Add(hexagon_tvm_runtime_rpc
68+
SOURCE_DIR "${TVM_SOURCE_DIR}"
69+
BUILD_COMMAND $(MAKE) runtime
70+
CMAKE_ARGS
71+
"-DCMAKE_C_COMPILER=${USE_HEXAGON_TOOLCHAIN}/bin/hexagon-clang"
72+
"-DCMAKE_CXX_COMPILER=${USE_HEXAGON_TOOLCHAIN}/bin/hexagon-clang++"
73+
"-DUSE_HEXAGON_SDK=${USE_HEXAGON_SDK}"
74+
"-DUSE_HEXAGON_ARCH=${USE_HEXAGON_ARCH}"
75+
"-DUSE_LIBBACKTRACE=OFF"
76+
"-DUSE_RPC=OFF"
77+
"-DUSE_HEXAGON_RPC=ON"
78+
"-DBUILD_STATIC_RUNTIME=ON"
79+
INSTALL_COMMAND ""
80+
BUILD_ALWAYS ON
81+
)
82+
ExternalProject_Get_Property(hexagon_tvm_runtime_rpc BINARY_DIR)
83+
ExternalProject_Add_Step(hexagon_tvm_runtime_rpc copy_binaries
84+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
85+
${BINARY_DIR}/libtvm_runtime.a
86+
${BINARY_DIR}/libhexagon_rpc_skel.so
87+
${HEXAGON_API_BINARY_DIR}
88+
DEPENDEES install
89+
)
90+

apps/hexagon_api/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<!--- Licensed to the Apache Software Foundation (ASF) under one -->
2+
<!--- or more contributor license agreements. See the NOTICE file -->
3+
<!--- distributed with this work for additional information -->
4+
<!--- regarding copyright ownership. The ASF licenses this file -->
5+
<!--- to you under the Apache License, Version 2.0 (the -->
6+
<!--- "License"); you may not use this file except in compliance -->
7+
<!--- with the License. You may obtain a copy of the License at -->
8+
9+
<!--- http://www.apache.org/licenses/LICENSE-2.0 -->
10+
11+
<!--- Unless required by applicable law or agreed to in writing, -->
12+
<!--- software distributed under the License is distributed on an -->
13+
<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
14+
<!--- KIND, either express or implied. See the License for the -->
15+
<!--- specific language governing permissions and limitations -->
16+
<!--- under the License. -->
17+
18+
# Hexagon API app
19+
20+
This is a meta-app that build the necessary binaries for use with
21+
the `HexagonLauncher` utility from `tvm.contrib.hexagon`.
22+
23+
It will build the TVM runtime for Android, the RPC server application
24+
for Android, and the RPC library for Hexagon with the TVM runtime for
25+
Hexagon built into it.
26+
27+
## Configuration
28+
29+
There is a set of configuration variables that are required for cmake:
30+
- `ANDROID_ABI`: Set this to `arm64-v8a`.
31+
- `ANDROID_PLATFORM`: This can be `android-28`.
32+
- `USE_ANDROID_TOOLCHAIN`: The path to the Android toolchain file, i.e.
33+
`android.toolchain.cmake`. This file is a part of the Android NDK.
34+
- `USE_HEXAGON_ARCH`: The version string of the Hexagon architecture
35+
to use, i.e. vNN. The typical setting would be `v68` or later.
36+
- `USE_HEXAGON_SDK`: The path to the Hexagon SDK. Set this path in such
37+
a way that `${USE_HEXAGON_SDK}/setup_sdk_env.source` exists.
38+
- `USE_HEXAGON_TOOLCHAIN`: Path to Hexagon toolchain. It can be the
39+
Hexagon toolchain included in the SDK, for example
40+
`${USE_HEXAGON_TOOLCHAIN}/tools/HEXAGON_Tools/x.y.z/Tools`. The `x.y.z`
41+
in the path is the toolchain version number, which is specific to the
42+
version of the SDK.
43+
44+
Additionally, the variable `USE_OUTPUT_BINARY_DIR` can be set to indicate
45+
the location where the generated binaries will be placed. If not set, it
46+
defaults to `hexagon_rpc` subdirectory in the current build directory.
47+
48+
49+
## Build
50+
51+
The build will generate the following binaries:
52+
- `tvm_runtime.so`: TVM runtime for Android (shared library).
53+
- `tvm_rpc_android`: RPC server for Android.
54+
- `libhexagon_rpc_skel.so`: RPC library for Hexagon.
55+
- `libtvm_runtime.a`: TVM runtime for Hexagon (static library).
56+
57+
The RPC library for Hexagon contains the TVM runtime, so the static
58+
TVM runtime for Hexagon is not strictly necessary.

apps/hexagon_launcher/README.md

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,43 +31,6 @@ The supported Snapdragon architectures are 855, 865, and 888.
3131
Android NDK can be downloaded from https://developer.android.com/ndk.
3232
Hexagon SDK is available at //developer.qualcomm.com/software/hexagon-dsp-sdk.
3333

34-
### Compilation with TVM
35-
36-
Building the Hexagon launcher application as a component of the main TVM build
37-
used for Hexagon codegen can be achieved by setting `USE_HEXAGON_LAUNCHER=ON`.
38-
This option will compile core tvm, the android launcher binary and its corresponding
39-
tvm_runtime, as well as the Hexagon launcher shared library and its corresponding
40-
tvm_runtime. As described in the [Manual compilation](#Manual compilation) section
41-
each component requires Hexagon and android dependencies. When building the launcher
42-
along with TVM these configurations must be providing when invoking cmake. A minimal
43-
example invocation for compiling TVM along with the Hexagon launcher is included below:
44-
45-
```
46-
cmake -DCMAKE_C_COMPILER=/path/to/clang \
47-
-DCMAKE_CXX_COMPILER=/path/to/clang++ \
48-
-DCMAKE_CXX_FLAGS='-stdlib=libc++' \
49-
-DCMAKE_CXX_STANDARD=14 \
50-
-DUSE_LLVM=/path/to/llvm/bin/llvm-config \
51-
-DUSE_HEXAGON_ARCH=v65|v66|v68 \
52-
-DUSE_HEXAGON_LAUNCHER=ON \
53-
-DUSE_HEXAGON_SDK=/path/to/hexagon/SDK \
54-
-DUSE_HEXAGON_TOOLCHAIN=/path/to/hexagon/toolchain/ ..
55-
-DANDROID_ABI=arm64-v8a \
56-
-DANDROID_PLATFORM=android-28 \
57-
-DUSE_ANDROID_TOOLCHAIN=/path/to/android-ndk/build/cmake/android.toolchain.cmake \
58-
..
59-
```
60-
61-
where `v65|v66|v68` means "one of" these architecture versions.
62-
The Hexagon launcher application is an android binary and thus requires the use
63-
of an android toolchain for compilation. Similarly, the Hexagon tvm runtime
64-
requires the use of the Hexagon toolchain and depends on the Hexagon SDK. The
65-
resulting hexagon launcher binaries can be found in the `apps_hexagon_launcher`
66-
subdirectory of the cmake build directory. The above command
67-
will build support for Hexagon codegen in the TVM library that requires
68-
`USE_LLVM` to be set to an llvm-config that has the Hexagon target built in.
69-
70-
7134
### Manual compilation
7235

7336
Since some source files are shared between the Hexagon and android builds,

0 commit comments

Comments
 (0)