|
| 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 | +#include <llvm/IR/Module.h> |
| 19 | + |
| 20 | +namespace { |
| 21 | +using namespace clang; |
| 22 | +using namespace clang::tooling; |
| 23 | +using namespace clang::driver; |
| 24 | + |
| 25 | +struct GetLLVMModuleAction : public ToolAction { |
| 26 | + // Code adapted from `FrontendActionFactory::runInvocation`. |
| 27 | + bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation, |
| 28 | + FileManager *Files, |
| 29 | + std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
| 30 | + DiagnosticConsumer *DiagConsumer) override { |
| 31 | + assert(!Module && "Action should only be invoked on a single file"); |
| 32 | + |
| 33 | + // Create a compiler instance to handle the actual work. |
| 34 | + CompilerInstance Compiler(std::move(PCHContainerOps)); |
| 35 | + Compiler.setInvocation(std::move(Invocation)); |
| 36 | + Compiler.setFileManager(Files); |
| 37 | + |
| 38 | + // Create the compiler's actual diagnostics engine. |
| 39 | + Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false); |
| 40 | + if (!Compiler.hasDiagnostics()) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + Compiler.createSourceManager(*Files); |
| 45 | + |
| 46 | + // Ignore `Compiler.getFrontendOpts().ProgramAction` (would be `EmitBC`) and |
| 47 | + // create/execute an `EmitLLVMOnlyAction` (= codegen to LLVM module without |
| 48 | + // emitting anything) instead. |
| 49 | + EmitLLVMOnlyAction ELOA; |
| 50 | + const bool Success = Compiler.ExecuteAction(ELOA); |
| 51 | + Files->clearStatCache(); |
| 52 | + if (!Success) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + // Take the module and its context to extend the objects' lifetime. |
| 57 | + Module = ELOA.takeModule(); |
| 58 | + ELOA.takeLLVMContext(); |
| 59 | + |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + std::unique_ptr<llvm::Module> Module; |
| 64 | +}; |
| 65 | + |
| 66 | +} // anonymous namespace |
| 67 | + |
| 68 | +std::unique_ptr<llvm::Module> jit_compiler::compileDeviceCode( |
| 69 | + const char *SYCLSource, View<IncludePair> IncludePairs, |
| 70 | + View<const char *> UserArgs, const char *DPCPPRoot) { |
| 71 | + |
| 72 | + SmallVector<std::string> CommandLine = {"-fsycl-device-only"}; |
| 73 | + // TODO: Allow instrumentation again when device library linking is |
| 74 | + // implemented. |
| 75 | + CommandLine.push_back("-fno-sycl-instrument-device-code"); |
| 76 | + CommandLine.append(UserArgs.begin(), UserArgs.end()); |
| 77 | + clang::tooling::FixedCompilationDatabase DB{"./", CommandLine}; |
| 78 | + |
| 79 | + constexpr auto SourcePath = "rtc.cpp"; |
| 80 | + clang::tooling::ClangTool Tool{DB, {SourcePath}}; |
| 81 | + |
| 82 | + // Set up in-memory filesystem. |
| 83 | + Tool.mapVirtualFile(SourcePath, SYCLSource); |
| 84 | + for (const auto &IP : IncludePairs) { |
| 85 | + Tool.mapVirtualFile(IP.Path, IP.Contents); |
| 86 | + } |
| 87 | + |
| 88 | + // Reset argument adjusters to drop the `-fsyntax-only` flag which is added by |
| 89 | + // default by this API. |
| 90 | + Tool.clearArgumentsAdjusters(); |
| 91 | + // Then, modify argv[0] and set the resource directory so that the driver |
| 92 | + // picks up the correct SYCL environment. |
| 93 | + Tool.appendArgumentsAdjuster( |
| 94 | + [&DPCPPRoot](const CommandLineArguments &Args, |
| 95 | + StringRef Filename) -> CommandLineArguments { |
| 96 | + (void)Filename; |
| 97 | + CommandLineArguments NewArgs = Args; |
| 98 | + NewArgs[0] = (Twine(DPCPPRoot) + "/bin/clang++").str(); |
| 99 | + NewArgs.push_back((Twine("-resource-dir=") + DPCPPRoot + "/lib/clang/" + |
| 100 | + Twine(CLANG_VERSION_MAJOR)) |
| 101 | + .str()); |
| 102 | + return NewArgs; |
| 103 | + }); |
| 104 | + |
| 105 | + GetLLVMModuleAction Action; |
| 106 | + if (!Tool.run(&Action)) { |
| 107 | + return std::move(Action.Module); |
| 108 | + } |
| 109 | + |
| 110 | + return {}; |
| 111 | +} |
0 commit comments