|
| 1 | +//==---------------------- DeviceCompilation.cpp ---------------------------==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "DeviceCompilation.h" |
| 10 | + |
| 11 | +#include <clang/Basic/Version.h> |
| 12 | +#include <clang/CodeGen/CodeGenAction.h> |
| 13 | +#include <clang/Driver/Compilation.h> |
| 14 | +#include <clang/Frontend/CompilerInstance.h> |
| 15 | +#include <clang/Tooling/CompilationDatabase.h> |
| 16 | +#include <clang/Tooling/Tooling.h> |
| 17 | + |
| 18 | +#ifdef _GNU_SOURCE |
| 19 | +#include <dlfcn.h> |
| 20 | +static char X; // Dummy symbol, used as an anchor for `dlinfo` below. |
| 21 | +#endif |
| 22 | + |
| 23 | +static constexpr auto InvalidDPCPPRoot = "<invalid>"; |
| 24 | +static constexpr auto JITLibraryPathSuffix = "/lib/libsycl-jit.so"; |
| 25 | + |
| 26 | +static const std::string &getDPCPPRoot() { |
| 27 | + thread_local std::string DPCPPRoot; |
| 28 | + |
| 29 | + if (!DPCPPRoot.empty()) { |
| 30 | + return DPCPPRoot; |
| 31 | + } |
| 32 | + DPCPPRoot = InvalidDPCPPRoot; |
| 33 | + |
| 34 | +#ifdef _GNU_SOURCE |
| 35 | + Dl_info Info; |
| 36 | + if (dladdr(&X, &Info)) { |
| 37 | + std::string LoadedLibraryPath = Info.dli_fname; |
| 38 | + auto Pos = LoadedLibraryPath.rfind(JITLibraryPathSuffix); |
| 39 | + if (Pos != std::string::npos) { |
| 40 | + DPCPPRoot = LoadedLibraryPath.substr(0, Pos); |
| 41 | + } |
| 42 | + } |
| 43 | +#endif // _GNU_SOURCE |
| 44 | + |
| 45 | + // TODO: Implemenent other means of determining the DPCPP root, e.g. |
| 46 | + // evaluating the `CMPLR_ROOT` env. |
| 47 | + |
| 48 | + return DPCPPRoot; |
| 49 | +} |
| 50 | + |
| 51 | +namespace { |
| 52 | +using namespace clang; |
| 53 | +using namespace clang::tooling; |
| 54 | +using namespace clang::driver; |
| 55 | + |
| 56 | +struct GetLLVMModuleAction : public ToolAction { |
| 57 | + // Code adapted from `FrontendActionFactory::runInvocation`. |
| 58 | + bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation, |
| 59 | + FileManager *Files, |
| 60 | + std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
| 61 | + DiagnosticConsumer *DiagConsumer) override { |
| 62 | + assert(!Module && "Action should only be invoked on a single file"); |
| 63 | + |
| 64 | + // Create a compiler instance to handle the actual work. |
| 65 | + CompilerInstance Compiler(std::move(PCHContainerOps)); |
| 66 | + Compiler.setInvocation(std::move(Invocation)); |
| 67 | + Compiler.setFileManager(Files); |
| 68 | + |
| 69 | + // Create the compiler's actual diagnostics engine. |
| 70 | + Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false); |
| 71 | + if (!Compiler.hasDiagnostics()) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + Compiler.createSourceManager(*Files); |
| 76 | + |
| 77 | + // Ignore `Compiler.getFrontendOpts().ProgramAction` (would be `EmitBC`) and |
| 78 | + // create/execute an `EmitLLVMOnlyAction` (= codegen to LLVM module without |
| 79 | + // emitting anything) instead. |
| 80 | + EmitLLVMOnlyAction ELOA; |
| 81 | + const bool Success = Compiler.ExecuteAction(ELOA); |
| 82 | + Files->clearStatCache(); |
| 83 | + if (!Success) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + // Take the module and its context to extend the objects' lifetime. |
| 88 | + Module = ELOA.takeModule(); |
| 89 | + ELOA.takeLLVMContext(); |
| 90 | + |
| 91 | + return true; |
| 92 | + } |
| 93 | + |
| 94 | + std::unique_ptr<llvm::Module> Module; |
| 95 | +}; |
| 96 | + |
| 97 | +} // anonymous namespace |
| 98 | + |
| 99 | +llvm::Expected<std::unique_ptr<llvm::Module>> |
| 100 | +jit_compiler::compileDeviceCode(InMemoryFile SourceFile, |
| 101 | + View<InMemoryFile> IncludeFiles, |
| 102 | + View<const char *> UserArgs) { |
| 103 | + const std::string &DPCPPRoot = getDPCPPRoot(); |
| 104 | + if (DPCPPRoot == InvalidDPCPPRoot) { |
| 105 | + return llvm::createStringError("Could not locate DPCPP root directory"); |
| 106 | + } |
| 107 | + |
| 108 | + SmallVector<std::string> CommandLine = {"-fsycl-device-only"}; |
| 109 | + // TODO: Allow instrumentation again when device library linking is |
| 110 | + // implemented. |
| 111 | + CommandLine.push_back("-fno-sycl-instrument-device-code"); |
| 112 | + CommandLine.append(UserArgs.begin(), UserArgs.end()); |
| 113 | + clang::tooling::FixedCompilationDatabase DB{".", CommandLine}; |
| 114 | + |
| 115 | + clang::tooling::ClangTool Tool{DB, {SourceFile.Path}}; |
| 116 | + |
| 117 | + // Set up in-memory filesystem. |
| 118 | + Tool.mapVirtualFile(SourceFile.Path, SourceFile.Contents); |
| 119 | + for (const auto &IF : IncludeFiles) { |
| 120 | + Tool.mapVirtualFile(IF.Path, IF.Contents); |
| 121 | + } |
| 122 | + |
| 123 | + // Reset argument adjusters to drop the `-fsyntax-only` flag which is added by |
| 124 | + // default by this API. |
| 125 | + Tool.clearArgumentsAdjusters(); |
| 126 | + // Then, modify argv[0] and set the resource directory so that the driver |
| 127 | + // picks up the correct SYCL environment. |
| 128 | + Tool.appendArgumentsAdjuster( |
| 129 | + [&DPCPPRoot](const CommandLineArguments &Args, |
| 130 | + StringRef Filename) -> CommandLineArguments { |
| 131 | + (void)Filename; |
| 132 | + CommandLineArguments NewArgs = Args; |
| 133 | + NewArgs[0] = (Twine(DPCPPRoot) + "/bin/clang++").str(); |
| 134 | + NewArgs.push_back((Twine("-resource-dir=") + DPCPPRoot + "/lib/clang/" + |
| 135 | + Twine(CLANG_VERSION_MAJOR)) |
| 136 | + .str()); |
| 137 | + return NewArgs; |
| 138 | + }); |
| 139 | + |
| 140 | + GetLLVMModuleAction Action; |
| 141 | + if (!Tool.run(&Action)) { |
| 142 | + return std::move(Action.Module); |
| 143 | + } |
| 144 | + |
| 145 | + // TODO: Capture compiler errors from the ClangTool. |
| 146 | + return llvm::createStringError("Unable to obtain LLVM module"); |
| 147 | +} |
0 commit comments