Skip to content

Commit 5c7cf42

Browse files
committed
[cling] JIT: find existing weak symbol without materializer:
m_JIT.getSymbolAddress() invokes the symbol materializers, which compile (which is sort of okay) but also try autoloading (which totally is not okay). Instead, implement a function to search existing JIT symbols. This can be accelerated by looking up the whole set of symbols, instead of doing it symbol by symbol. I leave that refactoring for later...
1 parent 21915b7 commit 5c7cf42

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

interpreter/cling/lib/Interpreter/BackendPasses.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,17 @@ namespace {
267267
if (!GV.isDiscardableIfUnused(LT) || !GV.isWeakForLinker(LT))
268268
return false;
269269

270-
// Find the symbol as existing, previously compiled symbol in the JIT,
271-
// or in shared libraries (without auto-loading).
272-
return (m_JIT.getSymbolAddress(GV.getName(), /*IncludeHostSyms*/ true));
270+
// Find the symbol as existing, previously compiled symbol in the JIT...
271+
if (m_JIT.lookupExistingSymbol(GV.getName()))
272+
return true;
273+
274+
// ...or in shared libraries (without auto-loading).
275+
std::string Name = GV.getName().str();
276+
#if !defined(_WIN32)
277+
return llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(Name);
278+
#else
279+
return platform::DLSym(Name);
280+
#endif
273281
}
274282

275283
bool runOnVar(GlobalVariable& GV) {
@@ -310,6 +318,7 @@ namespace {
310318

311319
bool runOnModule(Module &M) override {
312320
bool ret = false;
321+
// FIXME: use SymbolLookupSet, rather than looking up symbol by symbol.
313322
for (auto &&F: M)
314323
ret |= runOnFunc(F);
315324
for (auto &&G: M.globals())

interpreter/cling/lib/Interpreter/IncrementalJIT.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,4 +577,22 @@ void* IncrementalJIT::getSymbolAddress(StringRef Name, bool IncludeHostSymbols)
577577
return jitTargetAddressToPointer<void*>(Symbol->getAddress());
578578
}
579579

580+
void *IncrementalJIT::lookupExistingSymbol(StringRef UnmangledName) {
581+
auto &ES = Jit->getExecutionSession();
582+
auto &JD = Jit->getMainJITDylib();
583+
SymbolLookupSet Names({ES.intern(Jit->mangle(UnmangledName))});
584+
if (auto ResultMap = ES.lookup(llvm::orc::makeJITDylibSearchOrder(&JD,
585+
JITDylibLookupFlags::MatchAllSymbols),
586+
std::move(Names),
587+
LookupKind::DLSym)) {
588+
assert(ResultMap->size() == 1 && "Unexpected number of results");
589+
assert(ResultMap->count(jit->mangle(UnmangledName)) && "Missing result for symbol");
590+
return jitTargetAddressToPointer<void*>(ResultMap->begin()->second.getAddress());
591+
} else {
592+
consumeError(ResultMap.takeError());
593+
return nullptr;
594+
}
595+
}
596+
597+
580598
} // namespace cling

interpreter/cling/lib/Interpreter/IncrementalJIT.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ class IncrementalJIT {
7878
/// should include symbols from the host process (via dlsym) or not.
7979
void* getSymbolAddress(llvm::StringRef Name, bool IncludeHostSymbols);
8080

81+
/// @brief Lookup existing symbols in the set of JIT emitted symbols,
82+
/// based on its IR name (as coming from clang's mangler).
83+
void *lookupExistingSymbol(llvm::StringRef UnmangledName);
84+
8185
/// Inject a symbol with a known address. Name is not linker mangled, i.e.
8286
/// as known by the IR.
8387
llvm::JITTargetAddress addOrReplaceDefinition(llvm::StringRef Name,

0 commit comments

Comments
 (0)