Skip to content

Commit c5d9aca

Browse files
[SYCL RTC] Make resource.cpp independent of any includes (#20115)
The rest of the project can use flags like `-stdlib=<...>` or `--sysroot` that might affect ABI. Rather than trying to propagate those through our fragile CMake hacks to compile that file with freshly built `clang`, make sure that the file is as self-sufficient as possible (i.e, no STL/libc usage) as that seems to be a more stable solution.
1 parent 5e7bf28 commit c5d9aca

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

sycl-jit/jit-compiler/include/Resource.h

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,29 @@
88

99
#pragma once
1010

11-
#include <iterator>
12-
#include <string_view>
13-
#include <utility>
14-
15-
namespace jit_compiler {
11+
namespace jit_compiler::resource {
12+
// `resource.cpp` is compiled using freshly built clang and it's very hard to
13+
// sync compilation options between that and normal compilation for other files.
14+
// Note that some of the options might affect ABI (e.g., libstdc++ vs. libc++
15+
// usage, or custom sysroot/gcc installation directory). A much easier approach
16+
// is to ensure that `resource.cpp` doesn't have any includes at all, hence
17+
// these helpers:
18+
template <class T, unsigned long long N>
19+
constexpr unsigned long long size(const T (&)[N]) noexcept {
20+
return N;
21+
}
22+
struct resource_string_view {
23+
template <unsigned long long N>
24+
resource_string_view(const char (&S)[N]) : S(S), Size(N - 1) {}
25+
const char *S;
26+
unsigned long long Size;
27+
};
28+
struct resource_file {
29+
resource_string_view Path;
30+
resource_string_view Content;
31+
};
1632
// Defined in the auto-generated file:
17-
extern const std::pair<std::string_view, std::string_view> ToolchainFiles[];
18-
extern size_t NumToolchainFiles;
19-
extern std::string_view ToolchainPrefix;
20-
} // namespace jit_compiler
33+
extern const resource_file ToolchainFiles[];
34+
extern unsigned long long NumToolchainFiles;
35+
extern resource_string_view ToolchainPrefix;
36+
} // namespace jit_compiler::resource

sycl-jit/jit-compiler/lib/rtc/DeviceCompilation.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,12 @@ class HashPreprocessedAction : public PreprocessorFrontendAction {
100100

101101
class SYCLToolchain {
102102
SYCLToolchain() {
103+
using namespace jit_compiler::resource;
104+
103105
for (size_t i = 0; i < NumToolchainFiles; ++i) {
104-
auto [Path, Content] = ToolchainFiles[i];
106+
resource_file RF = ToolchainFiles[i];
107+
std::string_view Path{RF.Path.S, RF.Path.Size};
108+
std::string_view Content{RF.Content.S, RF.Content.Size};
105109
ToolchainFS->addFile(Path, 0, llvm::MemoryBuffer::getMemBuffer(Content));
106110
}
107111
}
@@ -196,12 +200,14 @@ class SYCLToolchain {
196200
return std::move(Lib);
197201
}
198202

203+
std::string_view getPrefix() const { return Prefix; }
199204
std::string_view getClangXXExe() const { return ClangXXExe; }
200205

201206
private:
202207
clang::IgnoringDiagConsumer IgnoreDiag;
203-
std::string ClangXXExe =
204-
(jit_compiler::ToolchainPrefix + "/bin/clang++").str();
208+
std::string_view Prefix{jit_compiler::resource::ToolchainPrefix.S,
209+
jit_compiler::resource::ToolchainPrefix.Size};
210+
std::string ClangXXExe = (Prefix + "/bin/clang++").str();
205211
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> ToolchainFS =
206212
llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
207213
};
@@ -500,7 +506,7 @@ Error jit_compiler::linkDeviceLibraries(llvm::Module &Module,
500506
LLVMContext &Context = Module.getContext();
501507
for (const std::string &LibName : LibNames) {
502508
std::string LibPath =
503-
(jit_compiler::ToolchainPrefix + "/lib/" + LibName).str();
509+
(SYCLToolchain::instance().getPrefix() + "/lib/" + LibName).str();
504510

505511
ModuleUPtr LibModule;
506512
if (auto Error = SYCLToolchain::instance()
@@ -519,7 +525,7 @@ Error jit_compiler::linkDeviceLibraries(llvm::Module &Module,
519525
// For GPU targets we need to link against vendor provided libdevice.
520526
if (IsCudaHIP) {
521527
Triple T{Module.getTargetTriple()};
522-
Driver D{(jit_compiler::ToolchainPrefix + "/bin/clang++").str(),
528+
Driver D{(SYCLToolchain::instance().getPrefix() + "/bin/clang++").str(),
523529
T.getTriple(), Diags};
524530
auto [CPU, Features] =
525531
Translator::getTargetCPUAndFeatureAttrs(&Module, "", Format);
@@ -765,7 +771,7 @@ jit_compiler::performPostLink(ModuleUPtr Module,
765771
auto &Ctx = Modules.front()->getContext();
766772
auto WrapLibraryInDevImg = [&](const std::string &LibName) -> Error {
767773
std::string LibPath =
768-
(jit_compiler::ToolchainPrefix + "/lib/" + LibName).str();
774+
(SYCLToolchain::instance().getPrefix() + "/lib/" + LibName).str();
769775
ModuleUPtr LibModule;
770776
if (auto Error = SYCLToolchain::instance()
771777
.loadBitcodeLibrary(LibPath, Ctx)

sycl-jit/jit-compiler/utils/generate.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def main():
2828
"""
2929
#include <Resource.h>
3030
31-
namespace jit_compiler {
32-
const std::pair<std::string_view, std::string_view> ToolchainFiles[] = {"""
31+
namespace jit_compiler::resource {
32+
const resource_file ToolchainFiles[] = {"""
3333
)
3434

3535
def process_file(file_path):
@@ -41,7 +41,7 @@ def process_file(file_path):
4141
static const char data[] = {{
4242
#embed "{file_path}" if_empty(0)
4343
, 0}};
44-
return std::string_view(data, std::size(data) - 1);
44+
return resource_string_view{{data}};
4545
}}()
4646
}},"""
4747
)
@@ -66,9 +66,9 @@ def process_dir(dir):
6666
f"""
6767
}};
6868
69-
size_t NumToolchainFiles = std::size(ToolchainFiles);
70-
std::string_view ToolchainPrefix = "{args.prefix}";
71-
}} // namespace jit_compiler
69+
unsigned long long NumToolchainFiles = size(ToolchainFiles);
70+
resource_string_view ToolchainPrefix{{"{args.prefix}"}};
71+
}} // namespace jit_compiler::resource
7272
"""
7373
)
7474

0 commit comments

Comments
 (0)