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
34 changes: 25 additions & 9 deletions sycl-jit/jit-compiler/include/Resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,29 @@

#pragma once

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

namespace jit_compiler {
namespace jit_compiler::resource {
// `resource.cpp` is compiled using freshly built clang and it's very hard to
// sync compilation options between that and normal compilation for other files.
// Note that some of the options might affect ABI (e.g., libstdc++ vs. libc++
// usage, or custom sysroot/gcc installation directory). A much easier approach
// is to ensure that `resource.cpp` doesn't have any includes at all, hence
// these helpers:
template <class T, unsigned long long N>
constexpr unsigned long long size(const T (&)[N]) noexcept {
return N;
}
struct resource_string_view {
template <unsigned long long N>
resource_string_view(const char (&S)[N]) : S(S), Size(N - 1) {}
const char *S;
unsigned long long Size;
};
struct resource_file {
resource_string_view Path;
resource_string_view Content;
};
// 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
extern const resource_file ToolchainFiles[];
extern unsigned long long NumToolchainFiles;
extern resource_string_view ToolchainPrefix;
} // namespace jit_compiler::resource
18 changes: 12 additions & 6 deletions sycl-jit/jit-compiler/lib/rtc/DeviceCompilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ class HashPreprocessedAction : public PreprocessorFrontendAction {

class SYCLToolchain {
SYCLToolchain() {
using namespace jit_compiler::resource;

for (size_t i = 0; i < NumToolchainFiles; ++i) {
auto [Path, Content] = ToolchainFiles[i];
resource_file RF = ToolchainFiles[i];
std::string_view Path{RF.Path.S, RF.Path.Size};
std::string_view Content{RF.Content.S, RF.Content.Size};
ToolchainFS->addFile(Path, 0, llvm::MemoryBuffer::getMemBuffer(Content));
}
}
Expand Down Expand Up @@ -196,12 +200,14 @@ class SYCLToolchain {
return std::move(Lib);
}

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

private:
clang::IgnoringDiagConsumer IgnoreDiag;
std::string ClangXXExe =
(jit_compiler::ToolchainPrefix + "/bin/clang++").str();
std::string_view Prefix{jit_compiler::resource::ToolchainPrefix.S,
jit_compiler::resource::ToolchainPrefix.Size};
std::string ClangXXExe = (Prefix + "/bin/clang++").str();
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> ToolchainFS =
llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
};
Expand Down Expand Up @@ -500,7 +506,7 @@ Error jit_compiler::linkDeviceLibraries(llvm::Module &Module,
LLVMContext &Context = Module.getContext();
for (const std::string &LibName : LibNames) {
std::string LibPath =
(jit_compiler::ToolchainPrefix + "/lib/" + LibName).str();
(SYCLToolchain::instance().getPrefix() + "/lib/" + LibName).str();

ModuleUPtr LibModule;
if (auto Error = SYCLToolchain::instance()
Expand All @@ -519,7 +525,7 @@ Error jit_compiler::linkDeviceLibraries(llvm::Module &Module,
// For GPU targets we need to link against vendor provided libdevice.
if (IsCudaHIP) {
Triple T{Module.getTargetTriple()};
Driver D{(jit_compiler::ToolchainPrefix + "/bin/clang++").str(),
Driver D{(SYCLToolchain::instance().getPrefix() + "/bin/clang++").str(),
T.getTriple(), Diags};
auto [CPU, Features] =
Translator::getTargetCPUAndFeatureAttrs(&Module, "", Format);
Expand Down Expand Up @@ -765,7 +771,7 @@ jit_compiler::performPostLink(ModuleUPtr Module,
auto &Ctx = Modules.front()->getContext();
auto WrapLibraryInDevImg = [&](const std::string &LibName) -> Error {
std::string LibPath =
(jit_compiler::ToolchainPrefix + "/lib/" + LibName).str();
(SYCLToolchain::instance().getPrefix() + "/lib/" + LibName).str();
ModuleUPtr LibModule;
if (auto Error = SYCLToolchain::instance()
.loadBitcodeLibrary(LibPath, Ctx)
Expand Down
12 changes: 6 additions & 6 deletions sycl-jit/jit-compiler/utils/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def main():
"""
#include <Resource.h>

namespace jit_compiler {
const std::pair<std::string_view, std::string_view> ToolchainFiles[] = {"""
namespace jit_compiler::resource {
const resource_file ToolchainFiles[] = {"""
)

def process_file(file_path):
Expand All @@ -41,7 +41,7 @@ def process_file(file_path):
static const char data[] = {{
#embed "{file_path}" if_empty(0)
, 0}};
return std::string_view(data, std::size(data) - 1);
return resource_string_view{{data}};
}}()
}},"""
)
Expand All @@ -66,9 +66,9 @@ def process_dir(dir):
f"""
}};

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

Expand Down