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"
32#include "llvm/Support/Error.h"
33
34namespace llvm::orc {
35
36/// Acts on the result of a completed lookup.
37///
38/// Produced by a LookupPrepareFn once it has added its symbols, so that it can
39/// capture their interned names rather than re-interning them here.
40using LookupApplyFn = unique_function<void(const SymbolMap &M)>;
41
42/// Contributes symbols to a lookup, and returns the function that will act on
43/// the result.
44///
45/// A prepare function may contribute any number of symbols, so one of them can
46/// stand for a whole service's worth of bindings. The applicator it returns
47/// runs only if the lookup succeeds.
48///
49/// The Mangler is built once by lookupAndApply (from the search order's target)
50/// and shared across all prepare functions, so a name-mangling prepare function
51/// need not construct its own.
52///
53/// The call operator is const so that these can be passed as a braced list (see
54/// lookupAndApply): they hold no mutable state.
56 SymbolLookupSet &LS, ExecutionSession &ES, const Mangler &Mangle) const>;
57
58/// Resolve the symbols contributed by every prepare function with a single
59/// lookup, then let each of their applicators act on the result.
60///
61/// Because one lookup covers them all, every applicator observes a single
62/// consistent view of the search order.
63///
64/// Prepare functions need not coordinate: if two of them ask for the same
65/// symbol the contributed entries are merged (see
66/// SymbolLookupSet::mergeEntries), and each applicator still reads its own
67/// value out of the result.
68///
69/// Asynchronous version: OnApplied is called once every applicator has run, or
70/// with an error if the lookup failed (in which case none of them run).
71///
72/// The ExecutionSession is taken from the first entry in SearchOrder (every
73/// entry must share the same session). If SearchOrder is empty then the lookup
74/// fails unconditionally. The only exception is an empty PrepareFns list, which
75/// trivially succeeds.
76///
77/// The prepare functions are only used during this call -- they are asked for
78/// their symbols up front, and only their applicators are retained -- so a
79/// braced list or other temporary is safe here.
80LLVM_ABI void lookupAndApply(unique_function<void(Error)> OnApplied,
82 const JITDylibSearchOrder &SearchOrder,
83 ArrayRef<LookupPrepareFn> PrepareFns);
84
85/// Blocking version of lookupAndApply above.
87 const JITDylibSearchOrder &SearchOrder,
88 ArrayRef<LookupPrepareFn> PrepareFns);
89
90/// lookupAndApply with a static lookup in the given JITDylib.
91LLVM_ABI void lookupAndApply(unique_function<void(Error)> OnApplied,
92 JITDylib &JD,
93 ArrayRef<LookupPrepareFn> PrepareFns);
94
95/// lookupAndApply with a static lookup in the given JITDylib. Blocking
96/// version.
98 ArrayRef<LookupPrepareFn> PrepareFns);
99
100/// Records the address of the symbol with the given name.
101///
102/// If the symbol is weakly referenced and not found then *A is set to null.
103///
104/// Name must remain valid until the lookupAndApply call it is passed to has
105/// collected its symbols: it is interned up front, and only the interned name
106/// is retained.
107inline LookupPrepareFn
110 return [Name, A, LF](SymbolLookupSet &LS, ExecutionSession &ES,
111 const Mangler &Mangle) -> LookupApplyFn {
112 auto N = Mangle.withMangledNameDo([&](StringRef M) { return ES.intern(M); },
113 Name);
114 LS.add(N, LF);
115 return [A, N = std::move(N)](const SymbolMap &M) {
116 *A = M.lookup(N).getAddress();
117 };
118 };
119}
120
121/// Records the address of the symbol with the given, already-interned name.
122///
123/// If the symbol is weakly referenced and not found then *A is set to null.
124inline LookupPrepareFn
127 return [Name = std::move(Name), A, LF](SymbolLookupSet &LS,
129 const Mangler &) -> LookupApplyFn {
130 LS.add(Name, LF);
131 return [A, Name](const SymbolMap &M) { *A = M.lookup(Name).getAddress(); };
132 };
133}
134
135} // namespace llvm::orc
136
137#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
Applies linker name-mangling for a target.
Definition Mangler.h:31
A set of symbols to look up, each associated with a SymbolLookupFlags value.
A symbol name together with the naming level (SymbolNameKind) it is expressed in, so that a Mangler /...
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.
LookupPrepareFn recordAddr(SymbolNameSpec Name, ExecutorAddr *A, SymbolLookupFlags LF=SymbolLookupFlags::RequiredSymbol)
Records the address of the symbol with the given name.
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.
unique_function< LookupApplyFn( SymbolLookupSet &LS, ExecutionSession &ES, const Mangler &Mangle) const > LookupPrepareFn
Contributes symbols to a lookup, and returns the function that will act on the result.
LookupKind
Describes the kind of lookup being performed.
Definition Core.h:144
#define N