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 ExecutionSession is taken from the first entry in SearchOrder (every
67/// entry must share the same session). If SearchOrder is empty then the lookup
68/// fails unconditionally. The only exception is an empty PrepareFns list, which
69/// trivially succeeds.
70///
71/// The prepare functions are only used during this call -- they are asked for
72/// their symbols up front, and only their applicators are retained -- so a
73/// braced list or other temporary is safe here.
74LLVM_ABI void lookupAndApply(unique_function<void(Error)> OnApplied,
75 LookupKind K,
76 const JITDylibSearchOrder &SearchOrder,
77 ArrayRef<LookupPrepareFn> PrepareFns);
78
79/// Blocking version of lookupAndApply above.
81 const JITDylibSearchOrder &SearchOrder,
82 ArrayRef<LookupPrepareFn> PrepareFns);
83
84/// lookupAndApply with a static lookup in the given JITDylib.
85LLVM_ABI void lookupAndApply(unique_function<void(Error)> OnApplied,
86 JITDylib &JD,
87 ArrayRef<LookupPrepareFn> PrepareFns);
88
89/// lookupAndApply with a static lookup in the given JITDylib. Blocking
90/// version.
92 ArrayRef<LookupPrepareFn> PrepareFns);
93
94/// Records the address of the symbol with the given name.
95///
96/// If the symbol is weakly referenced and not found then *A is set to null.
97///
98/// Name must remain valid until the lookupAndApply call it is passed to has
99/// collected its symbols: it is interned up front, and only the interned name
100/// is retained.
101inline LookupPrepareFn
104 return [Name, A, LF](SymbolLookupSet &LS,
106 auto N = ES.intern(Name);
107 LS.add(N, LF);
108 return [A, N = std::move(N)](const SymbolMap &M) {
109 *A = M.lookup(N).getAddress();
110 };
111 };
112}
113
114/// Records the address of the symbol with the given, already-interned name.
115///
116/// If the symbol is weakly referenced and not found then *A is set to null.
117inline LookupPrepareFn
120 return [Name = std::move(Name), A,
122 LS.add(Name, LF);
123 return [A, Name](const SymbolMap &M) { *A = M.lookup(Name).getAddress(); };
124 };
125}
126
127} // namespace llvm::orc
128
129#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.
Pointer to a pooled string representing a symbol name.
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
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...
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.
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