Switch the primary implementation of EPC lookupSymbols to be async, keeping a synchronous wrapper for compatibility. Use the new async implementation inside EPCDynamicLibrarySearchGenerator to work working towards a fully async search generator. Provide an asynchronous lookup API for EPCGenericDylibManager and adopt that from the SimpleRemoteEPC. This enables an end-to-end async EPCDynamicLibrarySearchGenerator. Note: currently we keep the current per-dlhandle lookup model, but a future improvement could do a single async call for a given lookup operation.
79 lines
2.6 KiB
C++
79 lines
2.6 KiB
C++
//===---------------- EPCDynamicLibrarySearchGenerator.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/EPCDynamicLibrarySearchGenerator.h"
|
|
#include "llvm/Support/Error.h"
|
|
|
|
namespace llvm {
|
|
namespace orc {
|
|
|
|
Expected<std::unique_ptr<EPCDynamicLibrarySearchGenerator>>
|
|
EPCDynamicLibrarySearchGenerator::Load(
|
|
ExecutionSession &ES, const char *LibraryPath, SymbolPredicate Allow,
|
|
AddAbsoluteSymbolsFn AddAbsoluteSymbols) {
|
|
auto Handle = ES.getExecutorProcessControl().loadDylib(LibraryPath);
|
|
if (!Handle)
|
|
return Handle.takeError();
|
|
|
|
return std::make_unique<EPCDynamicLibrarySearchGenerator>(
|
|
ES, *Handle, std::move(Allow), std::move(AddAbsoluteSymbols));
|
|
}
|
|
|
|
Error EPCDynamicLibrarySearchGenerator::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);
|
|
}
|
|
|
|
ExecutorProcessControl::LookupRequest Request(H, LookupSymbols);
|
|
// Copy-capture LookupSymbols, since LookupRequest keeps a reference.
|
|
EPC.lookupSymbolsAsync(Request, [this, &JD, LS = std::move(LS),
|
|
LookupSymbols](auto Result) mutable {
|
|
if (!Result)
|
|
return LS.continueLookup(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");
|
|
|
|
SymbolMap NewSymbols;
|
|
auto ResultI = Result->front().begin();
|
|
for (auto &KV : LookupSymbols) {
|
|
if (ResultI->getAddress())
|
|
NewSymbols[KV.first] = *ResultI;
|
|
++ResultI;
|
|
}
|
|
|
|
// If there were no resolved symbols bail out.
|
|
if (NewSymbols.empty())
|
|
return LS.continueLookup(Error::success());
|
|
|
|
// Define resolved symbols.
|
|
Error Err = AddAbsoluteSymbols
|
|
? AddAbsoluteSymbols(JD, std::move(NewSymbols))
|
|
: JD.define(absoluteSymbols(std::move(NewSymbols)));
|
|
|
|
LS.continueLookup(std::move(Err));
|
|
});
|
|
|
|
return Error::success();
|
|
}
|
|
|
|
} // end namespace orc
|
|
} // end namespace llvm
|