clang 17.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/ExecutionUtils.h"
21#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
22#include "llvm/ExecutionEngine/Orc/LLJIT.h"
23#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
24#include "llvm/ExecutionEngine/SectionMemoryManager.h"
25#include "llvm/IR/Module.h"
26#include "llvm/Support/ManagedStatic.h"
27#include "llvm/Support/TargetSelect.h"
28
29namespace clang {
30
31IncrementalExecutor::IncrementalExecutor(llvm::orc::ThreadSafeContext &TSC,
32 llvm::Error &Err,
33 const clang::TargetInfo &TI)
34 : TSCtx(TSC) {
35 using namespace llvm::orc;
36 llvm::ErrorAsOutParameter EAO(&Err);
37
38 auto JTMB = JITTargetMachineBuilder(TI.getTriple());
39 JTMB.addFeatures(TI.getTargetOpts().Features);
40 if (auto JitOrErr = LLJITBuilder().setJITTargetMachineBuilder(JTMB).create())
41 Jit = std::move(*JitOrErr);
42 else {
43 Err = JitOrErr.takeError();
44 return;
45 }
46
47 const char Pref = Jit->getDataLayout().getGlobalPrefix();
48 // Discover symbols from the process as a fallback.
49 if (auto PSGOrErr = DynamicLibrarySearchGenerator::GetForCurrentProcess(Pref))
50 Jit->getMainJITDylib().addGenerator(std::move(*PSGOrErr));
51 else {
52 Err = PSGOrErr.takeError();
53 return;
54 }
55}
56
58
60 llvm::orc::ResourceTrackerSP RT =
61 Jit->getMainJITDylib().createResourceTracker();
62 ResourceTrackers[&PTU] = RT;
63
64 return Jit->addIRModule(RT, {std::move(PTU.TheModule), TSCtx});
65}
66
68
69 llvm::orc::ResourceTrackerSP RT = std::move(ResourceTrackers[&PTU]);
70 if (!RT)
71 return llvm::Error::success();
72
73 ResourceTrackers.erase(&PTU);
74 if (llvm::Error Err = RT->remove())
75 return Err;
76 return llvm::Error::success();
77}
78
79// Clean up the JIT instance.
81 // This calls the global dtors of registered modules.
82 return Jit->deinitialize(Jit->getMainJITDylib());
83}
84
85llvm::Error IncrementalExecutor::runCtors() const {
86 return Jit->initialize(Jit->getMainJITDylib());
87}
88
91 SymbolNameKind NameKind) const {
92 auto Sym = (NameKind == LinkerName) ? Jit->lookupLinkerMangled(Name)
93 : Jit->lookup(Name);
94
95 if (!Sym)
96 return Sym.takeError();
97 return Sym->getValue();
98}
99
100} // end namespace clang
Defines the clang::TargetOptions class.
llvm::Expected< llvm::JITTargetAddress > getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const
llvm::Error addModule(PartialTranslationUnit &PTU)
IncrementalExecutor(llvm::orc::ThreadSafeContext &TSC, llvm::Error &Err, const clang::TargetInfo &TI)
llvm::Error removeModule(PartialTranslationUnit &PTU)
Exposes information about the current target.
Definition: TargetInfo.h:206
TargetOptions & getTargetOpts() const
Retrieve the target options.
Definition: TargetInfo.h:281
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:1195
std::vector< std::string > Features
The list of target specific features to enable or disable – this should be a list of strings starting...
Definition: TargetOptions.h:58
Defines the clang::TargetInfo interface.
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.