llvm-project/llvm/lib/ExecutionEngine/Orc/TPCDynamicLibrarySearchGenerator.cpp
Lang Hames 1d0676b54c [ORC] Break up OrcJIT library, add Orc-RPC based remote TargetProcessControl
implementation.

This patch aims to improve support for out-of-process JITing using OrcV2. It
introduces two new class templates, OrcRPCTargetProcessControlBase and
OrcRPCTPCServer, which together implement the TargetProcessControl API by
forwarding operations to an execution process via an Orc-RPC Endpoint. These
utilities are used to implement out-of-process JITing from llvm-jitlink to
a new llvm-jitlink-executor tool.

This patch also breaks the OrcJIT library into three parts:
  -- OrcTargetProcess: Contains code needed by the JIT execution process.
  -- OrcShared: Contains code needed by the JIT execution and compiler
     processes
  -- OrcJIT: Everything else.

This break-up allows JIT executor processes to link against OrcTargetProcess
and OrcShared only, without having to link in all of OrcJIT. Clients executing
JIT'd code in-process should start linking against OrcTargetProcess as well as
OrcJIT.

In the near future these changes will enable:
  -- Removal of the OrcRemoteTargetClient/OrcRemoteTargetServer class templates
     which provided similar functionality in OrcV1.
  -- Restoration of Chapter 5 of the Building-A-JIT tutorial series, which will
     serve as a simple usage example for these APIs.
  -- Implementation of lazy, cross-target compilation in lli's -jit-kind=orc-lazy
     mode.
2020-11-13 17:05:13 +11:00

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;
tpctypes::LookupRequest 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