|
| 1 | +#=============================================================================== |
| 2 | +# Fetches Unified Runtime used by SYCL language runtime to abstract SYCL open |
| 3 | +# standard and vendor heterogeneous offload interfaces. |
| 4 | +# |
| 5 | +# This will in time be replaced by the new LLVM Offload interface. |
| 6 | +# |
| 7 | +# Unified Runtime is Apache 2.0 license with LLVM exceptions. |
| 8 | +# |
| 9 | +#=============================================================================== |
| 10 | + |
| 11 | +# Options to override the default behaviour of the FetchContent to include UR |
| 12 | +# source code. |
| 13 | +set(LIBSYCL_UR_OVERRIDE_FETCH_CONTENT_REPO |
| 14 | + "" CACHE STRING "Override the Unified Runtime FetchContent repository") |
| 15 | +set(LIBSYCL_UR_OVERRIDE_FETCH_CONTENT_TAG |
| 16 | + "" CACHE STRING "Override the Unified Runtime FetchContent tag") |
| 17 | + |
| 18 | +# Options to disable use of FetchContent to include Unified Runtime source code |
| 19 | +# to improve developer workflow. |
| 20 | +option(LIBSYCL_UR_USE_FETCH_CONTENT |
| 21 | + "Use FetchContent to acquire the Unified Runtime source code" ON) |
| 22 | +set(LIBSYCL_UR_SOURCE_DIR |
| 23 | + "" CACHE PATH "Path to root of Unified Runtime repository") |
| 24 | + |
| 25 | +option(LIBSYCL_UR_BUILD_TESTS "Build tests for UR" OFF) |
| 26 | +set(UR_BUILD_TESTS "${LIBSYCL_UR_BUILD_TESTS}" CACHE BOOL "" FORCE) |
| 27 | +# UR tests require the examples to be built |
| 28 | +set(UR_BUILD_EXAMPLES "${LIBSYCL_UR_BUILD_TESTS}" CACHE BOOL "" FORCE) |
| 29 | + |
| 30 | +set(UR_EXTERNAL_DEPENDENCIES "sycl-headers" CACHE STRING |
| 31 | + "List of external CMake targets for executables/libraries to depend on" FORCE) |
| 32 | + |
| 33 | +if("level_zero" IN_LIST LIBSYCL_ENABLE_BACKENDS) |
| 34 | + set(UR_BUILD_ADAPTER_L0 ON) |
| 35 | +endif() |
| 36 | +if("cuda" IN_LIST LIBSYCL_ENABLE_BACKENDS) |
| 37 | + set(UR_BUILD_ADAPTER_CUDA ON) |
| 38 | +endif() |
| 39 | +if("hip" IN_LIST LIBSYCL_ENABLE_BACKENDS) |
| 40 | + set(UR_BUILD_ADAPTER_HIP ON) |
| 41 | +endif() |
| 42 | +if("opencl" IN_LIST LIBSYCL_ENABLE_BACKENDS) |
| 43 | + set(UR_BUILD_ADAPTER_OPENCL ON) |
| 44 | +endif() |
| 45 | + |
| 46 | +# Disable errors from warnings while building the UR. |
| 47 | +# And remember origin flags before doing that. |
| 48 | +set(CMAKE_CXX_FLAGS_BAK "${CMAKE_CXX_FLAGS}") |
| 49 | +if(WIN32) |
| 50 | + append("/WX-" CMAKE_CXX_FLAGS) |
| 51 | + append("/WX-" CMAKE_C_FLAGS) |
| 52 | + # Unified runtime build fails with /DUNICODE |
| 53 | + append("/UUNICODE" CMAKE_CXX_FLAGS) |
| 54 | + append("/UUNICODE" CMAKE_C_FLAGS) |
| 55 | +else() |
| 56 | + append("-Wno-error" CMAKE_CXX_FLAGS) |
| 57 | + append("-Wno-error" CMAKE_C_FLAGS) |
| 58 | +endif() |
| 59 | + |
| 60 | +if(LIBSYCL_UR_USE_FETCH_CONTENT) |
| 61 | + include(FetchContent) |
| 62 | + |
| 63 | + # The fetch_adapter_source function can be used to perform a separate content |
| 64 | + # fetch for a UR adapter (backend), this allows development of adapters to be decoupled |
| 65 | + # from each other. |
| 66 | + # |
| 67 | + # A separate content fetch will not be performed if: |
| 68 | + # * The adapter name is not present in the LIBSYCL_ENABLE_BACKENDS variable. |
| 69 | + # * The repo and tag provided match the values of the |
| 70 | + # UNIFIED_RUNTIME_REPO/UNIFIED_RUNTIME_TAG variables |
| 71 | + # |
| 72 | + # Args: |
| 73 | + # * name - Must be the directory name of the adapter |
| 74 | + # * repo - A valid Git URL of a Unified Runtime repo |
| 75 | + # * tag - A valid Git branch/tag/commit in the Unified Runtime repo |
| 76 | + function(fetch_adapter_source name repo tag) |
| 77 | + if(NOT ${name} IN_LIST LIBSYCL_ENABLE_BACKENDS) |
| 78 | + return() |
| 79 | + endif() |
| 80 | + if(repo STREQUAL UNIFIED_RUNTIME_REPO AND |
| 81 | + tag STREQUAL UNIFIED_RUNTIME_TAG) |
| 82 | + # If the adapter sources are taken from the main checkout, reset the |
| 83 | + # adapter specific source path. |
| 84 | + string(TOUPPER ${name} NAME) |
| 85 | + set(UR_ADAPTER_${NAME}_SOURCE_DIR "" |
| 86 | + CACHE PATH "Path to external '${name}' adapter source dir" FORCE) |
| 87 | + return() |
| 88 | + endif() |
| 89 | + message(STATUS |
| 90 | + "Will fetch Unified Runtime ${name} adapter from ${repo} at ${tag}") |
| 91 | + set(fetch-name ur-${name}) |
| 92 | + FetchContent_Declare(${fetch-name} |
| 93 | + GIT_REPOSITORY ${repo} GIT_TAG ${tag}) |
| 94 | + # We don't want to add this repo to the build, only fetch its source. |
| 95 | + FetchContent_Populate(${fetch-name}) |
| 96 | + # Get the path to the source directory |
| 97 | + string(TOUPPER ${name} NAME) |
| 98 | + set(source_dir_var UR_ADAPTER_${NAME}_SOURCE_DIR) |
| 99 | + FetchContent_GetProperties(${fetch-name} SOURCE_DIR UR_ADAPTER_${NAME}_SOURCE_DIR) |
| 100 | + # Set the variable which informs UR where to get the adapter source from. |
| 101 | + set(UR_ADAPTER_${NAME}_SOURCE_DIR |
| 102 | + "${UR_ADAPTER_${NAME}_SOURCE_DIR}/source/adapters/${name}" |
| 103 | + CACHE PATH "Path to external '${name}' adapter source dir" FORCE) |
| 104 | + endfunction() |
| 105 | + |
| 106 | + set(UNIFIED_RUNTIME_REPO "https://github.com/oneapi-src/unified-runtime.git") |
| 107 | + set(UNIFIED_RUNTIME_TAG 851ee6a2bb5f5c34c6e48a00e0b06255044e0f03) |
| 108 | + |
| 109 | + set(UMF_BUILD_EXAMPLES OFF CACHE INTERNAL "EXAMPLES") |
| 110 | + # Due to the use of dependentloadflag and no installer for UMF and hwloc we need |
| 111 | + # to link statically on windows |
| 112 | + if(WIN32) |
| 113 | + set(UMF_BUILD_SHARED_LIBRARY OFF CACHE INTERNAL "Build UMF shared library") |
| 114 | + set(UMF_LINK_HWLOC_STATICALLY ON CACHE INTERNAL "static HWLOC") |
| 115 | + endif() |
| 116 | + |
| 117 | + fetch_adapter_source(level_zero |
| 118 | + ${UNIFIED_RUNTIME_REPO} |
| 119 | + ${UNIFIED_RUNTIME_TAG} |
| 120 | + ) |
| 121 | + |
| 122 | + fetch_adapter_source(opencl |
| 123 | + ${UNIFIED_RUNTIME_REPO} |
| 124 | + ${UNIFIED_RUNTIME_TAG} |
| 125 | + ) |
| 126 | + |
| 127 | + fetch_adapter_source(cuda |
| 128 | + ${UNIFIED_RUNTIME_REPO} |
| 129 | + ${UNIFIED_RUNTIME_TAG} |
| 130 | + ) |
| 131 | + |
| 132 | + fetch_adapter_source(hip |
| 133 | + ${UNIFIED_RUNTIME_REPO} |
| 134 | + ${UNIFIED_RUNTIME_TAG} |
| 135 | + ) |
| 136 | + |
| 137 | + if(LIBSYCL_UR_OVERRIDE_FETCH_CONTENT_REPO) |
| 138 | + set(UNIFIED_RUNTIME_REPO "${LIBSYCL_UR_OVERRIDE_FETCH_CONTENT_REPO}") |
| 139 | + endif() |
| 140 | + if(LIBSYCL_UR_OVERRIDE_FETCH_CONTENT_TAG) |
| 141 | + set(UNIFIED_RUNTIME_TAG "${LIBSYCL_UR_OVERRIDE_FETCH_CONTENT_TAG}") |
| 142 | + endif() |
| 143 | + |
| 144 | + message(STATUS "Will fetch Unified Runtime from ${UNIFIED_RUNTIME_REPO}") |
| 145 | + FetchContent_Declare(unified-runtime |
| 146 | + GIT_REPOSITORY ${UNIFIED_RUNTIME_REPO} |
| 147 | + GIT_TAG ${UNIFIED_RUNTIME_TAG} |
| 148 | + ) |
| 149 | + |
| 150 | + FetchContent_GetProperties(unified-runtime) |
| 151 | + FetchContent_MakeAvailable(unified-runtime) |
| 152 | + |
| 153 | + set(UNIFIED_RUNTIME_SOURCE_DIR |
| 154 | + "${unified-runtime_SOURCE_DIR}" CACHE PATH |
| 155 | + "Path to Unified Runtime Headers" FORCE) |
| 156 | +elseif(LIBSYCL_UR_SOURCE_DIR) |
| 157 | + # LIBSYCL_UR_USE_FETCH_CONTENT is OFF and LIBSYCL_UR_SOURCE_DIR has been set, |
| 158 | + # use the external Unified Runtime source directory. |
| 159 | + set(UNIFIED_RUNTIME_SOURCE_DIR |
| 160 | + "${LIBSYCL_UR_SOURCE_DIR}" CACHE PATH |
| 161 | + "Path to Unified Runtime Headers" FORCE) |
| 162 | + add_subdirectory( |
| 163 | + ${UNIFIED_RUNTIME_SOURCE_DIR} |
| 164 | + ${CMAKE_CURRENT_BINARY_DIR}/unified-runtime) |
| 165 | +else() |
| 166 | + message(FATAL_ERROR |
| 167 | + "LIBSYCL_UR_USE_FETCH_CONTENT is disabled but no alternative Unified \ |
| 168 | + Runtime source directory has been provided, either: |
| 169 | +
|
| 170 | + * Set -DLIBSYCL_UR_SOURCE_DIR=/path/to/unified-runtime |
| 171 | + * Clone the UR repo in ${CMAKE_CURRENT_SOURCE_DIR}/unified-runtime") |
| 172 | +endif() |
| 173 | + |
| 174 | +# Restore original flags |
| 175 | +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_BAK}") |
| 176 | + |
| 177 | +message(STATUS |
| 178 | + "Using Unified Runtime source directory: ${UNIFIED_RUNTIME_SOURCE_DIR}") |
| 179 | + |
| 180 | +set(UNIFIED_RUNTIME_INCLUDE_DIR "${UNIFIED_RUNTIME_SOURCE_DIR}/include") |
| 181 | +set(UNIFIED_RUNTIME_SRC_INCLUDE_DIR "${UNIFIED_RUNTIME_SOURCE_DIR}/source") |
| 182 | +set(UNIFIED_RUNTIME_COMMON_INCLUDE_DIR "${UNIFIED_RUNTIME_SOURCE_DIR}/source/common") |
| 183 | + |
| 184 | +add_library(UnifiedRuntimeLoader ALIAS ur_loader) |
| 185 | +add_library(UnifiedRuntimeCommon ALIAS ur_common) |
| 186 | +add_library(UnifiedMemoryFramework ALIAS ur_umf) |
| 187 | + |
| 188 | +add_library(UnifiedRuntime-Headers INTERFACE) |
| 189 | + |
| 190 | +target_include_directories(UnifiedRuntime-Headers |
| 191 | + INTERFACE |
| 192 | + "${UNIFIED_RUNTIME_INCLUDE_DIR}" |
| 193 | +) |
| 194 | + |
| 195 | +find_package(Threads REQUIRED) |
| 196 | + |
| 197 | +if(TARGET UnifiedRuntimeLoader) |
| 198 | + # Install the UR loader. |
| 199 | + install(TARGETS ur_loader |
| 200 | + LIBRARY DESTINATION "lib${LLVM_LIBDIR_SUFFIX}" COMPONENT unified-runtime-loader |
| 201 | + ARCHIVE DESTINATION "lib${LLVM_LIBDIR_SUFFIX}" COMPONENT unified-runtime-loader |
| 202 | + RUNTIME DESTINATION "bin" COMPONENT unified-runtime-loader |
| 203 | + ) |
| 204 | +endif() |
| 205 | + |
| 206 | +add_custom_target(UnifiedRuntimeAdapters) |
| 207 | + |
| 208 | +function(add_sycl_ur_adapter NAME) |
| 209 | + add_dependencies(UnifiedRuntimeAdapters ur_adapter_${NAME}) |
| 210 | + |
| 211 | + install(TARGETS ur_adapter_${NAME} |
| 212 | + LIBRARY DESTINATION "lib${LLVM_LIBDIR_SUFFIX}" COMPONENT ur_adapter_${NAME} |
| 213 | + RUNTIME DESTINATION "bin" COMPONENT ur_adapter_${NAME}) |
| 214 | + |
| 215 | + set(manifest_file |
| 216 | + ${CMAKE_CURRENT_BINARY_DIR}/install_manifest_ur_adapter_${NAME}.txt) |
| 217 | + add_custom_command(OUTPUT ${manifest_file} |
| 218 | + COMMAND "${CMAKE_COMMAND}" |
| 219 | + "-DCMAKE_INSTALL_COMPONENT=ur_adapter_${NAME}" |
| 220 | + -P "${CMAKE_BINARY_DIR}/cmake_install.cmake" |
| 221 | + COMMENT "Deploying component ur_adapter_${NAME}" |
| 222 | + USES_TERMINAL |
| 223 | + ) |
| 224 | + add_custom_target(install-sycl-ur-adapter-${NAME} |
| 225 | + DEPENDS ${manifest_file} ur_adapter_${NAME} |
| 226 | + ) |
| 227 | +endfunction() |
| 228 | + |
| 229 | +if("level_zero" IN_LIST LIBSYCL_ENABLE_BACKENDS) |
| 230 | + add_sycl_ur_adapter(level_zero) |
| 231 | +endif() |
| 232 | + |
| 233 | +if("cuda" IN_LIST LIBSYCL_ENABLE_BACKENDS) |
| 234 | + add_sycl_ur_adapter(cuda) |
| 235 | +endif() |
| 236 | + |
| 237 | +if("hip" IN_LIST LIBSYCL_ENABLE_BACKENDS) |
| 238 | + add_sycl_ur_adapter(hip) |
| 239 | +endif() |
| 240 | + |
| 241 | +if("opencl" IN_LIST LIBSYCL_ENABLE_BACKENDS) |
| 242 | + add_sycl_ur_adapter(opencl) |
| 243 | +endif() |
| 244 | + |
| 245 | +install(TARGETS umf |
| 246 | + LIBRARY DESTINATION "lib${LLVM_LIBDIR_SUFFIX}" COMPONENT unified-memory-framework |
| 247 | + ARCHIVE DESTINATION "lib${LLVM_LIBDIR_SUFFIX}" COMPONENT unified-memory-framework |
| 248 | + RUNTIME DESTINATION "bin" COMPONENT unified-memory-framework) |
0 commit comments