LLVM  12.0.0git
Legacy.h
Go to the documentation of this file.
1 //===--- Legacy.h -- Adapters for ExecutionEngine API interop ---*- 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 // Contains core ORC APIs.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_ORC_LEGACY_H
14 #define LLVM_EXECUTIONENGINE_ORC_LEGACY_H
15 
18 
19 namespace llvm {
20 namespace orc {
21 
22 /// SymbolResolver is a composable interface for looking up symbol flags
23 /// and addresses using the AsynchronousSymbolQuery type. It will
24 /// eventually replace the LegacyJITSymbolResolver interface as the
25 /// stardard ORC symbol resolver type.
26 ///
27 /// FIXME: SymbolResolvers should go away and be replaced with VSOs with
28 /// defenition generators.
30 public:
31  virtual ~SymbolResolver() = default;
32 
33  /// Returns the subset of the given symbols that the caller is responsible for
34  /// materializing.
35  virtual SymbolNameSet getResponsibilitySet(const SymbolNameSet &Symbols) = 0;
36 
37  /// For each symbol in Symbols that can be found, assigns that symbols
38  /// value in Query. Returns the set of symbols that could not be found.
39  virtual SymbolNameSet lookup(std::shared_ptr<AsynchronousSymbolQuery> Query,
40  SymbolNameSet Symbols) = 0;
41 
42 private:
43  virtual void anchor();
44 };
45 
46 /// Implements SymbolResolver with a pair of supplied function objects
47 /// for convenience. See createSymbolResolver.
48 template <typename GetResponsibilitySetFn, typename LookupFn>
49 class LambdaSymbolResolver final : public SymbolResolver {
50 public:
51  template <typename GetResponsibilitySetFnRef, typename LookupFnRef>
52  LambdaSymbolResolver(GetResponsibilitySetFnRef &&GetResponsibilitySet,
53  LookupFnRef &&Lookup)
54  : GetResponsibilitySet(
55  std::forward<GetResponsibilitySetFnRef>(GetResponsibilitySet)),
56  Lookup(std::forward<LookupFnRef>(Lookup)) {}
57 
59  return GetResponsibilitySet(Symbols);
60  }
61 
62  SymbolNameSet lookup(std::shared_ptr<AsynchronousSymbolQuery> Query,
63  SymbolNameSet Symbols) final {
64  return Lookup(std::move(Query), std::move(Symbols));
65  }
66 
67 private:
68  GetResponsibilitySetFn GetResponsibilitySet;
69  LookupFn Lookup;
70 };
71 
72 /// Creates a SymbolResolver implementation from the pair of supplied
73 /// function objects.
74 template <typename GetResponsibilitySetFn, typename LookupFn>
75 std::unique_ptr<LambdaSymbolResolver<
76  std::remove_cv_t<std::remove_reference_t<GetResponsibilitySetFn>>,
77  std::remove_cv_t<std::remove_reference_t<LookupFn>>>>
78 createSymbolResolver(GetResponsibilitySetFn &&GetResponsibilitySet,
79  LookupFn &&Lookup) {
80  using LambdaSymbolResolverImpl = LambdaSymbolResolver<
81  std::remove_cv_t<std::remove_reference_t<GetResponsibilitySetFn>>,
82  std::remove_cv_t<std::remove_reference_t<LookupFn>>>;
83  return std::make_unique<LambdaSymbolResolverImpl>(
84  std::forward<GetResponsibilitySetFn>(GetResponsibilitySet),
85  std::forward<LookupFn>(Lookup));
86 }
87 
88 /// Legacy adapter. Remove once we kill off the old ORC layers.
90 public:
93  Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) override;
94  void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) override;
95 
96 private:
97  ExecutionSession &ES;
98  std::set<SymbolStringPtr> ResolvedStrings;
99  SymbolResolver &R;
101 };
102 
103 /// Use the given legacy-style FindSymbol function (i.e. a function that takes
104 /// a const std::string& or StringRef and returns a JITSymbol) to get the
105 /// subset of symbols that the caller is responsible for materializing. If any
106 /// JITSymbol returned by FindSymbol is in an error state the function returns
107 /// immediately with that error.
108 ///
109 /// Useful for implementing getResponsibilitySet bodies that query legacy
110 /// resolvers.
111 template <typename FindSymbolFn>
114  FindSymbolFn FindSymbol) {
115  SymbolNameSet Result;
116 
117  for (auto &S : Symbols) {
118  if (JITSymbol Sym = FindSymbol(*S)) {
119  if (!Sym.getFlags().isStrong())
120  Result.insert(S);
121  } else if (auto Err = Sym.takeError())
122  return std::move(Err);
123  }
124 
125  return Result;
126 }
127 
128 /// Use the given legacy-style FindSymbol function (i.e. a function that
129 /// takes a const std::string& or StringRef and returns a JITSymbol) to
130 /// find the address and flags for each symbol in Symbols and store the
131 /// result in Query. If any JITSymbol returned by FindSymbol is in an
132 /// error then Query.notifyFailed(...) is called with that error and the
133 /// function returns immediately. On success, returns the set of symbols
134 /// not found.
135 ///
136 /// Useful for implementing lookup bodies that query legacy resolvers.
137 template <typename FindSymbolFn>
140  const SymbolNameSet &Symbols, FindSymbolFn FindSymbol) {
142  bool NewSymbolsResolved = false;
143 
144  for (auto &S : Symbols) {
145  if (JITSymbol Sym = FindSymbol(*S)) {
146  if (auto Addr = Sym.getAddress()) {
148  S, JITEvaluatedSymbol(*Addr, Sym.getFlags()));
149  NewSymbolsResolved = true;
150  } else {
151  ES.legacyFailQuery(Query, Addr.takeError());
152  return SymbolNameSet();
153  }
154  } else if (auto Err = Sym.takeError()) {
155  ES.legacyFailQuery(Query, std::move(Err));
156  return SymbolNameSet();
157  } else
158  SymbolsNotFound.insert(S);
159  }
160 
161  if (NewSymbolsResolved && Query.isComplete())
162  Query.handleComplete();
163 
164  return SymbolsNotFound;
165 }
166 
167 /// An ORC SymbolResolver implementation that uses a legacy
168 /// findSymbol-like function to perform lookup;
169 template <typename LegacyLookupFn>
171 public:
172  using ErrorReporter = std::function<void(Error)>;
173 
174  LegacyLookupFnResolver(ExecutionSession &ES, LegacyLookupFn LegacyLookup,
175  ErrorReporter ReportError)
176  : ES(ES), LegacyLookup(std::move(LegacyLookup)),
177  ReportError(std::move(ReportError)) {}
178 
180  if (auto ResponsibilitySet =
181  getResponsibilitySetWithLegacyFn(Symbols, LegacyLookup))
182  return std::move(*ResponsibilitySet);
183  else {
184  ReportError(ResponsibilitySet.takeError());
185  return SymbolNameSet();
186  }
187  }
188 
189  SymbolNameSet lookup(std::shared_ptr<AsynchronousSymbolQuery> Query,
190  SymbolNameSet Symbols) final {
191  return lookupWithLegacyFn(ES, *Query, Symbols, LegacyLookup);
192  }
193 
194 private:
195  ExecutionSession &ES;
196  LegacyLookupFn LegacyLookup;
197  ErrorReporter ReportError;
198 };
199 
200 template <typename LegacyLookupFn>
201 std::shared_ptr<LegacyLookupFnResolver<LegacyLookupFn>>
202 createLegacyLookupResolver(ExecutionSession &ES, LegacyLookupFn LegacyLookup,
203  std::function<void(Error)> ErrorReporter) {
204  return std::make_shared<LegacyLookupFnResolver<LegacyLookupFn>>(
205  ES, std::move(LegacyLookup), std::move(ErrorReporter));
206 }
207 
208 } // End namespace orc
209 } // End namespace llvm
210 
211 #endif // LLVM_EXECUTIONENGINE_ORC_LEGACY_H
Represents a symbol in the JIT.
Definition: JITSymbol.h:265
This class represents lattice values for constants.
Definition: AllocatorList.h:23
SymbolNameSet getResponsibilitySet(const SymbolNameSet &Symbols) final
Returns the subset of the given symbols that the caller is responsible for materializing.
Definition: Legacy.h:58
bool isComplete() const
Returns true if all symbols covered by this query have been resolved.
Definition: Core.h:743
virtual SymbolNameSet getResponsibilitySet(const SymbolNameSet &Symbols)=0
Returns the subset of the given symbols that the caller is responsible for materializing.
void notifySymbolMetRequiredState(const SymbolStringPtr &Name, JITEvaluatedSymbol Sym)
Notify the query that a requested symbol has reached the required state.
Definition: Core.cpp:115
std::set< StringRef > LookupSet
Definition: JITSymbol.h:373
Implements SymbolResolver with a pair of supplied function objects for convenience.
Definition: Legacy.h:49
Definition: BitVector.h:959
LegacyLookupFnResolver(ExecutionSession &ES, LegacyLookupFn LegacyLookup, ErrorReporter ReportError)
Definition: Legacy.h:174
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:410
Expected< SymbolNameSet > getResponsibilitySetWithLegacyFn(const SymbolNameSet &Symbols, FindSymbolFn FindSymbol)
Use the given legacy-style FindSymbol function (i.e.
Definition: Legacy.h:113
void legacyFailQuery(AsynchronousSymbolQuery &Q, Error Err)
Definition: Core.cpp:1805
Legacy adapter. Remove once we kill off the old ORC layers.
Definition: Legacy.h:89
void handleComplete()
Call the NotifyComplete callback.
Definition: Core.cpp:131
LambdaSymbolResolver(GetResponsibilitySetFnRef &&GetResponsibilitySet, LookupFnRef &&Lookup)
Definition: Legacy.h:52
std::function< void(Error)> ErrorReporter
Definition: Legacy.h:172
DenseSet< SymbolStringPtr > SymbolNameSet
A set of symbol names (represented by SymbolStringPtrs for.
Definition: Core.h:44
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:193
SymbolNameSet lookupWithLegacyFn(ExecutionSession &ES, AsynchronousSymbolQuery &Query, const SymbolNameSet &Symbols, FindSymbolFn FindSymbol)
Use the given legacy-style FindSymbol function (i.e.
Definition: Legacy.h:139
Symbol resolution interface.
Definition: JITSymbol.h:371
std::shared_ptr< LegacyLookupFnResolver< LegacyLookupFn > > createLegacyLookupResolver(ExecutionSession &ES, LegacyLookupFn LegacyLookup, std::function< void(Error)> ErrorReporter)
Definition: Legacy.h:202
Used to notify clients when symbols can not be found during a lookup.
Definition: Core.h:337
virtual ~SymbolResolver()=default
std::unique_ptr< LambdaSymbolResolver< std::remove_cv_t< std::remove_reference_t< GetResponsibilitySetFn > >, std::remove_cv_t< std::remove_reference_t< LookupFn > > > > createSymbolResolver(GetResponsibilitySetFn &&GetResponsibilitySet, LookupFn &&Lookup)
Creates a SymbolResolver implementation from the pair of supplied function objects.
Definition: Legacy.h:78
SymbolNameSet getResponsibilitySet(const SymbolNameSet &Symbols) final
Returns the subset of the given symbols that the caller is responsible for materializing.
Definition: Legacy.h:179
unique_function is a type-erasing functor similar to std::function.
An ExecutionSession represents a running JIT program.
Definition: Core.h:1088
SymbolResolver is a composable interface for looking up symbol flags and addresses using the Asynchro...
Definition: Legacy.h:29
Represents a symbol that has been evaluated to an address already.
Definition: JITSymbol.h:229
A symbol query that returns results via a callback when results are ready.
Definition: Core.h:724
virtual SymbolNameSet lookup(std::shared_ptr< AsynchronousSymbolQuery > Query, SymbolNameSet Symbols)=0
For each symbol in Symbols that can be found, assigns that symbols value in Query.
SymbolNameSet lookup(std::shared_ptr< AsynchronousSymbolQuery > Query, SymbolNameSet Symbols) final
For each symbol in Symbols that can be found, assigns that symbols value in Query.
Definition: Legacy.h:189
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
SymbolNameSet lookup(std::shared_ptr< AsynchronousSymbolQuery > Query, SymbolNameSet Symbols) final
For each symbol in Symbols that can be found, assigns that symbols value in Query.
Definition: Legacy.h:62
An ORC SymbolResolver implementation that uses a legacy findSymbol-like function to perform lookup;...
Definition: Legacy.h:170
print Print MemDeps of function