14 #ifndef LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H
47 class EmissionDeferredModule {
49 EmissionDeferredModule(std::shared_ptr<Module> M,
50 std::shared_ptr<JITSymbolResolver> Resolver)
51 : M(std::move(M)), Resolver(std::move(Resolver)) {}
56 if (
auto GV = searchGVs(Name, ExportedSymbolsOnly)) {
60 std::string PName =
Name;
64 if (this->EmitState == Emitting)
66 else if (this->EmitState == NotEmitted) {
67 this->EmitState = Emitting;
68 if (
auto HandleOrErr = this->emitToBaseLayer(B))
69 Handle = std::move(*HandleOrErr);
72 this->EmitState = Emitted;
74 if (
auto Sym = B.findSymbolIn(Handle, PName, ExportedSymbolsOnly))
75 return Sym.getAddress();
76 else if (
auto Err = Sym.takeError())
77 return std::move(Err);
80 "definition address here");
82 return JITSymbol(std::move(GetAddress), Flags);
92 return B.findSymbolIn(Handle, Name, ExportedSymbolsOnly);
97 Error removeModuleFromBaseLayer(BaseLayerT& BaseLayer) {
98 return EmitState != NotEmitted ? BaseLayer.removeModule(Handle)
103 assert(EmitState != Emitting &&
104 "Cannot emitAndFinalize while already emitting");
105 if (EmitState == NotEmitted) {
106 EmitState = Emitting;
107 Handle = emitToBaseLayer(BaseLayer);
110 BaseLayer.emitAndFinalize(Handle);
116 bool ExportedSymbolsOnly)
const {
122 if (MangledSymbols) {
123 auto VI = MangledSymbols->find(Name);
124 if (
VI == MangledSymbols->end())
126 auto GV =
VI->second;
127 if (!ExportedSymbolsOnly || GV->hasDefaultVisibility())
135 return buildMangledSymbols(Name, ExportedSymbolsOnly);
141 MangledSymbols.reset();
142 return BaseLayer.addModule(std::move(M), std::move(Resolver));
152 bool ExportedSymbolsOnly)
const {
158 std::string MangledName;
166 if (MangledName == SearchName)
171 Names[MangledName] = &GV;
178 bool ExportedSymbolsOnly)
const {
179 assert(!MangledSymbols &&
"Mangled symbols map already exists?");
181 auto Symbols = llvm::make_unique<StringMap<const GlobalValue*>>();
185 for (
const auto &GO : M->global_objects())
186 if (
auto GV = addGlobalValue(*Symbols, GO, Mang, SearchName,
187 ExportedSymbolsOnly))
190 MangledSymbols = std::move(Symbols);
194 enum { NotEmitted, Emitting, Emitted } EmitState = NotEmitted;
196 std::shared_ptr<Module> M;
197 std::shared_ptr<JITSymbolResolver> Resolver;
198 mutable std::unique_ptr<StringMap<const GlobalValue*>> MangledSymbols;
201 using ModuleListT = std::list<std::unique_ptr<EmissionDeferredModule>>;
203 BaseLayerT &BaseLayer;
204 ModuleListT ModuleList;
217 std::shared_ptr<JITSymbolResolver> Resolver) {
218 return ModuleList.insert(
220 llvm::make_unique<EmissionDeferredModule>(std::move(M),
221 std::move(Resolver)));
229 Error Err = (*H)->removeModuleFromBaseLayer(BaseLayer);
240 if (
auto Symbol = BaseLayer.findSymbol(Name, ExportedSymbolsOnly))
246 for (
auto &DeferredMod : ModuleList)
247 if (
auto Symbol = DeferredMod->find(Name, ExportedSymbolsOnly, BaseLayer))
257 bool ExportedSymbolsOnly) {
258 return (*H)->find(Name, ExportedSymbolsOnly, BaseLayer);
265 return (*H)->emitAndFinalize(BaseLayer);
272 #endif // LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H
Error removeModule(ModuleHandleT H)
Remove the module represented by the given handle.
static JITSymbolFlags fromGlobalValue(const GlobalValue &GV)
Construct a JITSymbolFlags value based on the flags of the given global value.
Represents a symbol in the JIT.
Error takeError()
Take ownership of the stored error.
JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name, bool ExportedSymbolsOnly)
Get the address of the given symbol in the context of the of compiled modules represented by the hand...
bool hasCommonLinkage() const
bool hasDefaultVisibility() const
Tagged union holding either a T or a Error.
Error emitAndFinalize(ModuleHandleT H)
Immediately emit and finalize the module represented by the given handle.
Expected< ModuleHandleT > addModule(std::shared_ptr< Module > M, std::shared_ptr< JITSymbolResolver > Resolver)
Add the given module to the lazy emitting layer.
Flags for symbols in the JIT.
LazyEmittingLayer(BaseLayerT &BaseLayer)
Construct a lazy emitting layer.
static ErrorSuccess success()
Create a success value.
JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Module.h This file contains the declarations for the Module class.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
llvm_unreachable("Unknown RegBank!")
static std::vector< std::string > Flags
typename CompileLayerT::ModuleHandleT BaseLayerHandleT
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Lightweight error class with error context and mandatory checking.
StringRef - Represent a constant reference to a string, i.e.
typename ModuleListT::iterator ModuleHandleT
Handle to a loaded module.