LLVM 17.0.0git
LazyReexports.h
Go to the documentation of this file.
1//===------ LazyReexports.h -- Utilities for lazy reexports -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Lazy re-exports are similar to normal re-exports, except that for callable
10// symbols the definitions are replaced with trampolines that will look up and
11// call through to the re-exported symbol at runtime. This can be used to
12// enable lazy compilation.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_EXECUTIONENGINE_ORC_LAZYREEXPORTS_H
17#define LLVM_EXECUTIONENGINE_ORC_LAZYREEXPORTS_H
18
19#include "llvm/ADT/STLExtras.h"
23
24namespace llvm {
25
26class Triple;
27
28namespace orc {
29
30/// Manages a set of 'lazy call-through' trampolines. These are compiler
31/// re-entry trampolines that are pre-bound to look up a given symbol in a given
32/// JITDylib, then jump to that address. Since compilation of symbols is
33/// triggered on first lookup, these call-through trampolines can be used to
34/// implement lazy compilation.
35///
36/// The easiest way to construct these call-throughs is using the lazyReexport
37/// function.
39public:
42
44 JITTargetAddress ErrorHandlerAddr, TrampolinePool *TP);
45
46 // Return a free call-through trampoline and bind it to look up and call
47 // through to the given symbol.
50 NotifyResolvedFunction NotifyResolved);
51
53 JITTargetAddress TrampolineAddr,
55
56 virtual ~LazyCallThroughManager() = default;
57
58protected:
61
65 };
66
70 JITTargetAddress ResolvedAddr);
71 void setTrampolinePool(TrampolinePool &TP) { this->TP = &TP; }
72
73private:
74 using ReexportsMap = std::map<JITTargetAddress, ReexportsEntry>;
75
76 using NotifiersMap = std::map<JITTargetAddress, NotifyResolvedFunction>;
77
78 std::mutex LCTMMutex;
80 JITTargetAddress ErrorHandlerAddr;
81 TrampolinePool *TP = nullptr;
82 ReexportsMap Reexports;
83 NotifiersMap Notifiers;
84};
85
86/// A lazy call-through manager that builds trampolines in the current process.
88private:
90
92 JITTargetAddress ErrorHandlerAddr)
93 : LazyCallThroughManager(ES, ErrorHandlerAddr, nullptr) {}
94
95 template <typename ORCABI> Error init() {
97 [this](JITTargetAddress TrampolineAddr,
99 NotifyLandingResolved) {
100 resolveTrampolineLandingAddress(TrampolineAddr,
101 std::move(NotifyLandingResolved));
102 });
103
104 if (!TP)
105 return TP.takeError();
106
107 this->TP = std::move(*TP);
108 setTrampolinePool(*this->TP);
109 return Error::success();
110 }
111
112 std::unique_ptr<TrampolinePool> TP;
113
114public:
115 /// Create a LocalLazyCallThroughManager using the given ABI. See
116 /// createLocalLazyCallThroughManager.
117 template <typename ORCABI>
119 Create(ExecutionSession &ES, JITTargetAddress ErrorHandlerAddr) {
120 auto LLCTM = std::unique_ptr<LocalLazyCallThroughManager>(
121 new LocalLazyCallThroughManager(ES, ErrorHandlerAddr));
122
123 if (auto Err = LLCTM->init<ORCABI>())
124 return std::move(Err);
125
126 return std::move(LLCTM);
127 }
128};
129
130/// Create a LocalLazyCallThroughManager from the given triple and execution
131/// session.
133createLocalLazyCallThroughManager(const Triple &T, ExecutionSession &ES,
134 JITTargetAddress ErrorHandlerAddr);
135
136/// A materialization unit that builds lazy re-exports. These are callable
137/// entry points that call through to the given symbols.
138/// Unlike a 'true' re-export, the address of the lazy re-export will not
139/// match the address of the re-exported symbol, but calling it will behave
140/// the same as calling the re-exported symbol.
142public:
144 IndirectStubsManager &ISManager,
145 JITDylib &SourceJD,
146 SymbolAliasMap CallableAliases,
147 ImplSymbolMap *SrcJDLoc);
148
149 StringRef getName() const override;
150
151private:
152 void materialize(std::unique_ptr<MaterializationResponsibility> R) override;
153 void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
155 extractFlags(const SymbolAliasMap &Aliases);
156
157 LazyCallThroughManager &LCTManager;
158 IndirectStubsManager &ISManager;
159 JITDylib &SourceJD;
160 SymbolAliasMap CallableAliases;
161 ImplSymbolMap *AliaseeTable;
162};
163
164/// Define lazy-reexports based on the given SymbolAliasMap. Each lazy re-export
165/// is a callable symbol that will look up and dispatch to the given aliasee on
166/// first call. All subsequent calls will go directly to the aliasee.
167inline std::unique_ptr<LazyReexportsMaterializationUnit>
169 IndirectStubsManager &ISManager, JITDylib &SourceJD,
170 SymbolAliasMap CallableAliases,
171 ImplSymbolMap *SrcJDLoc = nullptr) {
172 return std::make_unique<LazyReexportsMaterializationUnit>(
173 LCTManager, ISManager, SourceJD, std::move(CallableAliases), SrcJDLoc);
174}
175
176} // End namespace orc
177} // End namespace llvm
178
179#endif // LLVM_EXECUTIONENGINE_ORC_LAZYREEXPORTS_H
std::string Name
This file contains some templates that are useful if you are working with the STL at all.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
Tagged union holding either a T or a Error.
Definition: Error.h:470
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
An ExecutionSession represents a running JIT program.
Definition: Core.h:1373
Base class for managing collections of named indirect stubs.
Represents a JIT'd dynamic library.
Definition: Core.h:962
Manages a set of 'lazy call-through' trampolines.
Definition: LazyReexports.h:38
void resolveTrampolineLandingAddress(JITTargetAddress TrampolineAddr, TrampolinePool::NotifyLandingResolvedFunction NotifyLandingResolved)
JITTargetAddress reportCallThroughError(Error Err)
Expected< JITTargetAddress > getCallThroughTrampoline(JITDylib &SourceJD, SymbolStringPtr SymbolName, NotifyResolvedFunction NotifyResolved)
Error notifyResolved(JITTargetAddress TrampolineAddr, JITTargetAddress ResolvedAddr)
void setTrampolinePool(TrampolinePool &TP)
Definition: LazyReexports.h:71
virtual ~LazyCallThroughManager()=default
Expected< ReexportsEntry > findReexport(JITTargetAddress TrampolineAddr)
A materialization unit that builds lazy re-exports.
StringRef getName() const override
Return the name of this materialization unit.
A lazy call-through manager that builds trampolines in the current process.
Definition: LazyReexports.h:87
static Expected< std::unique_ptr< LocalLazyCallThroughManager > > Create(ExecutionSession &ES, JITTargetAddress ErrorHandlerAddr)
Create a LocalLazyCallThroughManager using the given ABI.
static Expected< std::unique_ptr< LocalTrampolinePool > > Create(ResolveLandingFunction ResolveLanding)
Creates a LocalTrampolinePool with the given RunCallback function.
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group,...
Definition: Core.h:673
Pointer to a pooled string representing a symbol name.
Base class for pools of compiler re-entry trampolines.
unique_function< void(JITTargetAddress) const > NotifyLandingResolvedFunction
unique_function is a type-erasing functor similar to std::function.
std::unique_ptr< LazyReexportsMaterializationUnit > lazyReexports(LazyCallThroughManager &LCTManager, IndirectStubsManager &ISManager, JITDylib &SourceJD, SymbolAliasMap CallableAliases, ImplSymbolMap *SrcJDLoc=nullptr)
Define lazy-reexports based on the given SymbolAliasMap.
Expected< std::unique_ptr< LazyCallThroughManager > > createLocalLazyCallThroughManager(const Triple &T, ExecutionSession &ES, JITTargetAddress ErrorHandlerAddr)
Create a LocalLazyCallThroughManager from the given triple and execution session.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
uint64_t JITTargetAddress
Represents an address in the target process's address space.
Definition: JITSymbol.h:42