14 #ifndef LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H
40 class EmissionDeferredSet {
42 EmissionDeferredSet() : EmitState(NotEmitted) {}
43 virtual ~EmissionDeferredSet() {}
48 if (
auto GV = searchGVs(Name, ExportedSymbolsOnly)) {
52 std::string PName =
Name;
56 if (this->EmitState == Emitting)
58 else if (this->EmitState == NotEmitted) {
59 this->EmitState = Emitting;
60 Handle = this->emitToBaseLayer(B);
61 this->EmitState = Emitted;
63 auto Sym = B.findSymbolIn(Handle, PName, ExportedSymbolsOnly);
64 return Sym.getAddress();
66 return JITSymbol(std::move(GetAddress), Flags);
76 return B.findSymbolIn(Handle, Name, ExportedSymbolsOnly);
81 void removeModulesFromBaseLayer(BaseLayerT &BaseLayer) {
82 if (EmitState != NotEmitted)
83 BaseLayer.removeModuleSet(Handle);
87 assert(EmitState != Emitting &&
88 "Cannot emitAndFinalize while already emitting");
89 if (EmitState == NotEmitted) {
91 Handle = emitToBaseLayer(BaseLayer);
94 BaseLayer.emitAndFinalize(Handle);
97 template <
typename ModuleSetT,
typename MemoryManagerPtrT,
98 typename SymbolResolverPtrT>
99 static std::unique_ptr<EmissionDeferredSet>
100 create(BaseLayerT &B, ModuleSetT Ms, MemoryManagerPtrT MemMgr,
101 SymbolResolverPtrT Resolver);
105 bool ExportedSymbolsOnly)
const = 0;
109 enum { NotEmitted, Emitting, Emitted } EmitState;
113 template <
typename ModuleSetT,
typename MemoryManagerPtrT,
114 typename SymbolResolverPtrT>
115 class EmissionDeferredSetImpl :
public EmissionDeferredSet {
117 EmissionDeferredSetImpl(ModuleSetT Ms,
118 MemoryManagerPtrT MemMgr,
119 SymbolResolverPtrT Resolver)
120 : Ms(std::move(Ms)), MemMgr(std::move(MemMgr)),
121 Resolver(std::move(Resolver)) {}
126 bool ExportedSymbolsOnly)
const override {
132 if (MangledSymbols) {
133 auto VI = MangledSymbols->find(Name);
134 if (
VI == MangledSymbols->end())
136 auto GV =
VI->second;
137 if (!ExportedSymbolsOnly || GV->hasDefaultVisibility())
145 return buildMangledSymbols(Name, ExportedSymbolsOnly);
151 MangledSymbols.reset();
152 return BaseLayer.addModuleSet(std::move(Ms), std::move(MemMgr),
153 std::move(Resolver));
161 const GlobalValue* addGlobalValue(StringMap<const GlobalValue*> &Names,
162 const GlobalValue &GV,
163 const Mangler &Mang, StringRef SearchName,
164 bool ExportedSymbolsOnly)
const {
166 if (GV.isDeclaration() || GV.hasCommonLinkage())
170 std::string MangledName;
172 raw_string_ostream MangledNameStream(MangledName);
173 Mang.getNameWithPrefix(MangledNameStream, &GV,
false);
178 if (MangledName == SearchName)
179 if (!ExportedSymbolsOnly || GV.hasDefaultVisibility())
183 Names[MangledName] = &GV;
189 const GlobalValue* buildMangledSymbols(StringRef SearchName,
190 bool ExportedSymbolsOnly)
const {
191 assert(!MangledSymbols &&
"Mangled symbols map already exists?");
193 auto Symbols = llvm::make_unique<StringMap<const GlobalValue*>>();
195 for (
const auto &M : Ms) {
198 for (
const auto &V : M->globals())
199 if (
auto GV = addGlobalValue(*Symbols, V, Mang, SearchName,
200 ExportedSymbolsOnly))
203 for (
const auto &
F : *M)
204 if (
auto GV = addGlobalValue(*Symbols,
F, Mang, SearchName,
205 ExportedSymbolsOnly))
209 MangledSymbols = std::move(Symbols);
214 MemoryManagerPtrT MemMgr;
215 SymbolResolverPtrT Resolver;
216 mutable std::unique_ptr<StringMap<const GlobalValue*>> MangledSymbols;
219 typedef std::list<std::unique_ptr<EmissionDeferredSet>> ModuleSetListT;
221 BaseLayerT &BaseLayer;
222 ModuleSetListT ModuleSetList;
232 template <
typename ModuleSetT,
typename MemoryManagerPtrT,
233 typename SymbolResolverPtrT>
235 MemoryManagerPtrT MemMgr,
236 SymbolResolverPtrT Resolver) {
237 return ModuleSetList.insert(
239 EmissionDeferredSet::create(BaseLayer, std::move(Ms), std::move(MemMgr),
240 std::move(Resolver)));
248 (*H)->removeModulesFromBaseLayer(BaseLayer);
249 ModuleSetList.erase(H);
258 if (
auto Symbol = BaseLayer.findSymbol(Name, ExportedSymbolsOnly))
264 for (
auto &DeferredSet : ModuleSetList)
265 if (
auto Symbol = DeferredSet->find(Name, ExportedSymbolsOnly, BaseLayer))
275 bool ExportedSymbolsOnly) {
276 return (*H)->find(Name, ExportedSymbolsOnly, BaseLayer);
283 (*H)->emitAndFinalize(BaseLayer);
288 template <
typename BaseLayerT>
289 template <
typename ModuleSetT,
typename MemoryManagerPtrT,
290 typename SymbolResolverPtrT>
291 std::unique_ptr<typename LazyEmittingLayer<BaseLayerT>::EmissionDeferredSet>
292 LazyEmittingLayer<BaseLayerT>::EmissionDeferredSet::create(
293 BaseLayerT &B, ModuleSetT Ms, MemoryManagerPtrT MemMgr,
294 SymbolResolverPtrT Resolver) {
295 typedef EmissionDeferredSetImpl<ModuleSetT, MemoryManagerPtrT, SymbolResolverPtrT>
297 return llvm::make_unique<EDS>(std::move(Ms), std::move(MemMgr),
298 std::move(Resolver));
304 #endif // LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H
BaseLayerT::ModuleSetHandleT BaseLayerHandleT
ModuleSetHandleT addModuleSet(ModuleSetT Ms, MemoryManagerPtrT MemMgr, SymbolResolverPtrT Resolver)
Add the given set of modules to the lazy emitting layer.
void removeModuleSet(ModuleSetHandleT H)
Remove the module set represented by the given handle.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
JITSymbolFlags
Flags for symbols in the JIT.
void emitAndFinalize(ModuleSetHandleT H)
Immediately emit and finalize the moduleOB set represented by the given handle.
Represents a symbol in the JIT.
LazyEmittingLayer(BaseLayerT &BaseLayer)
Construct a lazy emitting layer.
ModuleSetListT::iterator ModuleSetHandleT
Handle to a set of loaded modules.
static JITSymbolFlags flagsFromGlobalValue(const GlobalValue &GV)
JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
Module.h This file contains the declarations for the Module class.
uint64_t TargetAddress
Represents an address in the target process's address space.
JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name, bool ExportedSymbolsOnly)
Get the address of the given symbol in the context of the set of compiled modules represented by the ...
StringRef - Represent a constant reference to a string, i.e.