Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[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...
  • Loading branch information
Axel-Naumann committed Jan 13, 2023
commit ba2f10e5154f45a6389b0be3ac0a2ea9e4123ccc
15 changes: 12 additions & 3 deletions interpreter/cling/lib/Interpreter/BackendPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,17 @@ namespace {
if (!GV.isDiscardableIfUnused(LT) || !GV.isWeakForLinker(LT))
return false;

// Find the symbol as existing, previously compiled symbol in the JIT,
// or in shared libraries (without auto-loading).
return (m_JIT.getSymbolAddress(GV.getName(), /*IncludeHostSyms*/ true));
// Find the symbol as existing, previously compiled symbol in the JIT...
if (m_JIT.doesSymbolAlreadyExist(GV.getName()))
return true;

// ...or in shared libraries (without auto-loading).
std::string Name = GV.getName().str();
#if !defined(_WIN32)
return llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(Name);
#else
return platform::DLSym(Name);
#endif
}

bool runOnVar(GlobalVariable& GV) {
Expand Down Expand Up @@ -310,6 +318,7 @@ namespace {

bool runOnModule(Module &M) override {
bool ret = false;
// FIXME: use SymbolLookupSet, rather than looking up symbol by symbol.
for (auto &&F: M)
ret |= runOnFunc(F);
for (auto &&G: M.globals())
Expand Down
15 changes: 14 additions & 1 deletion interpreter/cling/lib/Interpreter/IncrementalJIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,10 @@ llvm::Error IncrementalJIT::removeModule(const Transaction& T) {
m_ResourceTrackers.erase(&T);
if (Error Err = RT->remove())
return Err;
auto iMod = m_CompiledModules.find(T.m_CompiledModule);
if (iMod != m_CompiledModules.end())
m_CompiledModules.erase(iMod);

return llvm::Error::success();
}

Expand Down Expand Up @@ -550,7 +554,7 @@ IncrementalJIT::addOrReplaceDefinition(StringRef Name,
return KnownAddr;
}

void* IncrementalJIT::getSymbolAddress(StringRef Name, bool IncludeHostSymbols) {
void* IncrementalJIT::getSymbolAddress(StringRef Name, bool IncludeHostSymbols){
std::unique_lock<SharedAtomicFlag> G(SkipHostProcessLookup, std::defer_lock);
if (!IncludeHostSymbols)
G.lock();
Expand All @@ -577,4 +581,13 @@ void* IncrementalJIT::getSymbolAddress(StringRef Name, bool IncludeHostSymbols)
return jitTargetAddressToPointer<void*>(Symbol->getAddress());
}

bool IncrementalJIT::doesSymbolAlreadyExist(StringRef UnmangledName) {
auto Name = Jit->mangle(UnmangledName);
for (auto &&M: m_CompiledModules) {
if (M.first->getNamedValue(Name))
return true;
}
return false;
}

} // namespace cling
4 changes: 4 additions & 0 deletions interpreter/cling/lib/Interpreter/IncrementalJIT.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class IncrementalJIT {
/// should include symbols from the host process (via dlsym) or not.
void* getSymbolAddress(llvm::StringRef Name, bool IncludeHostSymbols);

/// @brief Check whether the JIT already has emitted or knows how to emit
/// a symbol based on its IR name (as coming from clang's mangler).
bool doesSymbolAlreadyExist(llvm::StringRef UnmangledName);

/// Inject a symbol with a known address. Name is not linker mangled, i.e.
/// as known by the IR.
llvm::JITTargetAddress addOrReplaceDefinition(llvm::StringRef Name,
Expand Down