LLVM 24.0.0git
LookupAndApply.h
Go to the documentation of this file.
1//===- LookupAndApply.h - Compose a lookup from handlers --------*- C++ -*-===//
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//
9// Compose an ExecutionSession lookup out of independent operations, each of
10// which contributes symbols to the lookup and then acts on the addresses those
11// symbols resolve to.
12//
13// The motivating use is binding a small, fixed set of controller-side variables
14// -- e.g. the proxies and instance address that make up an executor-side
15// service's handle -- with a single atomic lookup. Handlers are not limited to
16// that: they receive the whole result map and may do as they please with it.
17// Either way the operations do all the work, and the return path only signals
18// success or failure.
19//
20//===----------------------------------------------------------------------===//
21
22#ifndef LLVM_EXECUTIONENGINE_ORC_LOOKUPANDAPPLY_H
23#define LLVM_EXECUTIONENGINE_ORC_LOOKUPANDAPPLY_H
24
25#include "llvm/ADT/ArrayRef.h"
30#include "llvm/Support/Error.h"
31
32namespace llvm::orc {
33
34/// Acts on the result of a completed lookup.
35///
36/// Produced by a LookupPrepareFn once it has added its symbols, so that it can
37/// capture their interned names rather than re-interning them here.
38using LookupApplyFn = unique_function<void(const SymbolMap &M)>;
39
40/// Contributes symbols to a lookup, and returns the function that will act on
41/// the result.
42///
43/// A prepare function may contribute any number of symbols, so one of them can
44/// stand for a whole service's worth of bindings. The applicator it returns
45/// runs only if the lookup succeeds.
46///
47/// The call operator is const so that these can be passed as a braced list (see
48/// lookupAndApply): they hold no mutable state.
50 SymbolLookupSet &LS, ExecutionSession &ES) const>;
51
52/// Resolve the symbols contributed by every prepare function with a single
53/// lookup, then let each of their applicators act on the result.
54///
55/// Because one lookup covers them all, every applicator observes a single
56/// consistent view of the search order.
57///
58/// Prepare functions need not coordinate: if two of them ask for the same
59/// symbol the contributed entries are merged (see
60/// SymbolLookupSet::mergeEntries), and each applicator still reads its own
61/// value out of the result.
62///
63/// Asynchronous version: OnApplied is called once every applicator has run, or
64/// with an error if the lookup failed (in which case none of them run).
65///
66/// The prepare functions are only used during this call -- they are asked for
67/// their symbols up front, and only their applicators are retained -- so a
68/// braced list or other temporary is safe here.
69LLVM_ABI void lookupAndApply(unique_function<void(Error)> OnApplied,
71 const JITDylibSearchOrder &SearchOrder,
72 ArrayRef<LookupPrepareFn> PrepareFns);
73
74/// Blocking version of lookupAndApply above.
76 const JITDylibSearchOrder &SearchOrder,
77 ArrayRef<LookupPrepareFn> PrepareFns);
78
79/// lookupAndApply with a static lookup in the given JITDylib.
80LLVM_ABI void lookupAndApply(unique_function<void(Error)> OnApplied,
81 JITDylib &JD,
82 ArrayRef<LookupPrepareFn> PrepareFns);
83
84/// lookupAndApply with a static lookup in the given JITDylib. Blocking
85/// version.
87 ArrayRef<LookupPrepareFn> PrepareFns);
88
89/// Records the address of the symbol with the given name.
90///
91/// If the symbol is weakly referenced and not found then *A is set to null.
92///
93/// Name must remain valid until the lookupAndApply call it is passed to has
94/// collected its symbols: it is interned up front, and only the interned name
95/// is retained.
96inline LookupPrepareFn
99 return [Name, A, LF](SymbolLookupSet &LS,
101 auto N = ES.intern(Name);
102 LS.add(N, LF);
103 return [A, N = std::move(N)](const SymbolMap &M) {
104 *A = M.lookup(N).getAddress();
105 };
106 };
107}
108
109} // namespace llvm::orc
110
111#endif // LLVM_EXECUTIONENGINE_ORC_LOOKUPANDAPPLY_H
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition Compiler.h:215
This file provides a collection of function (or more generally, callable) type erasure utilities supp...
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
An ExecutionSession represents a running JIT program.
Definition Core.h:1111
Represents an address in the executor process.
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.
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
SymbolLookupFlags
Lookup flags that apply to each symbol in a lookup.
DenseMap< SymbolStringPtr, ExecutorSymbolDef > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
unique_function< void(const SymbolMap &M)> LookupApplyFn
Acts on the result of a completed lookup.
LLVM_ABI void lookupAndApply(unique_function< void(Error)> OnApplied, ExecutionSession &ES, 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...
LookupPrepareFn recordAddr(StringRef Name, ExecutorAddr *A, SymbolLookupFlags LF=SymbolLookupFlags::RequiredSymbol)
Records the address of the symbol with the given name.
LookupKind
Describes the kind of lookup being performed.
Definition Core.h:144
unique_function< LookupApplyFn( SymbolLookupSet &LS, ExecutionSession &ES) const > LookupPrepareFn
Contributes symbols to a lookup, and returns the function that will act on the result.
#define N