LCOV - code coverage report
Current view: top level - include/llvm/ExecutionEngine/Orc - Legacy.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 17 21 81.0 %
Date: 2018-05-20 00:06:23 Functions: 11 16 68.8 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : 
      17             : #include "llvm/ExecutionEngine/JITSymbol.h"
      18             : #include "llvm/ExecutionEngine/Orc/Core.h"
      19             : 
      20             : namespace llvm {
      21             : namespace orc {
      22             : 
      23         122 : class JITSymbolResolverAdapter : public JITSymbolResolver {
      24             : public:
      25             :   JITSymbolResolverAdapter(ExecutionSession &ES, SymbolResolver &R,
      26             :                            MaterializationResponsibility *MR);
      27             :   Expected<LookupFlagsResult> lookupFlags(const LookupSet &Symbols) override;
      28             :   Expected<LookupResult> lookup(const LookupSet &Symbols) override;
      29             : 
      30             : private:
      31             :   ExecutionSession &ES;
      32             :   std::set<SymbolStringPtr> ResolvedStrings;
      33             :   SymbolResolver &R;
      34             :   MaterializationResponsibility *MR;
      35             : };
      36             : 
      37             : /// Use the given legacy-style FindSymbol function (i.e. a function that
      38             : ///        takes a const std::string& or StringRef and returns a JITSymbol) to
      39             : ///        find the flags for each symbol in Symbols and store their flags in
      40             : ///        SymbolFlags. If any JITSymbol returned by FindSymbol is in an error
      41             : ///        state the function returns immediately with that error, otherwise it
      42             : ///        returns the set of symbols not found.
      43             : ///
      44             : /// Useful for implementing lookupFlags bodies that query legacy resolvers.
      45             : template <typename FindSymbolFn>
      46          39 : Expected<SymbolNameSet> lookupFlagsWithLegacyFn(SymbolFlagsMap &SymbolFlags,
      47             :                                                 const SymbolNameSet &Symbols,
      48             :                                                 FindSymbolFn FindSymbol) {
      49             :   SymbolNameSet SymbolsNotFound;
      50             : 
      51          45 :   for (auto &S : Symbols) {
      52          17 :     if (JITSymbol Sym = FindSymbol(*S))
      53           3 :       SymbolFlags[S] = Sym.getFlags();
      54           3 :     else if (auto Err = Sym.takeError())
      55             :       return std::move(Err);
      56             :     else
      57             :       SymbolsNotFound.insert(S);
      58             :   }
      59             : 
      60             :   return SymbolsNotFound;
      61             : }
      62             : 
      63             : /// Use the given legacy-style FindSymbol function (i.e. a function that
      64             : ///        takes a const std::string& or StringRef and returns a JITSymbol) to
      65             : ///        find the address and flags for each symbol in Symbols and store the
      66             : ///        result in Query. If any JITSymbol returned by FindSymbol is in an
      67             : ///        error then Query.notifyFailed(...) is called with that error and the
      68             : ///        function returns immediately. On success, returns the set of symbols
      69             : ///        not found.
      70             : ///
      71             : /// Useful for implementing lookup bodies that query legacy resolvers.
      72             : template <typename FindSymbolFn>
      73             : SymbolNameSet
      74          23 : lookupWithLegacyFn(ExecutionSession &ES, AsynchronousSymbolQuery &Query,
      75             :                    const SymbolNameSet &Symbols, FindSymbolFn FindSymbol) {
      76             :   SymbolNameSet SymbolsNotFound;
      77             :   bool NewSymbolsResolved = false;
      78             : 
      79          53 :   for (auto &S : Symbols) {
      80          68 :     if (JITSymbol Sym = FindSymbol(*S)) {
      81          46 :       if (auto Addr = Sym.getAddress()) {
      82          46 :         Query.resolve(S, JITEvaluatedSymbol(*Addr, Sym.getFlags()));
      83          23 :         Query.notifySymbolReady();
      84             :         NewSymbolsResolved = true;
      85             :       } else {
      86           0 :         ES.failQuery(Query, Addr.takeError());
      87           0 :         return SymbolNameSet();
      88             :       }
      89           7 :     } else if (auto Err = Sym.takeError()) {
      90           0 :       ES.failQuery(Query, std::move(Err));
      91           0 :       return SymbolNameSet();
      92             :     } else
      93             :       SymbolsNotFound.insert(S);
      94             :   }
      95             : 
      96          23 :   if (NewSymbolsResolved && Query.isFullyResolved())
      97          12 :     Query.handleFullyResolved();
      98             : 
      99          23 :   if (NewSymbolsResolved && Query.isFullyReady())
     100          12 :     Query.handleFullyReady();
     101             : 
     102             :   return SymbolsNotFound;
     103             : }
     104             : 
     105             : /// An ORC SymbolResolver implementation that uses a legacy
     106             : ///        findSymbol-like function to perform lookup;
     107             : template <typename LegacyLookupFn>
     108             : class LegacyLookupFnResolver final : public SymbolResolver {
     109             : public:
     110             :   using ErrorReporter = std::function<void(Error)>;
     111             : 
     112             :   LegacyLookupFnResolver(ExecutionSession &ES, LegacyLookupFn LegacyLookup,
     113             :                          ErrorReporter ReportError)
     114             :       : ES(ES), LegacyLookup(std::move(LegacyLookup)),
     115             :         ReportError(std::move(ReportError)) {}
     116             : 
     117             :   SymbolNameSet lookupFlags(SymbolFlagsMap &Flags,
     118             :                             const SymbolNameSet &Symbols) final {
     119             :     if (auto RemainingSymbols =
     120             :             lookupFlagsWithLegacyFn(Flags, Symbols, LegacyLookup))
     121             :       return std::move(*RemainingSymbols);
     122             :     else {
     123             :       ReportError(RemainingSymbols.takeError());
     124             :       return Symbols;
     125             :     }
     126             :   }
     127             : 
     128             :   SymbolNameSet lookup(std::shared_ptr<AsynchronousSymbolQuery> Query,
     129             :                        SymbolNameSet Symbols) final {
     130             :     return lookupWithLegacyFn(ES, *Query, Symbols, LegacyLookup);
     131             :   }
     132             : 
     133             : private:
     134             :   ExecutionSession &ES;
     135             :   LegacyLookupFn LegacyLookup;
     136             :   ErrorReporter ReportError;
     137             : };
     138             : 
     139             : template <typename LegacyLookupFn>
     140             : std::shared_ptr<LegacyLookupFnResolver<LegacyLookupFn>>
     141             : createLegacyLookupResolver(ExecutionSession &ES, LegacyLookupFn LegacyLookup,
     142             :                            std::function<void(Error)> ErrorReporter) {
     143             :   return std::make_shared<LegacyLookupFnResolver<LegacyLookupFn>>(
     144             :       ES, std::move(LegacyLookup), std::move(ErrorReporter));
     145             : }
     146             : 
     147             : } // End namespace orc
     148             : } // End namespace llvm
     149             : 
     150             : #endif // LLVM_EXECUTIONENGINE_ORC_LEGACY_H

Generated by: LCOV version 1.13