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))
47 [this,ExportedSymbolsOnly,Name,&B]() -> TargetAddress {
48 if (EmitState == Emitting)
50 else if (EmitState != Emitted) {
55 return B.findSymbolIn(Handle, Name, ExportedSymbolsOnly)
61 // Calling "emit" can trigger external symbol lookup (e.g. to check for
62 // pre-existing definitions of common-symbol), but it will never find in
63 // this module that it would not have found already, so return null from
67 return B.findSymbolIn(Handle, Name, ExportedSymbolsOnly);
69 llvm_unreachable("Invalid emit-state.");
72 void removeModulesFromBaseLayer(BaseLayerT &BaseLayer) {
73 if (EmitState != NotEmitted)
74 BaseLayer.removeModuleSet(Handle);
77 template <typename ModuleSetT>
78 static std::unique_ptr<EmissionDeferredSet>
79 create(BaseLayerT &B, ModuleSetT Ms,
80 std::unique_ptr<RTDyldMemoryManager> MM);
83 virtual bool provides(StringRef Name, bool ExportedSymbolsOnly) const = 0;
84 virtual BaseLayerHandleT emit(BaseLayerT &BaseLayer) = 0;
87 enum { NotEmitted, Emitting, Emitted } EmitState;
88 BaseLayerHandleT Handle;
91 template <typename ModuleSetT>
92 class EmissionDeferredSetImpl : public EmissionDeferredSet {
94 EmissionDeferredSetImpl(ModuleSetT Ms,
95 std::unique_ptr<RTDyldMemoryManager> MM)
96 : Ms(std::move(Ms)), MM(std::move(MM)) {}
99 BaseLayerHandleT emit(BaseLayerT &BaseLayer) override {
100 // We don't need the mangled names set any more: Once we've emitted this
101 // to the base layer we'll just look for symbols there.
102 MangledNames.reset();
103 return BaseLayer.addModuleSet(std::move(Ms), std::move(MM));
106 bool provides(StringRef Name, bool ExportedSymbolsOnly) const override {
107 // FIXME: We could clean all this up if we had a way to reliably demangle
108 // names: We could just demangle name and search, rather than
109 // mangling everything else.
111 // If we have already built the mangled name set then just search it.
113 auto VI = MangledNames->find(Name);
114 if (VI == MangledNames->end())
116 return !ExportedSymbolsOnly || VI->second;
119 // If we haven't built the mangled name set yet, try to build it. As an
120 // optimization this will leave MangledNames set to nullptr if we find
121 // Name in the process of building the set.
122 buildMangledNames(Name, ExportedSymbolsOnly);
129 // If the mangled name of the given GlobalValue matches the given search
130 // name (and its visibility conforms to the ExportedSymbolsOnly flag) then
131 // just return 'true'. Otherwise, add the mangled name to the Names map and
133 bool addGlobalValue(StringMap<bool> &Names, const GlobalValue &GV,
134 const Mangler &Mang, StringRef SearchName,
135 bool ExportedSymbolsOnly) const {
136 // Modules don't "provide" decls or common symbols.
137 if (GV.isDeclaration() || GV.hasCommonLinkage())
140 // Mangle the GV name.
141 std::string MangledName;
143 raw_string_ostream MangledNameStream(MangledName);
144 Mang.getNameWithPrefix(MangledNameStream, &GV, false);
147 // Check whether this is the name we were searching for, and if it is then
149 if (MangledName == SearchName)
150 if (!ExportedSymbolsOnly || GV.hasDefaultVisibility())
153 // Otherwise add this to the map for later.
154 Names[MangledName] = GV.hasDefaultVisibility();
158 // Build the MangledNames map. Bails out early (with MangledNames left set
159 // to nullptr) if the given SearchName is found while building the map.
160 void buildMangledNames(StringRef SearchName,
161 bool ExportedSymbolsOnly) const {
162 assert(!MangledNames && "Mangled names map already exists?");
164 auto Names = llvm::make_unique<StringMap<bool>>();
166 for (const auto &M : Ms) {
167 Mangler Mang(M->getDataLayout());
169 for (const auto &GV : M->globals())
170 if (addGlobalValue(*Names, GV, Mang, SearchName, ExportedSymbolsOnly))
173 for (const auto &F : *M)
174 if (addGlobalValue(*Names, F, Mang, SearchName, ExportedSymbolsOnly))
178 MangledNames = std::move(Names);
182 std::unique_ptr<RTDyldMemoryManager> MM;
183 mutable std::unique_ptr<StringMap<bool>> MangledNames;
186 typedef std::list<std::unique_ptr<EmissionDeferredSet>> ModuleSetListT;
188 BaseLayerT &BaseLayer;
189 ModuleSetListT ModuleSetList;
192 /// @brief Handle to a set of loaded modules.
193 typedef typename ModuleSetListT::iterator ModuleSetHandleT;
195 /// @brief Construct a lazy emitting layer.
196 LazyEmittingLayer(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
198 /// @brief Add the given set of modules to the lazy emitting layer.
199 template <typename ModuleSetT>
200 ModuleSetHandleT addModuleSet(ModuleSetT Ms,
201 std::unique_ptr<RTDyldMemoryManager> MM) {
202 return ModuleSetList.insert(
204 EmissionDeferredSet::create(BaseLayer, std::move(Ms), std::move(MM)));
207 /// @brief Remove the module set represented by the given handle.
209 /// This method will free the memory associated with the given module set,
210 /// both in this layer, and the base layer.
211 void removeModuleSet(ModuleSetHandleT H) {
212 (*H)->removeModulesFromBaseLayer(BaseLayer);
213 ModuleSetList.erase(H);
216 /// @brief Search for the given named symbol.
217 /// @param Name The name of the symbol to search for.
218 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
219 /// @return A handle for the given named symbol, if it exists.
220 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
221 // Look for the symbol among existing definitions.
222 if (auto Symbol = BaseLayer.findSymbol(Name, ExportedSymbolsOnly))
225 // If not found then search the deferred sets. If any of these contain a
226 // definition of 'Name' then they will return a JITSymbol that will emit
227 // the corresponding module when the symbol address is requested.
228 for (auto &DeferredSet : ModuleSetList)
229 if (auto Symbol = DeferredSet->find(Name, ExportedSymbolsOnly, BaseLayer))
232 // If no definition found anywhere return a null symbol.
236 /// @brief Get the address of the given symbol in the context of the set of
237 /// compiled modules represented by the handle H.
238 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
239 bool ExportedSymbolsOnly) {
240 return (*H)->find(Name, ExportedSymbolsOnly, BaseLayer);
244 template <typename BaseLayerT>
245 template <typename ModuleSetT>
246 std::unique_ptr<typename LazyEmittingLayer<BaseLayerT>::EmissionDeferredSet>
247 LazyEmittingLayer<BaseLayerT>::EmissionDeferredSet::create(
248 BaseLayerT &B, ModuleSetT Ms, std::unique_ptr<RTDyldMemoryManager> MM) {
249 return llvm::make_unique<EmissionDeferredSetImpl<ModuleSetT>>(std::move(Ms),
254 #endif // LLVM_EXECUTIONENGINE_ORC_LAZYEMITTINGLAYER_H