1//===------ Interpreter.cpp - Incremental Compilation and Execution -------===//
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// This file implements the component which performs incremental code
10// compilation and execution.
11//
12//===----------------------------------------------------------------------===//
13
14#include "DeviceOffload.h"
15#include "IncrementalAction.h"
16#include "IncrementalParser.h"
17#include "InterpreterUtils.h"
18
19#include "clang/AST/ASTConsumer.h"
20#include "clang/AST/ASTContext.h"
21#include "clang/AST/Mangle.h"
22#include "clang/AST/TypeVisitor.h"
23#include "clang/Basic/DiagnosticSema.h"
24#include "clang/Basic/FileManager.h"
25#include "clang/Basic/TargetInfo.h"
26#include "clang/CodeGen/CodeGenAction.h"
27#include "clang/CodeGen/ObjectFilePCHContainerWriter.h"
28#include "clang/Driver/Compilation.h"
29#include "clang/Driver/Driver.h"
30#include "clang/Driver/Job.h"
31#include "clang/Driver/Tool.h"
32#include "clang/Frontend/CompilerInstance.h"
33#include "clang/Frontend/FrontendAction.h"
34#include "clang/Frontend/FrontendOptions.h"
35#include "clang/Frontend/MultiplexConsumer.h"
36#include "clang/Frontend/TextDiagnosticBuffer.h"
37#include "clang/FrontendTool/Utils.h"
38#include "clang/Interpreter/IncrementalExecutor.h"
39#include "clang/Interpreter/Interpreter.h"
40#include "clang/Interpreter/Value.h"
41#include "clang/Lex/PreprocessorOptions.h"
42#include "clang/Options/OptionUtils.h"
43#include "clang/Options/Options.h"
44#include "clang/Sema/Lookup.h"
45#include "clang/Serialization/ASTReader.h"
46#include "clang/Serialization/ModuleCache.h"
47#include "clang/Serialization/ObjectFilePCHContainerReader.h"
48#include "llvm/ExecutionEngine/JITSymbol.h"
49#include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
50#include "llvm/ExecutionEngine/Orc/LLJIT.h"
51#include "llvm/IR/Module.h"
52#include "llvm/Support/Errc.h"
53#include "llvm/Support/ErrorHandling.h"
54#include "llvm/Support/VirtualFileSystem.h"
55#include "llvm/Support/raw_ostream.h"
56#include "llvm/TargetParser/Host.h"
57#include "llvm/TargetParser/Triple.h"
58#include "llvm/Transforms/Utils/Cloning.h" // for CloneModule
59
60#define DEBUG_TYPE "clang-repl"
61
62using namespace clang;
63// FIXME: Figure out how to unify with namespace init_convenience from
64// tools/clang-import-test/clang-import-test.cpp
65namespace {
66/// Retrieves the clang CC1 specific flags out of the compilation's jobs.
67/// \returns NULL on error.
68static llvm::Expected<const llvm::opt::ArgStringList *>
69GetCC1Arguments(DiagnosticsEngine *Diagnostics,
70 driver::Compilation *Compilation) {
71 // We expect to get back exactly one Command job, if we didn't something
72 // failed. Extract that job from the Compilation.
73 const driver::JobList &Jobs = Compilation->getJobs();
74 if (!Jobs.size())
75 return llvm::createStringError(EC: llvm::errc::not_supported,
76 S: "Driver initialization failed. "
77 "Unable to create a driver job");
78
79 // The one job we find should be to invoke clang again.
80 const driver::Command *Cmd = &*Jobs.begin();
81 if (llvm::StringRef(Cmd->getCreator().getName()) != "clang")
82 return llvm::createStringError(EC: llvm::errc::not_supported,
83 S: "Driver initialization failed");
84
85 return &Cmd->getArguments();
86}
87
88// ASTReaderListener that captures the PIC level stored in a serialized AST
89// file (PCH or PCM) so the interpreter can compare it against its own.
90class PICLevelReader : public ASTReaderListener {
91 unsigned &PICLevel;
92
93public:
94 PICLevelReader(unsigned &PICLevel) : PICLevel(PICLevel) {}
95
96 bool ReadLanguageOptions(const LangOptions &LangOpts,
97 StringRef ModuleFilename, bool Complain,
98 bool AllowCompatibleDifferences) override {
99 PICLevel = LangOpts.PICLevel;
100 return false;
101 }
102};
103
104// clang-repl always compiles position-independent code (it injects -fPIC), so a
105// PCH/PCM that was built with a different PIC level is incompatible: mixing the
106// two leads to relocations that may be out of range once the JIT maps code more
107// than 2GB away. PICLevel is a "compatible" language option, so the ASTReader
108// would otherwise accept the mismatch silently. Reject it up front.
109//
110// The file is probed with its own FileManager and ModuleCache (sharing only the
111// VFS) so this read leaves the CompilerInstance's state untouched for the real
112// load performed later by ExecuteAction().
113static llvm::Error checkASTFilePICLevel(CompilerInstance &Clang,
114 StringRef Filename) {
115 llvm::IntrusiveRefCntPtr<FileManager> FileMgr(new FileManager(
116 Clang.getFileSystemOpts(), Clang.getVirtualFileSystemPtr()));
117 std::shared_ptr<ModuleCache> ModCache = createCrossProcessModuleCache();
118 unsigned ASTPICLevel = 0;
119 PICLevelReader Reader(ASTPICLevel);
120 if (!ASTReader::readASTFileControlBlock(
121 Filename, FileMgr&: *FileMgr, ModCache: *ModCache, PCHContainerRdr: Clang.getPCHContainerReader(),
122 /*FindModuleFileExtensions=*/false, Listener&: Reader,
123 /*ValidateDiagnosticOptions=*/false) &&
124 ASTPICLevel != Clang.getLangOpts().PICLevel)
125 return llvm::createStringError(
126 EC: llvm::errc::not_supported,
127 Fmt: "AST file '%s' was built with PIC level %u, which is incompatible "
128 "with clang-repl's PIC level %u",
129 Vals: Filename.str().c_str(), Vals: ASTPICLevel, Vals: Clang.getLangOpts().PICLevel);
130 return llvm::Error::success();
131}
132
133static llvm::Expected<std::unique_ptr<CompilerInstance>>
134CreateCI(const llvm::opt::ArgStringList &Argv) {
135 std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
136
137 // Register the support for object-file-wrapped Clang modules.
138 // FIXME: Clang should register these container operations automatically.
139 auto PCHOps = Clang->getPCHContainerOperations();
140 PCHOps->registerWriter(Writer: std::make_unique<ObjectFilePCHContainerWriter>());
141 PCHOps->registerReader(Reader: std::make_unique<ObjectFilePCHContainerReader>());
142
143 // Buffer diagnostics from argument parsing so that we can output them using
144 // a well formed diagnostic object.
145 DiagnosticOptions DiagOpts;
146 TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
147 DiagnosticsEngine Diags(DiagnosticIDs::create(), DiagOpts, DiagsBuffer);
148 bool Success = CompilerInvocation::CreateFromArgs(
149 Res&: Clang->getInvocation(), CommandLineArgs: llvm::ArrayRef(Argv.begin(), Argv.size()), Diags);
150
151 // Infer the builtin include path if unspecified.
152 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
153 Clang->getHeaderSearchOpts().ResourceDir.empty())
154 Clang->getHeaderSearchOpts().ResourceDir =
155 GetResourcesPath(Argv0: Argv[0], MainAddr: nullptr);
156
157 Clang->createVirtualFileSystem();
158
159 // Create the actual diagnostics engine.
160 Clang->createDiagnostics();
161
162 DiagsBuffer->FlushDiagnostics(Diags&: Clang->getDiagnostics());
163 if (!Success)
164 return llvm::createStringError(EC: llvm::errc::not_supported,
165 S: "Initialization failed. "
166 "Unable to flush diagnostics");
167
168 // FIXME: Merge with CompilerInstance::ExecuteAction.
169 llvm::MemoryBuffer *MB = llvm::MemoryBuffer::getMemBuffer(InputData: "").release();
170 Clang->getPreprocessorOpts().addRemappedFile(From: "<<< inputs >>>", To: MB);
171
172 Clang->setTarget(TargetInfo::CreateTargetInfo(
173 Diags&: Clang->getDiagnostics(), Opts&: Clang->getInvocation().getTargetOpts()));
174 if (!Clang->hasTarget())
175 return llvm::createStringError(EC: llvm::errc::not_supported,
176 S: "Initialization failed. "
177 "Target is missing");
178
179 Clang->getTarget().adjust(Diags&: Clang->getDiagnostics(), Opts&: Clang->getLangOpts(),
180 Aux: Clang->getAuxTarget());
181
182 // Don't clear the AST before backend codegen since we do codegen multiple
183 // times, reusing the same AST.
184 Clang->getCodeGenOpts().ClearASTBeforeBackend = false;
185
186 Clang->getFrontendOpts().DisableFree = false;
187 Clang->getCodeGenOpts().DisableFree = false;
188
189 // Reject any precompiled input (PCH or PCM) built with a PIC level that
190 // differs from clang-repl's own, before any Interpreter/FrontendAction is
191 // constructed. See checkASTFilePICLevel for the rationale.
192 StringRef PCHInclude = Clang->getPreprocessorOpts().ImplicitPCHInclude;
193 if (!PCHInclude.empty())
194 if (llvm::Error Err = checkASTFilePICLevel(Clang&: *Clang, Filename: PCHInclude))
195 return std::move(Err);
196
197 // Explicitly loaded modules: -fmodule-file=<path> and
198 // -fmodule-file=<name>=<path>.
199 for (StringRef ModuleFile : Clang->getFrontendOpts().ModuleFiles)
200 if (llvm::Error Err = checkASTFilePICLevel(Clang&: *Clang, Filename: ModuleFile))
201 return std::move(Err);
202 for (const auto &NameAndFile :
203 Clang->getHeaderSearchOpts().PrebuiltModuleFiles)
204 if (llvm::Error Err = checkASTFilePICLevel(Clang&: *Clang, Filename: NameAndFile.second))
205 return std::move(Err);
206
207 return std::move(Clang);
208}
209
210static llvm::Error ExecuteIncrementalAction(CompilerInstance &CI,
211 IncrementalAction &Act) {
212 if (!CI.ExecuteAction(Act) || CI.getDiagnostics().hasErrorOccurred()) {
213 return llvm::createStringError(EC: llvm::errc::not_supported,
214 S: "Failed to execute incremental action");
215 }
216 return llvm::Error::success();
217}
218
219} // anonymous namespace
220
221namespace clang {
222
223llvm::Expected<std::unique_ptr<CompilerInstance>>
224IncrementalCompilerBuilder::create(std::string TT,
225 std::vector<const char *> &ClangArgv) {
226
227 // If we don't know ClangArgv0 or the address of main() at this point, try
228 // to guess it anyway (it's possible on some platforms).
229 std::string MainExecutableName =
230 llvm::sys::fs::getMainExecutable(argv0: nullptr, MainExecAddr: nullptr);
231
232 ClangArgv.insert(position: ClangArgv.begin(), x: MainExecutableName.c_str());
233
234 // Compile as position-independent code. This prevents the frontend from
235 // marking external symbols (e.g. C++ type-info such as _ZTIPKc used for
236 // exception handling) as dso_local and emitting direct PC-relative
237 // references. JITLink can place the GOT entry near the JIT'd code, keeping
238 // the relocation in range. Without -fPIC, a direct Delta32 relocation to a
239 // host symbol may be out of range when the JIT memory is mapped more than
240 // 2GB away (as on FreeBSD), breaking tests such as
241 // Interpreter/simple-exception.cpp. Insert before user arguments so it can
242 // still be overridden. On Windows (excluding Cygwin/MinGW) an explicit
243 // -fPIC is an unsupported driver option that would drop non-x86_64 targets
244 // to PIC level 0; PIC is already the forced default there where relevant,
245 // so don't inject it.
246 llvm::Triple TargetTriple(TT);
247 if (!TargetTriple.isOSWindows() || TargetTriple.isOSCygMing())
248 ClangArgv.insert(position: ClangArgv.begin() + 1, x: "-fPIC");
249
250 // Prepending -c to force the driver to do something if no action was
251 // specified. By prepending we allow users to override the default
252 // action and use other actions in incremental mode.
253 // FIXME: Print proper driver diagnostics if the driver flags are wrong.
254 // We do C++ by default; append right after argv[0] if no "-x" given
255 ClangArgv.insert(position: ClangArgv.end(), x: "-Xclang");
256 ClangArgv.insert(position: ClangArgv.end(), x: "-fincremental-extensions");
257 ClangArgv.insert(position: ClangArgv.end(), x: "-c");
258
259 // Put a dummy C++ file on to ensure there's at least one compile job for the
260 // driver to construct.
261 ClangArgv.push_back(x: "<<< inputs >>>");
262
263 // Buffer diagnostics from argument parsing so that we can output them using a
264 // well formed diagnostic object.
265 std::unique_ptr<DiagnosticOptions> DiagOpts =
266 CreateAndPopulateDiagOpts(Argv: ClangArgv);
267 TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
268 DiagnosticsEngine Diags(DiagnosticIDs::create(), *DiagOpts, DiagsBuffer);
269
270 driver::Driver Driver(/*MainBinaryName=*/ClangArgv[0], TT, Diags);
271 Driver.setCheckInputsExist(false); // the input comes from mem buffers
272 llvm::ArrayRef<const char *> RF = llvm::ArrayRef(ClangArgv);
273 std::unique_ptr<driver::Compilation> Compilation(Driver.BuildCompilation(Args: RF));
274
275 if (CompilationCB)
276 if (auto Err = (*CompilationCB)(*Compilation.get()))
277 return std::move(Err);
278
279 if (Compilation->getArgs().hasArg(Ids: options::OPT_v))
280 Compilation->getJobs().Print(OS&: llvm::errs(), Terminator: "\n", /*Quote=*/false);
281
282 auto ErrOrCC1Args = GetCC1Arguments(Diagnostics: &Diags, Compilation: Compilation.get());
283 if (auto Err = ErrOrCC1Args.takeError())
284 return std::move(Err);
285
286 return CreateCI(Argv: **ErrOrCC1Args);
287}
288
289llvm::Expected<std::unique_ptr<CompilerInstance>>
290IncrementalCompilerBuilder::CreateCpp() {
291 std::vector<const char *> Argv;
292 Argv.reserve(n: 5 + 1 + UserArgs.size());
293 Argv.push_back(x: "-xc++");
294#ifdef __EMSCRIPTEN__
295 Argv.push_back("-target");
296 Argv.push_back("wasm32-unknown-emscripten");
297 Argv.push_back("-fvisibility=default");
298#endif
299 llvm::append_range(C&: Argv, R&: UserArgs);
300
301 std::string TT = TargetTriple ? *TargetTriple : llvm::sys::getProcessTriple();
302 return IncrementalCompilerBuilder::create(TT, ClangArgv&: Argv);
303}
304
305llvm::Expected<std::unique_ptr<CompilerInstance>>
306IncrementalCompilerBuilder::createCuda(bool device) {
307 std::vector<const char *> Argv;
308 Argv.reserve(n: 5 + 4 + UserArgs.size());
309
310 Argv.push_back(x: "-xcuda");
311 if (device)
312 Argv.push_back(x: "--cuda-device-only");
313 else
314 Argv.push_back(x: "--cuda-host-only");
315
316 std::string SDKPathArg = "--cuda-path=";
317 if (!CudaSDKPath.empty()) {
318 SDKPathArg += CudaSDKPath;
319 Argv.push_back(x: SDKPathArg.c_str());
320 }
321
322 std::string ArchArg = "--offload-arch=";
323 if (!OffloadArch.empty()) {
324 ArchArg += OffloadArch;
325 Argv.push_back(x: ArchArg.c_str());
326 }
327
328 llvm::append_range(C&: Argv, R&: UserArgs);
329
330 std::string TT = TargetTriple ? *TargetTriple : llvm::sys::getProcessTriple();
331 return IncrementalCompilerBuilder::create(TT, ClangArgv&: Argv);
332}
333
334llvm::Expected<std::unique_ptr<CompilerInstance>>
335IncrementalCompilerBuilder::CreateCudaDevice() {
336 return IncrementalCompilerBuilder::createCuda(device: true);
337}
338
339llvm::Expected<std::unique_ptr<CompilerInstance>>
340IncrementalCompilerBuilder::CreateCudaHost() {
341 return IncrementalCompilerBuilder::createCuda(device: false);
342}
343
344Interpreter::Interpreter(std::unique_ptr<CompilerInstance> Instance,
345 llvm::Error &ErrOut,
346 std::unique_ptr<IncrementalExecutorBuilder> IEB,
347 std::unique_ptr<clang::ASTConsumer> Consumer)
348 : IncrExecutorBuilder(std::move(IEB)) {
349 CI = std::move(Instance);
350 llvm::ErrorAsOutParameter EAO(&ErrOut);
351 auto LLVMCtx = std::make_unique<llvm::LLVMContext>();
352 TSCtx = std::make_unique<llvm::orc::ThreadSafeContext>(args: std::move(LLVMCtx));
353
354 Act = TSCtx->withContextDo(F: [&](llvm::LLVMContext *Ctx) {
355 return std::make_unique<IncrementalAction>(args&: *CI, args&: *Ctx, args&: ErrOut, args&: *this,
356 args: std::move(Consumer));
357 });
358
359 if (ErrOut)
360 return;
361
362 if (llvm::Error E = ExecuteIncrementalAction(CI&: *CI, Act&: *Act)) {
363 ErrOut = joinErrors(E1: std::move(ErrOut), E2: std::move(E));
364 return;
365 }
366
367 IncrParser =
368 std::make_unique<IncrementalParser>(args&: *CI, args: Act.get(), args&: ErrOut, args&: PTUs);
369
370 if (ErrOut)
371 return;
372
373 if (Act->getCodeGen()) {
374 Act->CacheCodeGenModule();
375 // The initial PTU is filled by `-include`/`-include-pch` or by CUDA
376 // includes automatically.
377 if (!CI->getPreprocessorOpts().Includes.empty() ||
378 !CI->getPreprocessorOpts().ImplicitPCHInclude.empty()) {
379 // We can't really directly pass the CachedInCodeGenModule to the Jit
380 // because it will steal it, causing dangling references as explained in
381 // Interpreter::Execute
382 auto M = llvm::CloneModule(M: *Act->getCachedCodeGenModule());
383 ASTContext &C = CI->getASTContext();
384 IncrParser->RegisterPTU(TU: C.getTranslationUnitDecl(), M: std::move(M));
385 }
386 if (llvm::Error Err = CreateExecutor()) {
387 ErrOut = joinErrors(E1: std::move(ErrOut), E2: std::move(Err));
388 return;
389 }
390 }
391
392 // Not all frontends support code-generation, e.g. ast-dump actions don't
393 if (Act->getCodeGen()) {
394 // Process the PTUs that came from initialization. For example -include will
395 // give us a header that's processed at initialization of the preprocessor.
396 for (PartialTranslationUnit &PTU : PTUs)
397 if (llvm::Error Err = Execute(T&: PTU)) {
398 ErrOut = joinErrors(E1: std::move(ErrOut), E2: std::move(Err));
399 return;
400 }
401 }
402}
403
404Interpreter::~Interpreter() {
405 IncrParser.reset();
406 Act->FinalizeAction();
407 if (DeviceParser)
408 DeviceParser.reset();
409 if (DeviceAct)
410 DeviceAct->FinalizeAction();
411 if (IncrExecutor) {
412 if (llvm::Error Err = IncrExecutor->cleanUp())
413 llvm::report_fatal_error(
414 reason: llvm::Twine("Failed to clean up IncrementalExecutor: ") +
415 toString(E: std::move(Err)));
416 }
417}
418
419// These better to put in a runtime header but we can't. This is because we
420// can't find the precise resource directory in unittests so we have to hard
421// code them.
422const char *const Runtimes = R"(
423 #define __CLANG_REPL__ 1
424#ifdef __cplusplus
425 #define EXTERN_C extern "C"
426 struct __clang_Interpreter_NewTag{} __ci_newtag;
427 void* operator new(__SIZE_TYPE__, void* __p, __clang_Interpreter_NewTag) noexcept;
428 template <class T, class = T (*)() /*disable for arrays*/>
429 void __clang_Interpreter_SetValueCopyArr(const T* Src, void* Placement, unsigned long Size) {
430 for (unsigned long Idx = 0; Idx < Size; ++Idx)
431 new ((void*)(((T*)Placement) + Idx), __ci_newtag) T(Src[Idx]);
432 }
433 template <class T, unsigned long N>
434 void __clang_Interpreter_SetValueCopyArr(const T (*Src)[N], void* Placement, unsigned long Size) {
435 __clang_Interpreter_SetValueCopyArr(Src[0], Placement, Size);
436 }
437#else
438 #if __STDC_VERSION__ < 199901L
439 #define CI_RESTRICT
440 #define CI_INLINE
441 #else
442 #define CI_RESTRICT restrict
443 #define CI_INLINE inline
444 #endif
445 #define EXTERN_C extern
446 EXTERN_C void *memcpy(void *CI_RESTRICT dst, const void *CI_RESTRICT src, __SIZE_TYPE__ n);
447 EXTERN_C CI_INLINE void __clang_Interpreter_SetValueCopyArr(const void* Src, void* Placement, unsigned long Size) {
448 memcpy(Placement, Src, Size);
449 }
450#endif // __cplusplus
451 EXTERN_C void *__clang_Interpreter_SetValueWithAlloc(void*, void*, void*);
452 EXTERN_C void __clang_Interpreter_SetValueNoAlloc(void *This, void *OutVal, void *OpaqueType, ...);
453)";
454
455llvm::Expected<std::unique_ptr<Interpreter>> Interpreter::create(
456 std::unique_ptr<CompilerInstance> CI,
457 std::unique_ptr<IncrementalExecutorBuilder> IEB /*=nullptr*/) {
458 llvm::Error Err = llvm::Error::success();
459
460 auto Interp = std::unique_ptr<Interpreter>(new Interpreter(
461 std::move(CI), Err, std::move(IEB), /*Consumer=*/nullptr));
462 if (auto E = std::move(Err))
463 return std::move(E);
464
465 // Add runtime code and set a marker to hide it from user code. Undo will not
466 // go through that.
467 if (auto E = Interp->ParseAndExecute(Code: Runtimes))
468 return std::move(E);
469
470 Interp->markUserCodeStart();
471
472 return std::move(Interp);
473}
474
475llvm::Expected<std::unique_ptr<Interpreter>>
476Interpreter::createWithCUDA(std::unique_ptr<CompilerInstance> CI,
477 std::unique_ptr<CompilerInstance> DCI) {
478 // avoid writing fat binary to disk using an in-memory virtual file system
479 llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> IMVFS =
480 std::make_unique<llvm::vfs::InMemoryFileSystem>();
481 llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayVFS =
482 std::make_unique<llvm::vfs::OverlayFileSystem>(
483 args: llvm::vfs::getRealFileSystem());
484 OverlayVFS->pushOverlay(FS: IMVFS);
485 CI->createVirtualFileSystem(BaseFS: OverlayVFS);
486 CI->createFileManager();
487
488 llvm::Expected<std::unique_ptr<Interpreter>> InterpOrErr =
489 Interpreter::create(CI: std::move(CI));
490 if (!InterpOrErr)
491 return InterpOrErr;
492
493 std::unique_ptr<Interpreter> Interp = std::move(*InterpOrErr);
494
495 llvm::Error Err = llvm::Error::success();
496
497 auto DeviceAct = Interp->TSCtx->withContextDo(F: [&](llvm::LLVMContext *Ctx) {
498 return std::make_unique<IncrementalAction>(args&: *DCI, args&: *Ctx, args&: Err, args&: *Interp);
499 });
500
501 if (Err)
502 return std::move(Err);
503
504 Interp->DeviceAct = std::move(DeviceAct);
505
506 if (llvm::Error E = ExecuteIncrementalAction(CI&: *DCI, Act&: *Interp->DeviceAct))
507 return std::move(E);
508
509 Interp->DeviceCI = std::move(DCI);
510
511 auto DeviceParser = std::make_unique<IncrementalCUDADeviceParser>(
512 args&: *Interp->DeviceCI, args&: *Interp->getCompilerInstance(),
513 args: Interp->DeviceAct.get(), args&: IMVFS, args&: Err, args&: Interp->PTUs);
514
515 if (Err)
516 return std::move(Err);
517
518 Interp->DeviceParser = std::move(DeviceParser);
519 return std::move(Interp);
520}
521
522CompilerInstance *Interpreter::getCompilerInstance() { return CI.get(); }
523const CompilerInstance *Interpreter::getCompilerInstance() const {
524 return const_cast<Interpreter *>(this)->getCompilerInstance();
525}
526
527llvm::Expected<IncrementalExecutor &> Interpreter::getExecutionEngine() {
528 if (!IncrExecutor) {
529 if (auto Err = CreateExecutor())
530 return std::move(Err);
531 }
532
533 return *IncrExecutor.get();
534}
535
536ASTContext &Interpreter::getASTContext() {
537 return getCompilerInstance()->getASTContext();
538}
539
540const ASTContext &Interpreter::getASTContext() const {
541 return getCompilerInstance()->getASTContext();
542}
543
544void Interpreter::markUserCodeStart() {
545 assert(!InitPTUSize && "We only do this once");
546 InitPTUSize = PTUs.size();
547}
548
549size_t Interpreter::getEffectivePTUSize() const {
550 assert(PTUs.size() >= InitPTUSize && "empty PTU list?");
551 return PTUs.size() - InitPTUSize;
552}
553
554llvm::Expected<PartialTranslationUnit &>
555Interpreter::Parse(llvm::StringRef Code) {
556 // If we have a device parser, parse it first. The generated code will be
557 // included in the host compilation
558 if (DeviceParser) {
559 llvm::Expected<TranslationUnitDecl *> DeviceTU = DeviceParser->Parse(Input: Code);
560 if (auto E = DeviceTU.takeError())
561 return std::move(E);
562
563 DeviceParser->RegisterPTU(TU: *DeviceTU);
564
565 llvm::Expected<llvm::StringRef> PTX = DeviceParser->GeneratePTX();
566 if (!PTX)
567 return PTX.takeError();
568
569 llvm::Error Err = DeviceParser->GenerateFatbinary();
570 if (Err)
571 return std::move(Err);
572 }
573
574 // Tell the interpreter sliently ignore unused expressions since value
575 // printing could cause it.
576 getCompilerInstance()->getDiagnostics().setSeverity(
577 Diag: clang::diag::warn_unused_expr, Map: diag::Severity::Ignored, Loc: SourceLocation());
578
579 llvm::Expected<TranslationUnitDecl *> TuOrErr = IncrParser->Parse(Input: Code);
580 if (!TuOrErr)
581 return TuOrErr.takeError();
582
583 PartialTranslationUnit &LastPTU = IncrParser->RegisterPTU(TU: *TuOrErr);
584
585 // Under -emit-llvm, print the module IR.
586 if (InitPTUSize && LastPTU.TheModule &&
587 getCompilerInstance()->getFrontendOpts().ProgramAction ==
588 frontend::EmitLLVM)
589 LastPTU.TheModule->print(OS&: llvm::outs(), /*AAW=*/nullptr);
590
591 return LastPTU;
592}
593
594llvm::Error Interpreter::CreateExecutor() {
595 if (IncrExecutor)
596 return llvm::make_error<llvm::StringError>(Args: "Operation failed. "
597 "Execution engine exists",
598 Args: std::error_code());
599 if (!Act->getCodeGen())
600 return llvm::make_error<llvm::StringError>(Args: "Operation failed. "
601 "No code generator available",
602 Args: std::error_code());
603
604 if (!IncrExecutorBuilder)
605 IncrExecutorBuilder = std::make_unique<IncrementalExecutorBuilder>();
606
607 auto ExecutorOrErr = IncrExecutorBuilder->create(TSC&: *TSCtx, TI: CI->getTarget());
608 if (ExecutorOrErr)
609 IncrExecutor = std::move(*ExecutorOrErr);
610
611 return ExecutorOrErr.takeError();
612}
613
614llvm::Error Interpreter::Execute(PartialTranslationUnit &T) {
615 assert(T.TheModule);
616 LLVM_DEBUG(
617 llvm::dbgs() << "execute-ptu "
618 << (llvm::is_contained(PTUs, T)
619 ? std::distance(PTUs.begin(), llvm::find(PTUs, T))
620 : -1)
621 << ": [TU=" << T.TUPart << ", M=" << T.TheModule.get()
622 << " (" << T.TheModule->getName() << ")]\n");
623 if (!IncrExecutor) {
624 auto Err = CreateExecutor();
625 if (Err)
626 return Err;
627 }
628 // FIXME: Add a callback to retain the llvm::Module once the JIT is done.
629 if (auto Err = IncrExecutor->addModule(PTU&: T))
630 return Err;
631
632 if (auto Err = IncrExecutor->runCtors())
633 return Err;
634
635 return llvm::Error::success();
636}
637
638llvm::Error Interpreter::ParseAndExecute(llvm::StringRef Code, Value *V) {
639
640 auto PTU = Parse(Code);
641 if (!PTU)
642 return PTU.takeError();
643 if (PTU->TheModule)
644 if (llvm::Error Err = Execute(T&: *PTU))
645 return Err;
646
647 if (LastValue.isValid()) {
648 if (!V) {
649 LastValue.dump();
650 LastValue.clear();
651 } else
652 *V = std::move(LastValue);
653 }
654 return llvm::Error::success();
655}
656
657llvm::Expected<llvm::orc::ExecutorAddr>
658Interpreter::getSymbolAddress(GlobalDecl GD) const {
659 if (!IncrExecutor)
660 return llvm::make_error<llvm::StringError>(Args: "Operation failed. "
661 "No execution engine",
662 Args: std::error_code());
663 llvm::StringRef MangledName = Act->getCodeGen()->GetMangledName(GD);
664 return getSymbolAddress(IRName: MangledName);
665}
666
667llvm::Expected<llvm::orc::ExecutorAddr>
668Interpreter::getSymbolAddress(llvm::StringRef IRName) const {
669 if (!IncrExecutor)
670 return llvm::make_error<llvm::StringError>(Args: "Operation failed. "
671 "No execution engine",
672 Args: std::error_code());
673
674 return IncrExecutor->getSymbolAddress(Name: IRName, NameKind: IncrementalExecutor::IRName);
675}
676
677llvm::Expected<llvm::orc::ExecutorAddr>
678Interpreter::getSymbolAddressFromLinkerName(llvm::StringRef Name) const {
679 if (!IncrExecutor)
680 return llvm::make_error<llvm::StringError>(Args: "Operation failed. "
681 "No execution engine",
682 Args: std::error_code());
683
684 return IncrExecutor->getSymbolAddress(Name, NameKind: IncrementalExecutor::LinkerName);
685}
686
687llvm::Error Interpreter::Undo(unsigned N) {
688
689 if (getEffectivePTUSize() == 0) {
690 return llvm::make_error<llvm::StringError>(Args: "Operation failed. "
691 "No input left to undo",
692 Args: std::error_code());
693 } else if (N > getEffectivePTUSize()) {
694 return llvm::make_error<llvm::StringError>(
695 Args: llvm::formatv(
696 Fmt: "Operation failed. Wanted to undo {0} inputs, only have {1}.", Vals&: N,
697 Vals: getEffectivePTUSize()),
698 Args: std::error_code());
699 }
700
701 for (unsigned I = 0; I < N; I++) {
702 if (IncrExecutor) {
703 if (llvm::Error Err = IncrExecutor->removeModule(PTU&: PTUs.back()))
704 return Err;
705 }
706
707 IncrParser->CleanUpPTU(MostRecentTU: PTUs.back().TUPart);
708 PTUs.pop_back();
709 }
710 return llvm::Error::success();
711}
712
713llvm::Error Interpreter::LoadDynamicLibrary(const char *name) {
714 auto EEOrErr = getExecutionEngine();
715 if (!EEOrErr)
716 return EEOrErr.takeError();
717
718 return EEOrErr->LoadDynamicLibrary(name);
719}
720} // end namespace clang
721