Skip to content

Commit

Permalink
[Hexagon] Reenable compilation of TVM runtime for Hexagon (apache#7784)
Browse files Browse the repository at this point in the history
- Add support for Hexagon SDK 4.x (different directory structure)
- Conditionally disable functions not present on Hexagon (popen, etc.)
- Bump sim_dev architecture target to v65 (older versions can still be
  used with older compilers).

Co-authored-by: Ravishankar Kolachana <[email protected]>

Co-authored-by: Ravishankar Kolachana <[email protected]>
  • Loading branch information
2 people authored and Trevor Morris committed May 6, 2021
1 parent ca06cfd commit a83f6aa
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 12 deletions.
6 changes: 0 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,6 @@ if(BUILD_FOR_HEXAGON)
# Wrap pthread_create to allow setting custom stack size.
set_property(TARGET tvm_runtime APPEND PROPERTY LINK_FLAGS
"-Wl,--wrap=pthread_create")

target_include_directories(tvm_runtime SYSTEM
PUBLIC "${USE_HEXAGON_SDK}/libs/common/qurt/ADSPv62MP/include/posix"
PUBLIC "${USE_HEXAGON_SDK}/libs/common/qurt/ADSPv62MP/include/qurt"
PUBLIC "${USE_HEXAGON_SDK}/incs"
PUBLIC "${USE_HEXAGON_SDK}/incs/stddef")
endif()

if(USE_THREADS AND NOT BUILD_FOR_HEXAGON)
Expand Down
48 changes: 44 additions & 4 deletions cmake/modules/Hexagon.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ set(PICK_SIM "sim")
set(PICK_HW "target")
set(PICK_NONE "OFF")

set(FOUND_HEXAGON_SDK_ROOT FALSE)
set(FOUND_HEXAGON_TOOLCHAIN FALSE)

function(find_hexagon_toolchain)
if (NOT "${USE_HEXAGON_TOOLCHAIN}" STREQUAL "")
if(FOUND_HEXAGON_TOOLCHAIN)
return()
endif()
if(NOT "${USE_HEXAGON_TOOLCHAIN}" STREQUAL "")
set(TRY_PATH "${USE_HEXAGON_TOOLCHAIN}")
else()
set(TRY_PATH "${USE_HEXAGON_SDK}")
Expand All @@ -35,12 +41,16 @@ function(find_hexagon_toolchain)
get_filename_component(HEXAGON_TMP1 "${HEXAGON_TMP0}" DIRECTORY)
set(HEXAGON_TOOLCHAIN "${HEXAGON_TMP1}" CACHE PATH
"Path to the Hexagon toolchain")
set(FOUND_HEXAGON_TOOLCHAIN TRUE)
else(HEXAGON_CLANG)
message(SEND_ERROR "Cannot find Hexagon toolchain in ${TRY_PATH}")
endif()
endfunction()

function(find_hexagon_sdk_root)
if(FOUND_HEXAGON_SDK_ROOT)
return()
endif()
message(STATUS "Checking Hexagon SDK root: ${USE_HEXAGON_SDK}")
file(GLOB_RECURSE HEXAGON_AEESTDDEF "${USE_HEXAGON_SDK}/*/AEEStdDef.h")
if(HEXAGON_AEESTDDEF)
Expand All @@ -50,11 +60,30 @@ function(find_hexagon_sdk_root)
get_filename_component(HEXAGON_TMP2 "${HEXAGON_TMP1}" DIRECTORY)
set(HEXAGON_SDK_ROOT "${HEXAGON_TMP2}" CACHE PATH
"Root directory of Hexagon SDK")
set(FOUND_HEXAGON_SDK_ROOT TRUE)
else(HEXAGON_AEESTDDEF)
message(SEND_ERROR "Cannot validate Hexagon SDK in ${USE_HEXAGON_SDK}")
endif()
endfunction()

if(BUILD_FOR_HEXAGON)
find_hexagon_sdk_root()
if(HEXAGON_SDK_ROOT MATCHES "3.5.1")
message(SEND_ERROR "Hexagon SDK 3.5.1 is not supported")
elseif(HEXAGON_SDK_ROOT MATCHES "3\.[0-9]+\.[0-9]+")
include_directories(
SYSTEM "${USE_HEXAGON_SDK}/libs/common/qurt/ADSPv62MP/include/posix"
SYSTEM "${USE_HEXAGON_SDK}/libs/common/qurt/ADSPv62MP/include/qurt")
else()
include_directories(
SYSTEM "${HEXAGON_SDK_ROOT}/rtos/qurt/computev65/include/posix"
SYSTEM "${HEXAGON_SDK_ROOT}/rtos/qurt/computev65/include/qurt")
endif()
include_directories(
SYSTEM "${HEXAGON_SDK_ROOT}/incs"
SYSTEM "${HEXAGON_SDK_ROOT}/incs/stddef")
endif()

if(USE_HEXAGON_DEVICE STREQUAL "OFF")
list(APPEND COMPILER_SRCS src/target/opt/build_hexagon_off.cc)
return()
Expand All @@ -67,7 +96,7 @@ elseif(NOT USE_HEXAGON_DEVICE STREQUAL "${PICK_SIM}" AND
endif()
# If USE_HEXAGON_DEVICE is set to a valid value, make sure that USE_HEXAGON_SDK
# is defined.
if (NOT USE_HEXAGON_SDK)
if(NOT USE_HEXAGON_SDK)
message(SEND_ERROR "Please set USE_HEXAGON_SDK to the Hexagon SDK root")
return()
endif()
Expand All @@ -90,11 +119,22 @@ elseif(USE_HEXAGON_DEVICE STREQUAL "${PICK_HW}")
find_hexagon_sdk_root()
find_hexagon_toolchain()
message(STATUS "Hexagon SDK: ${HEXAGON_SDK_ROOT}")
if(HEXAGON_SDK_ROOT MATCHES "3.5.1")
message(SEND_ERROR "Hexagon SDK 3.5.1 is not supported")
elseif(HEXAGON_SDK_ROOT MATCHES "3\.[0-9]+\.[0-9]+")
set(RPCMEM_DIR "libs/common/rpcmem")
set(REMOTE_DIR "libs/common/remote/ship/android_Release_aarch64")
else()
set(RPCMEM_DIR "ipc/fastrpc/rpcmem")
set(REMOTE_DIR "ipc/fastrpc/remote/ship/android_aarch64")
endif()
file(GLOB RUNTIME_HEXAGON_DEVICE_SRCS src/runtime/hexagon/target/*.cc)
include_directories(SYSTEM "${HEXAGON_SDK_ROOT}/incs/stddef")
include_directories(SYSTEM "${HEXAGON_SDK_ROOT}/libs/common/rpcmem/inc")
include_directories(SYSTEM "${HEXAGON_SDK_ROOT}/${RPCMEM_DIR}/inc")
include_directories(
SYSTEM "${HEXAGON_SDK_ROOT}/incs")
include_directories(
SYSTEM "${HEXAGON_SDK_ROOT}/libs/common/remote/ship/android_Release_aarch64")
SYSTEM "${HEXAGON_SDK_ROOT}/${REMOTE_DIR}")
include_directories(SYSTEM "${HEXAGON_TOOLCHAIN}/include/iss")
list(APPEND TVM_RUNTIME_LINKER_LIBS "dl")
if(BUILD_FOR_ANDROID)
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/hexagon/sim/driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ set(EXTRA_CXX_FLAGS
"-O2"
"-Wno-format"
"-mhvx -mhvx-length=128b"
"-mv60"
"-mv65"
"-stdlib=libc++"
)

Expand Down
14 changes: 13 additions & 1 deletion src/support/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
#include <stdio.h>
#ifndef _WIN32
#include <sys/types.h>
#ifndef __hexagon__
#include <sys/wait.h>
#endif
#endif // __hexagon__
#endif // _WIN32
#include <algorithm>
#include <array>
#include <cctype>
Expand All @@ -45,52 +47,60 @@ namespace support {
* \param type "r" is for reading or "w" for writing.
* \return normal standard stream
*/
#ifndef __hexagon__
inline FILE* TVMPOpen(const char* command, const char* type) {
#if defined(_WIN32)
return _popen(command, type);
#else
return popen(command, type);
#endif
}
#endif // __hexagon__

/*!
* \brief TVMPClose wrapper of pclose between windows / linux
* \param stream the stream needed to be close.
* \return exit status
*/
#ifndef __hexagon__
inline int TVMPClose(FILE* stream) {
#if defined(_WIN32)
return _pclose(stream);
#else
return pclose(stream);
#endif
}
#endif // __hexagon__

/*!
* \brief TVMWifexited wrapper of WIFEXITED between windows / linux
* \param status The status field that was filled in by the wait or waitpid function
* \return the exit code of the child process
*/
#ifndef __hexagon__
inline int TVMWifexited(int status) {
#if defined(_WIN32)
return (status != 3);
#else
return WIFEXITED(status);
#endif
}
#endif // __hexagon__

/*!
* \brief TVMWexitstatus wrapper of WEXITSTATUS between windows / linux
* \param status The status field that was filled in by the wait or waitpid function.
* \return the child process exited normally or not
*/
#ifndef __hexagon__
inline int TVMWexitstatus(int status) {
#if defined(_WIN32)
return status;
#else
return WEXITSTATUS(status);
#endif
}
#endif // __hexagon__

/*!
* \brief IsNumber check whether string is a number.
Expand Down Expand Up @@ -137,6 +147,7 @@ inline bool EndsWith(std::string const& value, std::string const& end) {
* \param err_msg The error message if we have
* \return executed output status
*/
#ifndef __hexagon__
inline int Execute(std::string cmd, std::string* err_msg) {
std::array<char, 128> buffer;
std::string result;
Expand All @@ -151,6 +162,7 @@ inline int Execute(std::string cmd, std::string* err_msg) {
}
return 255;
}
#endif // __hexagon__

/*!
* \brief Combine two hash values into a single one.
Expand Down

0 comments on commit a83f6aa

Please sign in to comment.