Skip to content

Commit 6abc5c5

Browse files
author
Zak Kent
committed
[Immediate] Re-introduce lazy reexports
1 parent 48c36c9 commit 6abc5c5

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

include/swift/Immediate/SwiftMaterializationUnit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class SwiftJIT {
6666
llvm::Expected<llvm::orc::ExecutorAddr>
6767
lookupLinkerMangled(llvm::StringRef Name);
6868

69+
std::string mangle(llvm::StringRef Name);
70+
6971
llvm::orc::SymbolStringPtr mangleAndIntern(llvm::StringRef Name);
7072

7173
llvm::orc::SymbolStringPtr intern(llvm::StringRef Name);

lib/Immediate/SwiftMaterializationUnit.cpp

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ using namespace swift;
4949
static const std::string ManglingSuffix = "$impl";
5050

5151
/// Mangle a function for a lazy reexport
52-
static std::string mangleFunctionBody(const StringRef Unmangled) {
52+
static std::string mangle(const StringRef Unmangled) {
5353
return Unmangled.str() + ManglingSuffix;
5454
}
5555

@@ -59,8 +59,9 @@ static bool isMangled(const StringRef Symbol) {
5959
}
6060

6161
/// Demangle a lazy reexport
62-
static StringRef demangleFunctionBody(const StringRef Mangled) {
63-
return Mangled.drop_back(ManglingSuffix.size());
62+
static StringRef demangle(const StringRef Mangled) {
63+
return isMangled(Mangled) ? Mangled.drop_back(ManglingSuffix.size())
64+
: Mangled;
6465
}
6566

6667
llvm::Expected<std::unique_ptr<SwiftJIT>>
@@ -114,6 +115,8 @@ SwiftJIT::lookupLinkerMangled(llvm::StringRef Name) {
114115
return J->lookupLinkerMangled(Name);
115116
}
116117

118+
std::string SwiftJIT::mangle(StringRef Name) { return J->mangle(Name); }
119+
117120
llvm::orc::SymbolStringPtr SwiftJIT::mangleAndIntern(StringRef Name) {
118121
return J->mangleAndIntern(Name);
119122
}
@@ -167,29 +170,28 @@ renameFunctionBodies(llvm::orc::MaterializationResponsibility &MR,
167170
using namespace llvm;
168171
using namespace llvm::orc;
169172

170-
llvm::DenseSet<StringRef> ToRename;
173+
llvm::StringSet<> ToRename;
171174
for (auto &KV : MR.getSymbols()) {
172-
const auto &Name = *KV.first;
175+
StringRef Name = *KV.first;
173176
if (isMangled(Name))
174177
// All mangled functions we are responsible for
175178
// materializing must be mangled at the object levels
176-
ToRename.insert(demangleFunctionBody(Name));
179+
ToRename.insert(demangle(Name));
177180
}
178181
for (auto &Sec : G.sections()) {
179182
// Skip non-executable sections.
180183
if ((Sec.getMemProt() & MemProt::Exec) == MemProt::None)
181184
continue;
182185

183186
for (auto *Sym : Sec.symbols()) {
184-
185187
// Skip all anonymous and non-callables.
186188
if (!Sym->hasName() || !Sym->isCallable())
187189
continue;
188190

189191
if (ToRename.count(Sym->getName())) {
190192
// FIXME: Get rid of the temporary when Swift's llvm-project is
191193
// updated to LLVM 17.
192-
auto NewName = G.allocateString(mangleFunctionBody(Sym->getName()));
194+
auto NewName = G.allocateString(mangle(Sym->getName()));
193195
Sym->setName({NewName.data(), NewName.size()});
194196
}
195197
}
@@ -296,7 +298,8 @@ SwiftMaterializationUnit::Create(SwiftJIT &JIT, CompilerInstance &CI) {
296298
const auto &SymbolName = Entry.getKey();
297299
const auto Flags =
298300
llvm::JITSymbolFlags::Exported | llvm::JITSymbolFlags::Callable;
299-
PublicInterface[JIT.intern(SymbolName)] = Flags;
301+
auto MangledName = mangle(SymbolName);
302+
PublicInterface[JIT.intern(MangledName)] = Flags;
300303
}
301304
return std::unique_ptr<SwiftMaterializationUnit>(new SwiftMaterializationUnit(
302305
JIT, CI, std::move(Sources), std::move(PublicInterface)));
@@ -313,16 +316,17 @@ SwiftMaterializationUnit::SwiftMaterializationUnit(
313316
Sources(std::move(Sources)), JIT(JIT), CI(CI) {}
314317

315318
void SwiftMaterializationUnit::materialize(
316-
std::unique_ptr<llvm::orc::MaterializationResponsibility> R) {
319+
std::unique_ptr<llvm::orc::MaterializationResponsibility> MR) {
317320
SILRefsToEmit Refs;
318-
const auto &RS = R->getRequestedSymbols();
321+
const auto &RS = MR->getRequestedSymbols();
319322
for (auto &Sym : RS) {
320-
const auto &Source = Sources.storage->find(*Sym)->getValue();
323+
auto Name = demangle(*Sym);
324+
const auto &Source = Sources.storage->find(Name)->getValue();
321325
auto Ref = Source.getSILDeclRef();
322326
if (auto *AFD = Ref.getAbstractFunctionDecl()) {
323327
AFD->getTypecheckedBody();
324328
if (CI.getASTContext().hadError()) {
325-
R->failMaterialization();
329+
MR->failMaterialization();
326330
return;
327331
}
328332
}
@@ -333,13 +337,23 @@ void SwiftMaterializationUnit::materialize(
333337
runSILLoweringPasses(*SM);
334338
auto GM = generateModule(CI, std::move(SM));
335339
if (!GM) {
336-
R->failMaterialization();
340+
MR->failMaterialization();
337341
return;
338342
}
339343
auto *Module = GM->getModule();
344+
345+
// All renamings defined by `MR`, e.g. "foo" -> "foo$impl"
346+
llvm::StringMap<llvm::orc::SymbolStringPtr> Renamings;
347+
for (auto &[Sym, Flags] : MR->getSymbols()) {
348+
Renamings[demangle(*Sym)] = Sym;
349+
}
340350
// Now we must register all other public symbols defined by
341351
// the module with the JIT
342352
llvm::orc::SymbolFlagsMap LazilyDiscoveredSymbols;
353+
354+
// All symbols defined by the compiled module in `MR`
355+
llvm::DenseSet<llvm::orc::SymbolStringPtr> DefinedSymbols;
356+
343357
// Register all global values, including global
344358
// variables and functions
345359
for (const auto &GV : Module->global_values()) {
@@ -348,35 +362,37 @@ void SwiftMaterializationUnit::materialize(
348362
GV.isDeclaration()) {
349363
continue;
350364
}
351-
auto Name = GV.getName();
352-
auto MangledName = JIT.mangleAndIntern(Name);
353-
if (RS.contains(MangledName)) {
354-
continue;
365+
auto Name = JIT.mangle(GV.getName());
366+
auto itr = Renamings.find(Name);
367+
if (itr == Renamings.end()) {
368+
LazilyDiscoveredSymbols[JIT.intern(Name)] =
369+
llvm::JITSymbolFlags::fromGlobalValue(GV);
370+
} else {
371+
DefinedSymbols.insert(itr->getValue());
355372
}
356-
LazilyDiscoveredSymbols[MangledName] =
357-
llvm::JITSymbolFlags::fromGlobalValue(GV);
358373
}
374+
359375
llvm::orc::SymbolFlagsMap UnrequestedSymbols;
360-
for (auto &[Sym, Flags] : R->getSymbols()) {
361-
if (!RS.contains(Sym) && !LazilyDiscoveredSymbols.count(Sym)) {
376+
for (auto &[Sym, Flags] : MR->getSymbols()) {
377+
if (!DefinedSymbols.contains(Sym)) {
362378
UnrequestedSymbols[Sym] = Flags;
363379
}
364380
}
365381
std::unique_ptr<MaterializationUnit> UnrequestedMU(
366382
new SwiftMaterializationUnit(JIT, CI, std::move(Sources),
367383
std::move(UnrequestedSymbols)));
368-
if (auto Err = R->replace(std::move(UnrequestedMU))) {
384+
if (auto Err = MR->replace(std::move(UnrequestedMU))) {
369385
logError(std::move(Err));
370-
R->failMaterialization();
386+
MR->failMaterialization();
371387
return;
372388
}
373-
if (auto Err = R->defineMaterializing(std::move(LazilyDiscoveredSymbols))) {
389+
if (auto Err = MR->defineMaterializing(std::move(LazilyDiscoveredSymbols))) {
374390
logError(std::move(Err));
375-
R->failMaterialization();
391+
MR->failMaterialization();
376392
return;
377393
}
378394
auto TSM = std::move(*GM).intoThreadSafeContext();
379-
JIT.getIRCompileLayer().emit(std::move(R), std::move(TSM));
395+
JIT.getIRCompileLayer().emit(std::move(MR), std::move(TSM));
380396
}
381397

382398
llvm::Error
@@ -387,7 +403,7 @@ SwiftJIT::addSwift(llvm::orc::JITDylib &JD,
387403
for (auto &[Name, Flags] : MU->getSymbols()) {
388404
if (isMangled(*Name)) {
389405
// Create a stub for mangled functions
390-
auto OriginalName = demangleFunctionBody(*Name);
406+
auto OriginalName = demangle(*Name);
391407
Stubs.insert(
392408
{J->getExecutionSession().intern(OriginalName), {Name, Flags}});
393409
}

0 commit comments

Comments
 (0)