Skip to content

[SYCL][UR] Fix build with libc++ #19521

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

Merged
merged 4 commits into from
Jul 23, 2025
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
19 changes: 2 additions & 17 deletions buildbot/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,9 @@ def do_configure(args, passthrough_args):
cmake_cmd.append(llvm_dir)

if args.use_libcxx:
if not (args.libcxx_include and args.libcxx_library):
sys.exit(
"Please specify include and library path of libc++ when building sycl "
"runtime with it"
)
cmake_cmd.extend(
[
"-DSYCL_USE_LIBCXX=ON",
"-DSYCL_LIBCXX_INCLUDE_PATH={}".format(args.libcxx_include),
"-DSYCL_LIBCXX_LIBRARY_PATH={}".format(args.libcxx_library),
"-DLLVM_ENABLE_LIBCXX=ON",
]
)

Expand Down Expand Up @@ -383,15 +376,7 @@ def main():
help="Additional CMake option not configured via script parameters",
)
parser.add_argument("--cmake-gen", default="Ninja", help="CMake generator")
parser.add_argument(
"--use-libcxx", action="store_true", help="build sycl runtime with libcxx"
)
parser.add_argument(
"--libcxx-include", metavar="LIBCXX_INCLUDE_PATH", help="libcxx include path"
)
parser.add_argument(
"--libcxx-library", metavar="LIBCXX_LIBRARY_PATH", help="libcxx library path"
)
parser.add_argument("--use-libcxx", action="store_true", help="build with libcxx")
parser.add_argument(
"--use-lld", action="store_true", help="Use LLD linker for build"
)
Expand Down
12 changes: 12 additions & 0 deletions libdevice/cmake/modules/SYCLLibdevice.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,17 @@ set(compile_opts

set(SYCL_LIBDEVICE_GCC_TOOLCHAIN "" CACHE PATH "Path to GCC installation")

set(SYCL_LIBDEVICE_CXX_FLAGS "" CACHE STRING "C++ compiler flags for SYCL libdevice")
if (NOT SYCL_LIBDEVICE_GCC_TOOLCHAIN STREQUAL "")
list(APPEND compile_opts "--gcc-install-dir=${SYCL_LIBDEVICE_GCC_TOOLCHAIN}")
endif()
if(NOT SYCL_LIBDEVICE_CXX_FLAGS STREQUAL "")
separate_arguments(SYCL_LIBDEVICE_CXX_FLAGS NATIVE_COMMAND ${SYCL_LIBDEVICE_CXX_FLAGS})
list(APPEND compile_opts ${SYCL_LIBDEVICE_CXX_FLAGS})
endif()
if(LLVM_LIBCXX_USED)
list(APPEND compile_opts "-stdlib=libc++")
endif()

if (WIN32)
list(APPEND compile_opts -D_ALLOW_RUNTIME_LIBRARY_MISMATCH)
Expand Down Expand Up @@ -644,6 +652,10 @@ if (NOT SYCL_LIBDEVICE_GCC_TOOLCHAIN STREQUAL "")
list(APPEND imf_host_cxx_flags "--gcc-install-dir=${SYCL_LIBDEVICE_GCC_TOOLCHAIN}")
endif()

if(LLVM_LIBCXX_USED)
list(APPEND imf_host_cxx_flags "-stdlib=libc++")
endif()

macro(mangle_name str output)
string(STRIP "${str}" strippedStr)
string(REGEX REPLACE "^/" "" strippedStr "${strippedStr}")
Expand Down
23 changes: 2 additions & 21 deletions sycl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -303,37 +303,18 @@ endif()

# This function allows building multiple libraries with the same options.
# Currently used by add_sycl_library and add_sycl_rt_library.
# Currently handles linking with libcxx support and gcc workaround
# Currently handles a gcc workaround
function( add_common_options LIB_NAME)
if (SYCL_USE_LIBCXX)
if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR
(CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
if ((NOT (DEFINED SYCL_LIBCXX_INCLUDE_PATH)) OR (NOT (DEFINED SYCL_LIBCXX_LIBRARY_PATH)))
message(FATAL_ERROR "When building with libc++ SYCL_LIBCXX_INCLUDE_PATHS and"
"SYCL_LIBCXX_LIBRARY_PATH should be set")
endif()
target_link_libraries(${LIB_NAME} PRIVATE "-L${SYCL_LIBCXX_LIBRARY_PATH}" -Wl,-rpath,${SYCL_LIBCXX_LIBRARY_PATH} -nodefaultlibs -lc++ -lc++abi -lm -lc -lgcc_s -lgcc)
target_compile_options(${LIB_NAME} PRIVATE -nostdinc++)
target_include_directories(${LIB_NAME} PRIVATE "${SYCL_LIBCXX_INCLUDE_PATH}")
if (ARGC EQUAL 2)
target_compile_options(${ARGV1} PRIVATE -nostdinc++)
target_include_directories(${ARGV1} PRIVATE "${SYCL_LIBCXX_INCLUDE_PATH}")
endif()
else()
message(FATAL_ERROR "Build with libc++ is not yet supported for this compiler")
endif()
else()
# Workaround for bug in GCC version 5 and higher.
# More information https://bugs.launchpad.net/ubuntu/+source/gcc-5/+bug/1568899
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND
CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 5.0)
target_link_libraries(${ARGV0} PRIVATE gcc_s gcc)
endif()
endif()
endfunction(add_common_options)

if (LLVM_ENABLE_ASSERTIONS AND NOT SYCL_DISABLE_STL_ASSERTIONS AND NOT WIN32)
if(SYCL_USE_LIBCXX)
if(LLVM_LIBCXX_USED)
add_definitions(-D_LIBCPP_DEBUG=1)
set(SYCL_CLANG_EXTRA_FLAGS "${SYCL_CLANG_EXTRA_FLAGS} -D_LIBCPP_DEBUG=1")
else()
Expand Down
2 changes: 1 addition & 1 deletion sycl/cmake/modules/AddSYCLExecutable.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ macro(add_sycl_executable ARG_TARGET_NAME)
endforeach()

if (LLVM_ENABLE_ASSERTIONS AND NOT SYCL_DISABLE_STL_ASSERTIONS)
if(SYCL_USE_LIBCXX)
if(LLVM_ENABLE_LIBCXX)
set(_SYCL_EXTRA_FLAGS -D_LIBCPP_DEBUG=1)
else()
set(_SYCL_EXTRA_FLAGS -D_GLIBCXX_ASSERTIONS=1)
Expand Down
10 changes: 3 additions & 7 deletions sycl/doc/GetStartedGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,19 @@ needed at the moment.
### Build DPC++ toolchain with libc++ library

There is experimental support for building and linking DPC++ runtime with libc++
library instead of libstdc++. To enable it the following CMake options should be
library instead of libstdc++. To enable it the following CMake option should be
used.

**Linux**:

```sh
-DSYCL_USE_LIBCXX=ON \
-DSYCL_LIBCXX_INCLUDE_PATH=<path to libc++ headers> \
-DSYCL_LIBCXX_LIBRARY_PATH=<path to libc++ and libc++abi libraries>
-DLLVM_ENABLE_LIBCXX=ON
```

You can also use configure script to enable:

```sh
python %DPCPP_HOME%\llvm\buildbot\configure.py --use-libcxx \
--libcxx-include <path to libc++ headers> \
--libcxx-library <path to libc++ and libc++ abi libraries>
python %DPCPP_HOME%\llvm\buildbot\configure.py --use-libcxx
python %DPCPP_HOME%\llvm\buildbot\compile.py
```

Expand Down
2 changes: 1 addition & 1 deletion sycl/source/builtins/common_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ BUILTIN_COMMON(ONE_ARG, radians,

BUILTIN_COMMON(ONE_ARG, sign, [](auto x) -> decltype(x) {
using T = decltype(x);
if (std::isnan(x))
if (std::isnan(sycl::detail::cast_if_host_half(x)))
return T(0.0);
if (x > 0)
return T(1.0);
Expand Down
2 changes: 1 addition & 1 deletion sycl/test/lit.site.cfg.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ config.test_include_path = "@TEST_INCLUDE_PATH@"
config.llvm_enable_projects = "@LLVM_ENABLE_PROJECTS@"

config.sycl_threads_lib = '@SYCL_THREADS_LIB@'
config.sycl_use_libcxx = '@SYCL_USE_LIBCXX@'
config.sycl_use_libcxx = '@LLVM_LIBCXX_USEDX@'
config.extra_environment = lit_config.params.get("extra_environment", "@LIT_EXTRA_ENVIRONMENT@")
config.cuda = '@SYCL_BUILD_BACKEND_CUDA@'
config.hip = '@SYCL_BUILD_BACKEND_HIP@'
Expand Down
11 changes: 0 additions & 11 deletions unified-runtime/source/loader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -211,21 +211,10 @@ if(UR_ENABLE_SANITIZER)
# In in-tree build, if LLVM is built with libc++, we also need to build
# symbolizer.cpp with libc++ abi and link libc++ in.
if(NOT UR_STANDALONE_BUILD AND LLVM_LIBCXX_USED)
execute_process(
COMMAND ${CMAKE_CXX_COMPILER} --print-file-name=libc++.a
OUTPUT_VARIABLE LIBCXX_PATH
OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(
COMMAND ${CMAKE_CXX_COMPILER} --print-file-name=libc++abi.a
Copy link
Contributor Author

@sarnex sarnex Jul 21, 2025

Choose a reason for hiding this comment

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

the change here is because --print-file-name doesn't work as required here so the lookup always fails, so just fall back to the underlyting compiler's errors.

OUTPUT_VARIABLE LIBCXX_ABI_PATH
OUTPUT_STRIP_TRAILING_WHITESPACE)
set_property(SOURCE
${symbolizer_sources}
APPEND_STRING PROPERTY COMPILE_FLAGS
" -stdlib=libc++ ")
if(NOT EXISTS ${LIBCXX_PATH} OR NOT EXISTS ${LIBCXX_ABI_PATH})
message(FATAL_ERROR "libc++ is required but can't find the libraries")
endif()
# Link with gcc_s fisrt to avoid some symbols resolve to libc++/libc++abi/libunwind's one
target_link_libraries(ur_loader PRIVATE gcc_s ${LIBCXX_PATH} ${LIBCXX_ABI_PATH})
endif()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#pragma once

#include "sanitizer_common/sanitizer_libdevice.hpp"
#include <cstddef>

#if !defined(__SPIR__) && !defined(__SPIRV__)
namespace ur_sanitizer_layer {
Expand Down
12 changes: 0 additions & 12 deletions xpti/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,6 @@ if (MSVC)
set(CMAKE_CXX_FLAGS_DEBUG "")
endif()

if (SYCL_USE_LIBCXX)
if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR
(CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
if ((NOT (DEFINED SYCL_LIBCXX_INCLUDE_PATH)) OR (NOT (DEFINED SYCL_LIBCXX_LIBRARY_PATH)))
message(FATAL_ERROR "When building with libc++ SYCL_LIBCXX_INCLUDE_PATHS and"
"SYCL_LIBCXX_LIBRARY_PATH should be set")
endif()
set(CMAKE_CXX_FLAGS "-nostdinc++ -I ${SYCL_LIBCXX_INCLUDE_PATH} ${CMAKE_CXX_FLAGS}")
else()
message(FATAL_ERROR "Build with libc++ is not yet supported for this compiler")
endif()
endif()
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/lib/${CMAKE_BUILD_TYPE})
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
Expand Down
13 changes: 0 additions & 13 deletions xptifw/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,6 @@ endif()
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")
endif()
if (SYCL_USE_LIBCXX)
if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR
(CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
if ((NOT (DEFINED SYCL_LIBCXX_INCLUDE_PATH)) OR (NOT (DEFINED SYCL_LIBCXX_LIBRARY_PATH)))
message(FATAL_ERROR "When building with libc++ SYCL_LIBCXX_INCLUDE_PATHS and"
"SYCL_LIBCXX_LIBRARY_PATH should be set")
endif()
set(CMAKE_CXX_FLAGS "-nostdinc++ -I ${SYCL_LIBCXX_INCLUDE_PATH} ${CMAKE_CXX_FLAGS}")
else()
message(FATAL_ERROR "Build with libc++ is not yet supported for this compiler")
endif()
endif()

set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/lib/${CMAKE_BUILD_TYPE})
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
Expand Down
4 changes: 0 additions & 4 deletions xptifw/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ function(add_xpti_library LIB_NAME)
target_link_libraries(${LIB_NAME} PUBLIC $<BUILD_INTERFACE:tbb>)
endif()

if (SYCL_USE_LIBCXX)
target_link_libraries(${LIB_NAME} PRIVATE "-L${SYCL_LIBCXX_LIBRARY_PATH}" -Wl,-rpath,${SYCL_LIBCXX_LIBRARY_PATH} -nodefaultlibs -lc++ -lc++abi -lm -lc -lgcc_s -lgcc)
endif()

# Set the location of the library installation
include(GNUInstallDirs)
install(TARGETS ${LIB_NAME}
Expand Down