LLVM  9.0.0svn
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  typename std::remove_cv<
77  typename std::remove_reference<GetResponsibilitySetFn>::type>::type,
78  typename std::remove_cv<
79  typename std::remove_reference<LookupFn>::type>::type>>
80 createSymbolResolver(GetResponsibilitySetFn &&GetResponsibilitySet,
81  LookupFn &&Lookup) {
82  using LambdaSymbolResolverImpl = LambdaSymbolResolver<
83  typename std::remove_cv<
84  typename std::remove_reference<GetResponsibilitySetFn>::type>::type,
85  typename std::remove_cv<
86  typename std::remove_reference<LookupFn>::type>::type>;
87  return llvm::make_unique<LambdaSymbolResolverImpl>(
88  std::forward<GetResponsibilitySetFn>(GetResponsibilitySet),
89  std::forward<LookupFn>(Lookup));
90 }
91 
92 /// Legacy adapter. Remove once we kill off the old ORC layers.
94 public:
97  Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) override;
98  void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) override;
99 
100 private:
101  ExecutionSession &ES;
102  std::set<SymbolStringPtr> ResolvedStrings;
103  SymbolResolver &R;
105 };
106 
107 /// Use the given legacy-style FindSymbol function (i.e. a function that takes
108 /// a const std::string& or StringRef and returns a JITSymbol) to get the
109 /// subset of symbols that the caller is responsible for materializing. If any
110 /// JITSymbol returned by FindSymbol is in an error state the function returns
111 /// immediately with that error.
112 ///
113 /// Useful for implementing getResponsibilitySet bodies that query legacy
114 /// resolvers.
115 template <typename FindSymbolFn>
118  FindSymbolFn FindSymbol) {
119  SymbolNameSet Result;
120 
121  for (auto &S : Symbols) {
122  if (JITSymbol Sym = FindSymbol(*S)) {
123  if (!Sym.getFlags().isStrong())
124  Result.insert(S);
125  } else if (auto Err = Sym.takeError())
126  return std::move(Err);
127  }
128 
129  return Result;
130 }
131 
132 /// Use the given legacy-style FindSymbol function (i.e. a function that
133 /// takes a const std::string& or StringRef and returns a JITSymbol) to
134 /// find the address and flags for each symbol in Symbols and store the
135 /// result in Query. If any JITSymbol returned by FindSymbol is in an
136 /// error then Query.notifyFailed(...) is called with that error and the
137 /// function returns immediately. On success, returns the set of symbols
138 /// not found.
139 ///
140 /// Useful for implementing lookup bodies that query legacy resolvers.
141 template <typename FindSymbolFn>
144  const SymbolNameSet &Symbols, FindSymbolFn FindSymbol) {
146  bool NewSymbolsResolved = false;
147 
148  for (auto &S : Symbols) {
149  if (JITSymbol Sym = FindSymbol(*S)) {
150  if (auto Addr = Sym.getAddress()) {
151  Query.resolve(S, JITEvaluatedSymbol(*Addr, Sym.getFlags()));
152  Query.notifySymbolReady();
153  NewSymbolsResolved = true;
154  } else {
155  ES.legacyFailQuery(Query, Addr.takeError());
156  return SymbolNameSet();
157  }
158  } else if (auto Err = Sym.takeError()) {
159  ES.legacyFailQuery(Query, std::move(Err));
160  return SymbolNameSet();
161  } else
162  SymbolsNotFound.insert(S);
163  }
164 
165  if (NewSymbolsResolved && Query.isFullyResolved())
166  Query.handleFullyResolved();
167 
168  if (NewSymbolsResolved && Query.isFullyReady())
169  Query.handleFullyReady();
170 
171  return SymbolsNotFound;
172 }
173 
174 /// An ORC SymbolResolver implementation that uses a legacy
175 /// findSymbol-like function to perform lookup;
176 template <typename LegacyLookupFn>
178 public:
179  using ErrorReporter = std::function<void(Error)>;
180 
181  LegacyLookupFnResolver(ExecutionSession &ES, LegacyLookupFn LegacyLookup,
183  : ES(ES), LegacyLookup(std::move(LegacyLookup)),
184  ReportError(std::move(ReportError)) {}
185 
187  if (auto ResponsibilitySet =
188  getResponsibilitySetWithLegacyFn(Symbols, LegacyLookup))
189  return std::move(*ResponsibilitySet);
190  else {
191  ReportError(ResponsibilitySet.takeError());
192  return SymbolNameSet();
193  }
194  }
195 
196  SymbolNameSet lookup(std::shared_ptr<AsynchronousSymbolQuery> Query,
197  SymbolNameSet Symbols) final {
198  return lookupWithLegacyFn(ES, *Query, Symbols, LegacyLookup);
199  }
200 
201 private:
202  ExecutionSession &ES;
203  LegacyLookupFn LegacyLookup;
205 };
206 
207 template <typename LegacyLookupFn>
208 std::shared_ptr<LegacyLookupFnResolver<LegacyLookupFn>>
209 createLegacyLookupResolver(ExecutionSession &ES, LegacyLookupFn LegacyLookup,
210  std::function<void(Error)> ErrorReporter) {
211  return std::make_shared<LegacyLookupFnResolver<LegacyLookupFn>>(
212  ES, std::move(LegacyLookup), std::move(ErrorReporter));
213 }
214 
215 } // End namespace orc
216 } // End namespace llvm
217 
218 #endif // LLVM_EXECUTIONENGINE_ORC_LEGACY_H
std::function< void(Expected< LookupResult >)> OnResolvedFunction
Definition: JITSymbol.h:347
Represents a symbol in the JIT.
Definition: JITSymbol.h:237
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
void resolve(const SymbolStringPtr &Name, JITEvaluatedSymbol Sym)
Set the resolved symbol information for the given symbol name.
Definition: Core.cpp:272
virtual SymbolNameSet getResponsibilitySet(const SymbolNameSet &Symbols)=0
Returns the subset of the given symbols that the caller is responsible for materializing.
std::set< StringRef > LookupSet
Definition: JITSymbol.h:345
Implements SymbolResolver with a pair of supplied function objects for convenience.
Definition: Legacy.h:49
Definition: BitVector.h:937
LegacyLookupFnResolver(ExecutionSession &ES, LegacyLookupFn LegacyLookup, ErrorReporter ReportError)
Definition: Legacy.h:181
std::unique_ptr< LambdaSymbolResolver< typename std::remove_cv< typename std::remove_reference< GetResponsibilitySetFn >::type >::type, typename std::remove_cv< typename std::remove_reference< LookupFn >::type >::type > > createSymbolResolver(GetResponsibilitySetFn &&GetResponsibilitySet, LookupFn &&Lookup)
Creates a SymbolResolver implementation from the pair of supplied function objects.
Definition: Legacy.h:80
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:154
Expected< SymbolNameSet > getResponsibilitySetWithLegacyFn(const SymbolNameSet &Symbols, FindSymbolFn FindSymbol)
Use the given legacy-style FindSymbol function (i.e.
Definition: Legacy.h:117
void notifySymbolReady()
Notify the query that a requested symbol is ready for execution.
Definition: Core.cpp:298
void legacyFailQuery(AsynchronousSymbolQuery &Q, Error Err)
Definition: Core.cpp:1634
Legacy adapter. Remove once we kill off the old ORC layers.
Definition: Legacy.h:93
LambdaSymbolResolver(GetResponsibilitySetFnRef &&GetResponsibilitySet, LookupFnRef &&Lookup)
Definition: Legacy.h:52
std::function< void(Error)> ErrorReporter
Definition: Legacy.h:179
DenseSet< SymbolStringPtr > SymbolNameSet
A set of symbol names (represented by SymbolStringPtrs for.
Definition: Core.h:43
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:187
SymbolNameSet lookupWithLegacyFn(ExecutionSession &ES, AsynchronousSymbolQuery &Query, const SymbolNameSet &Symbols, FindSymbolFn FindSymbol)
Use the given legacy-style FindSymbol function (i.e.
Definition: Legacy.h:143
Symbol resolution interface.
Definition: JITSymbol.h:343
std::shared_ptr< LegacyLookupFnResolver< LegacyLookupFn > > createLegacyLookupResolver(ExecutionSession &ES, LegacyLookupFn LegacyLookup, std::function< void(Error)> ErrorReporter)
Definition: Legacy.h:209
Used to notify clients when symbols can not be found during a lookup.
Definition: Core.h:120
virtual ~SymbolResolver()=default
SymbolNameSet getResponsibilitySet(const SymbolNameSet &Symbols) final
Returns the subset of the given symbols that the caller is responsible for materializing.
Definition: Legacy.h:186
An ExecutionSession represents a running JIT program.
Definition: Core.h:696
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:208
A symbol query that returns results via a callback when results are ready.
Definition: Core.h:433
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:196
bool isFullyReady() const
Returns true if all symbols covered by this query are ready.
Definition: Core.h:463
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:177
print Print MemDeps of function
bool isFullyResolved() const
Returns true if all symbols covered by this query have been resolved.
Definition: Core.h:451
void handleFullyResolved()
Call the NotifySymbolsResolved callback.
Definition: Core.cpp:282
static void LLVM_ATTRIBUTE_NORETURN ReportError(uint32_t StartOffset, const char *ErrorMsg)
void handleFullyReady()
Calls the NotifySymbolsReady callback.
Definition: Core.cpp:303