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 "JITSymbol.h"
18 #include "LookasideRTDyldMM.h"
19 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
20 #include "llvm/IR/GlobalValue.h"
21 #include "llvm/IR/Mangler.h"
26 /// @brief Lazy-emitting IR layer.
28 /// This layer accepts sets of LLVM IR Modules (via addModuleSet), but does
29 /// not immediately emit them the layer below. Instead, emissing to the base
30 /// layer is deferred until the first time the client requests the address
31 /// (via JITSymbol::getAddress) for a symbol contained in this layer.
32 template <typename BaseLayerT> class LazyEmittingLayer {
34 typedef typename BaseLayerT::ModuleSetHandleT BaseLayerHandleT;
37 class EmissionDeferredSet {
39 EmissionDeferredSet() : EmitState(NotEmitted) {}
40 virtual ~EmissionDeferredSet() {}
42 JITSymbol find(StringRef Name, bool ExportedSymbolsOnly, BaseLayerT &B) {
45 if (provides(Name, ExportedSymbolsOnly)) {
46 // Create a std::string version of Name to capture here - the argument
47 // (a StringRef) may go away before the lambda is executed.
48 // FIXME: Use capture-init when we move to C++14.
49 std::string PName = Name;
51 [this, ExportedSymbolsOnly, PName, &B]() -> TargetAddress {
52 if (this->EmitState == Emitting)
54 else if (this->EmitState == NotEmitted) {
55 this->EmitState = Emitting;
56 Handle = this->emit(B);
57 this->EmitState = Emitted;
59 return B.findSymbolIn(Handle, PName, ExportedSymbolsOnly)
65 // Calling "emit" can trigger external symbol lookup (e.g. to check for
66 // pre-existing definitions of common-symbol), but it will never find in
67 // this module that it would not have found already, so return null from
71 return B.findSymbolIn(Handle, Name, ExportedSymbolsOnly);
73 llvm_unreachable("Invalid emit-state.");
76 void removeModulesFromBaseLayer(BaseLayerT &BaseLayer) {
77 if (EmitState != NotEmitted)
78 BaseLayer.removeModuleSet(Handle);
81 template <typename ModuleSetT>
82 static std::unique_ptr<EmissionDeferredSet>
83 create(BaseLayerT &B, ModuleSetT Ms,
84 std::unique_ptr<RTDyldMemoryManager> MM);
87 virtual bool provides(StringRef Name, bool ExportedSymbolsOnly) const = 0;
88 virtual BaseLayerHandleT emit(BaseLayerT &BaseLayer) = 0;
91 enum { NotEmitted, Emitting, Emitted } EmitState;
92 BaseLayerHandleT Handle;
95 template <typename ModuleSetT>
96 class EmissionDeferredSetImpl : public EmissionDeferredSet {
98 EmissionDeferredSetImpl(ModuleSetT Ms,
99 std::unique_ptr<RTDyldMemoryManager> MM)
100 : Ms(std::move(Ms)), MM(std::move(MM)) {}
103 BaseLayerHandleT emit(BaseLayerT &BaseLayer) override {
104 // We don't need the mangled names set any more: Once we've emitted this
105 // to the base layer we'll just look for symbols there.
106 MangledNames.reset();
107 return BaseLayer.addModuleSet(std::move(Ms), std::move(MM));
110 bool provides(StringRef Name, bool ExportedSymbolsOnly) const override {
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.
117 auto VI = MangledNames->find(Name);
118 if (VI == MangledNames->end())
120 return !ExportedSymbolsOnly || VI->second;
123 // If we haven't built the mangled name set yet, try to build it. As an
124 // optimization this will leave MangledNames set to nullptr if we find
125 // Name in the process of building the set.
126 buildMangledNames(Name, ExportedSymbolsOnly);
133 // If the mangled name of the given GlobalValue matches the given search
134 // name (and its visibility conforms to the ExportedSymbolsOnly flag) then
135 // just return 'true'. Otherwise, add the mangled name to the Names map and
137 bool addGlobalValue(StringMap<bool> &Names, const GlobalValue &GV,
138 const Mangler &Mang, StringRef SearchName,
139 bool ExportedSymbolsOnly) const {
140 // Modules don't "provide" decls or common symbols.
141 if (GV.isDeclaration() || GV.hasCommonLinkage())
144 // Mangle the GV name.
145 std::string MangledName;
147 raw_string_ostream MangledNameStream(MangledName);
148 Mang.getNameWithPrefix(MangledNameStream, &GV, false);
151 // Check whether this is the name we were searching for, and if it is then
153 if (MangledName == SearchName)
154 if (!ExportedSymbolsOnly || GV.hasDefaultVisibility())
157 // Otherwise add this to the map for later.
158 Names[MangledName] = GV.hasDefaultVisibility();
162 // Build the MangledNames map. Bails out early (with MangledNames left set
163 // to nullptr) if the given SearchName is found while building the map.
164 void buildMangledNames(StringRef SearchName,
165 bool ExportedSymbolsOnly) const {
166 assert(!MangledNames && "Mangled names map already exists?");
168 auto Names = llvm::make_unique<StringMap<bool>>();
170 for (const auto &M : Ms) {
171 Mangler Mang(M->getDataLayout());
173 for (const auto &GV : M->globals())
174 if (addGlobalValue(*Names, GV, Mang, SearchName, ExportedSymbolsOnly))
177 for (const auto &F : *M)
178 if (addGlobalValue(*Names, F, Mang, SearchName, ExportedSymbolsOnly))
182 MangledNames = std::move(Names);
186 std::unique_ptr<RTDyldMemoryManager> MM;
187 mutable std::unique_ptr<StringMap<bool>> MangledNames;
190 typedef std::list<std::unique_ptr<EmissionDeferredSet>> ModuleSetListT;
192 BaseLayerT &BaseLayer;
193 ModuleSetListT ModuleSetList;
196 /// @brief Handle to a set of loaded modules.
197 typedef typename ModuleSetListT::iterator ModuleSetHandleT;
199 /// @brief Construct a lazy emitting layer.
200 LazyEmittingLayer(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
202 /// @brief Add the given set of modules to the lazy emitting layer.
203 template <typename ModuleSetT>
204 ModuleSetHandleT addModuleSet(ModuleSetT Ms,
205 std::unique_ptr<RTDyldMemoryManager> MM) {
206 return ModuleSetList.insert(
208 EmissionDeferredSet::create(BaseLayer, std::move(Ms), std::move(MM)));
211 /// @brief Remove the module set represented by the given handle.
213 /// This method will free the memory associated with the given module set,
214 /// both in this layer, and the base layer.
215 void removeModuleSet(ModuleSetHandleT H) {
216 (*H)->removeModulesFromBaseLayer(BaseLayer);
217 ModuleSetList.erase(H);
220 /// @brief Search for the given named symbol.
221 /// @param Name The name of the symbol to search for.
222 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
223 /// @return A handle for the given named symbol, if it exists.
224 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
225 // Look for the symbol among existing definitions.
226 if (auto Symbol = BaseLayer.findSymbol(Name, ExportedSymbolsOnly))
229 // If not found then search the deferred sets. If any of these contain a
230 // definition of 'Name' then they will return a JITSymbol that will emit
231 // the corresponding module when the symbol address is requested.
232 for (auto &DeferredSet : ModuleSetList)
233 if (auto Symbol = DeferredSet->find(Name, ExportedSymbolsOnly, BaseLayer))
236 // If no definition found anywhere return a null symbol.
240 /// @brief Get the address of the given symbol in the context of the set of
241 /// compiled modules represented by the handle H.
242 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
243 bool ExportedSymbolsOnly) {
244 return (*H)->find(Name, ExportedSymbolsOnly, BaseLayer);
248 template <typename BaseLayerT>
249 template <typename ModuleSetT>
250 std::unique_ptr<typename LazyEmittingLayer<BaseLayerT>::EmissionDeferredSet>
251 LazyEmittingLayer<BaseLayerT>::EmissionDeferredSet::create(
252 BaseLayerT &B, ModuleSetT Ms, std::unique_ptr<RTDyldMemoryManager> MM) {
253 return llvm::make_unique<EmissionDeferredSetImpl<ModuleSetT>>(std::move(Ms),
258 #endif // LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H