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 "clang/Basic/TargetInfo.h"
16#include "clang/Basic/TargetOptions.h"
17#include "clang/Interpreter/PartialTranslationUnit.h"
18#include "llvm/ExecutionEngine/ExecutionEngine.h"
19#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
20#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h"
21#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
22#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
23#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
24#include "llvm/ExecutionEngine/Orc/LLJIT.h"
25#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
26#include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
27#include "llvm/ExecutionEngine/SectionMemoryManager.h"
28#include "llvm/IR/Module.h"
29#include "llvm/Support/ManagedStatic.h"
30#include "llvm/Support/TargetSelect.h"
31
32// Force linking some of the runtimes that helps attaching to a debugger.
33LLVM_ATTRIBUTE_USED void linkComponents() {
34 llvm::errs() << (void *)&llvm_orc_registerJITLoaderGDBWrapper
35 << (void *)&llvm_orc_registerJITLoaderGDBAllocAction;
36}
37
38namespace clang {
39
40llvm::Expected<std::unique_ptr<llvm::orc::LLJITBuilder>>
41IncrementalExecutor::createDefaultJITBuilder(
42 llvm::orc::JITTargetMachineBuilder JTMB) {
43 auto JITBuilder = std::make_unique<llvm::orc::LLJITBuilder>();
44 JITBuilder->setJITTargetMachineBuilder(std::move(JTMB));
45 JITBuilder->setPrePlatformSetup([](llvm::orc::LLJIT &J) {
46 // Try to enable debugging of JIT'd code (only works with JITLink for
47 // ELF and MachO).
48 consumeError(Err: llvm::orc::enableDebuggerSupport(J));
49 return llvm::Error::success();
50 });
51 return std::move(JITBuilder);
52}
53
54IncrementalExecutor::IncrementalExecutor(llvm::orc::ThreadSafeContext &TSC,
55 llvm::orc::LLJITBuilder &JITBuilder,
56 llvm::Error &Err)
57 : TSCtx(TSC) {
58 using namespace llvm::orc;
59 llvm::ErrorAsOutParameter EAO(&Err);
60
61 if (auto JitOrErr = JITBuilder.create())
62 Jit = std::move(*JitOrErr);
63 else {
64 Err = JitOrErr.takeError();
65 return;
66 }
67}
68
69IncrementalExecutor::~IncrementalExecutor() {}
70
71llvm::Error IncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
72 llvm::orc::ResourceTrackerSP RT =
73 Jit->getMainJITDylib().createResourceTracker();
74 ResourceTrackers[&PTU] = RT;
75
76 return Jit->addIRModule(RT, TSM: {std::move(PTU.TheModule), TSCtx});
77}
78
79llvm::Error IncrementalExecutor::removeModule(PartialTranslationUnit &PTU) {
80
81 llvm::orc::ResourceTrackerSP RT = std::move(ResourceTrackers[&PTU]);
82 if (!RT)
83 return llvm::Error::success();
84
85 ResourceTrackers.erase(Val: &PTU);
86 if (llvm::Error Err = RT->remove())
87 return Err;
88 return llvm::Error::success();
89}
90
91// Clean up the JIT instance.
92llvm::Error IncrementalExecutor::cleanUp() {
93 // This calls the global dtors of registered modules.
94 return Jit->deinitialize(JD&: Jit->getMainJITDylib());
95}
96
97llvm::Error IncrementalExecutor::runCtors() const {
98 return Jit->initialize(JD&: Jit->getMainJITDylib());
99}
100
101llvm::Expected<llvm::orc::ExecutorAddr>
102IncrementalExecutor::getSymbolAddress(llvm::StringRef Name,
103 SymbolNameKind NameKind) const {
104 using namespace llvm::orc;
105 auto SO = makeJITDylibSearchOrder(JDs: {&Jit->getMainJITDylib(),
106 Jit->getPlatformJITDylib().get(),
107 Jit->getProcessSymbolsJITDylib().get()});
108
109 ExecutionSession &ES = Jit->getExecutionSession();
110
111 auto SymOrErr =
112 ES.lookup(SearchOrder: SO, Symbol: (NameKind == LinkerName) ? ES.intern(SymName: Name)
113 : Jit->mangleAndIntern(UnmangledName: Name));
114 if (auto Err = SymOrErr.takeError())
115 return std::move(Err);
116 return SymOrErr->getAddress();
117}
118
119} // end namespace clang
120

source code of clang/lib/Interpreter/IncrementalExecutor.cpp