5151#include < array>
5252#include < sstream>
5353
54+ namespace jit_compiler {
55+ // Defined in the auto-generated file:
56+ extern const std::pair<std::string_view, std::string_view> ToolchainFiles[];
57+ extern size_t NumToolchainFiles;
58+ extern std::string_view ToolchainPrefix;
59+ } // namespace jit_compiler
60+
5461using namespace clang ;
5562using namespace clang ::tooling;
5663using namespace clang ::driver;
@@ -178,7 +185,12 @@ class HashPreprocessedAction : public PreprocessorFrontendAction {
178185};
179186
180187class SYCLToolchain {
181- SYCLToolchain () {}
188+ SYCLToolchain () {
189+ for (size_t i = 0 ; i < NumToolchainFiles; ++i) {
190+ auto [Path, Content] = ToolchainFiles[i];
191+ ToolchainFS->addFile (Path, 0 , llvm::MemoryBuffer::getMemBuffer (Content));
192+ }
193+ }
182194
183195 // Similar to FrontendActionFactory, but we don't take ownership of
184196 // `FrontendAction`, nor do we create copies of it as we only perform a single
@@ -231,6 +243,7 @@ class SYCLToolchain {
231243 DiagnosticConsumer *DiagConsumer = nullptr ) {
232244 auto FS = llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(
233245 llvm::vfs::getRealFileSystem ());
246+ FS->pushOverlay (ToolchainFS);
234247 if (FSOverlay)
235248 FS->pushOverlay (FSOverlay);
236249
@@ -245,8 +258,14 @@ class SYCLToolchain {
245258 return TI.run ();
246259 }
247260
261+ std::string_view getClangXXExe () const { return ClangXXExe; }
262+
248263private:
249264 clang::IgnoringDiagConsumer IgnoreDiag;
265+ std::string ClangXXExe =
266+ (jit_compiler::ToolchainPrefix + " /bin/clang++" ).str();
267+ llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> ToolchainFS =
268+ llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
250269};
251270
252271class ClangDiagnosticWrapper {
@@ -296,14 +315,11 @@ class LLVMDiagnosticWrapper : public llvm::DiagnosticHandler {
296315} // anonymous namespace
297316
298317static std::vector<std::string>
299- createCommandLine (const InputArgList &UserArgList, std::string_view DPCPPRoot ,
300- BinaryFormat Format, std::string_view SourceFilePath) {
318+ createCommandLine (const InputArgList &UserArgList, BinaryFormat Format ,
319+ std::string_view SourceFilePath) {
301320 DerivedArgList DAL{UserArgList};
302321 const auto &OptTable = getDriverOptTable ();
303322 DAL.AddFlagArg (nullptr , OptTable.getOption (OPT_fsycl_device_only));
304- DAL.AddJoinedArg (
305- nullptr , OptTable.getOption (OPT_resource_dir_EQ),
306- (DPCPPRoot + " /lib/clang/" + Twine (CLANG_VERSION_MAJOR)).str ());
307323 // User args may contain options not intended for the frontend, but we can't
308324 // claim them here to tell the driver they're used later. Hence, suppress the
309325 // unused argument warning.
@@ -327,7 +343,7 @@ createCommandLine(const InputArgList &UserArgList, std::string_view DPCPPRoot,
327343
328344 std::vector<std::string> CommandLine;
329345 CommandLine.reserve (ASL.size () + 2 );
330- CommandLine.emplace_back ((DPCPPRoot + " /bin/clang++ " ). str ());
346+ CommandLine.emplace_back (SYCLToolchain::instance (). getClangXXExe ());
331347 transform (ASL, std::back_inserter (CommandLine),
332348 [](const char *AS) { return std::string{AS}; });
333349 CommandLine.emplace_back (SourceFilePath);
@@ -355,13 +371,8 @@ Expected<std::string> jit_compiler::calculateHash(
355371 const InputArgList &UserArgList, BinaryFormat Format) {
356372 TimeTraceScope TTS{" calculateHash" };
357373
358- const std::string &DPCPPRoot = getDPCPPRoot ();
359- if (DPCPPRoot == InvalidDPCPPRoot) {
360- return createStringError (" Could not locate DPCPP root directory" );
361- }
362-
363374 std::vector<std::string> CommandLine =
364- createCommandLine (UserArgList, DPCPPRoot, Format, SourceFile.Path );
375+ createCommandLine (UserArgList, Format, SourceFile.Path );
365376
366377 HashPreprocessedAction HashAction;
367378
@@ -372,8 +383,7 @@ Expected<std::string> jit_compiler::calculateHash(
372383 // unique for each query, so drop it:
373384 CommandLine.pop_back ();
374385
375- // The command line contains the DPCPP root and clang major version in
376- // "-resource-dir=<...>" argument.
386+ // TODO: Include hash of the current libsycl-jit.so/.dll somehow...
377387 BLAKE3Result<> CommandLineHash =
378388 BLAKE3::hash (arrayRefFromStringRef (join (CommandLine, " ," )));
379389
@@ -394,18 +404,13 @@ Expected<ModuleUPtr> jit_compiler::compileDeviceCode(
394404 LLVMContext &Context, BinaryFormat Format) {
395405 TimeTraceScope TTS{" compileDeviceCode" };
396406
397- const std::string &DPCPPRoot = getDPCPPRoot ();
398- if (DPCPPRoot == InvalidDPCPPRoot) {
399- return createStringError (" Could not locate DPCPP root directory" );
400- }
401-
402407 EmitLLVMOnlyAction ELOA{&Context};
403408 DiagnosticOptions DiagOpts;
404409 ClangDiagnosticWrapper Wrapper (BuildLog, &DiagOpts);
405410
406411 if (SYCLToolchain::instance ().run (
407- createCommandLine (UserArgList, DPCPPRoot, Format, SourceFile.Path ),
408- ELOA, getInMemoryFS (SourceFile, IncludeFiles), Wrapper.consumer ())) {
412+ createCommandLine (UserArgList, Format, SourceFile.Path ), ELOA ,
413+ getInMemoryFS (SourceFile, IncludeFiles), Wrapper.consumer ())) {
409414 return ELOA.takeModule ();
410415 } else {
411416 return createStringError (BuildLog);
0 commit comments