LCOV - code coverage report
Current view: top level - include/llvm/ExecutionEngine/Orc - Core.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 55 55 100.0 %
Date: 2018-05-20 00:06:23 Functions: 35 56 62.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===------ Core.h -- Core ORC APIs (Layer, JITDylib, etc.) -----*- 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_CORE_H
      15             : #define LLVM_EXECUTIONENGINE_ORC_CORE_H
      16             : 
      17             : #include "llvm/ExecutionEngine/JITSymbol.h"
      18             : #include "llvm/ExecutionEngine/Orc/SymbolStringPool.h"
      19             : #include "llvm/IR/Module.h"
      20             : 
      21             : #include <list>
      22             : #include <map>
      23             : #include <memory>
      24             : #include <set>
      25             : #include <vector>
      26             : 
      27             : namespace llvm {
      28             : namespace orc {
      29             : 
      30             : // Forward declare some classes.
      31             : class AsynchronousSymbolQuery;
      32             : class ExecutionSession;
      33             : class MaterializationUnit;
      34             : class MaterializationResponsibility;
      35             : class VSO;
      36             : 
      37             : /// VModuleKey provides a unique identifier (allocated and managed by
      38             : /// ExecutionSessions) for a module added to the JIT.
      39             : using VModuleKey = uint64_t;
      40             : 
      41             : /// A set of symbol names (represented by SymbolStringPtrs for
      42             : //         efficiency).
      43             : using SymbolNameSet = std::set<SymbolStringPtr>;
      44             : 
      45             : /// Render a SymbolNameSet to an ostream.
      46             : raw_ostream &operator<<(raw_ostream &OS, const SymbolNameSet &Symbols);
      47             : 
      48             : /// A map from symbol names (as SymbolStringPtrs) to JITSymbols
      49             : ///        (address/flags pairs).
      50             : using SymbolMap = std::map<SymbolStringPtr, JITEvaluatedSymbol>;
      51             : 
      52             : /// Render a SymbolMap to an ostream.
      53             : raw_ostream &operator<<(raw_ostream &OS, const SymbolMap &Symbols);
      54             : 
      55             : /// A map from symbol names (as SymbolStringPtrs) to JITSymbolFlags.
      56             : using SymbolFlagsMap = std::map<SymbolStringPtr, JITSymbolFlags>;
      57             : 
      58             : /// Render a SymbolMap to an ostream.
      59             : raw_ostream &operator<<(raw_ostream &OS, const SymbolFlagsMap &Symbols);
      60             : 
      61             : /// A base class for materialization failures that allows the failing
      62             : ///        symbols to be obtained for logging.
      63             : using SymbolDependenceMap = std::map<VSO *, SymbolNameSet>;
      64             : 
      65             : /// Render a SymbolDependendeMap.
      66             : raw_ostream &operator<<(raw_ostream &OS, const SymbolDependenceMap &Deps);
      67             : 
      68             : /// Used to notify a VSO that the given set of symbols failed to materialize.
      69           2 : class FailedToMaterialize : public ErrorInfo<FailedToMaterialize> {
      70             : public:
      71             :   static char ID;
      72             : 
      73             :   FailedToMaterialize(SymbolNameSet Symbols);
      74             :   std::error_code convertToErrorCode() const override;
      75             :   void log(raw_ostream &OS) const override;
      76           1 :   const SymbolNameSet &getSymbols() const { return Symbols; }
      77             : 
      78             : private:
      79             :   SymbolNameSet Symbols;
      80             : };
      81             : 
      82             : /// Tracks responsibility for materialization, and mediates interactions between
      83             : /// MaterializationUnits and VSOs.
      84             : ///
      85             : /// An instance of this class is passed to MaterializationUnits when their
      86             : /// materialize method is called. It allows MaterializationUnits to resolve and
      87             : /// finalize symbols, or abandon materialization by notifying any unmaterialized
      88             : /// symbols of an error.
      89             : class MaterializationResponsibility {
      90             :   friend class MaterializationUnit;
      91             : public:
      92          17 :   MaterializationResponsibility(MaterializationResponsibility &&) = default;
      93             :   MaterializationResponsibility &
      94             :   operator=(MaterializationResponsibility &&) = default;
      95             : 
      96             :   /// Destruct a MaterializationResponsibility instance. In debug mode
      97             :   ///        this asserts that all symbols being tracked have been either
      98             :   ///        finalized or notified of an error.
      99             :   ~MaterializationResponsibility();
     100             : 
     101             :   /// Returns the target VSO that these symbols are being materialized
     102             :   ///        into.
     103             :   const VSO &getTargetVSO() const { return V; }
     104             : 
     105             :   /// Resolves the given symbols. Individual calls to this method may
     106             :   ///        resolve a subset of the symbols, but all symbols must have been
     107             :   ///        resolved prior to calling finalize.
     108             :   void resolve(const SymbolMap &Symbols);
     109             : 
     110             :   /// Finalizes all symbols tracked by this instance.
     111             :   void finalize();
     112             : 
     113             :   /// Adds new symbols to the VSO and this responsibility instance.
     114             :   ///        VSO entries start out in the materializing state.
     115             :   ///
     116             :   ///   This method can be used by materialization units that want to add
     117             :   /// additional symbols at materialization time (e.g. stubs, compile
     118             :   /// callbacks, metadata).
     119             :   Error defineMaterializing(const SymbolFlagsMap &SymbolFlags);
     120             : 
     121             :   /// Notify all unfinalized symbols that an error has occurred.
     122             :   /// This will remove all symbols covered by this MaterializationResponsibilty
     123             :   /// from V, and send an error to any queries waiting on these symbols.
     124             :   void failMaterialization();
     125             : 
     126             :   /// Transfers responsibility to the given MaterializationUnit for all
     127             :   /// symbols defined by that MaterializationUnit. This allows
     128             :   /// materializers to break up work based on run-time information (e.g.
     129             :   /// by introspecting which symbols have actually been looked up and
     130             :   /// materializing only those).
     131             :   void delegate(std::unique_ptr<MaterializationUnit> MU);
     132             : 
     133             :   /// Add dependencies for the symbols in this dylib.
     134             :   void addDependencies(const SymbolDependenceMap &Dependencies);
     135             : 
     136             : private:
     137             :   /// Create a MaterializationResponsibility for the given VSO and
     138             :   ///        initial symbols.
     139             :   MaterializationResponsibility(VSO &V, SymbolFlagsMap SymbolFlags);
     140             : 
     141             :   VSO &V;
     142             :   SymbolFlagsMap SymbolFlags;
     143             : };
     144             : 
     145             : /// A MaterializationUnit represents a set of symbol definitions that can
     146             : ///        be materialized as a group, or individually discarded (when
     147             : ///        overriding definitions are encountered).
     148             : ///
     149             : /// MaterializationUnits are used when providing lazy definitions of symbols to
     150             : /// VSOs. The VSO will call materialize when the address of a symbol is
     151             : /// requested via the lookup method. The VSO will call discard if a stronger
     152             : /// definition is added or already present.
     153             : class MaterializationUnit {
     154             : public:
     155             :   MaterializationUnit(SymbolFlagsMap InitalSymbolFlags)
     156          17 :       : SymbolFlags(std::move(InitalSymbolFlags)) {}
     157             : 
     158          17 :   virtual ~MaterializationUnit() {}
     159             : 
     160             :   /// Return the set of symbols that this source provides.
     161             :   const SymbolFlagsMap &getSymbols() const { return SymbolFlags; }
     162             : 
     163             :   /// Called by materialization dispatchers (see
     164             :   /// ExecutionSession::DispatchMaterializationFunction) to trigger
     165             :   /// materialization of this MaterializationUnit.
     166          11 :   void doMaterialize(VSO &V) {
     167          33 :     materialize(MaterializationResponsibility(V, std::move(SymbolFlags)));
     168          11 :   }
     169             : 
     170             :   /// Called by VSOs to notify MaterializationUnits that the given symbol has
     171             :   /// been overridden.
     172           3 :   void doDiscard(const VSO &V, SymbolStringPtr Name) {
     173             :     SymbolFlags.erase(Name);
     174           9 :     discard(V, std::move(Name));
     175           3 :   }
     176             : 
     177             : protected:
     178             :   SymbolFlagsMap SymbolFlags;
     179             : 
     180             : private:
     181             :   virtual void anchor();
     182             : 
     183             :   /// Implementations of this method should materialize all symbols
     184             :   ///        in the materialzation unit, except for those that have been
     185             :   ///        previously discarded.
     186             :   virtual void materialize(MaterializationResponsibility R) = 0;
     187             : 
     188             :   /// Implementations of this method should discard the given symbol
     189             :   ///        from the source (e.g. if the source is an LLVM IR Module and the
     190             :   ///        symbol is a function, delete the function body or mark it available
     191             :   ///        externally).
     192             :   virtual void discard(const VSO &V, SymbolStringPtr Name) = 0;
     193             : };
     194             : 
     195             : /// A MaterializationUnit implementation for pre-existing absolute symbols.
     196             : ///
     197             : /// All symbols will be resolved and marked ready as soon as the unit is
     198             : /// materialized.
     199          24 : class AbsoluteSymbolsMaterializationUnit : public MaterializationUnit {
     200             : public:
     201             :   AbsoluteSymbolsMaterializationUnit(SymbolMap Symbols);
     202             : 
     203             : private:
     204             :   void materialize(MaterializationResponsibility R) override;
     205             :   void discard(const VSO &V, SymbolStringPtr Name) override;
     206             :   static SymbolFlagsMap extractFlags(const SymbolMap &Symbols);
     207             : 
     208             :   SymbolMap Symbols;
     209             : };
     210             : 
     211             : /// Create an AbsoluteSymbolsMaterializationUnit with the given symbols.
     212             : /// Useful for inserting absolute symbols into a VSO. E.g.:
     213             : /// \code{.cpp}
     214             : ///   VSO &V = ...;
     215             : ///   SymbolStringPtr Foo = ...;
     216             : ///   JITEvaluatedSymbol FooSym = ...;
     217             : ///   if (auto Err = V.define(absoluteSymbols({{Foo, FooSym}})))
     218             : ///     return Err;
     219             : /// \endcode
     220             : ///
     221             : inline std::unique_ptr<AbsoluteSymbolsMaterializationUnit>
     222             : absoluteSymbols(SymbolMap Symbols) {
     223             :   return llvm::make_unique<AbsoluteSymbolsMaterializationUnit>(
     224           8 :       std::move(Symbols));
     225             : }
     226             : 
     227             : /// Base utilities for ExecutionSession.
     228          82 : class ExecutionSessionBase {
     229             : public:
     230             :   /// For reporting errors.
     231             :   using ErrorReporter = std::function<void(Error)>;
     232             : 
     233             :   /// For dispatching MaterializationUnit::materialize calls.
     234             :   using DispatchMaterializationFunction =
     235             :       std::function<void(VSO &V, std::unique_ptr<MaterializationUnit> MU)>;
     236             : 
     237             :   /// Construct an ExecutionSessionBase.
     238             :   ///
     239             :   /// SymbolStringPools may be shared between ExecutionSessions.
     240         107 :   ExecutionSessionBase(std::shared_ptr<SymbolStringPool> SSP = nullptr)
     241         428 :       : SSP(SSP ? std::move(SSP) : std::make_shared<SymbolStringPool>()) {}
     242             : 
     243             :   /// Returns the SymbolStringPool for this ExecutionSession.
     244             :   SymbolStringPool &getSymbolStringPool() const { return *SSP; }
     245             : 
     246             :   /// Run the given lambda with the session mutex locked.
     247             :   template <typename Func> auto runSessionLocked(Func &&F) -> decltype(F()) {
     248          69 :     std::lock_guard<std::recursive_mutex> Lock(SessionMutex);
     249          69 :     return F();
     250             :   }
     251             : 
     252             :   /// Set the error reporter function.
     253             :   ExecutionSessionBase &setErrorReporter(ErrorReporter ReportError) {
     254             :     this->ReportError = std::move(ReportError);
     255             :     return *this;
     256             :   }
     257             : 
     258             :   /// Set the materialization dispatch function.
     259             :   ExecutionSessionBase &setDispatchMaterialization(
     260             :       DispatchMaterializationFunction DispatchMaterialization) {
     261           2 :     this->DispatchMaterialization = std::move(DispatchMaterialization);
     262             :     return *this;
     263             :   }
     264             : 
     265             :   /// Report a error for this execution session.
     266             :   ///
     267             :   /// Unhandled errors can be sent here to log them.
     268          24 :   void reportError(Error Err) { ReportError(std::move(Err)); }
     269             : 
     270             :   /// Allocate a module key for a new module to add to the JIT.
     271         129 :   VModuleKey allocateVModule() { return ++LastKey; }
     272             : 
     273             :   /// Return a module key to the ExecutionSession so that it can be
     274             :   ///        re-used. This should only be done once all resources associated
     275             :   ///        with the original key have been released.
     276             :   void releaseVModule(VModuleKey Key) { /* FIXME: Recycle keys */
     277             :   }
     278             : 
     279             :   /// Cause the given query to fail with the given Error.
     280             :   ///
     281             :   /// This should only be used by legacy APIs and will be deprecated in the
     282             :   /// future.
     283             :   void failQuery(AsynchronousSymbolQuery &Q, Error Err);
     284             : 
     285             :   /// Materialize the given unit.
     286          11 :   void dispatchMaterialization(VSO &V,
     287             :                                std::unique_ptr<MaterializationUnit> MU) {
     288          11 :     DispatchMaterialization(V, std::move(MU));
     289          11 :   }
     290             : 
     291             : private:
     292          12 :   static void logErrorsToStdErr(Error Err) {
     293          36 :     logAllUnhandledErrors(std::move(Err), errs(), "JIT session error: ");
     294          12 :   }
     295             : 
     296             :   static void
     297           9 :   materializeOnCurrentThread(VSO &V, std::unique_ptr<MaterializationUnit> MU) {
     298           9 :     MU->doMaterialize(V);
     299           9 :   }
     300             : 
     301             :   mutable std::recursive_mutex SessionMutex;
     302             :   std::shared_ptr<SymbolStringPool> SSP;
     303             :   VModuleKey LastKey = 0;
     304             :   ErrorReporter ReportError = logErrorsToStdErr;
     305             :   DispatchMaterializationFunction DispatchMaterialization =
     306             :       materializeOnCurrentThread;
     307             : };
     308             : 
     309             : /// A symbol query that returns results via a callback when results are
     310             : ///        ready.
     311             : ///
     312             : /// makes a callback when all symbols are available.
     313         130 : class AsynchronousSymbolQuery {
     314             :   friend class ExecutionSessionBase;
     315             :   friend class VSO;
     316             : 
     317             : public:
     318         267 :   class ResolutionResult {
     319             :   public:
     320             :     ResolutionResult(SymbolMap Symbols, const SymbolDependenceMap &Dependencies)
     321         126 :         : Symbols(std::move(Symbols)), Dependencies(Dependencies) {}
     322             : 
     323             :     SymbolMap Symbols;
     324             :     const SymbolDependenceMap &Dependencies;
     325             :   };
     326             : 
     327             :   /// Callback to notify client that symbols have been resolved.
     328             :   using SymbolsResolvedCallback =
     329             :       std::function<void(Expected<ResolutionResult>)>;
     330             : 
     331             :   /// Callback to notify client that symbols are ready for execution.
     332             :   using SymbolsReadyCallback = std::function<void(Error)>;
     333             : 
     334             :   /// Create a query for the given symbols, notify-resolved and
     335             :   ///        notify-ready callbacks.
     336             :   AsynchronousSymbolQuery(const SymbolNameSet &Symbols,
     337             :                           SymbolsResolvedCallback NotifySymbolsResolved,
     338             :                           SymbolsReadyCallback NotifySymbolsReady);
     339             : 
     340             :   /// Set the resolved symbol information for the given symbol name.
     341             :   void resolve(const SymbolStringPtr &Name, JITEvaluatedSymbol Sym);
     342             : 
     343             :   /// Returns true if all symbols covered by this query have been
     344             :   ///        resolved.
     345           1 :   bool isFullyResolved() const { return NotYetResolvedCount == 0; }
     346             : 
     347             :   /// Call the NotifySymbolsResolved callback.
     348             :   ///
     349             :   /// This should only be called if all symbols covered by the query have been
     350             :   /// resolved.
     351             :   void handleFullyResolved();
     352             : 
     353             :   /// Notify the query that a requested symbol is ready for execution.
     354             :   void notifySymbolReady();
     355             : 
     356             :   /// Returns true if all symbols covered by this query are ready.
     357             :   bool isFullyReady() const { return NotYetReadyCount == 0; }
     358             : 
     359             :   /// Calls the NotifySymbolsReady callback.
     360             :   ///
     361             :   /// This should only be called if all symbols covered by this query are ready.
     362             :   void handleFullyReady();
     363             : 
     364             : private:
     365             :   void addQueryDependence(VSO &V, SymbolStringPtr Name);
     366             : 
     367             :   void removeQueryDependence(VSO &V, const SymbolStringPtr &Name);
     368             : 
     369             :   void handleFailed(Error Err);
     370             : 
     371             :   void detach();
     372             : 
     373             :   SymbolsResolvedCallback NotifySymbolsResolved;
     374             :   SymbolsReadyCallback NotifySymbolsReady;
     375             :   SymbolDependenceMap QueryRegistrations;
     376             :   SymbolMap ResolvedSymbols;
     377             :   size_t NotYetResolvedCount;
     378             :   size_t NotYetReadyCount;
     379             : };
     380             : 
     381             : /// SymbolResolver is a composable interface for looking up symbol flags
     382             : ///        and addresses using the AsynchronousSymbolQuery type. It will
     383             : ///        eventually replace the LegacyJITSymbolResolver interface as the
     384             : ///        stardard ORC symbol resolver type.
     385             : class SymbolResolver {
     386             : public:
     387             :   virtual ~SymbolResolver() = default;
     388             : 
     389             :   /// Returns the flags for each symbol in Symbols that can be found,
     390             :   ///        along with the set of symbol that could not be found.
     391             :   virtual SymbolNameSet lookupFlags(SymbolFlagsMap &Flags,
     392             :                                     const SymbolNameSet &Symbols) = 0;
     393             : 
     394             :   /// For each symbol in Symbols that can be found, assigns that symbols
     395             :   ///        value in Query. Returns the set of symbols that could not be found.
     396             :   virtual SymbolNameSet lookup(std::shared_ptr<AsynchronousSymbolQuery> Query,
     397             :                                SymbolNameSet Symbols) = 0;
     398             : 
     399             : private:
     400             :   virtual void anchor();
     401             : };
     402             : 
     403             : /// Implements SymbolResolver with a pair of supplied function objects
     404             : ///        for convenience. See createSymbolResolver.
     405             : template <typename LookupFlagsFn, typename LookupFn>
     406          30 : class LambdaSymbolResolver final : public SymbolResolver {
     407             : public:
     408             :   template <typename LookupFlagsFnRef, typename LookupFnRef>
     409          30 :   LambdaSymbolResolver(LookupFlagsFnRef &&LookupFlags, LookupFnRef &&Lookup)
     410             :       : LookupFlags(std::forward<LookupFlagsFnRef>(LookupFlags)),
     411          30 :         Lookup(std::forward<LookupFnRef>(Lookup)) {}
     412             : 
     413          39 :   SymbolNameSet lookupFlags(SymbolFlagsMap &Flags,
     414             :                             const SymbolNameSet &Symbols) final {
     415          39 :     return LookupFlags(Flags, Symbols);
     416             :   }
     417             : 
     418          24 :   SymbolNameSet lookup(std::shared_ptr<AsynchronousSymbolQuery> Query,
     419             :                        SymbolNameSet Symbols) final {
     420          73 :     return Lookup(std::move(Query), std::move(Symbols));
     421             :   }
     422             : 
     423             : private:
     424             :   LookupFlagsFn LookupFlags;
     425             :   LookupFn Lookup;
     426             : };
     427             : 
     428             : /// Creates a SymbolResolver implementation from the pair of supplied
     429             : ///        function objects.
     430             : template <typename LookupFlagsFn, typename LookupFn>
     431             : std::unique_ptr<LambdaSymbolResolver<
     432             :     typename std::remove_cv<
     433             :         typename std::remove_reference<LookupFlagsFn>::type>::type,
     434             :     typename std::remove_cv<
     435             :         typename std::remove_reference<LookupFn>::type>::type>>
     436             : createSymbolResolver(LookupFlagsFn &&LookupFlags, LookupFn &&Lookup) {
     437             :   using LambdaSymbolResolverImpl = LambdaSymbolResolver<
     438             :       typename std::remove_cv<
     439             :           typename std::remove_reference<LookupFlagsFn>::type>::type,
     440             :       typename std::remove_cv<
     441             :           typename std::remove_reference<LookupFn>::type>::type>;
     442             :   return llvm::make_unique<LambdaSymbolResolverImpl>(
     443          20 :       std::forward<LookupFlagsFn>(LookupFlags), std::forward<LookupFn>(Lookup));
     444             : }
     445             : 
     446             : /// A symbol table that supports asynchoronous symbol queries.
     447             : ///
     448             : /// Represents a virtual shared object. Instances can not be copied or moved, so
     449             : /// their addresses may be used as keys for resource management.
     450             : /// VSO state changes must be made via an ExecutionSession to guarantee that
     451             : /// they are synchronized with respect to other VSO operations.
     452          22 : class VSO {
     453             :   friend class AsynchronousSymbolQuery;
     454             :   friend class ExecutionSession;
     455             : public:
     456             :   using AsynchronousSymbolQuerySet =
     457             :       std::set<std::shared_ptr<AsynchronousSymbolQuery>>;
     458             : 
     459             :   using MaterializationUnitList =
     460             :       std::vector<std::unique_ptr<MaterializationUnit>>;
     461             : 
     462             :   using VSOList = std::vector<VSO *>;
     463             : 
     464             :   VSO(const VSO &) = delete;
     465             :   VSO &operator=(const VSO &) = delete;
     466             :   VSO(VSO &&) = delete;
     467             :   VSO &operator=(VSO &&) = delete;
     468             : 
     469             :   /// Get the name for this VSO.
     470             :   const std::string &getName() const { return VSOName; }
     471             : 
     472             :   /// Get a reference to the ExecutionSession for this VSO.
     473             :   ExecutionSessionBase &getExecutionSession() const { return ES; }
     474             : 
     475             :   /// Define all symbols provided by the materialization unit to be part
     476             :   ///        of the given VSO.
     477             :   template <typename UniquePtrToMaterializationUnit>
     478             :   typename std::enable_if<
     479             :       std::is_convertible<
     480             :           typename std::decay<UniquePtrToMaterializationUnit>::type,
     481             :           std::unique_ptr<MaterializationUnit>>::value,
     482             :       Error>::type
     483          17 :   define(UniquePtrToMaterializationUnit &&MU) {
     484          55 :     return ES.runSessionLocked([&, this]() -> Error {
     485             :       assert(MU && "Can't define with a null MU");
     486             : 
     487          68 :       if (auto Err = defineImpl(*MU))
     488             :         return Err;
     489             : 
     490             :       /// defineImpl succeeded.
     491             :       auto UMI = std::make_shared<UnmaterializedInfo>(std::move(MU));
     492          38 :       for (auto &KV : UMI->MU->getSymbols())
     493          42 :         UnmaterializedInfos[KV.first] = UMI;
     494             : 
     495             :       return Error::success();
     496          34 :     });
     497             :   }
     498             : 
     499             :   /// Define a set of symbols already in the materializing state.
     500             :   Error defineMaterializing(const SymbolFlagsMap &SymbolFlags);
     501             : 
     502             :   /// Replace the definition of a set of materializing symbols with a new
     503             :   /// MaterializationUnit.
     504             :   ///
     505             :   /// All symbols being replaced must be in the materializing state. If any
     506             :   /// symbol being replaced has pending queries then the MU will be returned
     507             :   /// for materialization. Otherwise it will be stored in the VSO and all
     508             :   /// symbols covered by MU moved back to the lazy state.
     509             :   void replace(std::unique_ptr<MaterializationUnit> MU);
     510             : 
     511             :   /// Record dependencies between symbols in this VSO and symbols in
     512             :   ///        other VSOs.
     513             :   void addDependencies(const SymbolFlagsMap &Dependents,
     514             :                        const SymbolDependenceMap &Dependencies);
     515             : 
     516             :   /// Resolve the given symbols.
     517             :   ///
     518             :   /// Returns the list of queries that become fully resolved as a consequence of
     519             :   /// this operation.
     520             :   void resolve(const SymbolMap &Resolved);
     521             : 
     522             :   /// Finalize the given symbols.
     523             :   ///
     524             :   /// Returns the list of queries that become fully ready as a consequence of
     525             :   /// this operation.
     526             :   void finalize(const SymbolFlagsMap &Finalized);
     527             : 
     528             :   /// Fail to materialize the given symbols.
     529             :   ///
     530             :   /// Returns the list of queries that fail as a consequence.
     531             :   void notifyFailed(const SymbolNameSet &FailedSymbols);
     532             : 
     533             :   /// Search the given VSO for the symbols in Symbols. If found, store
     534             :   ///        the flags for each symbol in Flags. Returns any unresolved symbols.
     535             :   SymbolNameSet lookupFlags(SymbolFlagsMap &Flags, const SymbolNameSet &Names);
     536             : 
     537             :   /// Search the given VSOs in order for the symbols in Symbols. Results
     538             :   ///        (once they become available) will be returned via the given Query.
     539             :   ///
     540             :   /// If any symbol is not found then the unresolved symbols will be returned,
     541             :   /// and the query will not be applied. The Query is not failed and can be
     542             :   /// re-used in a subsequent lookup once the symbols have been added, or
     543             :   /// manually failed.
     544             :   SymbolNameSet lookup(std::shared_ptr<AsynchronousSymbolQuery> Q,
     545             :                        SymbolNameSet Names);
     546             : 
     547             :   /// Dump current VSO state to OS.
     548             :   void dump(raw_ostream &OS);
     549             : 
     550             : private:
     551             :   using AsynchronousSymbolQueryList =
     552             :       std::vector<std::shared_ptr<AsynchronousSymbolQuery>>;
     553             : 
     554          17 :   struct UnmaterializedInfo {
     555             :     UnmaterializedInfo(std::unique_ptr<MaterializationUnit> MU)
     556             :         : MU(std::move(MU)) {}
     557             : 
     558             :     std::unique_ptr<MaterializationUnit> MU;
     559             :   };
     560             : 
     561             :   using UnmaterializedInfosMap =
     562             :       std::map<SymbolStringPtr, std::shared_ptr<UnmaterializedInfo>>;
     563             : 
     564          42 :   struct MaterializingInfo {
     565             :     AsynchronousSymbolQueryList PendingQueries;
     566             :     SymbolDependenceMap Dependants;
     567             :     SymbolDependenceMap UnfinalizedDependencies;
     568             :     bool IsFinalized = false;
     569             :   };
     570             : 
     571             :   using MaterializingInfosMap = std::map<SymbolStringPtr, MaterializingInfo>;
     572             : 
     573             :   VSO(ExecutionSessionBase &ES, std::string Name)
     574          11 :       : ES(ES), VSOName(std::move(Name)) {}
     575             : 
     576             :   ExecutionSessionBase &ES;
     577             :   std::string VSOName;
     578             :   SymbolMap Symbols;
     579             :   UnmaterializedInfosMap UnmaterializedInfos;
     580             :   MaterializingInfosMap MaterializingInfos;
     581             : 
     582             :   Error defineImpl(MaterializationUnit &MU);
     583             : 
     584             :   void detachQueryHelper(AsynchronousSymbolQuery &Q,
     585             :                          const SymbolNameSet &QuerySymbols);
     586             : 
     587             :   void transferFinalizedNodeDependencies(MaterializingInfo &DependantMI,
     588             :                                          const SymbolStringPtr &DependantName,
     589             :                                          MaterializingInfo &FinalizedMI);
     590             : };
     591             : 
     592             : /// An ExecutionSession represents a running JIT program.
     593          41 : class ExecutionSession : public ExecutionSessionBase {
     594             : public:
     595             :   using ErrorReporter = std::function<void(Error)>;
     596             : 
     597             :   using DispatchMaterializationFunction =
     598             :       std::function<void(VSO &V, std::unique_ptr<MaterializationUnit> MU)>;
     599             : 
     600             :   /// Construct an ExecutionEngine.
     601             :   ///
     602             :   /// SymbolStringPools may be shared between ExecutionSessions.
     603         107 :   ExecutionSession(std::shared_ptr<SymbolStringPool> SSP = nullptr)
     604         428 :       : ExecutionSessionBase(std::move(SSP)) {}
     605             : 
     606             :   /// Add a new VSO to this ExecutionSession.
     607             :   VSO &createVSO(std::string Name);
     608             : 
     609             : private:
     610             :   std::vector<std::unique_ptr<VSO>> VSOs;
     611             : };
     612             : 
     613             : /// Look up the given names in the given VSOs.
     614             : /// VSOs will be searched in order and no VSO pointer may be null.
     615             : /// All symbols must be found within the given VSOs or an error
     616             : /// will be returned.
     617             : ///
     618             : /// If this lookup is being performed on behalf of a
     619             : /// MaterializationResponsibility then it must be passed in as R
     620             : /// (in order to record the symbol dependencies).
     621             : /// If this lookup is not being performed on behalf of a
     622             : /// MaterializationResponsibility then R should be left null.
     623             : Expected<SymbolMap> lookup(const std::vector<VSO *> &VSOs, SymbolNameSet Names,
     624             :                            MaterializationResponsibility *R);
     625             : 
     626             : /// Look up a symbol by searching a list of VSOs.
     627             : Expected<JITEvaluatedSymbol> lookup(const std::vector<VSO *> VSOs,
     628             :                                     SymbolStringPtr Name,
     629             :                                     MaterializationResponsibility *R);
     630             : 
     631             : } // End namespace orc
     632             : } // End namespace llvm
     633             : 
     634             : #endif // LLVM_EXECUTIONENGINE_ORC_CORE_H

Generated by: LCOV version 1.13