LLVM  8.0.1
Legacy.h
Go to the documentation of this file.
1 //===--- Legacy.h -- Adapters for ExecutionEngine API interop ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Contains core ORC APIs.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_LEGACY_H
15 #define LLVM_EXECUTIONENGINE_ORC_LEGACY_H
16 
19 
20 namespace llvm {
21 namespace orc {
22 
23 /// SymbolResolver is a composable interface for looking up symbol flags
24 /// and addresses using the AsynchronousSymbolQuery type. It will
25 /// eventually replace the LegacyJITSymbolResolver interface as the
26 /// stardard ORC symbol resolver type.
27 ///
28 /// FIXME: SymbolResolvers should go away and be replaced with VSOs with
29 /// defenition generators.
31 public:
32  virtual ~SymbolResolver() = default;
33 
34  /// Returns the subset of the given symbols that the caller is responsible for
35  /// materializing.
36  virtual SymbolNameSet getResponsibilitySet(const SymbolNameSet &Symbols) = 0;
37 
38  /// For each symbol in Symbols that can be found, assigns that symbols
39  /// value in Query. Returns the set of symbols that could not be found.
40  virtual SymbolNameSet lookup(std::shared_ptr<AsynchronousSymbolQuery> Query,
41  SymbolNameSet Symbols) = 0;
42 
43 private:
44  virtual void anchor();
45 };
46 
47 /// Implements SymbolResolver with a pair of supplied function objects
48 /// for convenience. See createSymbolResolver.
49 template <typename GetResponsibilitySetFn, typename LookupFn>
50 class LambdaSymbolResolver final : public SymbolResolver {
51 public:
52  template <typename GetResponsibilitySetFnRef, typename LookupFnRef>
53  LambdaSymbolResolver(GetResponsibilitySetFnRef &&GetResponsibilitySet,
54  LookupFnRef &&Lookup)
55  : GetResponsibilitySet(
56  std::forward<GetResponsibilitySetFnRef>(GetResponsibilitySet)),
57  Lookup(std::forward<LookupFnRef>(Lookup)) {}
58 
60  return GetResponsibilitySet(Symbols);
61  }
62 
63  SymbolNameSet lookup(std::shared_ptr<AsynchronousSymbolQuery> Query,
64  SymbolNameSet Symbols) final {
65  return Lookup(std::move(Query), std::move(Symbols));
66  }
67 
68 private:
69  GetResponsibilitySetFn GetResponsibilitySet;
70  LookupFn Lookup;
71 };
72 
73 /// Creates a SymbolResolver implementation from the pair of supplied
74 /// function objects.
75 template <typename GetResponsibilitySetFn, typename LookupFn>
76 std::unique_ptr<LambdaSymbolResolver<
77  typename std::remove_cv<
78  typename std::remove_reference<GetResponsibilitySetFn>::type>::type,
79  typename std::remove_cv<
80  typename std::remove_reference<LookupFn>::type>::type>>
81 createSymbolResolver(GetResponsibilitySetFn &&GetResponsibilitySet,
82  LookupFn &&Lookup) {
83  using LambdaSymbolResolverImpl = LambdaSymbolResolver<
84  typename std::remove_cv<
85  typename std::remove_reference<GetResponsibilitySetFn>::type>::type,
86  typename std::remove_cv<
87  typename std::remove_reference<LookupFn>::type>::type>;
88  return llvm::make_unique<LambdaSymbolResolverImpl>(
89  std::forward<GetResponsibilitySetFn>(GetResponsibilitySet),
90  std::forward<LookupFn>(Lookup));
91 }
92 
93 /// Legacy adapter. Remove once we kill off the old ORC layers.
95 public:
98  Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) override;
99  void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) override;
100 
101 private:
102  ExecutionSession &ES;
103  std::set<SymbolStringPtr> ResolvedStrings;
104  SymbolResolver &R;
106 };
107 
108 /// Use the given legacy-style FindSymbol function (i.e. a function that takes
109 /// a const std::string& or StringRef and returns a JITSymbol) to get the
110 /// subset of symbols that the caller is responsible for materializing. If any
111 /// JITSymbol returned by FindSymbol is in an error state the function returns
112 /// immediately with that error.
113 ///
114 /// Useful for implementing getResponsibilitySet bodies that query legacy
115 /// resolvers.
116 template <typename FindSymbolFn>
119  FindSymbolFn FindSymbol) {
120  SymbolNameSet Result;
121 
122  for (auto &S : Symbols) {
123  if (JITSymbol Sym = FindSymbol(*S)) {
124  if (!Sym.getFlags().isStrong())
125  Result.insert(S);
126  } else if (auto Err = Sym.takeError())
127  return std::move(Err);
128  }
129 
130  return Result;
131 }
132 
133 /// Use the given legacy-style FindSymbol function (i.e. a function that
134 /// takes a const std::string& or StringRef and returns a JITSymbol) to
135 /// find the address and flags for each symbol in Symbols and store the
136 /// result in Query. If any JITSymbol returned by FindSymbol is in an
137 /// error then Query.notifyFailed(...) is called with that error and the
138 /// function returns immediately. On success, returns the set of symbols
139 /// not found.
140 ///
141 /// Useful for implementing lookup bodies that query legacy resolvers.
142 template <typename FindSymbolFn>
145  const SymbolNameSet &Symbols, FindSymbolFn FindSymbol) {
147  bool NewSymbolsResolved = false;
148 
149  for (auto &S : Symbols) {
150  if (JITSymbol Sym = FindSymbol(*S)) {
151  if (auto Addr = Sym.getAddress()) {
152  Query.resolve(S, JITEvaluatedSymbol(*Addr, Sym.getFlags()));
153  Query.notifySymbolReady();
154  NewSymbolsResolved = true;
155  } else {
156  ES.legacyFailQuery(Query, Addr.takeError());
157  return SymbolNameSet();
158  }
159  } else if (auto Err = Sym.takeError()) {
160  ES.legacyFailQuery(Query, std::move(Err));
161  return SymbolNameSet();
162  } else
163  SymbolsNotFound.insert(S);
164  }
165 
166  if (NewSymbolsResolved && Query.isFullyResolved())
167  Query.handleFullyResolved();
168 
169  if (NewSymbolsResolved && Query.isFullyReady())
170  Query.handleFullyReady();
171 
172  return SymbolsNotFound;
173 }
174 
175 /// An ORC SymbolResolver implementation that uses a legacy
176 /// findSymbol-like function to perform lookup;
177 template <typename LegacyLookupFn>
179 public:
180  using ErrorReporter = std::function<void(Error)>;
181 
182  LegacyLookupFnResolver(ExecutionSession &ES, LegacyLookupFn LegacyLookup,
184  : ES(ES), LegacyLookup(std::move(LegacyLookup)),
185  ReportError(std::move(ReportError)) {}
186 
188  if (auto ResponsibilitySet =
189  getResponsibilitySetWithLegacyFn(Symbols, LegacyLookup))
190  return std::move(*ResponsibilitySet);
191  else {
192  ReportError(ResponsibilitySet.takeError());
193  return SymbolNameSet();
194  }
195  }
196 
197  SymbolNameSet lookup(std::shared_ptr<AsynchronousSymbolQuery> Query,
198  SymbolNameSet Symbols) final {
199  return lookupWithLegacyFn(ES, *Query, Symbols, LegacyLookup);
200  }
201 
202 private:
203  ExecutionSession &ES;
204  LegacyLookupFn LegacyLookup;
206 };
207 
208 template <typename LegacyLookupFn>
209 std::shared_ptr<LegacyLookupFnResolver<LegacyLookupFn>>
210 createLegacyLookupResolver(ExecutionSession &ES, LegacyLookupFn LegacyLookup,
211  std::function<void(Error)> ErrorReporter) {
212  return std::make_shared<LegacyLookupFnResolver<LegacyLookupFn>>(
213  ES, std::move(LegacyLookup), std::move(ErrorReporter));
214 }
215 
216 } // End namespace orc
217 } // End namespace llvm
218 
219 #endif // LLVM_EXECUTIONENGINE_ORC_LEGACY_H
std::function< void(Expected< LookupResult >)> OnResolvedFunction
Definition: JITSymbol.h:348
Represents a symbol in the JIT.
Definition: JITSymbol.h:238
This class represents lattice values for constants.
Definition: AllocatorList.h:24
SymbolNameSet getResponsibilitySet(const SymbolNameSet &Symbols) final
Returns the subset of the given symbols that the caller is responsible for materializing.
Definition: Legacy.h:59
void resolve(const SymbolStringPtr &Name, JITEvaluatedSymbol Sym)
Set the resolved symbol information for the given symbol name.
Definition: Core.cpp:275
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:346
Implements SymbolResolver with a pair of supplied function objects for convenience.
Definition: Legacy.h:50
Definition: BitVector.h:938
LegacyLookupFnResolver(ExecutionSession &ES, LegacyLookupFn LegacyLookup, ErrorReporter ReportError)
Definition: Legacy.h:182
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:81
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:155
Expected< SymbolNameSet > getResponsibilitySetWithLegacyFn(const SymbolNameSet &Symbols, FindSymbolFn FindSymbol)
Use the given legacy-style FindSymbol function (i.e.
Definition: Legacy.h:118
void notifySymbolReady()
Notify the query that a requested symbol is ready for execution.
Definition: Core.cpp:301
void legacyFailQuery(AsynchronousSymbolQuery &Q, Error Err)
Definition: Core.cpp:1596
Legacy adapter. Remove once we kill off the old ORC layers.
Definition: Legacy.h:94
LambdaSymbolResolver(GetResponsibilitySetFnRef &&GetResponsibilitySet, LookupFnRef &&Lookup)
Definition: Legacy.h:53
std::function< void(Error)> ErrorReporter
Definition: Legacy.h:180
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:188
SymbolNameSet lookupWithLegacyFn(ExecutionSession &ES, AsynchronousSymbolQuery &Query, const SymbolNameSet &Symbols, FindSymbolFn FindSymbol)
Use the given legacy-style FindSymbol function (i.e.
Definition: Legacy.h:144
Symbol resolution interface.
Definition: JITSymbol.h:344
std::shared_ptr< LegacyLookupFnResolver< LegacyLookupFn > > createLegacyLookupResolver(ExecutionSession &ES, LegacyLookupFn LegacyLookup, std::function< void(Error)> ErrorReporter)
Definition: Legacy.h:210
Used to notify clients when symbols can not be found during a lookup.
Definition: Core.h:121
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:187
An ExecutionSession represents a running JIT program.
Definition: Core.h:697
SymbolResolver is a composable interface for looking up symbol flags and addresses using the Asynchro...
Definition: Legacy.h:30
Represents a symbol that has been evaluated to an address already.
Definition: JITSymbol.h:209
A symbol query that returns results via a callback when results are ready.
Definition: Core.h:434
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:197
static void Query(const MachineInstr &MI, AliasAnalysis &AA, bool &Read, bool &Write, bool &Effects, bool &StackPointer)
bool isFullyReady() const
Returns true if all symbols covered by this query are ready.
Definition: Core.h:464
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
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:63
An ORC SymbolResolver implementation that uses a legacy findSymbol-like function to perform lookup;...
Definition: Legacy.h:178
print Print MemDeps of function
bool isFullyResolved() const
Returns true if all symbols covered by this query have been resolved.
Definition: Core.h:452
void handleFullyResolved()
Call the NotifySymbolsResolved callback.
Definition: Core.cpp:285
static void LLVM_ATTRIBUTE_NORETURN ReportError(uint32_t StartOffset, const char *ErrorMsg)
void handleFullyReady()
Calls the NotifySymbolsReady callback.
Definition: Core.cpp:306