clang 19.0.0git
IncrementalExecutor.cpp
Go to the documentation of this file.
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
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 {
39IncrementalExecutor::IncrementalExecutor(llvm::orc::ThreadSafeContext &TSC)
40 : TSCtx(TSC) {}
41
44 llvm::orc::JITTargetMachineBuilder JTMB) {
45 auto JITBuilder = std::make_unique<llvm::orc::LLJITBuilder>();
46 JITBuilder->setJITTargetMachineBuilder(std::move(JTMB));
47 JITBuilder->setPrePlatformSetup([](llvm::orc::LLJIT &J) {
48 // Try to enable debugging of JIT'd code (only works with JITLink for
49 // ELF and MachO).
50 consumeError(llvm::orc::enableDebuggerSupport(J));
51 return llvm::Error::success();
52 });
53 return std::move(JITBuilder);
54}
55
56IncrementalExecutor::IncrementalExecutor(llvm::orc::ThreadSafeContext &TSC,
57 llvm::orc::LLJITBuilder &JITBuilder,
58 llvm::Error &Err)
59 : TSCtx(TSC) {
60 using namespace llvm::orc;
61 llvm::ErrorAsOutParameter EAO(&Err);
62
63 if (auto JitOrErr = JITBuilder.create())
64 Jit = std::move(*JitOrErr);
65 else {
66 Err = JitOrErr.takeError();
67 return;
68 }
69}
70
72
74 llvm::orc::ResourceTrackerSP RT =
75 Jit->getMainJITDylib().createResourceTracker();
76 ResourceTrackers[&PTU] = RT;
77
78 return Jit->addIRModule(RT, {std::move(PTU.TheModule), TSCtx});
79}
80
82
83 llvm::orc::ResourceTrackerSP RT = std::move(ResourceTrackers[&PTU]);
84 if (!RT)
85 return llvm::Error::success();
86
87 ResourceTrackers.erase(&PTU);
88 if (llvm::Error Err = RT->remove())
89 return Err;
90 return llvm::Error::success();
91}
92
93// Clean up the JIT instance.
95 // This calls the global dtors of registered modules.
96 return Jit->deinitialize(Jit->getMainJITDylib());
97}
98
99llvm::Error IncrementalExecutor::runCtors() const {
100 return Jit->initialize(Jit->getMainJITDylib());
101}
102
105 SymbolNameKind NameKind) const {
106 using namespace llvm::orc;
107 auto SO = makeJITDylibSearchOrder({&Jit->getMainJITDylib(),
108 Jit->getPlatformJITDylib().get(),
109 Jit->getProcessSymbolsJITDylib().get()});
110
111 ExecutionSession &ES = Jit->getExecutionSession();
112
113 auto SymOrErr =
114 ES.lookup(SO, (NameKind == LinkerName) ? ES.intern(Name)
115 : Jit->mangleAndIntern(Name));
116 if (auto Err = SymOrErr.takeError())
117 return std::move(Err);
118 return SymOrErr->getAddress();
119}
120
121} // end namespace clang
LLVM_ATTRIBUTE_USED void linkComponents()
Defines the clang::TargetOptions class.
virtual llvm::Error runCtors() const
virtual llvm::Error addModule(PartialTranslationUnit &PTU)
llvm::Expected< llvm::orc::ExecutorAddr > getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const
static llvm::Expected< std::unique_ptr< llvm::orc::LLJITBuilder > > createDefaultJITBuilder(llvm::orc::JITTargetMachineBuilder JTMB)
virtual llvm::Error removeModule(PartialTranslationUnit &PTU)
IncrementalExecutor(llvm::orc::ThreadSafeContext &TSC)
Defines the clang::TargetInfo interface.
The JSON file list parser is used to communicate input to InstallAPI.
The class keeps track of various objects created as part of processing incremental inputs.
std::unique_ptr< llvm::Module > TheModule
The llvm IR produced for the input.