14 #ifndef LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H 15 #define LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H 44 class EmissionDeferredModule {
46 EmissionDeferredModule(
VModuleKey K, std::unique_ptr<Module> M)
47 : K(std::move(K)), M(std::move(M)) {}
52 if (
auto GV = searchGVs(Name, ExportedSymbolsOnly)) {
56 std::string PName =
Name;
60 if (this->EmitState == Emitting)
62 else if (this->EmitState == NotEmitted) {
63 this->EmitState = Emitting;
64 if (
auto Err = this->emitToBaseLayer(B))
65 return std::move(Err);
66 this->EmitState = Emitted;
68 if (
auto Sym = B.findSymbolIn(K, PName, ExportedSymbolsOnly))
69 return Sym.getAddress();
70 else if (
auto Err = Sym.takeError())
71 return std::move(Err);
74 "definition address here");
76 return JITSymbol(std::move(GetAddress), Flags);
86 return B.findSymbolIn(K, Name, ExportedSymbolsOnly);
91 Error removeModuleFromBaseLayer(BaseLayerT& BaseLayer) {
92 return EmitState != NotEmitted ? BaseLayer.removeModule(K)
97 assert(EmitState != Emitting &&
98 "Cannot emitAndFinalize while already emitting");
99 if (EmitState == NotEmitted) {
100 EmitState = Emitting;
101 emitToBaseLayer(BaseLayer);
104 BaseLayer.emitAndFinalize(K);
110 bool ExportedSymbolsOnly)
const {
116 if (MangledSymbols) {
117 auto VI = MangledSymbols->find(Name);
118 if (
VI == MangledSymbols->end())
120 auto GV =
VI->second;
121 if (!ExportedSymbolsOnly || GV->hasDefaultVisibility())
129 return buildMangledSymbols(Name, ExportedSymbolsOnly);
132 Error emitToBaseLayer(BaseLayerT &BaseLayer) {
135 MangledSymbols.reset();
136 return BaseLayer.addModule(std::move(K), std::move(M));
146 bool ExportedSymbolsOnly)
const {
152 std::string MangledName;
160 if (MangledName == SearchName)
165 Names[MangledName] = &GV;
172 bool ExportedSymbolsOnly)
const {
173 assert(!MangledSymbols &&
"Mangled symbols map already exists?");
175 auto Symbols = llvm::make_unique<StringMap<const GlobalValue*>>();
179 for (
const auto &GO : M->global_objects())
180 if (
auto GV = addGlobalValue(*Symbols, GO, Mang, SearchName,
181 ExportedSymbolsOnly))
184 MangledSymbols = std::move(Symbols);
188 enum { NotEmitted, Emitting, Emitted } EmitState = NotEmitted;
190 std::unique_ptr<Module> M;
191 mutable std::unique_ptr<StringMap<const GlobalValue*>> MangledSymbols;
194 BaseLayerT &BaseLayer;
195 std::map<VModuleKey, std::unique_ptr<EmissionDeferredModule>> ModuleMap;
204 assert(!ModuleMap.count(K) &&
"VModuleKey K already in use");
206 llvm::make_unique<EmissionDeferredModule>(std::move(K), std::move(M));
215 auto I = ModuleMap.find(K);
216 assert(
I != ModuleMap.end() &&
"VModuleKey K not valid here");
217 auto EDM = std::move(
I.second);
219 return EDM->removeModuleFromBaseLayer(BaseLayer);
228 if (
auto Symbol = BaseLayer.findSymbol(Name, ExportedSymbolsOnly))
234 for (
auto &KV : ModuleMap)
235 if (
auto Symbol = KV.second->find(Name, ExportedSymbolsOnly, BaseLayer))
245 bool ExportedSymbolsOnly) {
246 assert(ModuleMap.count(K) &&
"VModuleKey K not valid here");
247 return ModuleMap[K]->find(Name, ExportedSymbolsOnly, BaseLayer);
253 assert(ModuleMap.count(K) &&
"VModuleKey K not valid here");
254 return ModuleMap[K]->emitAndFinalize(BaseLayer);
261 #endif // LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H Error emitAndFinalize(VModuleKey K)
Immediately emit and finalize the module represented by the given key.
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.
This class represents lattice values for constants.
uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
amdgpu Simplify well known AMD library false Value Value const Twine & Name
Error removeModule(VModuleKey K)
Remove the module represented by the given handle.
Tagged union holding either a T or a Error.
bool hasCommonLinkage() const
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Flags for symbols in the JIT.
LazyEmittingLayer(BaseLayerT &BaseLayer)
Construct a lazy emitting layer.
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static ErrorSuccess success()
Create a success value.
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.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
JITSymbol findSymbolIn(VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly)
Get the address of the given symbol in the context of the of compiled modules represented by the key ...
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
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.
bool hasDefaultVisibility() const
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
StringRef - Represent a constant reference to a string, i.e.
Error addModule(VModuleKey K, std::unique_ptr< Module > M)
Add the given module to the lazy emitting layer.