LLVM 24.0.0git
LookupAndApply.cpp
Go to the documentation of this file.
1//===- LookupAndApply.cpp - Compose a lookup from handlers ----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
12
13#include <future>
14
15namespace llvm::orc {
16
18 const JITDylibSearchOrder &SearchOrder,
19 ArrayRef<LookupPrepareFn> PrepareFns) {
20
21 /// Empty PrepareFns list trivially succeeds.
22 if (PrepareFns.empty())
23 return OnApplied(Error::success());
24
25 /// Otherwise an empty SearchOrder trivially fails: Without it we don't have
26 /// the ExecutionSession that we need to pass into the prepare functions.
27 if (SearchOrder.empty())
28 return OnApplied(
29 make_error<StringError>("lookupAndApply failed: search order is empty",
31
32 auto &ES = SearchOrder.front().first->getExecutionSession();
33
34 // Collect the symbols to look up. Each prepare function hands back the
35 // applicator that will act on the result; the prepare functions themselves
36 // are not needed beyond this point.
37 SymbolLookupSet Symbols;
38 std::vector<LookupApplyFn> Applies;
39 Applies.reserve(PrepareFns.size());
40 for (const auto &PF : PrepareFns)
41 Applies.push_back(PF(Symbols, ES));
42
43 // PrepareFns are independent, so two of them may legitimately ask for the
44 // same symbol. ExecutionSession::lookup requires a duplicate-free set, and
45 // the applicators read the result by name, so merging here is invisible to
46 // them.
47 Symbols.mergeEntries();
48
49 ES.lookup(
50 K, SearchOrder, std::move(Symbols), SymbolState::Ready,
51 [Applies = std::move(Applies),
52 OnApplied = std::move(OnApplied)](Expected<SymbolMap> Result) mutable {
53 if (!Result)
54 return OnApplied(Result.takeError());
55 for (auto &Apply : Applies)
56 Apply(*Result);
57 OnApplied(Error::success());
58 },
60}
61
63 ArrayRef<LookupPrepareFn> PrepareFns) {
64 std::promise<MSVCPError> ResultP;
65 auto ResultF = ResultP.get_future();
66 lookupAndApply([&](Error Err) { ResultP.set_value(std::move(Err)); }, K,
67 SearchOrder, PrepareFns);
68 return ResultF.get();
69}
70
71void lookupAndApply(unique_function<void(Error)> OnApplied, JITDylib &JD,
72 ArrayRef<LookupPrepareFn> PrepareFns) {
73 lookupAndApply(std::move(OnApplied), LookupKind::Static,
74 makeJITDylibSearchOrder(&JD), PrepareFns);
75}
76
81
82} // namespace llvm::orc
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Represents a JIT'd dynamic library.
Definition Core.h:675
A set of symbols to look up, each associated with a SymbolLookupFlags value.
unique_function is a type-erasing functor similar to std::function.
JITDylibSearchOrder makeJITDylibSearchOrder(ArrayRef< JITDylib * > JDs, JITDylibLookupFlags Flags=JITDylibLookupFlags::MatchExportedSymbolsOnly)
Convenience function for creating a search order from an ArrayRef of JITDylib*, all with the same fla...
Definition Core.h:153
std::vector< std::pair< JITDylib *, JITDylibLookupFlags > > JITDylibSearchOrder
A list of (JITDylib*, JITDylibLookupFlags) pairs to be used as a search order during symbol lookup.
Definition Core.h:148
LLVM_ABI void lookupAndApply(unique_function< void(Error)> OnApplied, LookupKind K, const JITDylibSearchOrder &SearchOrder, ArrayRef< LookupPrepareFn > PrepareFns)
Resolve the symbols contributed by every prepare function with a single lookup, then let each of thei...
LookupKind
Describes the kind of lookup being performed.
Definition Core.h:144
LLVM_ABI RegisterDependenciesFunction NoDependenciesToRegister
This can be used as the value for a RegisterDependenciesFunction if there are no dependants to regist...
Definition Core.cpp:40
@ Ready
Emitted to memory, but waiting on transitive dependencies.
Definition Core.h:551
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:94
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340