@@ -49,7 +49,7 @@ using namespace swift;
49
49
static const std::string ManglingSuffix = " $impl" ;
50
50
51
51
// / Mangle a function for a lazy reexport
52
- static std::string mangleFunctionBody (const StringRef Unmangled) {
52
+ static std::string mangle (const StringRef Unmangled) {
53
53
return Unmangled.str () + ManglingSuffix;
54
54
}
55
55
@@ -59,8 +59,9 @@ static bool isMangled(const StringRef Symbol) {
59
59
}
60
60
61
61
// / 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;
64
65
}
65
66
66
67
llvm::Expected<std::unique_ptr<SwiftJIT>>
@@ -114,6 +115,8 @@ SwiftJIT::lookupLinkerMangled(llvm::StringRef Name) {
114
115
return J->lookupLinkerMangled (Name);
115
116
}
116
117
118
+ std::string SwiftJIT::mangle (StringRef Name) { return J->mangle (Name); }
119
+
117
120
llvm::orc::SymbolStringPtr SwiftJIT::mangleAndIntern (StringRef Name) {
118
121
return J->mangleAndIntern (Name);
119
122
}
@@ -167,29 +170,28 @@ renameFunctionBodies(llvm::orc::MaterializationResponsibility &MR,
167
170
using namespace llvm ;
168
171
using namespace llvm ::orc;
169
172
170
- llvm::DenseSet<StringRef > ToRename;
173
+ llvm::StringSet< > ToRename;
171
174
for (auto &KV : MR.getSymbols ()) {
172
- const auto & Name = *KV.first ;
175
+ StringRef Name = *KV.first ;
173
176
if (isMangled (Name))
174
177
// All mangled functions we are responsible for
175
178
// materializing must be mangled at the object levels
176
- ToRename.insert (demangleFunctionBody (Name));
179
+ ToRename.insert (demangle (Name));
177
180
}
178
181
for (auto &Sec : G.sections ()) {
179
182
// Skip non-executable sections.
180
183
if ((Sec.getMemProt () & MemProt::Exec) == MemProt::None)
181
184
continue ;
182
185
183
186
for (auto *Sym : Sec.symbols ()) {
184
-
185
187
// Skip all anonymous and non-callables.
186
188
if (!Sym->hasName () || !Sym->isCallable ())
187
189
continue ;
188
190
189
191
if (ToRename.count (Sym->getName ())) {
190
192
// FIXME: Get rid of the temporary when Swift's llvm-project is
191
193
// updated to LLVM 17.
192
- auto NewName = G.allocateString (mangleFunctionBody (Sym->getName ()));
194
+ auto NewName = G.allocateString (mangle (Sym->getName ()));
193
195
Sym->setName ({NewName.data (), NewName.size ()});
194
196
}
195
197
}
@@ -296,7 +298,8 @@ SwiftMaterializationUnit::Create(SwiftJIT &JIT, CompilerInstance &CI) {
296
298
const auto &SymbolName = Entry.getKey ();
297
299
const auto Flags =
298
300
llvm::JITSymbolFlags::Exported | llvm::JITSymbolFlags::Callable;
299
- PublicInterface[JIT.intern (SymbolName)] = Flags;
301
+ auto MangledName = mangle (SymbolName);
302
+ PublicInterface[JIT.intern (MangledName)] = Flags;
300
303
}
301
304
return std::unique_ptr<SwiftMaterializationUnit>(new SwiftMaterializationUnit (
302
305
JIT, CI, std::move (Sources), std::move (PublicInterface)));
@@ -313,16 +316,17 @@ SwiftMaterializationUnit::SwiftMaterializationUnit(
313
316
Sources(std::move(Sources)), JIT(JIT), CI(CI) {}
314
317
315
318
void SwiftMaterializationUnit::materialize (
316
- std::unique_ptr<llvm::orc::MaterializationResponsibility> R ) {
319
+ std::unique_ptr<llvm::orc::MaterializationResponsibility> MR ) {
317
320
SILRefsToEmit Refs;
318
- const auto &RS = R ->getRequestedSymbols ();
321
+ const auto &RS = MR ->getRequestedSymbols ();
319
322
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 ();
321
325
auto Ref = Source.getSILDeclRef ();
322
326
if (auto *AFD = Ref.getAbstractFunctionDecl ()) {
323
327
AFD->getTypecheckedBody ();
324
328
if (CI.getASTContext ().hadError ()) {
325
- R ->failMaterialization ();
329
+ MR ->failMaterialization ();
326
330
return ;
327
331
}
328
332
}
@@ -333,13 +337,23 @@ void SwiftMaterializationUnit::materialize(
333
337
runSILLoweringPasses (*SM);
334
338
auto GM = generateModule (CI, std::move (SM));
335
339
if (!GM) {
336
- R ->failMaterialization ();
340
+ MR ->failMaterialization ();
337
341
return ;
338
342
}
339
343
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
+ }
340
350
// Now we must register all other public symbols defined by
341
351
// the module with the JIT
342
352
llvm::orc::SymbolFlagsMap LazilyDiscoveredSymbols;
353
+
354
+ // All symbols defined by the compiled module in `MR`
355
+ llvm::DenseSet<llvm::orc::SymbolStringPtr> DefinedSymbols;
356
+
343
357
// Register all global values, including global
344
358
// variables and functions
345
359
for (const auto &GV : Module->global_values ()) {
@@ -348,35 +362,37 @@ void SwiftMaterializationUnit::materialize(
348
362
GV.isDeclaration ()) {
349
363
continue ;
350
364
}
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 ());
355
372
}
356
- LazilyDiscoveredSymbols[MangledName] =
357
- llvm::JITSymbolFlags::fromGlobalValue (GV);
358
373
}
374
+
359
375
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)) {
362
378
UnrequestedSymbols[Sym] = Flags;
363
379
}
364
380
}
365
381
std::unique_ptr<MaterializationUnit> UnrequestedMU (
366
382
new SwiftMaterializationUnit (JIT, CI, std::move (Sources),
367
383
std::move (UnrequestedSymbols)));
368
- if (auto Err = R ->replace (std::move (UnrequestedMU))) {
384
+ if (auto Err = MR ->replace (std::move (UnrequestedMU))) {
369
385
logError (std::move (Err));
370
- R ->failMaterialization ();
386
+ MR ->failMaterialization ();
371
387
return ;
372
388
}
373
- if (auto Err = R ->defineMaterializing (std::move (LazilyDiscoveredSymbols))) {
389
+ if (auto Err = MR ->defineMaterializing (std::move (LazilyDiscoveredSymbols))) {
374
390
logError (std::move (Err));
375
- R ->failMaterialization ();
391
+ MR ->failMaterialization ();
376
392
return ;
377
393
}
378
394
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));
380
396
}
381
397
382
398
llvm::Error
@@ -387,7 +403,7 @@ SwiftJIT::addSwift(llvm::orc::JITDylib &JD,
387
403
for (auto &[Name, Flags] : MU->getSymbols ()) {
388
404
if (isMangled (*Name)) {
389
405
// Create a stub for mangled functions
390
- auto OriginalName = demangleFunctionBody (*Name);
406
+ auto OriginalName = demangle (*Name);
391
407
Stubs.insert (
392
408
{J->getExecutionSession ().intern (OriginalName), {Name, Flags}});
393
409
}
0 commit comments