Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
12 changes: 12 additions & 0 deletions buildbot/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ def do_configure(args, passthrough_args):

install_dir = os.path.join(abs_obj_dir, "install")

llvm_enable_runtimes = "libcxx"
# Matches `libcxx`'s requirements/platform ABI:
if platform.system() != "Windows":
llvm_enable_runtimes += ";libcxxabi;libunwind"
llvm_enable_runtimes += ";libc"

cmake_cmd = [
"cmake",
"-G",
Expand All @@ -195,6 +201,12 @@ def do_configure(args, passthrough_args):
"-DLLVM_EXTERNAL_LIBDEVICE_SOURCE_DIR={}".format(libdevice_dir),
"-DLLVM_EXTERNAL_SYCL_JIT_SOURCE_DIR={}".format(jit_dir),
"-DLLVM_ENABLE_PROJECTS={}".format(llvm_enable_projects),
"-DLLVM_ENABLE_RUNTIMES={}".format(llvm_enable_runtimes),
"-DLLVM_LIBC_FULL_BUILD=ON",
"-DLLVM_LIBC_ALL_HEADERS=1",
"-DLIBC_CONFIG_PATH={}".format(
os.path.join(abs_src_dir, "sycl-jit/jit-compiler/lib/libc-config")
),
"-DSYCL_BUILD_PI_HIP_PLATFORM={}".format(sycl_build_pi_hip_platform),
"-DLLVM_BUILD_TOOLS=ON",
"-DLLVM_ENABLE_ZSTD={}".format(llvm_enable_zstd),
Expand Down
8 changes: 8 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -7561,6 +7561,14 @@ let Visibility = [SYCLRTCOnlyOption] in {
: Joined<["--"], "persistent-auto-pch=">,
HelpText<"Use Persistent Auto-PCH cache located at <dir> for SYCL "
"RTC Compilation">;
def sycl_rtc_exp_redist_mode
: Flag<["--"], "sycl-rtc-experimental-redist-mode">,
HelpText<"Use in-memory system includes">;
def sycl_rtc_in_memory_fs_only
: Flag<["--"], "sycl-rtc-in-memory-fs-only">,
HelpText<"Disable real filesystem access for SYCL RTC compilation, "
"debugging/testing only">,
Flags<[HelpHidden]>;
} // let Group = sycl_rtc_only_Group
} // let Visibility = [SYCLRTCOnlyOption]

Expand Down
9 changes: 6 additions & 3 deletions clang/lib/Headers/mm_malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

#include <stdlib.h>

#ifdef _WIN32
#if defined(_WIN32) && \
!(defined(__SYCL_DEVICE_ONLY__) && defined(__LLVM_LIBC__))
#include <malloc.h>
#else
#ifndef __cplusplus
Expand Down Expand Up @@ -41,7 +42,8 @@ _mm_malloc(size_t __size, size_t __align) {
void *__mallocedMemory;
#if defined(__MINGW32__)
__mallocedMemory = __mingw_aligned_malloc(__size, __align);
#elif defined(_WIN32)
#elif defined(_WIN32) && \
!(defined(__SYCL_DEVICE_ONLY__) && defined(__LLVM_LIBC__))
__mallocedMemory = _aligned_malloc(__size, __align);
#else
if (posix_memalign(&__mallocedMemory, __align, __size))
Expand All @@ -56,7 +58,8 @@ _mm_free(void *__p)
{
#if defined(__MINGW32__)
__mingw_aligned_free(__p);
#elif defined(_WIN32)
#elif defined(_WIN32) && \
!(defined(__SYCL_DEVICE_ONLY__) && defined(__LLVM_LIBC__))
_aligned_free(__p);
#else
free(__p);
Expand Down
2 changes: 2 additions & 0 deletions libc/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,8 @@ foreach(target IN LISTS all_install_header_targets)
endforeach()

if(LLVM_LIBC_FULL_BUILD)
add_custom_target(generate-libc-headers
DEPENDS libc-headers)
Comment on lines +862 to +863
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's commit libc and libcxx changes to https://github.com/llvm/llvm-project/ as well. It would be ideal if we merge them to https://github.com/llvm/llvm-project/ before merging this PR to gather the feedback from the LLVM's libc and libcxx communities.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have an in-tree use-case to make upstream PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't have an in-tree use-case to make upstream PR.

Could you please check with libc/libcxx maintainers if such use case is required?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't feel comfortable asking to contribute something that I myself believe shouldn't go upstream. For bugfixes I intended to do so, only to find later that those were fixed in the trunk, so it's not that I refuse to work with them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bader , ping

Copy link
Contributor

Choose a reason for hiding this comment

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

@bader , ping

@aelovikov-intel, do you have a question for me?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, I have an answer to your previous question. I don't think those changes can/should be upstreamed. If it's your stance that somebody has to go to community and get either an approval or rejection there before moving this PR forward, then we need to find another owner for this task.

add_custom_target(install-libc-headers
DEPENDS libc-headers
COMMAND "${CMAKE_COMMAND}"
Expand Down
4 changes: 2 additions & 2 deletions libc/include/wchar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ functions:
- type: const wchar_t **__restrict
- type: size_t
- type: size_t
- type: mbstate_t
- type: mbstate_t *__restrict
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 and the one below are part of the upstream llvm/llvm-project#164666.

- name: wcsrtombs
standards:
- stdc
Expand All @@ -255,7 +255,7 @@ functions:
- type: char *__restrict
- type: const wchar_t **__restrict
- type: size_t
- type: mbstate_t
- type: mbstate_t *__restrict
- name: wcrtomb
standards:
- stdc
Expand Down
4 changes: 2 additions & 2 deletions libcxx/include/__config
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ _LIBCPP_HARDENING_MODE_DEBUG
# define _LIBCPP_MSVCRT_LIKE
// If mingw not explicitly detected, assume using MS C runtime only if
// a MS compatibility version is specified.
# if defined(_MSC_VER) && !defined(__MINGW32__)
# if defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_LIBCPP_NO_VCRUNTIME)
# define _LIBCPP_MSVCRT // Using Microsoft's C Runtime library
# endif
# if (defined(_M_AMD64) || defined(__x86_64__)) || (defined(_M_ARM) || defined(__arm__))
Expand Down Expand Up @@ -911,7 +911,7 @@ typedef __char32_t char32_t;
# endif

# if defined(__BIONIC__) || defined(__NuttX__) || defined(__Fuchsia__) || defined(__wasi__) || \
_LIBCPP_HAS_MUSL_LIBC || defined(__OpenBSD__) || defined(__LLVM_LIBC__)
_LIBCPP_HAS_MUSL_LIBC || defined(__OpenBSD__) || defined(__LLVM_LIBC__) || defined(__SYCL_DEVICE_ONLY__)
# define _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE
# endif

Expand Down
5 changes: 3 additions & 2 deletions libcxx/include/__locale_dir/locale_base_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@
// }

#if _LIBCPP_HAS_LOCALIZATION

# if defined(__APPLE__)
# if defined(__SYCL_DEVICE_ONLY__)
# include <__locale_dir/support/fuchsia.h> // no_locale
# elif defined(__APPLE__)
# include <__locale_dir/support/apple.h>
# elif defined(__FreeBSD__)
# include <__locale_dir/support/freebsd.h>
Expand Down
42 changes: 41 additions & 1 deletion sycl-jit/jit-compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ endif()

set(SYCL_JIT_RESOURCE_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/rtc-resources-install)

set(SYCL_JIT_PREPARE_RESOURCE_COMMANDS)
set(SYCL_JIT_PREPARE_RESOURCE_COMMANDS "")
foreach(component IN LISTS SYCL_JIT_RESOURCE_INSTALL_COMPONENTS)
list(APPEND SYCL_JIT_PREPARE_RESOURCE_COMMANDS
COMMAND ${CMAKE_COMMAND} --install ${CMAKE_BINARY_DIR} --prefix ${SYCL_JIT_RESOURCE_INSTALL_DIR} --component "${component}"
Expand All @@ -45,13 +45,52 @@ add_custom_target(rtc-prepare-resources
${SYCL_JIT_RESOURCE_FILES}
)

set(SYCL_JIT_RUNTIME_RESOURCE_DEPS "")
set(SYCL_JIT_PREPARE_RUNTIME_RESOURCE_COMMANDS "")

if ("libcxx" IN_LIST LLVM_ENABLE_RUNTIMES)
list(APPEND SYCL_JIT_RUNTIME_RESOURCE_DEPS runtimes-configure)

if (WIN32)
set(SYCL_JIT_CXX_CONFIG_SITE ${CMAKE_CURRENT_BINARY_DIR}/rtc-resources-install/include/c++/v1/__config_site)
else()
set(SYCL_JIT_CXX_CONFIG_SITE ${CMAKE_CURRENT_BINARY_DIR}/rtc-resources-install/include/x86_64-unknown-linux-gnu/c++/v1/__config_site)
endif()

list(APPEND SYCL_JIT_PREPARE_RUNTIME_RESOURCE_COMMANDS
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/runtimes/runtimes-bins --target generate-cxx-headers
COMMAND ${CMAKE_COMMAND} --install ${CMAKE_BINARY_DIR}/runtimes/runtimes-bins --prefix ${CMAKE_CURRENT_BINARY_DIR}/rtc-resources-install --component cxx-headers
# `<thread>` functionality isn't really supported on the device, just make
# it pass compilation. The easiest way to do that is to configure `libc++`
# to use "external threading API".
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/rtc-resources-install/include/sycl-rtc-standalone
COMMAND sed 's/_LIBCPP_HAS_THREAD_API_EXTERNAL 0/_LIBCPP_HAS_THREAD_API_EXTERNAL 1/' ${SYCL_JIT_CXX_CONFIG_SITE} > ${CMAKE_CURRENT_BINARY_DIR}/rtc-resources-install/include/sycl-rtc-standalone/__config_site
)
endif()

if ("libc" IN_LIST LLVM_ENABLE_RUNTIMES)
list(APPEND SYCL_JIT_RUNTIME_RESOURCE_DEPS runtimes-configure)

list(APPEND SYCL_JIT_PREPARE_RUNTIME_RESOURCE_COMMANDS
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/runtimes/runtimes-bins --target generate-libc-headers
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_BINARY_DIR}/runtimes/runtimes-bins/libc/include ${SYCL_JIT_RESOURCE_INSTALL_DIR}/include/libc
)
endif()

add_custom_target(rtc-prepare-runtime-resources
DEPENDS ${SYCL_JIT_RUNTIME_RESOURCE_DEPS}
${SYCL_JIT_PREPARE_RUNTIME_RESOURCE_COMMANDS}
)

add_custom_command(
OUTPUT ${SYCL_JIT_RESOURCE_CPP}
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/utils/generate.py --toolchain-dir ${SYCL_JIT_RESOURCE_INSTALL_DIR} --output ${SYCL_JIT_RESOURCE_CPP} --prefix ${SYCL_JIT_VIRTUAL_TOOLCHAIN_ROOT}
DEPENDS
rtc-prepare-resources
${SYCL_JIT_RESOURCE_DEPS}
${SYCL_JIT_RESOURCE_FILES}
rtc-prepare-runtime-resources
${SYCL_JIT_RUNTIME_RESOURCE_DEPS}
${CMAKE_CURRENT_SOURCE_DIR}/utils/generate.py
)

Expand Down Expand Up @@ -94,6 +133,7 @@ add_custom_command(
${SYCL_JIT_RESOURCE_CPP}
${SYCL_JIT_RESOURCE_DEPS}
${SYCL_JIT_RESOURCE_FILES}
${SYCL_JIT_RUNTIME_RESOURCE_DEPS}
${CMAKE_CURRENT_SOURCE_DIR}/include/Resource.h
)

Expand Down
5 changes: 5 additions & 0 deletions sycl-jit/jit-compiler/lib/libc-config/entrypoints.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
else()
include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
endif()
1 change: 1 addition & 0 deletions sycl-jit/jit-compiler/lib/libc-config/headers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include("${LIBC_SOURCE_DIR}/config/linux/x86_64/headers.txt")
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
_LIBCPP_BEGIN_NAMESPACE_STD

using __libcpp_timespec_t = int;

//
// Mutex
//
using __libcpp_mutex_t = int;
#define _LIBCPP_MUTEX_INITIALIZER 0

using __libcpp_recursive_mutex_t = int;

Copy link
Contributor

Choose a reason for hiding this comment

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

I have a feeling I asked this before on another PR but are these files basically to support stuff not supported by LLVM's libc/cxx?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The short answer something didn't work when I used libcxx in (default) mode that uses pthreads to implement <thread>. GPU doesn't really support anything, so all we need is declarations and it's just easier to do this. I see that there is libc/include/pthread.yml, so maybe it would be possible to make that work, but there would be no benefit.

int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t*);
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t*);
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t*);
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t*);
int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t*);

_LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_mutex_lock(__libcpp_mutex_t*);
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS bool __libcpp_mutex_trylock(__libcpp_mutex_t*);
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_mutex_unlock(__libcpp_mutex_t*);
int __libcpp_mutex_destroy(__libcpp_mutex_t*);

//
// Condition Variable
//
using __libcpp_condvar_t = int;
#define _LIBCPP_CONDVAR_INITIALIZER 0

int __libcpp_condvar_signal(__libcpp_condvar_t*);
int __libcpp_condvar_broadcast(__libcpp_condvar_t*);
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_condvar_wait(__libcpp_condvar_t*, __libcpp_mutex_t*);
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS
int __libcpp_condvar_timedwait(__libcpp_condvar_t*, __libcpp_mutex_t*, __libcpp_timespec_t*);
int __libcpp_condvar_destroy(__libcpp_condvar_t*);

//
// Execute once
//
using __libcpp_exec_once_flag = int;
#define _LIBCPP_EXEC_ONCE_INITIALIZER 0

int __libcpp_execute_once(__libcpp_exec_once_flag*, void (*__init_routine)());

//
// Thread id
//
using __libcpp_thread_id = int;

bool __libcpp_thread_id_equal(__libcpp_thread_id, __libcpp_thread_id);
bool __libcpp_thread_id_less(__libcpp_thread_id, __libcpp_thread_id);

//
// Thread
//
#define _LIBCPP_NULL_THREAD 0
using __libcpp_thread_t = int;

bool __libcpp_thread_isnull(const __libcpp_thread_t*);
int __libcpp_thread_create(__libcpp_thread_t*, void* (*__func)(void*), void* __arg);
__libcpp_thread_id __libcpp_thread_get_current_id();
__libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t*);
int __libcpp_thread_join(__libcpp_thread_t*);
int __libcpp_thread_detach(__libcpp_thread_t*);
void __libcpp_thread_yield();
void __libcpp_thread_sleep_for(const chrono::nanoseconds&);

//
// Thread local storage
//
#define _LIBCPP_TLS_DESTRUCTOR_CC 0
using __libcpp_tls_key = int;

int __libcpp_tls_create(__libcpp_tls_key*, void (*__at_exit)(void*));
void* __libcpp_tls_get(__libcpp_tls_key);
int __libcpp_tls_set(__libcpp_tls_key, void*);

_LIBCPP_END_NAMESPACE_STD
48 changes: 45 additions & 3 deletions sycl-jit/jit-compiler/lib/rtc/DeviceCompilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,42 @@ class SYCLToolchain {
DAL.AddJoinedArg(nullptr, OptTable.getOption(OPT_offload_arch_EQ), CPU);
}

if (UserArgList.hasArg(OPT_sycl_rtc_exp_redist_mode)) {
DAL.AddFlagArg(nullptr, OptTable.getOption(OPT_nostdlibinc));
auto AddInc = [&](auto RelPath) {
DAL.AddJoinedArg(nullptr, OptTable.getOption(OPT_isystem),
(getPrefix() + RelPath).str());
};
AddInc("include/sycl/stl_wrappers");
// Contains modified `__config_site` for libc++, need to come earlier in
// the search path:
AddInc("include/sycl-rtc-standalone/");
#if !defined(_WIN32)
// AFAIK, it only contains original `__config_site` that we don't use (see
// above), but it seems safer to add this path anyway, in case any extra
// files are added. On Windows `LIBCXX_GENERATED_INCLUDE_TARGET_DIR` is
// off and thus we don't need it.
AddInc("include/x86_64-unknown-linux-gnu/c++/v1");
#endif
AddInc("include/c++/v1");
AddInc("include/libc");
AddInc("include/");
AddInc("include/lib/clang/22/include/");
DAL.AddJoinedArg(nullptr, OptTable.getOption(OPT_D),
"_LIBCPP_REMOVE_TRANSITIVE_INCLUDES");
#if defined(_WIN32)
DAL.AddJoinedArg(nullptr, OptTable.getOption(OPT_D),
"_LIBCPP_WCHAR_H_HAS_CONST_OVERLOADS");
DAL.AddJoinedArg(nullptr, OptTable.getOption(OPT_D),
"_LIBCPP_NO_VCRUNTIME");
DAL.AddJoinedArg(nullptr, OptTable.getOption(OPT_U), "__ELF__");

#endif
DAL.AddJoinedArg(nullptr, OptTable.getOption(OPT_include), "stdio.h");
DAL.AddJoinedArg(nullptr, OptTable.getOption(OPT_include), "wchar.h");
DAL.AddJoinedArg(nullptr, OptTable.getOption(OPT_include), "time.h");
}

ArgStringList ASL;
for (Arg *A : DAL)
A->render(DAL, ASL);
Expand Down Expand Up @@ -543,9 +579,15 @@ class SYCLToolchain {
std::vector<std::string> CommandLine =
createCommandLine(UserArgList, Format, SourceFilePath);

auto FS = llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(
llvm::vfs::getRealFileSystem());
FS->pushOverlay(getToolchainFS());
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> FS;
if (UserArgList.hasArg(OPT_sycl_rtc_in_memory_fs_only)) {
FS = llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(
getToolchainFS());
} else {
FS = llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(
llvm::vfs::getRealFileSystem());
FS->pushOverlay(getToolchainFS());
}
if (FSOverlay)
FS->pushOverlay(std::move(FSOverlay));

Expand Down
14 changes: 11 additions & 3 deletions sycl-jit/jit-compiler/utils/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def main():
const resource_file ToolchainFiles[] = {"""
)

def process_file(file_path):
def process_file(file_path, relative_to):
out.write(
f"""
{{
{{"{args.prefix}{os.path.relpath(file_path, toolchain_dir).replace(os.sep, "/")}"}} ,
{{"{args.prefix}{os.path.relpath(file_path, relative_to).replace(os.sep, "/")}"}} ,
[]() {{
static const char data[] = {{
#embed "{file_path}" if_empty(0)
Expand All @@ -50,9 +50,17 @@ def process_dir(dir):
for root, _, files in os.walk(dir):
for file in files:
file_path = os.path.join(root, file)
process_file(file_path)
process_file(file_path, dir)

process_dir(args.toolchain_dir)
process_dir(
os.path.realpath(
os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"../lib/resource-includes/",
Copy link
Contributor

Choose a reason for hiding this comment

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

what do we get from using the relative path

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think os.path.relpath at line 39 didn't work for me without this. From the docs:

Return a relative filepath to path either from the current directory or from an optional start directory. This is a path computation: the filesystem is not accessed to confirm the existence or nature of path or start.

so my recollection is probably correct here.

)
)
)

out.write(
f"""
Expand Down
Loading
Loading