Skip to content
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
5 changes: 3 additions & 2 deletions clang/lib/Driver/ToolChains/SYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "llvm/SYCLLowerIR/DeviceConfigFile.hpp"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/VirtualFileSystem.h"
#include <sstream>

using namespace clang::driver;
Expand Down Expand Up @@ -55,7 +56,7 @@ const char *SYCLInstallationDetector::findLibspirvPath(

// If -fsycl-libspirv-path= is specified, try to use that path directly.
if (Arg *A = Args.getLastArg(options::OPT_fsycl_libspirv_path_EQ)) {
if (llvm::sys::fs::exists(A->getValue()))
if (D.getVFS().exists(A->getValue()))
return A->getValue();

return nullptr;
Expand All @@ -68,7 +69,7 @@ const char *SYCLInstallationDetector::findLibspirvPath(
SmallString<128> LibraryPath(Path);
llvm::sys::path::append(LibraryPath, a, b, c, Basename);

if (llvm::sys::fs::exists(LibraryPath))
if (D.getVFS().exists(LibraryPath))
return Args.MakeArgString(LibraryPath);

return nullptr;
Expand Down
48 changes: 48 additions & 0 deletions sycl-jit/jit-compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,50 @@


set(SYCL_JIT_RESOURCE_CPP "${CMAKE_CURRENT_BINARY_DIR}/resource.cpp")
set(SYCL_JIT_RESOURCE_OBJ "${CMAKE_CURRENT_BINARY_DIR}/resource.cpp.o")

if (WIN32)
set(SYCL_JIT_VIRTUAL_TOOLCHAIN_ROOT "c:/sycl-jit-toolchain/")
else()
set(SYCL_JIT_VIRTUAL_TOOLCHAIN_ROOT "/sycl-jit-toolchain/")
endif()

add_custom_command(
OUTPUT ${SYCL_JIT_RESOURCE_CPP}
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/utils/generate.py --toolchain-dir ${CMAKE_BINARY_DIR} --output ${SYCL_JIT_RESOURCE_CPP} --prefix ${SYCL_JIT_VIRTUAL_TOOLCHAIN_ROOT}
DEPENDS
sycl-headers # include/sycl
libclc # lib/clc
clang # lib/clang
# TODO: libdevice
${CMAKE_CURRENT_SOURCE_DIR}/utils/generate.py
)

# We use C23/C++26's `#embed` to implement this resource creation, and "current"
# CMAKE_CXX_COMPILER might not have support for it. As such, use freshly built
# `clang++` instead.
if (WIN32)
set(clang_exe ${CMAKE_BINARY_DIR}/bin/clang-cl.exe)
set(SYCL_JIT_RESOURCE_CXX_FLAGS /O2 /std:c++17 /W0)
if (CMAKE_BUILD_TYPE MATCHES "Debug")
list(APPEND SYCL_JIT_RESOURCE_CXX_FLAGS /MDd)
else()
list(APPEND SYCL_JIT_RESOURCE_CXX_FLAGS /MD)
endif()
else()
get_host_tool_path( clang CLANG clang_exe clang_target )
set(SYCL_JIT_RESOURCE_CXX_FLAGS -O2 -Wno-c23-extensions -std=c++17 -fPIC -fvisibility=hidden)
endif()

add_custom_command(
OUTPUT ${SYCL_JIT_RESOURCE_OBJ}
COMMAND
${clang_exe} ${SYCL_JIT_RESOURCE_CPP} -I ${CMAKE_CURRENT_SOURCE_DIR}/include -c -o ${SYCL_JIT_RESOURCE_OBJ} ${SYCL_JIT_RESOURCE_CXX_FLAGS}
DEPENDS
${SYCL_JIT_RESOURCE_CPP}
${CMAKE_CURRENT_SOURCE_DIR}/include/Resource.h
)

add_llvm_library(sycl-jit
lib/translation/JITContext.cpp
lib/translation/SPIRVLLVMTranslation.cpp
Expand All @@ -11,6 +57,8 @@ add_llvm_library(sycl-jit
lib/helper/ConfigHelper.cpp
lib/helper/ErrorHelper.cpp

${SYCL_JIT_RESOURCE_OBJ}

SHARED

DEPENDS
Expand Down
20 changes: 20 additions & 0 deletions sycl-jit/jit-compiler/include/Resource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#pragma once

#include <iterator>
#include <string_view>
#include <utility>

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
43 changes: 21 additions & 22 deletions sycl-jit/jit-compiler/lib/rtc/DeviceCompilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "DeviceCompilation.h"
#include "ESIMD.h"
#include "JITBinaryInfo.h"
#include "Resource.h"
#include "translation/Translation.h"

#include <clang/Basic/DiagnosticDriver.h>
Expand Down Expand Up @@ -178,7 +179,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 +237,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 +252,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 +309,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 +337,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 +365,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 +377,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 +398,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
67 changes: 67 additions & 0 deletions sycl-jit/jit-compiler/utils/generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 <Resource.h>

namespace jit_compiler {
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).replace(os.sep, "/")}"}} ,
[]() {{
static const char data[] = {{
#embed "{file_path}" if_empty(0)
, 0}};
return std::string_view(data, std::size(data) - 1);
}}()
}},"""
)

process_dir(os.path.join(args.toolchain_dir, "include/"))
process_dir(os.path.join(args.toolchain_dir, "lib/clang/"))
process_dir(os.path.join(args.toolchain_dir, "lib/clc/"))

out.write(
f"""
}};

size_t NumToolchainFiles = std::size(ToolchainFiles);
std::string_view ToolchainPrefix = "{args.prefix}";
}} // namespace jit_compiler
"""
)


if __name__ == "__main__":
main()