1 //===--- IncrementalExecutor.cpp - Incremental Execution --------*- C++ -*-===//
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 class which performs incremental code execution.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "IncrementalExecutor.h"
14 
15 #include "llvm/ExecutionEngine/ExecutionEngine.h"
16 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
17 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
18 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
19 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
20 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
21 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/TargetSelect.h"
25 
26 namespace clang {
27 
28 IncrementalExecutor::IncrementalExecutor(llvm::orc::ThreadSafeContext &TSC,
29                                          llvm::Error &Err)
30     : TSCtx(TSC) {
31   using namespace llvm::orc;
32   llvm::ErrorAsOutParameter EAO(&Err);
33 
34   if (auto JitOrErr = LLJITBuilder().create())
35     Jit = std::move(*JitOrErr);
36   else {
37     Err = JitOrErr.takeError();
38     return;
39   }
40 
41   const char Pref = Jit->getDataLayout().getGlobalPrefix();
42   // Discover symbols from the process as a fallback.
43   if (auto PSGOrErr = DynamicLibrarySearchGenerator::GetForCurrentProcess(Pref))
44     Jit->getMainJITDylib().addGenerator(std::move(*PSGOrErr));
45   else {
46     Err = PSGOrErr.takeError();
47     return;
48   }
49 }
50 
51 IncrementalExecutor::~IncrementalExecutor() {}
52 
53 llvm::Error IncrementalExecutor::addModule(std::unique_ptr<llvm::Module> M) {
54   return Jit->addIRModule(llvm::orc::ThreadSafeModule(std::move(M), TSCtx));
55 }
56 
57 llvm::Error IncrementalExecutor::runCtors() const {
58   return Jit->initialize(Jit->getMainJITDylib());
59 }
60 
61 } // end namespace clang
62