Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions sycl/cmake/modules/BuildUnifiedRuntime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,24 @@ if("offload" IN_LIST SYCL_ENABLE_BACKENDS)
add_sycl_ur_adapter(offload)
endif()

# Determine whether the given adapter is statically linked into the loader by
# querying the corresponding UR_STATIC_ADAPTER_* variable (if it exists).
# Sets OUT_VAR in the caller's scope to TRUE if the adapter is static,
# FALSE otherwise.
function(ur_adapter_is_static adapter out_var)
set(static_var "UR_STATIC_ADAPTER_${adapter}")
string(TOUPPER "${static_var}" static_var)
# Special case: level_zero uses UR_STATIC_ADAPTER_L0
if(adapter STREQUAL "level_zero")
set(static_var "UR_STATIC_ADAPTER_L0")
endif()
if(DEFINED ${static_var} AND ${static_var})
set(${out_var} TRUE PARENT_SCOPE)
else()
set(${out_var} FALSE PARENT_SCOPE)
endif()
endfunction()

if(CMAKE_SYSTEM_NAME STREQUAL Windows)
# On Windows, also build/install debug libraries with the d suffix that are
# compiled with /MDd so users can link against these in debug builds.
Expand Down Expand Up @@ -220,6 +238,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL Windows)
-DUR_BUILD_ADAPTER_HIP:BOOL=${UR_BUILD_ADAPTER_HIP}
-DUR_BUILD_ADAPTER_NATIVE_CPU:BOOL=${UR_BUILD_ADAPTER_NATIVE_CPU}
-DUR_STATIC_LOADER:BOOL=${UR_STATIC_LOADER}
-DUR_STATIC_ADAPTER_OPENCL:BOOL=${UR_STATIC_ADAPTER_OPENCL}
-DUMF_BUILD_EXAMPLES:BOOL=${UMF_BUILD_EXAMPLES}
-DUMF_BUILD_SHARED_LIBRARY:BOOL=${UMF_BUILD_SHARED_LIBRARY}
-DUMF_LINK_HWLOC_STATICALLY:BOOL=${UMF_LINK_HWLOC_STATICALLY}
Expand Down Expand Up @@ -268,14 +287,25 @@ if(CMAKE_SYSTEM_NAME STREQUAL Windows)
${LLVM_BINARY_DIR}/lib/ur_loaderd.lib
${LLVM_BINARY_DIR}/lib/ur_commond.lib
dbghelp)
# Link static adapters into the loader
foreach(adapter ${SYCL_ENABLE_BACKENDS})
ur_adapter_is_static(${adapter} adapter_is_static)
set(build_var "UR_BUILD_ADAPTER_${adapter}")
string(TOUPPER "${build_var}" build_var)
if(adapter_is_static AND DEFINED ${build_var} AND ${build_var})
target_link_libraries(UnifiedRuntimeLoaderDebug INTERFACE
${LLVM_BINARY_DIR}/lib/ur_adapter_${adapter}d.lib)
endif()
endforeach()
Comment thread
pbalcer marked this conversation as resolved.
endif()
foreach(adatper ${SYCL_ENABLE_BACKENDS})
if(adapter MATCHES "level_zero")
set(shared "NOT;${UR_STATIC_ADAPTER_L0}")
foreach(adapter ${SYCL_ENABLE_BACKENDS})
Comment thread
pbalcer marked this conversation as resolved.
ur_adapter_is_static(${adapter} adapter_is_static)
if(adapter_is_static)
set(shared FALSE)
else()
set(shared TRUE)
endif()
urd_copy_library_to_build(ur_adapter_${adatper}d "${shared}")
urd_copy_library_to_build(ur_adapter_${adapter}d "${shared}")
endforeach()
# Also copy umfd.dll/umfd.lib
urd_copy_library_to_build(umfd ${UMF_BUILD_SHARED_LIBRARY})
Expand All @@ -290,9 +320,12 @@ if(CMAKE_SYSTEM_NAME STREQUAL Windows)
DESTINATION "bin" COMPONENT unified-runtime-loader)
endif()
foreach(adapter ${SYCL_ENABLE_BACKENDS})
install(
FILES ${URD_INSTALL_DIR}/bin/ur_adapter_${adapter}d.dll
DESTINATION "bin" COMPONENT ur_adapter_${adapter})
ur_adapter_is_static(${adapter} adapter_is_static)
if(NOT adapter_is_static)
install(
FILES ${URD_INSTALL_DIR}/bin/ur_adapter_${adapter}d.dll
DESTINATION "bin" COMPONENT ur_adapter_${adapter})
endif()
endforeach()
if(UMF_BUILD_SHARED_LIBRARY)
# Also install umfd.dll
Expand Down
16 changes: 10 additions & 6 deletions sycl/include/sycl/detail/os_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,18 @@ fn *dynLookupFunction(const char *const *LibNames, size_t LibNameSize,
return reinterpret_cast<fn *>(dynLookup(LibNames, LibNameSize, FunName));
}

// On Linux, first try to load from libur_adapter_opencl.so, then
// libur_adapter_opencl.so.0 if the first is not found. libur_adapter_opencl.so
// and libur_adapter_opencl.so.0 might be different libraries if they are not
// symlinked, which is the case with PyPi compiler distribution package.
// We can't load libur_adapter_opencl.so.0 always as the first choice because
// that would break SYCL unittests, which rely on mocking libur_adapter_opencl.
// On Linux, probe libur_adapter_opencl.so first, then libur_adapter_opencl.so.0
// (these may differ in PyPi distributions where they are not symlinked).
//
// When the OpenCL adapter is statically linked into the loader, there is no
// libur_adapter_opencl.so to look up symbols in. The static adapter dlopens
// libOpenCL.so.1 (or libOpenCL.so) at initialization, so probe those names
// instead to find clRetain* and friends.
#ifdef __SYCL_RT_OS_WINDOWS
constexpr std::array<const char *, 1> OCLLibNames = {"OpenCL"};
#elif defined(SYCL_UR_STATIC_ADAPTER_OPENCL)
constexpr std::array<const char *, 2> OCLLibNames = {"libOpenCL.so.1",
"libOpenCL.so"};
#else
constexpr std::array<const char *, 2> OCLLibNames = {
"libur_adapter_opencl.so", "libur_adapter_opencl.so.0"};
Expand Down
1 change: 1 addition & 0 deletions sycl/source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function(add_sycl_rt_library LIB_NAME LIB_OBJ_NAME)
__SYCL_DISABLE_DEPRECATION_WARNINGS
$<$<BOOL:${MSVC}>:__SYCL_BUILD_SYCL_DLL>
$<$<BOOL:${UR_STATIC_LOADER}>:SYCL_UR_STATIC_LOADER>
$<$<BOOL:${UR_STATIC_ADAPTER_OPENCL}>:SYCL_UR_STATIC_ADAPTER_OPENCL>
)

target_include_directories(
Expand Down
12 changes: 9 additions & 3 deletions sycl/unittests/mock_opencl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
get_target_property(SYCL_BINARY_DIR sycl-toolchain BINARY_DIR)

# Linux looks up ur_adapter_opencl rather than libOpenCL.
# On Windows, this is copied into libOpenCL.dll.
# On Windows: mock provides libOpenCL.dll (always).
# On Linux: mock is always OpenCL (the ICD), with VERSION 1 so CMake produces
# both libOpenCL.so.1 and libOpenCL.so symlinks. The static adapter dlopens
# libOpenCL.so.1, and the dynamic adapter links to it at build time.
if(WIN32)
set(LIBNAME OpenCL)
else()
set(LIBNAME ur_adapter_opencl)
set(LIBNAME OpenCL)
set(MOCK_OCL_VERSION 1)
endif()

add_library(mockOpenCL SHARED EXCLUDE_FROM_ALL mock_opencl.cpp)
Expand All @@ -15,3 +18,6 @@ set_target_properties(mockOpenCL PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${SYCL_BINARY_DIR}/unittests/lib
RUNTIME_OUTPUT_NAME ${LIBNAME}
)
if(DEFINED MOCK_OCL_VERSION)
set_target_properties(mockOpenCL PROPERTIES VERSION ${MOCK_OCL_VERSION})
endif()
1 change: 1 addition & 0 deletions unified-runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ option(UR_BUILD_ADAPTER_ALL "Build all currently supported adapters" OFF)
option(UR_BUILD_ADAPTER_L0_V2 "Build the (experimental) Level-Zero v2 adapter" OFF)
option(UR_BUILD_ADAPTER_OFFLOAD "Build the experimental Offload adapter" OFF)
option(UR_STATIC_ADAPTER_L0 "Build the Level-Zero adapter as static and embed in the loader" OFF)
option(UR_STATIC_ADAPTER_OPENCL "Build the OpenCL adapter as static library with dynamic OpenCL loading" ON)
option(UR_BUILD_EXAMPLE_CODEGEN "Build the codegen example." OFF)
option(VAL_USE_LIBBACKTRACE_BACKTRACE "enable libbacktrace validation backtrace for linux" OFF)
option(UR_ENABLE_ASSERTIONS "Enable assertions for all build types" OFF)
Expand Down
6 changes: 6 additions & 0 deletions unified-runtime/scripts/generate_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,12 @@ def generate_adapters(path, section, namespace, tags, version, specs, meta):
loc += _mako_interface_loader_api(
dstpath, "level_zero", "hpp", namespace, tags, version, specs, meta
)
loc += _mako_interface_loader_api(
dstpath, "opencl", "cpp", namespace, tags, version, specs, meta
)
loc += _mako_interface_loader_api(
dstpath, "opencl", "hpp", namespace, tags, version, specs, meta
)

print("Generated %s lines of code.\n" % loc)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ${x}_result_t ${th.make_func_name(n, tags, obj)}(
%endif
%endfor
%endfor
#ifdef UR_STATIC_ADAPTER_LEVEL_ZERO
#ifdef UR_STATIC_ADAPTER_${Adapter}
ur_result_t urAdapterGetDdiTables(ur_dditable_t *ddi);
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ if(UR_BUILD_ADAPTER_L0)
target_compile_definitions(ur_adapter_level_zero PUBLIC UR_STATIC_LEVEL_ZERO)
if(UR_STATIC_ADAPTER_L0)
target_compile_definitions(ur_adapter_level_zero PUBLIC UR_STATIC_ADAPTER_LEVEL_ZERO)
set(ADAPTER_L0_TARGETS_TO_INSTALL ur_umf LevelZeroLoader LevelZeroLoader-Headers ComputeRuntimeLevelZero-Headers)
set(ADAPTER_L0_TARGETS_TO_INSTALL LevelZeroLoader LevelZeroLoader-Headers ComputeRuntimeLevelZero-Headers)
# 'utils' target from 'level-zero-loader' includes path which is prefixed
# in the source directory, this breaks the installation of 'utils' target.
if(TARGET level_zero_utils)
Expand Down
58 changes: 49 additions & 9 deletions unified-runtime/source/adapters/opencl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ find_package(Threads REQUIRED)

set(TARGET_NAME ur_adapter_opencl)

add_ur_adapter(${TARGET_NAME} SHARED
# Determine adapter library type (SHARED or STATIC)
set(ADAPTER_LIB_TYPE SHARED)
if(UR_STATIC_ADAPTER_OPENCL)
set(ADAPTER_LIB_TYPE STATIC)
endif()

add_ur_adapter(${TARGET_NAME} ${ADAPTER_LIB_TYPE}
${CMAKE_CURRENT_SOURCE_DIR}/ur_interface_loader.cpp
${CMAKE_CURRENT_SOURCE_DIR}/adapter.hpp
${CMAKE_CURRENT_SOURCE_DIR}/adapter.cpp
Expand Down Expand Up @@ -61,11 +67,45 @@ target_include_directories(${TARGET_NAME} PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/../../"
)

target_link_libraries(${TARGET_NAME} PRIVATE
${PROJECT_NAME}::headers
${PROJECT_NAME}::common
${PROJECT_NAME}::umf
Threads::Threads
OpenCL-Headers
${OpenCL_LIBRARY}
)
if(UR_STATIC_ADAPTER_OPENCL)
# When building as static library, add dynamic loading source files
# and don't link against OpenCL library
target_sources(${TARGET_NAME} PRIVATE
Comment thread
kswiecicki marked this conversation as resolved.
${CMAKE_CURRENT_SOURCE_DIR}/ocl_dynamic_lib.cpp
)
target_compile_definitions(${TARGET_NAME} PRIVATE
UR_STATIC_ADAPTER_OPENCL
# Neutralize dllexport/dllimport annotations on MSVC: the generated .cpp
# files use UR_APIEXPORT/UR_APICALL but a static library must not have
# __declspec(dllimport) on definitions (causes C2375) and does not need
# __declspec(dllexport) either.
UR_APIEXPORT=
UR_APICALL=
UR_DLLEXPORT=
)
target_link_libraries(${TARGET_NAME} PRIVATE
${PROJECT_NAME}::headers
${PROJECT_NAME}::common
${PROJECT_NAME}::umf
Threads::Threads
)
# OpenCL-Headers is header-only; use its include path directly to avoid
# exporting a target whose INTERFACE_INCLUDE_DIRECTORIES points into the
# build tree, which CMake rejects in install(EXPORT ...).
target_include_directories(${TARGET_NAME} PRIVATE
$<TARGET_PROPERTY:OpenCL-Headers,INTERFACE_INCLUDE_DIRECTORIES>)
# Add dl library for dynamic loading on Unix systems
if(UNIX)
target_link_libraries(${TARGET_NAME} PRIVATE ${CMAKE_DL_LIBS})
endif()
else()
# Standard shared library build - link against OpenCL library
target_link_libraries(${TARGET_NAME} PRIVATE
${PROJECT_NAME}::headers
${PROJECT_NAME}::common
${PROJECT_NAME}::umf
Threads::Threads
OpenCL-Headers
${OpenCL_LIBRARY}
)
endif()
Loading
Loading