Skip to content

Commit 7fb13a9

Browse files
committed
[ORC] lazyReexports: Swap IndirectStubsManager for RedirectableSymbolsManager.
RedirectableSymbolsManager is a native SymbolStringPtr API (requires fewer string operations) and has a narrower interface that permits a wider range of implementations. IndirectStubsManager is updated to make it a RedirectableSymbolsManager so that existing uses continue to work.
1 parent 3a26feb commit 7fb13a9

File tree

4 files changed

+60
-29
lines changed

4 files changed

+60
-29
lines changed

llvm/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/ExecutionEngine/JITSymbol.h"
1919
#include "llvm/ExecutionEngine/Orc/Core.h"
2020
#include "llvm/ExecutionEngine/Orc/OrcABISupport.h"
21+
#include "llvm/ExecutionEngine/Orc/RedirectionManager.h"
2122
#include "llvm/Support/Error.h"
2223
#include "llvm/Support/Memory.h"
2324
#include "llvm/Support/Process.h"
@@ -278,7 +279,7 @@ class LocalJITCompileCallbackManager : public JITCompileCallbackManager {
278279
};
279280

280281
/// Base class for managing collections of named indirect stubs.
281-
class IndirectStubsManager {
282+
class IndirectStubsManager : public RedirectableSymbolManager {
282283
public:
283284
/// Map type for initializing the manager. See init.
284285
using StubInitsMap = StringMap<std::pair<ExecutorAddr, JITSymbolFlags>>;
@@ -305,8 +306,15 @@ class IndirectStubsManager {
305306
/// Change the value of the implementation pointer for the stub.
306307
virtual Error updatePointer(StringRef Name, ExecutorAddr NewAddr) = 0;
307308

309+
/// --- RedirectableSymbolManager implementation ---
310+
Error redirect(JITDylib &JD, const SymbolMap &NewDests) override;
311+
312+
void
313+
emitRedirectableSymbols(std::unique_ptr<MaterializationResponsibility> MR,
314+
SymbolMap InitialDests) override;
315+
308316
private:
309-
virtual void anchor();
317+
void anchor() override;
310318
};
311319

312320
template <typename ORCABI> class LocalIndirectStubsInfo {

llvm/include/llvm/ExecutionEngine/Orc/LazyReexports.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ADT/STLExtras.h"
2020
#include "llvm/ExecutionEngine/Orc/Core.h"
2121
#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
22+
#include "llvm/ExecutionEngine/Orc/RedirectionManager.h"
2223
#include "llvm/ExecutionEngine/Orc/Speculation.h"
2324

2425
namespace llvm {
@@ -140,7 +141,7 @@ createLocalLazyCallThroughManager(const Triple &T, ExecutionSession &ES,
140141
class LazyReexportsMaterializationUnit : public MaterializationUnit {
141142
public:
142143
LazyReexportsMaterializationUnit(LazyCallThroughManager &LCTManager,
143-
IndirectStubsManager &ISManager,
144+
RedirectableSymbolManager &RSManager,
144145
JITDylib &SourceJD,
145146
SymbolAliasMap CallableAliases,
146147
ImplSymbolMap *SrcJDLoc);
@@ -154,7 +155,7 @@ class LazyReexportsMaterializationUnit : public MaterializationUnit {
154155
extractFlags(const SymbolAliasMap &Aliases);
155156

156157
LazyCallThroughManager &LCTManager;
157-
IndirectStubsManager &ISManager;
158+
RedirectableSymbolManager &RSManager;
158159
JITDylib &SourceJD;
159160
SymbolAliasMap CallableAliases;
160161
ImplSymbolMap *AliaseeTable;
@@ -165,11 +166,11 @@ class LazyReexportsMaterializationUnit : public MaterializationUnit {
165166
/// first call. All subsequent calls will go directly to the aliasee.
166167
inline std::unique_ptr<LazyReexportsMaterializationUnit>
167168
lazyReexports(LazyCallThroughManager &LCTManager,
168-
IndirectStubsManager &ISManager, JITDylib &SourceJD,
169+
RedirectableSymbolManager &RSManager, JITDylib &SourceJD,
169170
SymbolAliasMap CallableAliases,
170171
ImplSymbolMap *SrcJDLoc = nullptr) {
171172
return std::make_unique<LazyReexportsMaterializationUnit>(
172-
LCTManager, ISManager, SourceJD, std::move(CallableAliases), SrcJDLoc);
173+
LCTManager, RSManager, SourceJD, std::move(CallableAliases), SrcJDLoc);
173174
}
174175

175176
} // End namespace orc

llvm/lib/ExecutionEngine/Orc/IndirectionUtils.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,38 @@ JITCompileCallbackManager::executeCompileCallback(ExecutorAddr TrampolineAddr) {
115115
}
116116
}
117117

118+
Error IndirectStubsManager::redirect(JITDylib &JD, const SymbolMap &NewDests) {
119+
for (auto &[Name, Dest] : NewDests)
120+
if (auto Err = updatePointer(*Name, Dest.getAddress()))
121+
return Err;
122+
return Error::success();
123+
}
124+
125+
void IndirectStubsManager::emitRedirectableSymbols(
126+
std::unique_ptr<MaterializationResponsibility> MR, SymbolMap InitialDests) {
127+
StubInitsMap StubInits;
128+
for (auto &[Name, Dest] : InitialDests)
129+
StubInits[*Name] = {Dest.getAddress(), Dest.getFlags()};
130+
if (auto Err = createStubs(StubInits)) {
131+
MR->getExecutionSession().reportError(std::move(Err));
132+
return MR->failMaterialization();
133+
}
134+
SymbolMap Stubs;
135+
for (auto &[Name, Dest] : InitialDests) {
136+
auto StubSym = findStub(*Name, false);
137+
assert(StubSym.getAddress() && "Stub symbol should be present");
138+
Stubs[Name] = StubSym;
139+
}
140+
if (auto Err = MR->notifyResolved(Stubs)) {
141+
MR->getExecutionSession().reportError(std::move(Err));
142+
return MR->failMaterialization();
143+
}
144+
if (auto Err = MR->notifyEmitted({})) {
145+
MR->getExecutionSession().reportError(std::move(Err));
146+
return MR->failMaterialization();
147+
}
148+
}
149+
118150
Expected<std::unique_ptr<JITCompileCallbackManager>>
119151
createLocalCompileCallbackManager(const Triple &T, ExecutionSession &ES,
120152
ExecutorAddr ErrorHandlerAddress) {

llvm/lib/ExecutionEngine/Orc/LazyReexports.cpp

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ createLocalLazyCallThroughManager(const Triple &T, ExecutionSession &ES,
151151
}
152152

153153
LazyReexportsMaterializationUnit::LazyReexportsMaterializationUnit(
154-
LazyCallThroughManager &LCTManager, IndirectStubsManager &ISManager,
154+
LazyCallThroughManager &LCTManager, RedirectableSymbolManager &RSManager,
155155
JITDylib &SourceJD, SymbolAliasMap CallableAliases, ImplSymbolMap *SrcJDLoc)
156156
: MaterializationUnit(extractFlags(CallableAliases)),
157-
LCTManager(LCTManager), ISManager(ISManager), SourceJD(SourceJD),
157+
LCTManager(LCTManager), RSManager(RSManager), SourceJD(SourceJD),
158158
CallableAliases(std::move(CallableAliases)), AliaseeTable(SrcJDLoc) {}
159159

160160
StringRef LazyReexportsMaterializationUnit::getName() const {
@@ -174,51 +174,41 @@ void LazyReexportsMaterializationUnit::materialize(
174174
}
175175

176176
if (!CallableAliases.empty())
177-
if (auto Err = R->replace(lazyReexports(LCTManager, ISManager, SourceJD,
177+
if (auto Err = R->replace(lazyReexports(LCTManager, RSManager, SourceJD,
178178
std::move(CallableAliases),
179179
AliaseeTable))) {
180180
R->getExecutionSession().reportError(std::move(Err));
181181
R->failMaterialization();
182182
return;
183183
}
184184

185-
IndirectStubsManager::StubInitsMap StubInits;
185+
SymbolMap Inits;
186186
for (auto &Alias : RequestedAliases) {
187-
188187
auto CallThroughTrampoline = LCTManager.getCallThroughTrampoline(
189188
SourceJD, Alias.second.Aliasee,
190-
[&ISManager = this->ISManager,
189+
[&TargetJD = R->getTargetJITDylib(), &RSManager = this->RSManager,
191190
StubSym = Alias.first](ExecutorAddr ResolvedAddr) -> Error {
192-
return ISManager.updatePointer(*StubSym, ResolvedAddr);
191+
return RSManager.redirect(TargetJD, StubSym,
192+
ExecutorSymbolDef(ResolvedAddr, {}));
193193
});
194194

195195
if (!CallThroughTrampoline) {
196-
SourceJD.getExecutionSession().reportError(
197-
CallThroughTrampoline.takeError());
196+
R->getExecutionSession().reportError(CallThroughTrampoline.takeError());
198197
R->failMaterialization();
199198
return;
200199
}
201200

202-
StubInits[*Alias.first] =
203-
std::make_pair(*CallThroughTrampoline, Alias.second.AliasFlags);
201+
Inits[Alias.first] = {*CallThroughTrampoline, Alias.second.AliasFlags};
204202
}
205203

206204
if (AliaseeTable != nullptr && !RequestedAliases.empty())
207205
AliaseeTable->trackImpls(RequestedAliases, &SourceJD);
208206

209-
if (auto Err = ISManager.createStubs(StubInits)) {
210-
SourceJD.getExecutionSession().reportError(std::move(Err));
211-
R->failMaterialization();
212-
return;
207+
if (auto Err = R->replace(std::make_unique<RedirectableMaterializationUnit>(
208+
RSManager, std::move(Inits)))) {
209+
R->getExecutionSession().reportError(std::move(Err));
210+
return R->failMaterialization();
213211
}
214-
215-
SymbolMap Stubs;
216-
for (auto &Alias : RequestedAliases)
217-
Stubs[Alias.first] = ISManager.findStub(*Alias.first, false);
218-
219-
// No registered dependencies, so these calls cannot fail.
220-
cantFail(R->notifyResolved(Stubs));
221-
cantFail(R->notifyEmitted({}));
222212
}
223213

224214
void LazyReexportsMaterializationUnit::discard(const JITDylib &JD,

0 commit comments

Comments
 (0)