
This patch moves definition generation out from the session lock, instead running it under a per-dylib generator lock. It also makes the DefinitionGenerator::tryToGenerate method optionally asynchronous: Generators are handed an opaque LookupState object which can be captured to stop/restart the lookup process. The new scheme provides the following benefits and guarantees: (1) Queries that do not need to attempt definition generation (because all requested symbols matched against existing definitions in the JITDylib) can proceed without being blocked by any running definition generators. (2) Definition generators can capture the LookupState to continue their work asynchronously. This allows generators to run for an arbitrary amount of time without blocking a thread. Definition generators that do not need to run asynchronously can return without capturing the LookupState to eliminate unnecessary recursion and improve lookup performance. (3) Definition generators still do not need to worry about concurrency or re-entrance: Since they are still run under a (per-dylib) lock, generators will never be re-entered concurrently, or given overlapping symbol sets to generate. Finally, the new system distinguishes between symbols that are candidates for generation (generation candidates) and symbols that failed to match for a query (due to symbol visibility). This fixes a bug where an unresolved symbol could trigger generation of a duplicate definition for an existing hidden symbol.
70 lines
2.2 KiB
C++
70 lines
2.2 KiB
C++
//===---------------- TPCDynamicLibrarySearchGenerator.cpp ----------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/ExecutionEngine/Orc/TPCDynamicLibrarySearchGenerator.h"
|
|
|
|
namespace llvm {
|
|
namespace orc {
|
|
|
|
Expected<std::unique_ptr<TPCDynamicLibrarySearchGenerator>>
|
|
TPCDynamicLibrarySearchGenerator::Load(TargetProcessControl &TPC,
|
|
const char *LibraryPath,
|
|
SymbolPredicate Allow) {
|
|
auto Handle = TPC.loadDylib(LibraryPath);
|
|
if (!Handle)
|
|
return Handle.takeError();
|
|
|
|
return std::make_unique<TPCDynamicLibrarySearchGenerator>(TPC, *Handle,
|
|
std::move(Allow));
|
|
}
|
|
|
|
Error TPCDynamicLibrarySearchGenerator::tryToGenerate(
|
|
LookupState &LS, LookupKind K, JITDylib &JD,
|
|
JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &Symbols) {
|
|
|
|
if (Symbols.empty())
|
|
return Error::success();
|
|
|
|
SymbolLookupSet LookupSymbols;
|
|
|
|
for (auto &KV : Symbols) {
|
|
// Skip symbols that don't match the filter.
|
|
if (Allow && !Allow(KV.first))
|
|
continue;
|
|
LookupSymbols.add(KV.first, SymbolLookupFlags::WeaklyReferencedSymbol);
|
|
}
|
|
|
|
SymbolMap NewSymbols;
|
|
|
|
TargetProcessControl::LookupRequestElement Request(H, LookupSymbols);
|
|
auto Result = TPC.lookupSymbols(Request);
|
|
if (!Result)
|
|
return Result.takeError();
|
|
|
|
assert(Result->size() == 1 && "Results for more than one library returned");
|
|
assert(Result->front().size() == LookupSymbols.size() &&
|
|
"Result has incorrect number of elements");
|
|
|
|
SymbolNameVector MissingSymbols;
|
|
auto ResultI = Result->front().begin();
|
|
for (auto &KV : LookupSymbols)
|
|
if (*ResultI)
|
|
NewSymbols[KV.first] =
|
|
JITEvaluatedSymbol(*ResultI++, JITSymbolFlags::Exported);
|
|
|
|
// If there were no resolved symbols bail out.
|
|
if (NewSymbols.empty())
|
|
return Error::success();
|
|
|
|
// Define resolved symbols.
|
|
return JD.define(absoluteSymbols(std::move(NewSymbols)));
|
|
}
|
|
|
|
} // end namespace orc
|
|
} // end namespace llvm
|