13 #ifndef LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H 14 #define LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H 43 class EmissionDeferredModule {
45 EmissionDeferredModule(
VModuleKey K, std::unique_ptr<Module> M)
46 : K(std::move(K)), M(std::move(M)) {}
51 if (
auto GV = searchGVs(Name, ExportedSymbolsOnly)) {
53 auto GetAddress = [
this, ExportedSymbolsOnly, Name = Name.
str(),
55 if (this->EmitState == Emitting)
57 else if (this->EmitState == NotEmitted) {
58 this->EmitState = Emitting;
59 if (
auto Err = this->emitToBaseLayer(B))
60 return std::move(Err);
63 if (
auto Sym = B.findSymbolIn(K, Name, ExportedSymbolsOnly))
64 return Sym.getAddress();
65 else if (
auto Err = Sym.takeError())
66 return std::move(Err);
69 "definition address here");
71 return JITSymbol(std::move(GetAddress), Flags);
81 return B.findSymbolIn(K, Name, ExportedSymbolsOnly);
86 Error removeModuleFromBaseLayer(BaseLayerT& BaseLayer) {
87 return EmitState != NotEmitted ? BaseLayer.removeModule(K)
92 assert(EmitState != Emitting &&
93 "Cannot emitAndFinalize while already emitting");
94 if (EmitState == NotEmitted) {
96 emitToBaseLayer(BaseLayer);
99 BaseLayer.emitAndFinalize(K);
105 bool ExportedSymbolsOnly)
const {
111 if (MangledSymbols) {
112 auto VI = MangledSymbols->find(Name);
113 if (
VI == MangledSymbols->end())
115 auto GV =
VI->second;
116 if (!ExportedSymbolsOnly || GV->hasDefaultVisibility())
124 return buildMangledSymbols(Name, ExportedSymbolsOnly);
127 Error emitToBaseLayer(BaseLayerT &BaseLayer) {
130 MangledSymbols.reset();
131 return BaseLayer.addModule(std::move(K), std::move(M));
141 bool ExportedSymbolsOnly)
const {
147 std::string MangledName;
155 if (MangledName == SearchName)
160 Names[MangledName] = &GV;
167 bool ExportedSymbolsOnly)
const {
168 assert(!MangledSymbols &&
"Mangled symbols map already exists?");
170 auto Symbols = std::make_unique<StringMap<const GlobalValue*>>();
174 for (
const auto &GO : M->global_objects())
175 if (
auto GV = addGlobalValue(*Symbols, GO, Mang, SearchName,
176 ExportedSymbolsOnly))
179 MangledSymbols = std::move(Symbols);
183 enum { NotEmitted, Emitting,
Emitted } EmitState = NotEmitted;
185 std::unique_ptr<Module> M;
186 mutable std::unique_ptr<StringMap<const GlobalValue*>> MangledSymbols;
189 BaseLayerT &BaseLayer;
190 std::map<VModuleKey, std::unique_ptr<EmissionDeferredModule>> ModuleMap;
197 "ORCv1 layers (including LazyEmittingLayer) are deprecated. Please use " 198 "ORCv2, where lazy emission is the default");
202 : BaseLayer(BaseLayer) {}
206 assert(!ModuleMap.count(K) &&
"VModuleKey K already in use");
208 std::make_unique<EmissionDeferredModule>(std::move(K), std::move(M));
217 auto I = ModuleMap.find(K);
218 assert(
I != ModuleMap.end() &&
"VModuleKey K not valid here");
219 auto EDM = std::move(
I.second);
221 return EDM->removeModuleFromBaseLayer(BaseLayer);
230 if (
auto Symbol = BaseLayer.findSymbol(Name, ExportedSymbolsOnly))
236 for (
auto &KV : ModuleMap)
237 if (
auto Symbol = KV.second->find(Name, ExportedSymbolsOnly, BaseLayer))
247 bool ExportedSymbolsOnly) {
248 assert(ModuleMap.count(K) &&
"VModuleKey K not valid here");
249 return ModuleMap[K]->find(Name, ExportedSymbolsOnly, BaseLayer);
255 assert(ModuleMap.count(K) &&
"VModuleKey K not valid here");
256 return ModuleMap[K]->emitAndFinalize(BaseLayer);
260 template <
typename BaseLayerT>
262 : BaseLayer(BaseLayer) {}
267 #endif // LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H Error emitAndFinalize(VModuleKey K)
Immediately emit and finalize the module represented by the given key.
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
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.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
LLVM_ATTRIBUTE_DEPRECATED(LazyEmittingLayer(BaseLayerT &BaseLayer), "ORCv1 layers (including LazyEmittingLayer) are deprecated. Please use " "ORCv2, where lazy emission is the default")
Construct a lazy emitting layer.
Error removeModule(VModuleKey K)
Remove the module represented by the given handle.
Tagged union holding either a T or a Error.
bool hasCommonLinkage() const
ORCv1DeprecationAcknowledgement
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Flags for symbols in the JIT.
LazyEmittingLayer(ORCv1DeprecationAcknowledgement, 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"...
uint8_t uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
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.
Assigned address, still materializing.
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.