LLVM  7.0.0
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 flags for each symbol in Symbols that can be found,
35  /// along with the set of symbol that could not be found.
36  virtual SymbolFlagsMap lookupFlags(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 LookupFlagsFn, typename LookupFn>
50 class LambdaSymbolResolver final : public SymbolResolver {
51 public:
52  template <typename LookupFlagsFnRef, typename LookupFnRef>
53  LambdaSymbolResolver(LookupFlagsFnRef &&LookupFlags, LookupFnRef &&Lookup)
54  : LookupFlags(std::forward<LookupFlagsFnRef>(LookupFlags)),
55  Lookup(std::forward<LookupFnRef>(Lookup)) {}
56 
57  SymbolFlagsMap lookupFlags(const SymbolNameSet &Symbols) final {
58  return LookupFlags(Symbols);
59  }
60 
61  SymbolNameSet lookup(std::shared_ptr<AsynchronousSymbolQuery> Query,
62  SymbolNameSet Symbols) final {
63  return Lookup(std::move(Query), std::move(Symbols));
64  }
65 
66 private:
67  LookupFlagsFn LookupFlags;
68  LookupFn Lookup;
69 };
70 
71 /// Creates a SymbolResolver implementation from the pair of supplied
72 /// function objects.
73 template <typename LookupFlagsFn, typename LookupFn>
74 std::unique_ptr<LambdaSymbolResolver<
75  typename std::remove_cv<
76  typename std::remove_reference<LookupFlagsFn>::type>::type,
77  typename std::remove_cv<
78  typename std::remove_reference<LookupFn>::type>::type>>
79 createSymbolResolver(LookupFlagsFn &&LookupFlags, LookupFn &&Lookup) {
80  using LambdaSymbolResolverImpl = LambdaSymbolResolver<
81  typename std::remove_cv<
82  typename std::remove_reference<LookupFlagsFn>::type>::type,
83  typename std::remove_cv<
84  typename std::remove_reference<LookupFn>::type>::type>;
85  return llvm::make_unique<LambdaSymbolResolverImpl>(
86  std::forward<LookupFlagsFn>(LookupFlags), std::forward<LookupFn>(Lookup));
87 }
88 
90 public:
93  Expected<LookupFlagsResult> lookupFlags(const LookupSet &Symbols) override;
94  Expected<LookupResult> lookup(const LookupSet &Symbols) 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
104 /// takes a const std::string& or StringRef and returns a JITSymbol) to
105 /// find the flags for each symbol in Symbols and store their flags in
106 /// SymbolFlags. If any JITSymbol returned by FindSymbol is in an error
107 /// state the function returns immediately with that error, otherwise it
108 /// returns the set of symbols not found.
109 ///
110 /// Useful for implementing lookupFlags bodies that query legacy resolvers.
111 template <typename FindSymbolFn>
113  FindSymbolFn FindSymbol) {
114  SymbolFlagsMap SymbolFlags;
115 
116  for (auto &S : Symbols) {
117  if (JITSymbol Sym = FindSymbol(*S))
118  SymbolFlags[S] = Sym.getFlags();
119  else if (auto Err = Sym.takeError())
120  return std::move(Err);
121  }
122 
123  return SymbolFlags;
124 }
125 
126 /// Use the given legacy-style FindSymbol function (i.e. a function that
127 /// takes a const std::string& or StringRef and returns a JITSymbol) to
128 /// find the address and flags for each symbol in Symbols and store the
129 /// result in Query. If any JITSymbol returned by FindSymbol is in an
130 /// error then Query.notifyFailed(...) is called with that error and the
131 /// function returns immediately. On success, returns the set of symbols
132 /// not found.
133 ///
134 /// Useful for implementing lookup bodies that query legacy resolvers.
135 template <typename FindSymbolFn>
138  const SymbolNameSet &Symbols, FindSymbolFn FindSymbol) {
140  bool NewSymbolsResolved = false;
141 
142  for (auto &S : Symbols) {
143  if (JITSymbol Sym = FindSymbol(*S)) {
144  if (auto Addr = Sym.getAddress()) {
145  Query.resolve(S, JITEvaluatedSymbol(*Addr, Sym.getFlags()));
146  Query.notifySymbolReady();
147  NewSymbolsResolved = true;
148  } else {
149  ES.legacyFailQuery(Query, Addr.takeError());
150  return SymbolNameSet();
151  }
152  } else if (auto Err = Sym.takeError()) {
153  ES.legacyFailQuery(Query, std::move(Err));
154  return SymbolNameSet();
155  } else
156  SymbolsNotFound.insert(S);
157  }
158 
159  if (NewSymbolsResolved && Query.isFullyResolved())
160  Query.handleFullyResolved();
161 
162  if (NewSymbolsResolved && Query.isFullyReady())
163  Query.handleFullyReady();
164 
165  return SymbolsNotFound;
166 }
167 
168 /// An ORC SymbolResolver implementation that uses a legacy
169 /// findSymbol-like function to perform lookup;
170 template <typename LegacyLookupFn>
172 public:
173  using ErrorReporter = std::function<void(Error)>;
174 
175  LegacyLookupFnResolver(ExecutionSession &ES, LegacyLookupFn LegacyLookup,
177  : ES(ES), LegacyLookup(std::move(LegacyLookup)),
178  ReportError(std::move(ReportError)) {}
179 
180  SymbolFlagsMap lookupFlags(const SymbolNameSet &Symbols) final {
181  if (auto SymbolFlags = lookupFlagsWithLegacyFn(Symbols, LegacyLookup))
182  return std::move(*SymbolFlags);
183  else {
184  ReportError(SymbolFlags.takeError());
185  return SymbolFlagsMap();
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;
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
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:61
Represents a symbol in the JIT.
Definition: JITSymbol.h:186
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
void resolve(const SymbolStringPtr &Name, JITEvaluatedSymbol Sym)
Set the resolved symbol information for the given symbol name.
Definition: Core.cpp:511
std::set< StringRef > LookupSet
Definition: JITSymbol.h:294
void legacyFailQuery(AsynchronousSymbolQuery &Q, Error Err)
Definition: Core.cpp:145
Implements SymbolResolver with a pair of supplied function objects for convenience.
Definition: Legacy.h:50
LambdaSymbolResolver(LookupFlagsFnRef &&LookupFlags, LookupFnRef &&Lookup)
Definition: Legacy.h:53
Definition: BitVector.h:921
LegacyLookupFnResolver(ExecutionSession &ES, LegacyLookupFn LegacyLookup, ErrorReporter ReportError)
Definition: Legacy.h:175
std::map< SymbolStringPtr, JITSymbolFlags > SymbolFlagsMap
A map from symbol names (as SymbolStringPtrs) to JITSymbolFlags.
Definition: Core.h:57
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:124
void notifySymbolReady()
Notify the query that a requested symbol is ready for execution.
Definition: Core.cpp:529
std::function< void(Error)> ErrorReporter
Definition: Legacy.h:173
SymbolNameSet lookupWithLegacyFn(ExecutionSession &ES, AsynchronousSymbolQuery &Query, const SymbolNameSet &Symbols, FindSymbolFn FindSymbol)
Use the given legacy-style FindSymbol function (i.e.
Definition: Legacy.h:137
Symbol resolution interface.
Definition: JITSymbol.h:292
virtual SymbolFlagsMap lookupFlags(const SymbolNameSet &Symbols)=0
Returns the flags for each symbol in Symbols that can be found, along with the set of symbol that cou...
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:104
virtual ~SymbolResolver()=default
Expected< SymbolFlagsMap > lookupFlagsWithLegacyFn(const SymbolNameSet &Symbols, FindSymbolFn FindSymbol)
Use the given legacy-style FindSymbol function (i.e.
Definition: Legacy.h:112
SymbolFlagsMap lookupFlags(const SymbolNameSet &Symbols) final
Returns the flags for each symbol in Symbols that can be found, along with the set of symbol that cou...
Definition: Legacy.h:57
An ExecutionSession represents a running JIT program.
Definition: Core.h:735
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:157
A symbol query that returns results via a callback when results are ready.
Definition: Core.h:483
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
SymbolFlagsMap lookupFlags(const SymbolNameSet &Symbols) final
Returns the flags for each symbol in Symbols that can be found, along with the set of symbol that cou...
Definition: Legacy.h:180
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:512
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
An ORC SymbolResolver implementation that uses a legacy findSymbol-like function to perform lookup;...
Definition: Legacy.h:171
print Print MemDeps of function
bool isFullyResolved() const
Returns true if all symbols covered by this query have been resolved.
Definition: Core.h:500
void handleFullyResolved()
Call the NotifySymbolsResolved callback.
Definition: Core.cpp:521
static void LLVM_ATTRIBUTE_NORETURN ReportError(uint32_t StartOffset, const char *ErrorMsg)
std::set< SymbolStringPtr > SymbolNameSet
A set of symbol names (represented by SymbolStringPtrs for.
Definition: Core.h:44
void handleFullyReady()
Calls the NotifySymbolsReady callback.
Definition: Core.cpp:534
std::unique_ptr< LambdaSymbolResolver< typename std::remove_cv< typename std::remove_reference< LookupFlagsFn >::type >::type, typename std::remove_cv< typename std::remove_reference< LookupFn >::type >::type > > createSymbolResolver(LookupFlagsFn &&LookupFlags, LookupFn &&Lookup)
Creates a SymbolResolver implementation from the pair of supplied function objects.
Definition: Legacy.h:79