Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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-jit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if (NOT WIN32 AND NOT CYGWIN)
endif(SYCL_JIT_ENABLE_WERROR)
endif()


add_subdirectory(jit-compiler-resource)
add_subdirectory(jit-compiler)
add_subdirectory(passes)

Expand Down
27 changes: 27 additions & 0 deletions sycl-jit/jit-compiler-resource/CMakeLists.txt
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++)
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'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.

Copy link
Contributor

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_SCOPE CACHE and FORCE

Copy link
Contributor

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++?

Copy link
Contributor

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

70 changes: 70 additions & 0 deletions sycl-jit/jit-compiler-resource/generate.py
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()
1 change: 1 addition & 0 deletions sycl-jit/jit-compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ add_llvm_library(sycl-jit
clangCodeGen
clangTooling
clangSerialization
sycl-jit-resource
)

if(WIN32)
Expand Down
49 changes: 27 additions & 22 deletions sycl-jit/jit-compiler/lib/rtc/DeviceCompilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there any ABI problems looming here?

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 wouldn't think so - the use is limited to libsycl-jit.so and this isn't being exported outside the DSO.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense. What I was getting at was, IIUC, the TU containing ToolchainFiles is compiled with a different compiler and flags than the rest of sycl-jit.so, so maybe there's future ABI problem hiding in there. Seems fine for now, though.

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 TU containing ToolchainFiles is compiled with a different compiler and flags than the rest of sycl-jit.so, so maybe there's future ABI problem hiding in there.

I see now what you meant. I believe if any such issues will happen, we'll catch them immediately with our E2E tests, because now that these files are distributed with the libsycl-jit.so we have much more reproducibility in the behavior. Also, resource.cpp.o is very simple after compiling with optimizations.

extern size_t NumToolchainFiles;
extern std::string_view ToolchainPrefix;
} // namespace jit_compiler

using namespace clang;
using namespace clang::tooling;
using namespace clang::driver;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -245,8 +258,14 @@ class SYCLToolchain {
return TI.run();
}

std::string_view getClangXXExe() const { return ClangXXExe; }

private:
clang::IgnoringDiagConsumer IgnoreDiag;
std::string ClangXXExe =
(jit_compiler::ToolchainPrefix + "/bin/clang++").str();
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> ToolchainFS =
llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
};

class ClangDiagnosticWrapper {
Expand Down Expand Up @@ -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.
Expand All @@ -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);
Expand Down Expand Up @@ -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;

Expand All @@ -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...
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 SourceHash due to changes in the headers.

BLAKE3Result<> CommandLineHash =
BLAKE3::hash(arrayRefFromStringRef(join(CommandLine, ",")));

Expand All @@ -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);
Expand Down
Loading