-
Notifications
You must be signed in to change notification settings - Fork 796
[SYCL-RTC] Distribute SYCL toolchain header files inside libsycl-jit.so #19924
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
Changes from 1 commit
c4c086b
800c427
961362b
7ef5c4a
51f3828
da51f6b
8e0c900
4cb6d54
f2e0e53
49255bb
268c4b4
422a2ac
7e1ac0a
c82168c
62dc670
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # We use C23/C++26's `#embed` to implement this resource creation, and "host" | ||
| # CXX compiler might not have support for it. As such, use freshly built | ||
| # `clang++` instead. That, in turn, requires dedicated directory for CMake to | ||
| # allow to override `CMAKE_CXX_COMPILER`. | ||
|
|
||
|
|
||
| set(SYCL_JIT_RESOURCE_CPP "${CMAKE_CURRENT_BINARY_DIR}/resource.cpp") | ||
| add_custom_command( | ||
| OUTPUT ${SYCL_JIT_RESOURCE_CPP} | ||
| COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/generate.py --toolchain-dir ${CMAKE_BINARY_DIR} --output ${SYCL_JIT_RESOURCE_CPP} --prefix /sycl-jit-toolchain/ | ||
| WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" | ||
| DEPENDS sycl-headers ${CMAKE_CURRENT_SOURCE_DIR}/generate.py | ||
| ) | ||
|
|
||
| set_source_files_properties(${SYCL_JIT_RESOURCE_CPP} PROPERTIES | ||
| COMPILE_FLAGS -Wno-c23-extensions | ||
| ) | ||
|
|
||
| add_llvm_library(sycl-jit-resource | ||
| ${SYCL_JIT_RESOURCE_CPP} | ||
| OBJECT | ||
| DEPENDS | ||
| clang | ||
| # TODO: libdevice | ||
| ) | ||
|
|
||
| set(CMAKE_CXX_COMPILER ${CMAKE_BINARY_DIR}/bin/clang++) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import os | ||
| import argparse | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser( | ||
| description="Generate SYCL Headers Resource C++ file" | ||
| ) | ||
| parser.add_argument("-o", "--output", type=str, required=True, help="Output file") | ||
| parser.add_argument( | ||
| "-i", | ||
| "--toolchain-dir", | ||
| type=str, | ||
| required=True, | ||
| help="Path to toolchain root directory", | ||
| ) | ||
| parser.add_argument( | ||
| "--prefix", type=str, required=True, help="Prefix for file locations" | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| # abspath also strips trailing "/" | ||
| toolchain_dir = os.path.abspath(args.toolchain_dir) | ||
|
|
||
| with open(args.output, "w") as out: | ||
| out.write( | ||
| """ | ||
| #include <utility> | ||
| #include <string_view> | ||
|
|
||
| namespace jit_compiler { | ||
| extern const std::pair<std::string_view, std::string_view> ToolchainFiles[]; | ||
| const std::pair<std::string_view, std::string_view> ToolchainFiles[] = {""" | ||
| ) | ||
|
|
||
| def process_dir(dir): | ||
| for root, _, files in os.walk(dir): | ||
| for file in files: | ||
| file_path = os.path.join(root, file) | ||
| out.write( | ||
| f""" | ||
| {{ | ||
| {{"{args.prefix}{os.path.relpath(file_path, toolchain_dir)}"}} , | ||
| []() {{ | ||
| static const char data[] = {{ | ||
| #embed "{file_path}" | ||
| , 0}}; | ||
| return std::string_view(data); | ||
| }}() | ||
| }},""" | ||
| ) | ||
|
|
||
| process_dir(os.path.join(args.toolchain_dir, "include/")) | ||
| process_dir(os.path.join(args.toolchain_dir, "lib/clang/")) | ||
|
|
||
| out.write( | ||
| f""" | ||
| }}; | ||
|
|
||
| extern size_t NumToolchainFiles; | ||
| size_t NumToolchainFiles = std::size(ToolchainFiles); | ||
| extern std::string_view ToolchainPrefix; | ||
| std::string_view ToolchainPrefix = "{args.prefix}"; | ||
| }} // namespace jit_compiler | ||
| """ | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,13 @@ | |
| #include <array> | ||
| #include <sstream> | ||
|
|
||
| namespace jit_compiler { | ||
| // Defined in the auto-generated file: | ||
| extern const std::pair<std::string_view, std::string_view> ToolchainFiles[]; | ||
|
||
| extern size_t NumToolchainFiles; | ||
| extern std::string_view ToolchainPrefix; | ||
| } // namespace jit_compiler | ||
|
|
||
| using namespace clang; | ||
| using namespace clang::tooling; | ||
| using namespace clang::driver; | ||
|
|
@@ -178,7 +185,12 @@ class HashPreprocessedAction : public PreprocessorFrontendAction { | |
| }; | ||
|
|
||
| class SYCLToolchain { | ||
| SYCLToolchain() {} | ||
| SYCLToolchain() { | ||
| for (size_t i = 0; i < NumToolchainFiles; ++i) { | ||
| auto [Path, Content] = ToolchainFiles[i]; | ||
| ToolchainFS->addFile(Path, 0, llvm::MemoryBuffer::getMemBuffer(Content)); | ||
| } | ||
| } | ||
|
|
||
| // Similar to FrontendActionFactory, but we don't take ownership of | ||
| // `FrontendAction`, nor do we create copies of it as we only perform a single | ||
|
|
@@ -231,6 +243,7 @@ class SYCLToolchain { | |
| DiagnosticConsumer *DiagConsumer = nullptr) { | ||
| auto FS = llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>( | ||
| llvm::vfs::getRealFileSystem()); | ||
| FS->pushOverlay(ToolchainFS); | ||
| if (FSOverlay) | ||
| FS->pushOverlay(FSOverlay); | ||
|
|
||
|
|
@@ -245,8 +258,14 @@ class SYCLToolchain { | |
| return TI.run(); | ||
| } | ||
|
|
||
| std::string_view getClangXXExe() const { return ClangXXExe; } | ||
cperkinsintel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private: | ||
| clang::IgnoringDiagConsumer IgnoreDiag; | ||
| std::string ClangXXExe = | ||
| (jit_compiler::ToolchainPrefix + "/bin/clang++").str(); | ||
| llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> ToolchainFS = | ||
| llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>(); | ||
jopperm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| class ClangDiagnosticWrapper { | ||
|
|
@@ -296,14 +315,11 @@ class LLVMDiagnosticWrapper : public llvm::DiagnosticHandler { | |
| } // anonymous namespace | ||
|
|
||
| static std::vector<std::string> | ||
| createCommandLine(const InputArgList &UserArgList, std::string_view DPCPPRoot, | ||
| BinaryFormat Format, std::string_view SourceFilePath) { | ||
| createCommandLine(const InputArgList &UserArgList, BinaryFormat Format, | ||
| std::string_view SourceFilePath) { | ||
| DerivedArgList DAL{UserArgList}; | ||
| const auto &OptTable = getDriverOptTable(); | ||
| DAL.AddFlagArg(nullptr, OptTable.getOption(OPT_fsycl_device_only)); | ||
| DAL.AddJoinedArg( | ||
| nullptr, OptTable.getOption(OPT_resource_dir_EQ), | ||
| (DPCPPRoot + "/lib/clang/" + Twine(CLANG_VERSION_MAJOR)).str()); | ||
| // User args may contain options not intended for the frontend, but we can't | ||
| // claim them here to tell the driver they're used later. Hence, suppress the | ||
| // unused argument warning. | ||
|
|
@@ -327,7 +343,7 @@ createCommandLine(const InputArgList &UserArgList, std::string_view DPCPPRoot, | |
|
|
||
| std::vector<std::string> CommandLine; | ||
| CommandLine.reserve(ASL.size() + 2); | ||
| CommandLine.emplace_back((DPCPPRoot + "/bin/clang++").str()); | ||
| CommandLine.emplace_back(SYCLToolchain::instance().getClangXXExe()); | ||
| transform(ASL, std::back_inserter(CommandLine), | ||
| [](const char *AS) { return std::string{AS}; }); | ||
| CommandLine.emplace_back(SourceFilePath); | ||
|
|
@@ -355,13 +371,8 @@ Expected<std::string> jit_compiler::calculateHash( | |
| const InputArgList &UserArgList, BinaryFormat Format) { | ||
| TimeTraceScope TTS{"calculateHash"}; | ||
|
|
||
| const std::string &DPCPPRoot = getDPCPPRoot(); | ||
| if (DPCPPRoot == InvalidDPCPPRoot) { | ||
| return createStringError("Could not locate DPCPP root directory"); | ||
| } | ||
|
|
||
| std::vector<std::string> CommandLine = | ||
| createCommandLine(UserArgList, DPCPPRoot, Format, SourceFile.Path); | ||
| createCommandLine(UserArgList, Format, SourceFile.Path); | ||
|
|
||
| HashPreprocessedAction HashAction; | ||
|
|
||
|
|
@@ -372,8 +383,7 @@ Expected<std::string> jit_compiler::calculateHash( | |
| // unique for each query, so drop it: | ||
| CommandLine.pop_back(); | ||
|
|
||
| // The command line contains the DPCPP root and clang major version in | ||
| // "-resource-dir=<...>" argument. | ||
| // TODO: Include hash of the current libsycl-jit.so/.dll somehow... | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably not that important and can be skipped for now, because different releases (and most of the commits) will likely result in different |
||
| BLAKE3Result<> CommandLineHash = | ||
| BLAKE3::hash(arrayRefFromStringRef(join(CommandLine, ","))); | ||
|
|
||
|
|
@@ -394,18 +404,13 @@ Expected<ModuleUPtr> jit_compiler::compileDeviceCode( | |
| LLVMContext &Context, BinaryFormat Format) { | ||
| TimeTraceScope TTS{"compileDeviceCode"}; | ||
|
|
||
| const std::string &DPCPPRoot = getDPCPPRoot(); | ||
| if (DPCPPRoot == InvalidDPCPPRoot) { | ||
| return createStringError("Could not locate DPCPP root directory"); | ||
| } | ||
|
|
||
| EmitLLVMOnlyAction ELOA{&Context}; | ||
| DiagnosticOptions DiagOpts; | ||
| ClangDiagnosticWrapper Wrapper(BuildLog, &DiagOpts); | ||
|
|
||
| if (SYCLToolchain::instance().run( | ||
| createCommandLine(UserArgList, DPCPPRoot, Format, SourceFile.Path), | ||
| ELOA, getInMemoryFS(SourceFile, IncludeFiles), Wrapper.consumer())) { | ||
| createCommandLine(UserArgList, Format, SourceFile.Path), ELOA, | ||
| getInMemoryFS(SourceFile, IncludeFiles), Wrapper.consumer())) { | ||
| return ELOA.takeModule(); | ||
| } else { | ||
| return createStringError(BuildLog); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not enough (see Win CI failure), but I don't know if we have some precedents to doing something similar. Any pointers would be greatly appreciated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could try random combinations of
PARENT_SCOPECACHEandFORCEThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, maybe try setting the C compiler too because cl is both C and C++?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also doing it this way is unsupported, it seems if we make this a subproject then it will reliably work