xref: /freebsd/contrib/llvm-project/clang/lib/Interpreter/IncrementalExecutor.cpp (revision 1f1e2261e341e6ca6862f82261066ef1705f0a7a)
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                                          const llvm::Triple &Triple)
31     : TSCtx(TSC) {
32   using namespace llvm::orc;
33   llvm::ErrorAsOutParameter EAO(&Err);
34 
35   auto JTMB = JITTargetMachineBuilder(Triple);
36   if (auto JitOrErr = LLJITBuilder().setJITTargetMachineBuilder(JTMB).create())
37     Jit = std::move(*JitOrErr);
38   else {
39     Err = JitOrErr.takeError();
40     return;
41   }
42 
43   const char Pref = Jit->getDataLayout().getGlobalPrefix();
44   // Discover symbols from the process as a fallback.
45   if (auto PSGOrErr = DynamicLibrarySearchGenerator::GetForCurrentProcess(Pref))
46     Jit->getMainJITDylib().addGenerator(std::move(*PSGOrErr));
47   else {
48     Err = PSGOrErr.takeError();
49     return;
50   }
51 }
52 
53 IncrementalExecutor::~IncrementalExecutor() {}
54 
55 llvm::Error IncrementalExecutor::addModule(std::unique_ptr<llvm::Module> M) {
56   return Jit->addIRModule(llvm::orc::ThreadSafeModule(std::move(M), TSCtx));
57 }
58 
59 llvm::Error IncrementalExecutor::runCtors() const {
60   return Jit->initialize(Jit->getMainJITDylib());
61 }
62 
63 llvm::Expected<llvm::JITTargetAddress>
64 IncrementalExecutor::getSymbolAddress(llvm::StringRef Name,
65                                       SymbolNameKind NameKind) const {
66   auto Sym = (NameKind == LinkerName) ? Jit->lookupLinkerMangled(Name)
67                                       : Jit->lookup(Name);
68 
69   if (!Sym)
70     return Sym.takeError();
71   return Sym->getAddress();
72 }
73 
74 } // end namespace clang
75