Skip to content

[SYCL][UR] Add Windows debug libs to install target #17512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: sycl
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion sycl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ add_custom_target( sycl-toolchain ALL
)

if (WIN32)
add_dependencies(sycl-toolchain ur_win_proxy_loader)
add_dependencies(sycl-toolchain ur_win_proxy_loader unified-runtimed)
endif()

if("cuda" IN_LIST SYCL_ENABLE_BACKENDS)
Expand Down
103 changes: 103 additions & 0 deletions sycl/cmake/modules/FetchUnifiedRuntime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,109 @@ if("native_cpu" IN_LIST SYCL_ENABLE_BACKENDS)
endif()
endif()

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.
include(ExternalProject)
set(URD_BINARY_DIR ${CMAKE_BINARY_DIR}/unified-runtimed)
set(URD_INSTALL_DIR ${URD_BINARY_DIR}/install)

# This creates a subbuild which can be used in dependencies with the
# unified-runtimed target. It invokes the install-unified-runtime-libraries
# target to install the UR runtime libraries.
ExternalProject_Add(unified-runtimed
SOURCE_DIR ${UNIFIED_RUNTIME_SOURCE_DIR}
BINARY_DIR ${URD_BINARY_DIR}
INSTALL_DIR ${URD_INSTALL_DIR}
INSTALL_COMMAND ${CMAKE_COMMAND}
--build <BINARY_DIR>
--target install-unified-runtime-libraries
CMAKE_CACHE_ARGS
-DCMAKE_BUILD_TYPE:STRING=Debug
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows generators might ignore CMAKE_BUILD_TYPE. Consider using a cmake argument --config Debug instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would need to be included in the install command?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in the install command add: --config Debug. And perhaps leave -DCMAKE_BUILD_TYPE:STRING=Debug here as well, just for the case where Unix Makefiles generator is used on Windows.

-DCMAKE_INSTALL_PREFIX:STRING=<INSTALL_DIR>
# Enable d suffix on libraries
-DUR_USE_DEBUG_POSTFIX:BOOL=ON
# Don't build unnecessary targets in subbuild.
-DUR_BUILD_EXAMPLES:BOOL=OFF
-DUR_BUILD_TESTS:BOOL=OFF
-DUR_BUILD_TOOLS:BOOL=OFF
# Sanitizer layer is not supported on Windows.
-DUR_ENABLE_SYMBOLIZER:BOOL=OFF
# Inherit settings from parent build.
-DUR_ENABLE_TRACING:BOOL=${UR_ENABLE_TRACING}
-DUR_ENABLE_COMGR:BOOL=${UR_ENABLE_COMGR}
-DUR_BUILD_ADAPTER_L0:BOOL=${UR_BUILD_ADAPTER_L0}
-DUR_BUILD_ADAPTER_L0_V2:BOOL=${UR_BUILD_ADAPTER_L0_V2}
-DUR_BUILD_ADAPTER_OPENCL:BOOL=${UR_BUILD_ADAPTER_OPENCL}
-DUR_BUILD_ADAPTER_CUDA:BOOL=${UR_BUILD_ADAPTER_CUDA}
-DUR_BUILD_ADAPTER_HIP:BOOL=${UR_BUILD_ADAPTER_HIP}
-DUR_BUILD_ADAPTER_NATIVE_CPU:BOOL=${UR_BUILD_ADAPTER_NATIVE_CPU}
-DUMF_BUILD_EXAMPLES:BOOL=${UMF_BUILD_EXAMPLES}
-DUMF_BUILD_SAHRED_LIBRARY:BOOL=${UMF_BUILD_SHARED_LIBRARY}
-DUMF_LINK_HWLOC_STATICALLY:BOOL=${UMF_LINK_HWLOC_STATICALLY}
-DUMF_DISABLE_HWLOC:BOOL=${UMF_DISABLE_HWLOC}
# TODO: Enable d suffix in UMF
# -DUMF_USE_DEBUG_POSTFIX=ON
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enable building umfd.dll/umfd.lib in the UR subbuild.

)

# Copy the debug UR runtime libraries to <build>/bin & <build>/lib for use in
# the parent build, e.g. integration testing.
set(URD_COPY_FILES)
macro(urd_copy_library_to_build library)
list(APPEND URD_COPY_FILES
${LLVM_BINARY_DIR}/bin/${library}.dll
${LLVM_BINARY_DIR}/lib/${library}.lib
)
add_custom_command(
OUTPUT
${LLVM_BINARY_DIR}/bin/${library}.dll
${LLVM_BINARY_DIR}/lib/${library}.lib
COMMAND ${CMAKE_COMMAND} -E copy
${URD_INSTALL_DIR}/bin/${library}.dll
${LLVM_BINARY_DIR}/bin/${library}.dll
COMMAND ${CMAKE_COMMAND} -E copy
${URD_INSTALL_DIR}/lib/${library}.lib
${LLVM_BINARY_DIR}/lib/${library}.lib
)
endmacro()

urd_copy_library_to_build(ur_loaderd)
foreach(adapter ${SYCL_ENABLE_BACKENDS})
urd_copy_library_to_build(ur_adapter_${adapter}d)
endforeach()
# TODO: Also copy umfd.dll/umfd.lib
# urd_copy_library_to_build(umfd)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy umfd.dll/umfd.lib into the parent <build>/bin/<build>/lib directories for use in testing.


add_custom_target(unified-runtimed-copy-to-build DEPENDS ${URD_COPY_FILES})
add_dependencies(unified-runtimed-copy-to-build unified-runtimed)

# Add the debug UR runtime libraries to the parent install.
install(
FILES ${URD_INSTALL_DIR}/bin/ur_loaderd.dll
DESTINATION "bin" COMPONENT unified-runtime-loader)
install(
FILES ${URD_INSTALL_DIR}/lib/ur_loaderd.lib
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}" COMPONENT unified-runtime-loader)

foreach(adapter ${SYCL_ENABLE_BACKENDS})
install(
FILES ${URD_INSTALL_DIR}/bin/ur_adapter_${adapter}d.dll
DESTINATION "bin" COMPONENT ur_adapter_${adapter})
install(
FILES ${URD_INSTALL_DIR}/lib/ur_adapter_${adapter}d.lib
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}" COMPONENT ur_adapter_${adapter})
add_dependencies(install-sycl-ur-adapter-${adapter} unified-runtimed)
endforeach()

# TODO: Also install umfd.dll/umfd.lib
# install(
# FILES ${URD_INSTALL_DIR}/bin/umfd.dll
# DESTINATION "bin" COMPONENT unified-memory-framework)
# install(
# FILES ${URD_INSTALL_DIR}/lib/umfd.lib
# DESTINATION "lib${LLVM_LIBDIR_SUFFIX}" COMPONENT unified-memory-framework)
Comment on lines +414 to +420
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add umfd.dll/umfd.lib to the install target.

endif()

install(TARGETS umf
LIBRARY DESTINATION "lib${LLVM_LIBDIR_SUFFIX}" COMPONENT unified-memory-framework
ARCHIVE DESTINATION "lib${LLVM_LIBDIR_SUFFIX}" COMPONENT unified-memory-framework
Expand Down
1 change: 1 addition & 0 deletions sycl/ur_win_proxy_loader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ if (MSVC)
add_library(ur_win_proxy_loaderd SHARED ur_win_proxy_loader.cpp ${CMAKE_CURRENT_BINARY_DIR}/versioninfo.rc)
target_compile_options(ur_win_proxy_loaderd PRIVATE ${WINUNLOAD_CXX_FLAGS_DEBUG})
target_compile_options(ur_win_proxy_loader PRIVATE ${WINUNLOAD_CXX_FLAGS_RELEASE})
target_compile_definitions(ur_win_proxy_loaderd PRIVATE UR_WIN_PROXY_LOADER_DEBUG)
target_link_libraries(ur_win_proxy_loaderd PRIVATE shlwapi)
target_link_libraries(ur_win_proxy_loader PRIVATE shlwapi)
# 0x2000: LOAD_LIBRARY_SAFE_CURRENT_DIRS flag. Using this flag means that loading dependency DLLs
Expand Down
44 changes: 21 additions & 23 deletions sycl/ur_win_proxy_loader/ur_win_proxy_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,22 @@ static std::wstring getCurrentDSODir() {
return Path;
}

// these are cribbed from include/sycl/detail/ur.hpp
// a new adapter must be added to both places.
#ifdef _MSC_VER
#define __SYCL_UNIFIED_RUNTIME_LOADER_NAME "ur_loader.dll"
#define __SYCL_OPENCL_ADAPTER_NAME "ur_adapter_opencl.dll"
#define __SYCL_LEVEL_ZERO_ADAPTER_NAME "ur_adapter_level_zero.dll"
#define __SYCL_LEVEL_ZERO_V2_ADAPTER_NAME "ur_adapter_level_zero_v2.dll"
#define __SYCL_CUDA_ADAPTER_NAME "ur_adapter_cuda.dll"
#define __SYCL_HIP_ADAPTER_NAME "ur_adapter_hip.dll"
#define __SYCL_NATIVE_CPU_ADAPTER_NAME "ur_adapter_native_cpu.dll"

#ifdef UR_WIN_PROXY_LOADER_DEBUG_POSTFIX
#define UR_LIBRARY_NAME(NAME) "ur_" #NAME "d.dll"
#else
#define UR_LIBRARY_NAME(NAME) "ur_" #NAME ".dll"
#endif

#else // llvm-mingw
#define __SYCL_UNIFIED_RUNTIME_LOADER_NAME "libur_loader.dll"
#define __SYCL_OPENCL_ADAPTER_NAME "libur_adapter_opencl.dll"
#define __SYCL_LEVEL_ZERO_ADAPTER_NAME "libur_adapter_level_zero.dll"
#define __SYCL_LEVEL_ZERO_V2_ADAPTER_NAME "libur_adapter_level_zero_v2.dll"
#define __SYCL_CUDA_ADAPTER_NAME "libur_adapter_cuda.dll"
#define __SYCL_HIP_ADAPTER_NAME "libur_adapter_hip.dll"
#define __SYCL_NATIVE_CPU_ADAPTER_NAME "libur_adapter_native_cpu.dll"

#ifdef UR_WIN_PROXY_LOADER_DEBUG_POSTFIX
#define UR_LIBRARY_NAME(NAME) "libur" #NAME "d.dll"
#else
#define UR_LIBRARY_NAME(NAME) "libur" #NAME ".dll"
#endif

#endif

// ------------------------------------
Expand Down Expand Up @@ -141,13 +139,13 @@ void preloadLibraries() {
};
// We keep the UR Loader handle so it can be fetched by the runtime, but the
// adapter libraries themselves won't be used.
getDllHandle() = loadAdapter(__SYCL_UNIFIED_RUNTIME_LOADER_NAME);
loadAdapter(__SYCL_OPENCL_ADAPTER_NAME);
loadAdapter(__SYCL_LEVEL_ZERO_ADAPTER_NAME);
loadAdapter(__SYCL_LEVEL_ZERO_V2_ADAPTER_NAME);
loadAdapter(__SYCL_CUDA_ADAPTER_NAME);
loadAdapter(__SYCL_HIP_ADAPTER_NAME);
loadAdapter(__SYCL_NATIVE_CPU_ADAPTER_NAME);
getDllHandle() = loadAdapter(UR_LIBRARY_NAME(loader));
loadAdapter(UR_LIBRARY_NAME(adapter_opencl));
loadAdapter(UR_LIBRARY_NAME(adapter_level_zero));
loadAdapter(UR_LIBRARY_NAME(adapter_level_zero_v2));
loadAdapter(UR_LIBRARY_NAME(adapter_cuda));
loadAdapter(UR_LIBRARY_NAME(adapter_hip));
loadAdapter(UR_LIBRARY_NAME(adapter_native_cpu));

// Restore system error handling.
(void)SetErrorMode(SavedMode);
Expand Down
1 change: 1 addition & 0 deletions unified-runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ set(UR_CONFORMANCE_TARGET_TRIPLES "" CACHE STRING
set(UR_CONFORMANCE_AMD_ARCH "" CACHE STRING "AMD device target ID to build CTS binaries for")
option(UR_CONFORMANCE_ENABLE_MATCH_FILES "Enable CTS match files" ON)
option(UR_CONFORMANCE_TEST_LOADER "Also test the loader in the conformance tests" OFF)
option(UR_USE_DEBUG_POSTFIX "Enable debug postfix 'd' for libraries" OFF)
set(UR_ADAPTER_LEVEL_ZERO_SOURCE_DIR "" CACHE PATH
"Path to external 'level_zero' adapter source dir")
set(UR_ADAPTER_OPENCL_SOURCE_DIR "" CACHE PATH
Expand Down
22 changes: 21 additions & 1 deletion unified-runtime/cmake/helpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,40 @@ function(add_ur_library name)
$<$<STREQUAL:$<TARGET_LINKER_FILE_NAME:${name}>,link.exe>:LINKER:/DEPENDENTLOADFLAG:0x2000>
)
endif()
if(UR_USE_DEBUG_POSTFIX)
set_target_properties(${name} PROPERTIES DEBUG_POSTFIX d)
endif()
if(UR_EXTERNAL_DEPENDENCIES)
add_dependencies(${name} ${UR_EXTERNAL_DEPENDENCIES})
endif()
add_dependencies(unified-runtime-libraries ${name})
endfunction()

if(NOT TARGET unified-runtime-libraries)
add_custom_target(unified-runtime-libraries)
endif()

function(install_ur_library name)
install(TARGETS ${name}
COMPONENT unified-runtime
EXPORT ${PROJECT_NAME}-targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT unified-runtime
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
endfunction()

if(NOT TARGET install-unified-runtime-libraries)
add_custom_target(install-unified-runtime-libraries
COMMAND ${CMAKE_COMMAND}
-DCOMPONENT=unified-runtime
-P ${CMAKE_BINARY_DIR}/cmake_install.cmake
# TODO: Also install debug UMF runtime libraries component
DEPENDS unified-runtime-libraries
# TODO: Add dependency on building debug UMF libraries
Comment on lines +227 to +230
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add command to install only umfd.dll/umfd.lib, we don't want a full install here since we only care about the runtime libraries being installed with the debug postfix.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll test this today.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mostly working, I've commented on the PR with one issue.

)
endif()

include(FetchContent)

function(FetchSource GIT_REPOSITORY GIT_TAG GIT_DIR DEST)
Expand Down
14 changes: 11 additions & 3 deletions unified-runtime/source/common/ur_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,23 @@ int ur_duplicate_fd(int pid, int fd_in);
defined(SANITIZER_THREAD)
#define SANITIZER_ANY
#endif

///////////////////////////////////////////////////////////////////////////////
#if UR_USE_DEBUG_POSTFIX
#define LIBRARY_NAME(NAME) NAME "d"
#else
#define LIBRARY_NAME(NAME) NAME
#endif

#if defined(_WIN32)
#define MAKE_LIBRARY_NAME(NAME, VERSION) NAME ".dll"
#define MAKE_LIBRARY_NAME(NAME, VERSION) LIBRARY_NAME(NAME) ".dll"
#define STATIC_LIBRARY_EXTENSION ".lib"
#else
#if defined(__APPLE__)
#define MAKE_LIBRARY_NAME(NAME, VERSION) "lib" NAME "." VERSION ".dylib"
#define MAKE_LIBRARY_NAME(NAME, VERSION) \
"lib" LIBRARY_NAME(NAME) "." VERSION ".dylib"
#else
#define MAKE_LIBRARY_NAME(NAME, VERSION) "lib" NAME ".so." VERSION
#define MAKE_LIBRARY_NAME(NAME, VERSION) "lib" LIBRARY_NAME(NAME) ".so." VERSION
#endif
#define STATIC_LIBRARY_EXTENSION ".a"
#endif
Expand Down
9 changes: 4 additions & 5 deletions unified-runtime/source/loader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ add_ur_library(ur_loader
)
install_ur_library(ur_loader)

target_compile_definitions(ur_loader PRIVATE
UR_USE_DEBUG_POSTFIX=$<BOOL:${UR_USE_DEBUG_POSTFIX}>
)

if (MSVC)
set(TARGET_LIBNAME ur_loader)
string(TOUPPER ${TARGET_LIBNAME} TARGET_LIBNAME)
Expand All @@ -39,11 +43,6 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_link_options(ur_loader PRIVATE "-Wl,--version-script=${LOADER_VERSION_SCRIPT}")
endif()

set_target_properties(ur_loader PROPERTIES
LIBRARY_OUTPUT_NAME ur_loader
RUNTIME_OUTPUT_NAME ur_loader
)

add_library(${PROJECT_NAME}::loader ALIAS ur_loader)

target_include_directories(ur_loader PRIVATE
Expand Down