diff --git a/cmake/FindHexagonSDK.cmake b/cmake/FindHexagonSDK.cmake new file mode 100644 index 000000000000..9ee72008254b --- /dev/null +++ b/cmake/FindHexagonSDK.cmake @@ -0,0 +1,92 @@ +include(FindPackageHandleStandardArgs) + +## +# Find the Hexagon SDK root + +# We use the presense of the hexagon toolchain file to determine the SDK +# root. Other files have names that are too generic (like readme.txt) or +# are platform-specific (like setup_sdk_env.source) to and so can't be +# used to autodetect the path. Plus, we need to find this file anyway. + +find_path( + HEXAGON_SDK_ROOT build/cmake/hexagon_toolchain.cmake + HINTS ENV HEXAGON_SDK_ROOT +) + +## +# Detect the installed Hexagon tools version + +if (NOT DEFINED HEXAGON_TOOLS_VER AND DEFINED ENV{HEXAGON_TOOLS_VER}) + set(HEXAGON_TOOLS_VER "$ENV{HEXAGON_TOOLS_VER}") +endif () + +if (NOT DEFINED HEXAGON_TOOLS_VER) + # No other way to list a directory; no need for CONFIGURE_DEPENDS here + # since this is just used to initialize a cache variable. + file( + GLOB tools_versions + RELATIVE "${HEXAGON_SDK_ROOT}/tools/HEXAGON_Tools" + "${HEXAGON_SDK_ROOT}/tools/HEXAGON_Tools/*" + ) + if (NOT tools_versions STREQUAL "") + list(GET tools_versions 0 HEXAGON_TOOLS_VER) + endif () +endif () + +set(HEXAGON_TOOLS_VER "${HEXAGON_TOOLS_VER}" + CACHE STRING "Version of the Hexagon tools to use") + +set(HEXAGON_TOOLS_ROOT "${HEXAGON_SDK_ROOT}/tools/HEXAGON_Tools/${HEXAGON_TOOLS_VER}") + +## +# Set known paths + +set(HEXAGON_TOOLCHAIN ${HEXAGON_SDK_ROOT}/build/cmake/hexagon_toolchain.cmake) +set(HEXAGON_QAIC ${HEXAGON_SDK_ROOT}/ipc/fastrpc/qaic/Ubuntu16/qaic) + +set(ANDROID_NDK_ROOT ${HEXAGON_SDK_ROOT}/tools/android-ndk-r19c) +set(ANDROID_NDK_TOOLCHAIN ${ANDROID_NDK_ROOT}/build/cmake/android.toolchain.cmake) + +## +# Find ISS wrapper library and headers + +find_library( + HEXAGON_ISS_WRAPPER_LIBRARY + NAMES wrapper + HINTS "${HEXAGON_TOOLS_ROOT}" + PATH_SUFFIXES Tools/lib/iss lib/iss iss +) + +find_path( + HEXAGON_ISS_WRAPPER_INCLUDE_DIRECTORY + NAMES HexagonWrapper.h + HINTS "${HEXAGON_TOOLS_ROOT}" + PATH_SUFFIXES Tools/include/iss include/iss iss +) + +## +# Validate we found everything correctly + +find_package_handle_standard_args( + HexagonSDK + REQUIRED_VARS + HEXAGON_SDK_ROOT + HEXAGON_TOOLS_ROOT + HEXAGON_TOOLCHAIN + HEXAGON_ISS_WRAPPER_LIBRARY + HEXAGON_ISS_WRAPPER_INCLUDE_DIRECTORY + HANDLE_COMPONENTS +) + +## +# Create imported targets + +if (HexagonSDK_FOUND AND NOT TARGET HexagonSDK::wrapper) + add_library(HexagonSDK::wrapper UNKNOWN IMPORTED) + set_target_properties( + HexagonSDK::wrapper + PROPERTIES + IMPORTED_LOCATION "${HEXAGON_ISS_WRAPPER_LIBRARY}" + INTERFACE_INCLUDE_DIRECTORIES "${HEXAGON_ISS_WRAPPER_INCLUDE_DIRECTORY}" + ) +endif () diff --git a/src/runtime/CMakeLists.txt b/src/runtime/CMakeLists.txt index 71af475c2eb4..b1331ed07e52 100644 --- a/src/runtime/CMakeLists.txt +++ b/src/runtime/CMakeLists.txt @@ -338,4 +338,8 @@ add_library(Halide_Runtime INTERFACE) add_library(Halide::Runtime ALIAS Halide_Runtime) target_include_directories(Halide_Runtime INTERFACE $) set_target_properties(Halide_Runtime PROPERTIES EXPORT_NAME Runtime) +option(Halide_BUILD_HEXAGON_REMOTE_RUNTIME "Build the hexagon remote runtime for offloading to Hexagon (HVX)" OFF) +if (Halide_BUILD_HEXAGON_REMOTE_RUNTIME AND NOT Halide_CLANG_TIDY_BUILD) + add_subdirectory(hexagon_remote) +endif () diff --git a/src/runtime/hexagon_host.cpp b/src/runtime/hexagon_host.cpp index 98a5bcd943ef..7035741a9fdf 100644 --- a/src/runtime/hexagon_host.cpp +++ b/src/runtime/hexagon_host.cpp @@ -147,7 +147,11 @@ WEAK int init_hexagon_runtime(void *user_context) { if (!host_lib) { host_lib = halide_load_library("libhalide_hexagon_host.dll"); } - + if (!host_lib) { + // This will now cause a more specific error 'halide_error_code_symbol_not_found' down the line. + // So, just print this message and continue on instead of returning a generic error here. + error(user_context) << "Hexagon: unable to load libhalide_hexagon_host.so"; + } debug(user_context) << "Hexagon: init_hexagon_runtime (user_context: " << user_context << ")\n"; // Get the symbols we need from the library. diff --git a/src/runtime/hexagon_remote/CMakeLists.txt b/src/runtime/hexagon_remote/CMakeLists.txt new file mode 100644 index 000000000000..c5fe73ab7405 --- /dev/null +++ b/src/runtime/hexagon_remote/CMakeLists.txt @@ -0,0 +1,74 @@ +include(ExternalProject) + +find_package(HexagonSDK REQUIRED) + +add_custom_command( + OUTPUT + halide_hexagon_remote.h + halide_hexagon_remote_skel.c + halide_hexagon_remote_stub.c + COMMAND ${HEXAGON_QAIC} -I ${HEXAGON_SDK_ROOT}/incs/stddef ${CMAKE_CURRENT_SOURCE_DIR}/halide_hexagon_remote.idl + DEPENDS halide_hexagon_remote.idl + VERBATIM +) + +add_custom_target( + halide_hexagon_remote_idl + DEPENDS + ${CMAKE_CURRENT_BINARY_DIR}/halide_hexagon_remote.h + ${CMAKE_CURRENT_BINARY_DIR}/halide_hexagon_remote_skel.c + ${CMAKE_CURRENT_BINARY_DIR}/halide_hexagon_remote_stub.c +) + +set(common_cache_args + "-DHALIDE_HEXAGON_REMOTE_IDL:PATH=${CMAKE_CURRENT_BINARY_DIR}" + "-DHEXAGON_SDK_ROOT:PATH=${HEXAGON_SDK_ROOT}" + "-DHEXAGON_TOOLS_ROOT:PATH=${HEXAGON_TOOLS_ROOT}" + "-DCMAKE_INSTALL_PREFIX:PATH=" +) + +if (CMAKE_BUILD_TYPE) + list(APPEND common_cache_args "-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}") +endif () + +ExternalProject_Add( + hexagon_remote-qurt + SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/qurt" + CMAKE_CACHE_ARGS + "-DCMAKE_TOOLCHAIN_FILE:FILEPATH=${HEXAGON_TOOLCHAIN}" + ${common_cache_args} + PREFIX hexagon + DEPENDS halide_hexagon_remote_idl + CONFIGURE_HANDLED_BY_BUILD ON +) + +set(arm_abis armeabi-v7a arm64-v8a) +set(arm_bits 32 64) +foreach (abi bits IN ZIP_LISTS arm_abis arm_bits) + ExternalProject_Add( + halide_hexagon_host-${abi} + SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android" + CMAKE_CACHE_ARGS + "-DCMAKE_TOOLCHAIN_FILE:FILEPATH=${ANDROID_NDK_TOOLCHAIN}" + "-DANDROID_ABI:STRING=${abi}" + "-DANDROID_PLATFORM:STRING=21" + ${common_cache_args} + PREFIX arm-${bits}-android + DEPENDS halide_hexagon_remote_idl + CONFIGURE_HANDLED_BY_BUILD ON + ) +endforeach () + +add_library(halide_hexagon_host SHARED sim_host.cpp sim_protocol.h) +target_compile_features(halide_hexagon_host PRIVATE cxx_std_17) +target_include_directories(halide_hexagon_host PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/..) +target_link_libraries(halide_hexagon_host PRIVATE HexagonSDK::wrapper) + +add_custom_target(hexagon_remote) +add_dependencies( + hexagon_remote + hexagon_remote-qurt + halide_hexagon_host + halide_hexagon_host-armeabi-v7a + halide_hexagon_host-arm64-v8a +) diff --git a/src/runtime/hexagon_remote/android/CMakeLists.txt b/src/runtime/hexagon_remote/android/CMakeLists.txt new file mode 100644 index 000000000000..1e7465f68f75 --- /dev/null +++ b/src/runtime/hexagon_remote/android/CMakeLists.txt @@ -0,0 +1,41 @@ +cmake_minimum_required(VERSION 3.22) +project(halide-hexagon_remote-android) + +set(_aarch64 "") +if (CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64") + set(_aarch64 "_aarch64") +endif () + +add_library(fastrpc::cdsprpc SHARED IMPORTED) +set_target_properties( + fastrpc::cdsprpc + PROPERTIES + IMPORTED_LOCATION "${HEXAGON_SDK_ROOT}/ipc/fastrpc/remote/ship/android${_aarch64}/libcdsprpc.so" +) + +add_library( + halide_hexagon_host + MODULE + ${HALIDE_HEXAGON_REMOTE_IDL}/halide_hexagon_remote_stub.c + host_malloc.cpp + host_shim.cpp + libadsprpc_shim.cpp +) +target_include_directories( + halide_hexagon_host + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/../.. +) +target_include_directories( + halide_hexagon_host + SYSTEM PRIVATE + ${HALIDE_HEXAGON_REMOTE_IDL} + ${HEXAGON_SDK_ROOT}/incs + ${HEXAGON_SDK_ROOT}/incs/stddef +) +target_link_libraries(halide_hexagon_host PRIVATE fastrpc::cdsprpc log) + +install( + TARGETS halide_hexagon_host + DESTINATION bin +) diff --git a/src/runtime/hexagon_remote/host_malloc.cpp b/src/runtime/hexagon_remote/android/host_malloc.cpp similarity index 100% rename from src/runtime/hexagon_remote/host_malloc.cpp rename to src/runtime/hexagon_remote/android/host_malloc.cpp diff --git a/src/runtime/hexagon_remote/host_shim.cpp b/src/runtime/hexagon_remote/android/host_shim.cpp similarity index 100% rename from src/runtime/hexagon_remote/host_shim.cpp rename to src/runtime/hexagon_remote/android/host_shim.cpp diff --git a/src/runtime/hexagon_remote/libadsprpc_shim.cpp b/src/runtime/hexagon_remote/android/libadsprpc_shim.cpp similarity index 100% rename from src/runtime/hexagon_remote/libadsprpc_shim.cpp rename to src/runtime/hexagon_remote/android/libadsprpc_shim.cpp diff --git a/src/runtime/hexagon_remote/qurt/CMakeLists.txt b/src/runtime/hexagon_remote/qurt/CMakeLists.txt new file mode 100644 index 000000000000..1a0f42566752 --- /dev/null +++ b/src/runtime/hexagon_remote/qurt/CMakeLists.txt @@ -0,0 +1,43 @@ +cmake_minimum_required(VERSION 3.22) + +# The Hexagon toolchain is broken +set(ENV{HEXAGON_SDK_ROOT} "${HEXAGON_SDK_ROOT}") +set(ENV{HEXAGON_TOOLS_ROOT} "${HEXAGON_TOOLS_ROOT}") + +project(halide-hexagon_remote-qurt) + +add_library(sim_qurt STATIC sim_qurt.cpp sim_qurt_vtcm.cpp) +target_include_directories(sim_qurt SYSTEM PRIVATE ${HALIDE_HEXAGON_REMOTE_IDL}) + +add_executable( + hexagon_sim_remote + known_symbols.cpp + sim_remote.cpp + $ +) +target_include_directories( + hexagon_sim_remote + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/.. + ${CMAKE_CURRENT_SOURCE_DIR}/../.. +) +target_include_directories(hexagon_sim_remote SYSTEM PRIVATE ${HALIDE_HEXAGON_REMOTE_IDL}) +target_link_libraries(hexagon_sim_remote PRIVATE ${CMAKE_DL_LIBS}) + +add_library( + halide_hexagon_remote_skel + MODULE + c11_stubs.cpp + halide_remote.cpp + known_symbols.cpp + log.cpp + nearbyint.cpp + ${HALIDE_HEXAGON_REMOTE_IDL}/halide_hexagon_remote_skel.c +) +target_include_directories(halide_hexagon_remote_skel PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../..) +target_include_directories(halide_hexagon_remote_skel SYSTEM PRIVATE ${HALIDE_HEXAGON_REMOTE_IDL}) + +install( + TARGETS sim_qurt hexagon_sim_remote halide_hexagon_remote_skel + DESTINATION bin +) diff --git a/src/runtime/hexagon_remote/c11_stubs.cpp b/src/runtime/hexagon_remote/qurt/c11_stubs.cpp similarity index 100% rename from src/runtime/hexagon_remote/c11_stubs.cpp rename to src/runtime/hexagon_remote/qurt/c11_stubs.cpp diff --git a/src/runtime/hexagon_remote/halide_remote.cpp b/src/runtime/hexagon_remote/qurt/halide_remote.cpp similarity index 99% rename from src/runtime/hexagon_remote/halide_remote.cpp rename to src/runtime/hexagon_remote/qurt/halide_remote.cpp index d990caeeb89b..2af8b8606cdb 100644 --- a/src/runtime/hexagon_remote/halide_remote.cpp +++ b/src/runtime/hexagon_remote/qurt/halide_remote.cpp @@ -15,8 +15,8 @@ extern "C" { #include "known_symbols.h" #include "log.h" -const int stack_alignment = 128; -const int stack_size = 1024 * 1024; +// const int stack_alignment = 128; +// const int stack_size = 1024 * 1024; typedef halide_hexagon_remote_handle_t handle_t; typedef halide_hexagon_remote_buffer buffer; diff --git a/src/runtime/hexagon_remote/known_symbols.cpp b/src/runtime/hexagon_remote/qurt/known_symbols.cpp similarity index 100% rename from src/runtime/hexagon_remote/known_symbols.cpp rename to src/runtime/hexagon_remote/qurt/known_symbols.cpp diff --git a/src/runtime/hexagon_remote/known_symbols.h b/src/runtime/hexagon_remote/qurt/known_symbols.h similarity index 100% rename from src/runtime/hexagon_remote/known_symbols.h rename to src/runtime/hexagon_remote/qurt/known_symbols.h diff --git a/src/runtime/hexagon_remote/log.cpp b/src/runtime/hexagon_remote/qurt/log.cpp similarity index 100% rename from src/runtime/hexagon_remote/log.cpp rename to src/runtime/hexagon_remote/qurt/log.cpp diff --git a/src/runtime/hexagon_remote/log.h b/src/runtime/hexagon_remote/qurt/log.h similarity index 100% rename from src/runtime/hexagon_remote/log.h rename to src/runtime/hexagon_remote/qurt/log.h diff --git a/src/runtime/hexagon_remote/nearbyint.cpp b/src/runtime/hexagon_remote/qurt/nearbyint.cpp similarity index 100% rename from src/runtime/hexagon_remote/nearbyint.cpp rename to src/runtime/hexagon_remote/qurt/nearbyint.cpp diff --git a/src/runtime/hexagon_remote/sim_qurt.cpp b/src/runtime/hexagon_remote/qurt/sim_qurt.cpp similarity index 100% rename from src/runtime/hexagon_remote/sim_qurt.cpp rename to src/runtime/hexagon_remote/qurt/sim_qurt.cpp diff --git a/src/runtime/hexagon_remote/sim_qurt_vtcm.cpp b/src/runtime/hexagon_remote/qurt/sim_qurt_vtcm.cpp similarity index 100% rename from src/runtime/hexagon_remote/sim_qurt_vtcm.cpp rename to src/runtime/hexagon_remote/qurt/sim_qurt_vtcm.cpp diff --git a/src/runtime/hexagon_remote/sim_remote.cpp b/src/runtime/hexagon_remote/qurt/sim_remote.cpp similarity index 100% rename from src/runtime/hexagon_remote/sim_remote.cpp rename to src/runtime/hexagon_remote/qurt/sim_remote.cpp