1 //===- LazyEmittingLayer.h - Lazily emit IR to lower JIT layers -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Contains the definition for a lazy-emitting layer for the JIT.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ExecutionEngine/JITSymbol.h"
21 #include "llvm/ExecutionEngine/Orc/Core.h"
22 #include "llvm/IR/GlobalValue.h"
23 #include "llvm/IR/Mangler.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
36 /// Lazy-emitting IR layer.
38 /// This layer accepts LLVM IR Modules (via addModule), but does not
39 /// immediately emit them the layer below. Instead, emissing to the base layer
40 /// is deferred until the first time the client requests the address (via
41 /// JITSymbol::getAddress) for a symbol contained in this layer.
42 template <typename BaseLayerT> class LazyEmittingLayer {
44 class EmissionDeferredModule {
46 EmissionDeferredModule(VModuleKey K, std::unique_ptr<Module> M)
47 : K(std::move(K)), M(std::move(M)) {}
49 JITSymbol find(StringRef Name, bool ExportedSymbolsOnly, BaseLayerT &B) {
52 if (auto GV = searchGVs(Name, ExportedSymbolsOnly)) {
53 // Create a std::string version of Name to capture here - the argument
54 // (a StringRef) may go away before the lambda is executed.
55 // FIXME: Use capture-init when we move to C++14.
56 std::string PName = Name;
57 JITSymbolFlags Flags = JITSymbolFlags::fromGlobalValue(*GV);
59 [this, ExportedSymbolsOnly, PName, &B]() -> Expected<JITTargetAddress> {
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);
73 llvm_unreachable("Successful symbol lookup should return "
74 "definition address here");
76 return JITSymbol(std::move(GetAddress), Flags);
80 // Calling "emit" can trigger a recursive call to 'find' (e.g. to check
81 // for pre-existing definitions of common-symbol), but any symbol in
82 // this module would already have been found internally (in the
83 // RuntimeDyld that did the lookup), so just return a nullptr here.
86 return B.findSymbolIn(K, Name, ExportedSymbolsOnly);
88 llvm_unreachable("Invalid emit-state.");
91 Error removeModuleFromBaseLayer(BaseLayerT& BaseLayer) {
92 return EmitState != NotEmitted ? BaseLayer.removeModule(K)
96 void emitAndFinalize(BaseLayerT &BaseLayer) {
97 assert(EmitState != Emitting &&
98 "Cannot emitAndFinalize while already emitting");
99 if (EmitState == NotEmitted) {
100 EmitState = Emitting;
101 emitToBaseLayer(BaseLayer);
104 BaseLayer.emitAndFinalize(K);
109 const GlobalValue* searchGVs(StringRef Name,
110 bool ExportedSymbolsOnly) const {
111 // FIXME: We could clean all this up if we had a way to reliably demangle
112 // names: We could just demangle name and search, rather than
113 // mangling everything else.
115 // If we have already built the mangled name set then just search it.
116 if (MangledSymbols) {
117 auto VI = MangledSymbols->find(Name);
118 if (VI == MangledSymbols->end())
120 auto GV = VI->second;
121 if (!ExportedSymbolsOnly || GV->hasDefaultVisibility())
126 // If we haven't built the mangled name set yet, try to build it. As an
127 // optimization this will leave MangledNames set to nullptr if we find
128 // Name in the process of building the set.
129 return buildMangledSymbols(Name, ExportedSymbolsOnly);
132 Error emitToBaseLayer(BaseLayerT &BaseLayer) {
133 // We don't need the mangled names set any more: Once we've emitted this
134 // to the base layer we'll just look for symbols there.
135 MangledSymbols.reset();
136 return BaseLayer.addModule(std::move(K), std::move(M));
139 // If the mangled name of the given GlobalValue matches the given search
140 // name (and its visibility conforms to the ExportedSymbolsOnly flag) then
141 // return the symbol. Otherwise, add the mangled name to the Names map and
143 const GlobalValue* addGlobalValue(StringMap<const GlobalValue*> &Names,
144 const GlobalValue &GV,
145 const Mangler &Mang, StringRef SearchName,
146 bool ExportedSymbolsOnly) const {
147 // Modules don't "provide" decls or common symbols.
148 if (GV.isDeclaration() || GV.hasCommonLinkage())
151 // Mangle the GV name.
152 std::string MangledName;
154 raw_string_ostream MangledNameStream(MangledName);
155 Mang.getNameWithPrefix(MangledNameStream, &GV, false);
158 // Check whether this is the name we were searching for, and if it is then
160 if (MangledName == SearchName)
161 if (!ExportedSymbolsOnly || GV.hasDefaultVisibility())
164 // Otherwise add this to the map for later.
165 Names[MangledName] = &GV;
169 // Build the MangledSymbols map. Bails out early (with MangledSymbols left set
170 // to nullptr) if the given SearchName is found while building the map.
171 const GlobalValue* buildMangledSymbols(StringRef SearchName,
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;
199 /// Construct a lazy emitting layer.
200 LazyEmittingLayer(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
202 /// Add the given module to the lazy emitting layer.
203 Error addModule(VModuleKey K, std::unique_ptr<Module> M) {
204 assert(!ModuleMap.count(K) && "VModuleKey K already in use");
206 llvm::make_unique<EmissionDeferredModule>(std::move(K), std::move(M));
207 return Error::success();
210 /// Remove the module represented by the given handle.
212 /// This method will free the memory associated with the given module, both
213 /// in this layer, and the base layer.
214 Error removeModule(VModuleKey K) {
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);
222 /// Search for the given named symbol.
223 /// @param Name The name of the symbol to search for.
224 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
225 /// @return A handle for the given named symbol, if it exists.
226 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
227 // Look for the symbol among existing definitions.
228 if (auto Symbol = BaseLayer.findSymbol(Name, ExportedSymbolsOnly))
231 // If not found then search the deferred modules. If any of these contain a
232 // definition of 'Name' then they will return a JITSymbol that will emit
233 // the corresponding module when the symbol address is requested.
234 for (auto &KV : ModuleMap)
235 if (auto Symbol = KV.second->find(Name, ExportedSymbolsOnly, BaseLayer))
238 // If no definition found anywhere return a null symbol.
242 /// Get the address of the given symbol in the context of the of
243 /// compiled modules represented by the key K.
244 JITSymbol findSymbolIn(VModuleKey K, const std::string &Name,
245 bool ExportedSymbolsOnly) {
246 assert(ModuleMap.count(K) && "VModuleKey K not valid here");
247 return ModuleMap[K]->find(Name, ExportedSymbolsOnly, BaseLayer);
250 /// Immediately emit and finalize the module represented by the given
252 Error emitAndFinalize(VModuleKey K) {
253 assert(ModuleMap.count(K) && "VModuleKey K not valid here");
254 return ModuleMap[K]->emitAndFinalize(BaseLayer);
258 } // end namespace orc
259 } // end namespace llvm
261 #endif // LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H