14 #ifndef LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H
46 class EmissionDeferredSet {
48 EmissionDeferredSet() =
default;
49 virtual ~EmissionDeferredSet() =
default;
54 if (
auto GV = searchGVs(Name, ExportedSymbolsOnly)) {
58 std::string PName =
Name;
62 if (this->EmitState == Emitting)
64 else if (this->EmitState == NotEmitted) {
65 this->EmitState = Emitting;
66 Handle = this->emitToBaseLayer(B);
67 this->EmitState = Emitted;
69 auto Sym = B.findSymbolIn(Handle, PName, ExportedSymbolsOnly);
70 return Sym.getAddress();
72 return JITSymbol(std::move(GetAddress), Flags);
82 return B.findSymbolIn(Handle, Name, ExportedSymbolsOnly);
87 void removeModulesFromBaseLayer(BaseLayerT &BaseLayer) {
88 if (EmitState != NotEmitted)
89 BaseLayer.removeModuleSet(Handle);
93 assert(EmitState != Emitting &&
94 "Cannot emitAndFinalize while already emitting");
95 if (EmitState == NotEmitted) {
97 Handle = emitToBaseLayer(BaseLayer);
100 BaseLayer.emitAndFinalize(Handle);
103 template <
typename ModuleSetT,
typename MemoryManagerPtrT,
104 typename SymbolResolverPtrT>
105 static std::unique_ptr<EmissionDeferredSet>
106 create(BaseLayerT &B, ModuleSetT Ms, MemoryManagerPtrT MemMgr,
107 SymbolResolverPtrT Resolver);
111 bool ExportedSymbolsOnly)
const = 0;
115 enum { NotEmitted, Emitting, Emitted } EmitState = NotEmitted;
119 template <
typename ModuleSetT,
typename MemoryManagerPtrT,
120 typename SymbolResolverPtrT>
121 class EmissionDeferredSetImpl :
public EmissionDeferredSet {
123 EmissionDeferredSetImpl(ModuleSetT Ms,
124 MemoryManagerPtrT MemMgr,
125 SymbolResolverPtrT Resolver)
126 : Ms(std::move(Ms)), MemMgr(std::move(MemMgr)),
127 Resolver(std::move(Resolver)) {}
131 bool ExportedSymbolsOnly)
const override {
137 if (MangledSymbols) {
138 auto VI = MangledSymbols->find(Name);
139 if (
VI == MangledSymbols->end())
141 auto GV =
VI->second;
142 if (!ExportedSymbolsOnly || GV->hasDefaultVisibility())
150 return buildMangledSymbols(Name, ExportedSymbolsOnly);
156 MangledSymbols.reset();
157 return BaseLayer.addModuleSet(std::move(Ms), std::move(MemMgr),
158 std::move(Resolver));
166 const GlobalValue* addGlobalValue(StringMap<const GlobalValue*> &Names,
167 const GlobalValue &GV,
168 const Mangler &Mang, StringRef SearchName,
169 bool ExportedSymbolsOnly)
const {
171 if (GV.isDeclaration() || GV.hasCommonLinkage())
175 std::string MangledName;
177 raw_string_ostream MangledNameStream(MangledName);
178 Mang.getNameWithPrefix(MangledNameStream, &GV,
false);
183 if (MangledName == SearchName)
184 if (!ExportedSymbolsOnly || GV.hasDefaultVisibility())
188 Names[MangledName] = &GV;
194 const GlobalValue* buildMangledSymbols(StringRef SearchName,
195 bool ExportedSymbolsOnly)
const {
196 assert(!MangledSymbols &&
"Mangled symbols map already exists?");
198 auto Symbols = llvm::make_unique<StringMap<const GlobalValue*>>();
200 for (
const auto &M : Ms) {
203 for (
const auto &GO : M->global_objects())
204 if (
auto GV = addGlobalValue(*Symbols, GO, 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);
287 template <
typename BaseLayerT>
288 template <
typename ModuleSetT,
typename MemoryManagerPtrT,
289 typename SymbolResolverPtrT>
290 std::unique_ptr<typename LazyEmittingLayer<BaseLayerT>::EmissionDeferredSet>
291 LazyEmittingLayer<BaseLayerT>::EmissionDeferredSet::create(
292 BaseLayerT &
B, ModuleSetT Ms, MemoryManagerPtrT MemMgr,
293 SymbolResolverPtrT Resolver) {
294 typedef EmissionDeferredSetImpl<ModuleSetT, MemoryManagerPtrT, SymbolResolverPtrT>
296 return llvm::make_unique<EDS>(std::move(Ms), std::move(MemMgr),
297 std::move(Resolver));
303 #endif // LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H
BaseLayerT::ModuleSetHandleT BaseLayerHandleT
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.
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.
struct fuzzer::@269 Flags
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
void emitAndFinalize(ModuleSetHandleT H)
Immediately emit and finalize the moduleOB set represented by the given handle.
Flags for symbols in the JIT.
LazyEmittingLayer(BaseLayerT &BaseLayer)
Construct a lazy emitting layer.
uint64_t JITTargetAddress
Represents an address in the target process's address space.
ModuleSetListT::iterator ModuleSetHandleT
Handle to a set of loaded modules.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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.
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 ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.