LLVM 24.0.0git
Core.h
Go to the documentation of this file.
1//===------ Core.h -- Core ORC APIs (Layer, JITDylib, etc.) -----*- 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_CORE_H
14#define LLVM_EXECUTIONENGINE_ORC_CORE_H
15
17#include "llvm/ADT/DenseSet.h"
33#include "llvm/Support/Debug.h"
35
36#include <atomic>
37#include <deque>
38#include <future>
39#include <memory>
40#include <vector>
41
42namespace llvm {
43namespace orc {
44
45// Forward declare some classes.
49class JITDylib;
50class ResourceTracker;
52
53enum class SymbolState : uint8_t;
54
57
60
62
63/// API to remove / transfer ownership of JIT resources.
64class ResourceTracker : public ThreadSafeRefCountedBase<ResourceTracker> {
65private:
66 friend class ExecutionSession;
67 friend class JITDylib;
69
70public:
75
77
78 /// Return the JITDylib targeted by this tracker.
80 return *reinterpret_cast<JITDylib *>(JDAndFlag.load() &
81 ~static_cast<uintptr_t>(1));
82 }
83
84 /// Runs the given callback under the session lock, passing in the associated
85 /// ResourceKey. This is the safe way to associate resources with trackers.
86 template <typename Func> Error withResourceKeyDo(Func &&F);
87
88 /// Remove all resources associated with this key.
90
91 /// Transfer all resources associated with this key to the given
92 /// tracker, which must target the same JITDylib as this one.
94
95 /// Return true if this tracker has become defunct.
96 bool isDefunct() const { return JDAndFlag.load() & 0x1; }
97
98 /// Returns the key associated with this tracker.
99 /// This method should not be used except for debug logging: there is no
100 /// guarantee that the returned value will remain valid.
101 ResourceKey getKeyUnsafe() const { return reinterpret_cast<uintptr_t>(this); }
102
103private:
105
106 void makeDefunct();
107
108 std::atomic_uintptr_t JDAndFlag;
109};
110
111/// Listens for ResourceTracker operations.
113public:
115
116 /// This function will be called *outside* the session lock. ResourceManagers
117 /// should perform book-keeping under the session lock, and any expensive
118 /// cleanup outside the session lock.
120
121 /// This function will be called *inside* the session lock. ResourceManagers
122 /// DO NOT need to re-lock the session.
124 ResourceKey SrcK) = 0;
125};
126
127/// Lookup flags that apply to each dylib in the search order for a lookup.
128///
129/// If MatchHiddenSymbolsOnly is used (the default) for a given dylib, then
130/// only symbols in that Dylib's interface will be searched. If
131/// MatchHiddenSymbols is used then symbols with hidden visibility will match
132/// as well.
134
135/// Describes the kind of lookup being performed. The lookup kind is passed to
136/// symbol generators (if they're invoked) to help them determine what
137/// definitions to generate.
138///
139/// Static -- Lookup is being performed as-if at static link time (e.g.
140/// generators representing static archives should pull in new
141/// definitions).
142///
143/// DLSym -- Lookup is being performed as-if at runtime (e.g. generators
144/// representing static archives should not pull in new definitions).
145enum class LookupKind { Static, DLSym };
146
147/// A list of (JITDylib*, JITDylibLookupFlags) pairs to be used as a search
148/// order during symbol lookup.
150 std::vector<std::pair<JITDylib *, JITDylibLookupFlags>>;
151
152/// Convenience function for creating a search order from an ArrayRef of
153/// JITDylib*, all with the same flags.
158 O.reserve(JDs.size());
159 for (auto *JD : JDs)
160 O.push_back(std::make_pair(JD, Flags));
161 return O;
162}
163
172
173/// A map of Symbols to (Symbol, Flags) pairs.
175
176/// Callback to notify client that symbols have been resolved.
178
179/// Callback to register the dependencies for a given query.
181 std::function<void(const SymbolDependenceMap &)>;
182
183/// This can be used as the value for a RegisterDependenciesFunction if there
184/// are no dependants to register with.
186
188 : public ErrorInfo<ResourceTrackerDefunct> {
189public:
190 static char ID;
191
193 std::error_code convertToErrorCode() const override;
194 void log(raw_ostream &OS) const override;
195
196private:
198};
199
200/// Returned by operations that fail because a JITDylib has been closed.
201class LLVM_ABI JITDylibDefunct : public ErrorInfo<JITDylibDefunct> {
202public:
203 static char ID;
204
206 std::error_code convertToErrorCode() const override;
207 void log(raw_ostream &OS) const override;
208
209private:
210 JITDylibSP JD;
211};
212
213/// Used to notify a JITDylib that the given set of symbols failed to
214/// materialize.
215class LLVM_ABI FailedToMaterialize : public ErrorInfo<FailedToMaterialize> {
216public:
217 static char ID;
218
219 FailedToMaterialize(std::shared_ptr<SymbolStringPool> SSP,
220 std::shared_ptr<SymbolDependenceMap> Symbols);
221 ~FailedToMaterialize() override;
222 std::error_code convertToErrorCode() const override;
223 void log(raw_ostream &OS) const override;
224 const SymbolDependenceMap &getSymbols() const { return *Symbols; }
225
226private:
227 std::shared_ptr<SymbolStringPool> SSP;
228 std::shared_ptr<SymbolDependenceMap> Symbols;
229};
230
231/// Used to report failure due to unsatisfiable symbol dependencies.
233 : public ErrorInfo<UnsatisfiedSymbolDependencies> {
234public:
235 static char ID;
236
237 UnsatisfiedSymbolDependencies(std::shared_ptr<SymbolStringPool> SSP,
238 JITDylibSP JD, SymbolNameSet FailedSymbols,
239 SymbolDependenceMap BadDeps,
240 std::string Explanation);
241 std::error_code convertToErrorCode() const override;
242 void log(raw_ostream &OS) const override;
243
244private:
245 std::shared_ptr<SymbolStringPool> SSP;
246 JITDylibSP JD;
247 SymbolNameSet FailedSymbols;
248 SymbolDependenceMap BadDeps;
249 std::string Explanation;
250};
251
252/// Used to notify clients when symbols can not be found during a lookup.
253class LLVM_ABI SymbolsNotFound : public ErrorInfo<SymbolsNotFound> {
254public:
255 static char ID;
256
257 SymbolsNotFound(std::shared_ptr<SymbolStringPool> SSP, SymbolNameSet Symbols);
258 SymbolsNotFound(std::shared_ptr<SymbolStringPool> SSP,
259 SymbolNameVector Symbols);
260 std::error_code convertToErrorCode() const override;
261 void log(raw_ostream &OS) const override;
262 std::shared_ptr<SymbolStringPool> getSymbolStringPool() { return SSP; }
263 const SymbolNameVector &getSymbols() const { return Symbols; }
264
265private:
266 std::shared_ptr<SymbolStringPool> SSP;
267 SymbolNameVector Symbols;
268};
269
270/// Used to notify clients that a set of symbols could not be removed.
272 : public ErrorInfo<SymbolsCouldNotBeRemoved> {
273public:
274 static char ID;
275
276 SymbolsCouldNotBeRemoved(std::shared_ptr<SymbolStringPool> SSP,
277 SymbolNameSet Symbols);
278 std::error_code convertToErrorCode() const override;
279 void log(raw_ostream &OS) const override;
280 std::shared_ptr<SymbolStringPool> getSymbolStringPool() { return SSP; }
281 const SymbolNameSet &getSymbols() const { return Symbols; }
282
283private:
284 std::shared_ptr<SymbolStringPool> SSP;
285 SymbolNameSet Symbols;
286};
287
288/// Errors of this type should be returned if a module fails to include
289/// definitions that are claimed by the module's associated
290/// MaterializationResponsibility. If this error is returned it is indicative of
291/// a broken transformation / compiler / object cache.
293 : public ErrorInfo<MissingSymbolDefinitions> {
294public:
295 static char ID;
296
297 MissingSymbolDefinitions(std::shared_ptr<SymbolStringPool> SSP,
298 std::string ModuleName, SymbolNameVector Symbols)
299 : SSP(std::move(SSP)), ModuleName(std::move(ModuleName)),
300 Symbols(std::move(Symbols)) {}
301 std::error_code convertToErrorCode() const override;
302 void log(raw_ostream &OS) const override;
303 std::shared_ptr<SymbolStringPool> getSymbolStringPool() { return SSP; }
304 const std::string &getModuleName() const { return ModuleName; }
305 const SymbolNameVector &getSymbols() const { return Symbols; }
306private:
307 std::shared_ptr<SymbolStringPool> SSP;
308 std::string ModuleName;
309 SymbolNameVector Symbols;
310};
311
312/// Errors of this type should be returned if a module contains definitions for
313/// symbols that are not claimed by the module's associated
314/// MaterializationResponsibility. If this error is returned it is indicative of
315/// a broken transformation / compiler / object cache.
317 : public ErrorInfo<UnexpectedSymbolDefinitions> {
318public:
319 static char ID;
320
321 UnexpectedSymbolDefinitions(std::shared_ptr<SymbolStringPool> SSP,
322 std::string ModuleName, SymbolNameVector Symbols)
323 : SSP(std::move(SSP)), ModuleName(std::move(ModuleName)),
324 Symbols(std::move(Symbols)) {}
325 std::error_code convertToErrorCode() const override;
326 void log(raw_ostream &OS) const override;
327 std::shared_ptr<SymbolStringPool> getSymbolStringPool() { return SSP; }
328 const std::string &getModuleName() const { return ModuleName; }
329 const SymbolNameVector &getSymbols() const { return Symbols; }
330private:
331 std::shared_ptr<SymbolStringPool> SSP;
332 std::string ModuleName;
333 SymbolNameVector Symbols;
334};
335
336/// A set of symbols and the their dependencies. Used to describe dependencies
337/// for the MaterializationResponsibility::notifyEmitted operation.
342
343/// Tracks responsibility for materialization, and mediates interactions between
344/// MaterializationUnits and JDs.
345///
346/// An instance of this class is passed to MaterializationUnits when their
347/// materialize method is called. It allows MaterializationUnits to resolve and
348/// emit symbols, or abandon materialization by notifying any unmaterialized
349/// symbols of an error.
351 friend class ExecutionSession;
352 friend class JITDylib;
353
354public:
358
359 /// Destruct a MaterializationResponsibility instance. In debug mode
360 /// this asserts that all symbols being tracked have been either
361 /// emitted or notified of an error.
363
364 /// Return the ResourceTracker associated with this instance.
365 const ResourceTrackerSP &getResourceTracker() const { return RT; }
366
367 /// Runs the given callback under the session lock, passing in the associated
368 /// ResourceKey. This is the safe way to associate resources with trackers.
369 template <typename Func> Error withResourceKeyDo(Func &&F) const {
370 return RT->withResourceKeyDo(std::forward<Func>(F));
371 }
372
373 /// Returns the target JITDylib that these symbols are being materialized
374 /// into.
375 JITDylib &getTargetJITDylib() const { return JD; }
376
377 /// Returns the ExecutionSession for this instance.
379
380 /// Returns the symbol flags map for this responsibility instance.
381 /// Note: The returned flags may have transient flags (Lazy, Materializing)
382 /// set. These should be stripped with JITSymbolFlags::stripTransientFlags
383 /// before using.
384 const SymbolFlagsMap &getSymbols() const { return SymbolFlags; }
385
386 /// Returns the initialization pseudo-symbol, if any. This symbol will also
387 /// be present in the SymbolFlagsMap for this MaterializationResponsibility
388 /// object.
389 const SymbolStringPtr &getInitializerSymbol() const { return InitSymbol; }
390
391 /// Returns the names of any symbols covered by this
392 /// MaterializationResponsibility object that have queries pending. This
393 /// information can be used to return responsibility for unrequested symbols
394 /// back to the JITDylib via the delegate method.
396
397 /// Notifies the target JITDylib that the given symbols have been resolved.
398 /// This will update the given symbols' addresses in the JITDylib, and notify
399 /// any pending queries on the given symbols of their resolution. The given
400 /// symbols must be ones covered by this MaterializationResponsibility
401 /// instance. Individual calls to this method may resolve a subset of the
402 /// symbols, but all symbols must have been resolved prior to calling emit.
403 ///
404 /// This method will return an error if any symbols being resolved have been
405 /// moved to the error state due to the failure of a dependency. If this
406 /// method returns an error then clients should log it and call
407 /// failMaterialize. If no dependencies have been registered for the
408 /// symbols covered by this MaterializationResponsibility then this method
409 /// is guaranteed to return Error::success() and can be wrapped with cantFail.
410 Error notifyResolved(const SymbolMap &Symbols);
411
412 /// Notifies the target JITDylib (and any pending queries on that JITDylib)
413 /// that all symbols covered by this MaterializationResponsibility instance
414 /// have been emitted.
415 ///
416 /// The DepGroups array describes the dependencies of symbols being emitted on
417 /// symbols that are outside this MaterializationResponsibility object. Each
418 /// group consists of a pair of a set of symbols and a SymbolDependenceMap
419 /// that describes the dependencies for the symbols in the first set. The
420 /// elements of DepGroups must be non-overlapping (no symbol should appear in
421 /// more than one of hte symbol sets), but do not have to be exhaustive. Any
422 /// symbol in this MaterializationResponsibility object that is not covered
423 /// by an entry will be treated as having no dependencies.
424 ///
425 /// This method will return an error if any symbols being resolved have been
426 /// moved to the error state due to the failure of a dependency. If this
427 /// method returns an error then clients should log it and call
428 /// failMaterialize. If no dependencies have been registered for the
429 /// symbols covered by this MaterializationResponsibility then this method
430 /// is guaranteed to return Error::success() and can be wrapped with cantFail.
432
433 /// Attempt to claim responsibility for new definitions. This method can be
434 /// used to claim responsibility for symbols that are added to a
435 /// materialization unit during the compilation process (e.g. literal pool
436 /// symbols). Symbol linkage rules are the same as for symbols that are
437 /// defined up front: duplicate strong definitions will result in errors.
438 /// Duplicate weak definitions will be discarded (in which case they will
439 /// not be added to this responsibility instance).
440 ///
441 /// This method can be used by materialization units that want to add
442 /// additional symbols at materialization time (e.g. stubs, compile
443 /// callbacks, metadata).
445
446 /// Notify all not-yet-emitted covered by this MaterializationResponsibility
447 /// instance that an error has occurred.
448 /// This will remove all symbols covered by this MaterializationResponsibility
449 /// from the target JITDylib, and send an error to any queries waiting on
450 /// these symbols.
451 void failMaterialization();
452
453 /// Transfers responsibility to the given MaterializationUnit for all
454 /// symbols defined by that MaterializationUnit. This allows
455 /// materializers to break up work based on run-time information (e.g.
456 /// by introspecting which symbols have actually been looked up and
457 /// materializing only those).
458 Error replace(std::unique_ptr<MaterializationUnit> MU);
459
460 /// Delegates responsibility for the given symbols to the returned
461 /// materialization responsibility. Useful for breaking up work between
462 /// threads, or different kinds of materialization processes.
464 delegate(const SymbolNameSet &Symbols);
465
466private:
467 /// Create a MaterializationResponsibility for the given JITDylib and
468 /// initial symbols.
470 SymbolFlagsMap SymbolFlags,
471 SymbolStringPtr InitSymbol)
472 : JD(RT->getJITDylib()), RT(std::move(RT)),
473 SymbolFlags(std::move(SymbolFlags)), InitSymbol(std::move(InitSymbol)) {
474 assert(!this->SymbolFlags.empty() && "Materializing nothing?");
475 }
476
477 JITDylib &JD;
479 SymbolFlagsMap SymbolFlags;
480 SymbolStringPtr InitSymbol;
481};
482
483/// A materialization unit for symbol aliases. Allows existing symbols to be
484/// aliased with alternate flags.
486public:
487 /// SourceJD is allowed to be nullptr, in which case the source JITDylib is
488 /// taken to be whatever JITDylib these definitions are materialized in (and
489 /// MatchNonExported has no effect). This is useful for defining aliases
490 /// within a JITDylib.
491 ///
492 /// Note: Care must be taken that no sets of aliases form a cycle, as such
493 /// a cycle will result in a deadlock when any symbol in the cycle is
494 /// resolved.
496 JITDylibLookupFlags SourceJDLookupFlags,
497 SymbolAliasMap Aliases);
498
499 StringRef getName() const override;
500
501private:
502 void materialize(std::unique_ptr<MaterializationResponsibility> R) override;
503 void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
505 extractFlags(const SymbolAliasMap &Aliases);
506
507 JITDylib *SourceJD = nullptr;
508 JITDylibLookupFlags SourceJDLookupFlags;
509 SymbolAliasMap Aliases;
510};
511
512/// Create a ReExportsMaterializationUnit with the given aliases.
513/// Useful for defining symbol aliases.: E.g., given a JITDylib JD containing
514/// symbols "foo" and "bar", we can define aliases "baz" (for "foo") and "qux"
515/// (for "bar") with: \code{.cpp}
516/// SymbolStringPtr Baz = ...;
517/// SymbolStringPtr Qux = ...;
518/// if (auto Err = JD.define(symbolAliases({
519/// {Baz, { Foo, JITSymbolFlags::Exported }},
520/// {Qux, { Bar, JITSymbolFlags::Weak }}}))
521/// return Err;
522/// \endcode
523inline std::unique_ptr<ReExportsMaterializationUnit>
525 return std::make_unique<ReExportsMaterializationUnit>(
526 nullptr, JITDylibLookupFlags::MatchAllSymbols, std::move(Aliases));
527}
528
529/// Create a materialization unit for re-exporting symbols from another JITDylib
530/// with alternative names/flags.
531/// SourceJD will be searched using the given JITDylibLookupFlags.
532inline std::unique_ptr<ReExportsMaterializationUnit>
534 JITDylibLookupFlags SourceJDLookupFlags =
536 return std::make_unique<ReExportsMaterializationUnit>(
537 &SourceJD, SourceJDLookupFlags, std::move(Aliases));
538}
539
540/// Build a SymbolAliasMap for the common case where you want to re-export
541/// symbols from another JITDylib with the same linkage/flags.
543buildSimpleReexportsAliasMap(JITDylib &SourceJD, const SymbolNameSet &Symbols);
544
545/// Represents the state that a symbol has reached during materialization.
546enum class SymbolState : uint8_t {
547 Invalid, /// No symbol should be in this state.
548 NeverSearched, /// Added to the symbol table, never queried.
549 Materializing, /// Queried, materialization begun.
550 Resolved, /// Assigned address, still materializing.
551 Emitted, /// Emitted to memory, but waiting on transitive dependencies.
552 Ready = 0x3f /// Ready and safe for clients to access.
553};
554
555/// A symbol query that returns results via a callback when results are
556/// ready.
557///
558/// makes a callback when all symbols are available.
560 friend class ExecutionSession;
562 friend class JITDylib;
565
566public:
567 /// Create a query for the given symbols. The NotifyComplete
568 /// callback will be called once all queried symbols reach the given
569 /// minimum state.
571 SymbolState RequiredState,
572 SymbolsResolvedCallback NotifyComplete);
573
574 /// Notify the query that a requested symbol has reached the required state.
577
578 /// Returns true if all symbols covered by this query have been
579 /// resolved.
580 bool isComplete() const { return OutstandingSymbolsCount == 0; }
581
582
583private:
584 void handleComplete(ExecutionSession &ES);
585
586 SymbolState getRequiredState() { return RequiredState; }
587
588 void addQueryDependence(JITDylib &JD, SymbolStringPtr Name);
589
590 void removeQueryDependence(JITDylib &JD, const SymbolStringPtr &Name);
591
592 void dropSymbol(const SymbolStringPtr &Name);
593
594 void handleFailed(Error Err);
595
596 void detach();
597
598 SymbolsResolvedCallback NotifyComplete;
599 SymbolDependenceMap QueryRegistrations;
600 SymbolMap ResolvedSymbols;
601 size_t OutstandingSymbolsCount;
602 SymbolState RequiredState;
603};
604
605/// Wraps state for a lookup-in-progress.
606/// DefinitionGenerators can optionally take ownership of a LookupState object
607/// to suspend a lookup-in-progress while they search for definitions.
609 friend class OrcV2CAPIHelper;
610 friend class ExecutionSession;
611
612public:
617
618 /// Continue the lookup. This can be called by DefinitionGenerators
619 /// to re-start a captured query-application operation.
620 LLVM_ABI void continueLookup(Error Err);
621
622private:
623 LookupState(std::unique_ptr<InProgressLookupState> IPLS);
624
625 // For C API.
626 void reset(InProgressLookupState *IPLS);
627
628 std::unique_ptr<InProgressLookupState> IPLS;
629};
630
631/// Definition generators can be attached to JITDylibs to generate new
632/// definitions for otherwise unresolved symbols during lookup.
634 friend class ExecutionSession;
635
636public:
637 virtual ~DefinitionGenerator();
638
639 /// DefinitionGenerators should override this method to insert new
640 /// definitions into the parent JITDylib. K specifies the kind of this
641 /// lookup. JD specifies the target JITDylib being searched, and
642 /// JDLookupFlags specifies whether the search should match against
643 /// hidden symbols. Finally, Symbols describes the set of unresolved
644 /// symbols and their associated lookup flags.
646 JITDylibLookupFlags JDLookupFlags,
647 const SymbolLookupSet &LookupSet) = 0;
648
649private:
650 std::mutex M;
651 bool InUse = false;
652 std::deque<LookupState> PendingLookups;
653};
654
655/// Represents a JIT'd dynamic library.
656///
657/// This class aims to mimic the behavior of a regular dylib or shared object,
658/// but without requiring the contained program representations to be compiled
659/// up-front. The JITDylib's content is defined by adding MaterializationUnits,
660/// and contained MaterializationUnits will typically rely on the JITDylib's
661/// links-against order to resolve external references (similar to a regular
662/// dylib).
663///
664/// The JITDylib object is a thin wrapper that references state held by the
665/// ExecutionSession. JITDylibs can be removed, clearing this underlying state
666/// and leaving the JITDylib object in a defunct state. In this state the
667/// JITDylib's name is guaranteed to remain accessible. If the ExecutionSession
668/// is still alive then other operations are callable but will return an Error
669/// or null result (depending on the API). It is illegal to call any operation
670/// other than getName on a JITDylib after the ExecutionSession has been torn
671/// down.
672///
673/// JITDylibs cannot be moved or copied. Their address is stable, and useful as
674/// a key in some JIT data structures.
675class JITDylib : public ThreadSafeRefCountedBase<JITDylib>,
676 public jitlink::JITLinkDylib {
678 friend class ExecutionSession;
679 friend class Platform;
681public:
682
683 JITDylib(const JITDylib &) = delete;
684 JITDylib &operator=(const JITDylib &) = delete;
685 JITDylib(JITDylib &&) = delete;
688
689 /// Get a reference to the ExecutionSession for this JITDylib.
690 ///
691 /// It is legal to call this method on a defunct JITDylib, however the result
692 /// will only usable if the ExecutionSession is still alive. If this JITDylib
693 /// is held by an error that may have torn down the JIT then the result
694 /// should not be used.
695 ExecutionSession &getExecutionSession() const { return ES; }
696
697 /// Dump current JITDylib state to OS.
698 ///
699 /// It is legal to call this method on a defunct JITDylib.
700 LLVM_ABI void dump(raw_ostream &OS);
701
702 /// Calls remove on all trackers currently associated with this JITDylib.
703 /// Does not run static deinits.
704 ///
705 /// Note that removal happens outside the session lock, so new code may be
706 /// added concurrently while the clear is underway, and the newly added
707 /// code will *not* be cleared. Adding new code concurrently with a clear
708 /// is usually a bug and should be avoided.
709 ///
710 /// It is illegal to call this method on a defunct JITDylib and the client
711 /// is responsible for ensuring that they do not do so.
713
714 /// Get the default resource tracker for this JITDylib.
715 ///
716 /// It is illegal to call this method on a defunct JITDylib and the client
717 /// is responsible for ensuring that they do not do so.
719
720 /// Create a resource tracker for this JITDylib.
721 ///
722 /// It is illegal to call this method on a defunct JITDylib and the client
723 /// is responsible for ensuring that they do not do so.
725
726 /// Adds a definition generator to this JITDylib and returns a referenece to
727 /// it.
728 ///
729 /// When JITDylibs are searched during lookup, if no existing definition of
730 /// a symbol is found, then any generators that have been added are run (in
731 /// the order that they were added) to potentially generate a definition.
732 ///
733 /// It is illegal to call this method on a defunct JITDylib and the client
734 /// is responsible for ensuring that they do not do so.
735 template <typename GeneratorT>
736 GeneratorT &addGenerator(std::unique_ptr<GeneratorT> DefGenerator);
737
738 /// Remove a definition generator from this JITDylib.
739 ///
740 /// The given generator must exist in this JITDylib's generators list (i.e.
741 /// have been added and not yet removed).
742 ///
743 /// It is illegal to call this method on a defunct JITDylib and the client
744 /// is responsible for ensuring that they do not do so.
746
747 /// Set the link order to be used when fixing up definitions in JITDylib.
748 /// This will replace the previous link order, and apply to any symbol
749 /// resolutions made for definitions in this JITDylib after the call to
750 /// setLinkOrder (even if the definition itself was added before the
751 /// call).
752 ///
753 /// If LinkAgainstThisJITDylibFirst is true (the default) then this JITDylib
754 /// will add itself to the beginning of the LinkOrder (Clients should not
755 /// put this JITDylib in the list in this case, to avoid redundant lookups).
756 ///
757 /// If LinkAgainstThisJITDylibFirst is false then the link order will be used
758 /// as-is. The primary motivation for this feature is to support deliberate
759 /// shadowing of symbols in this JITDylib by a facade JITDylib. For example,
760 /// the facade may resolve function names to stubs, and the stubs may compile
761 /// lazily by looking up symbols in this dylib. Adding the facade dylib
762 /// as the first in the link order (instead of this dylib) ensures that
763 /// definitions within this dylib resolve to the lazy-compiling stubs,
764 /// rather than immediately materializing the definitions in this dylib.
765 ///
766 /// It is illegal to call this method on a defunct JITDylib and the client
767 /// is responsible for ensuring that they do not do so.
768 LLVM_ABI void setLinkOrder(JITDylibSearchOrder NewSearchOrder,
769 bool LinkAgainstThisJITDylibFirst = true);
770
771 /// Append the given JITDylibSearchOrder to the link order for this
772 /// JITDylib (discarding any elements already present in this JITDylib's
773 /// link order).
774 LLVM_ABI void addToLinkOrder(const JITDylibSearchOrder &NewLinks);
775
776 /// Add the given JITDylib to the link order for definitions in this
777 /// JITDylib.
778 ///
779 /// It is illegal to call this method on a defunct JITDylib and the client
780 /// is responsible for ensuring that they do not do so.
781 LLVM_ABI void
783 JITDylibLookupFlags JDLookupFlags =
785
786 /// Replace OldJD with NewJD in the link order if OldJD is present.
787 /// Otherwise this operation is a no-op.
788 ///
789 /// It is illegal to call this method on a defunct JITDylib and the client
790 /// is responsible for ensuring that they do not do so.
791 LLVM_ABI void
792 replaceInLinkOrder(JITDylib &OldJD, JITDylib &NewJD,
793 JITDylibLookupFlags JDLookupFlags =
795
796 /// Remove the given JITDylib from the link order for this JITDylib if it is
797 /// present. Otherwise this operation is a no-op.
798 ///
799 /// It is illegal to call this method on a defunct JITDylib and the client
800 /// is responsible for ensuring that they do not do so.
802
803 /// Do something with the link order (run under the session lock).
804 ///
805 /// It is illegal to call this method on a defunct JITDylib and the client
806 /// is responsible for ensuring that they do not do so.
807 template <typename Func>
808 auto withLinkOrderDo(Func &&F)
809 -> decltype(F(std::declval<const JITDylibSearchOrder &>()));
810
811 /// Define all symbols provided by the materialization unit to be part of this
812 /// JITDylib.
813 ///
814 /// If RT is not specified then the default resource tracker will be used.
815 ///
816 /// This overload always takes ownership of the MaterializationUnit. If any
817 /// errors occur, the MaterializationUnit consumed.
818 ///
819 /// It is illegal to call this method on a defunct JITDylib and the client
820 /// is responsible for ensuring that they do not do so.
821 template <typename MaterializationUnitType>
822 Error define(std::unique_ptr<MaterializationUnitType> &&MU,
823 ResourceTrackerSP RT = nullptr);
824
825 /// Define all symbols provided by the materialization unit to be part of this
826 /// JITDylib.
827 ///
828 /// This overload only takes ownership of the MaterializationUnit no error is
829 /// generated. If an error occurs, ownership remains with the caller. This
830 /// may allow the caller to modify the MaterializationUnit to correct the
831 /// issue, then re-call define.
832 ///
833 /// It is illegal to call this method on a defunct JITDylib and the client
834 /// is responsible for ensuring that they do not do so.
835 template <typename MaterializationUnitType>
836 Error define(std::unique_ptr<MaterializationUnitType> &MU,
837 ResourceTrackerSP RT = nullptr);
838
839 /// Tries to remove the given symbols.
840 ///
841 /// If any symbols are not defined in this JITDylib this method will return
842 /// a SymbolsNotFound error covering the missing symbols.
843 ///
844 /// If all symbols are found but some symbols are in the process of being
845 /// materialized this method will return a SymbolsCouldNotBeRemoved error.
846 ///
847 /// On success, all symbols are removed. On failure, the JITDylib state is
848 /// left unmodified (no symbols are removed).
849 ///
850 /// It is illegal to call this method on a defunct JITDylib and the client
851 /// is responsible for ensuring that they do not do so.
852 LLVM_ABI Error remove(const SymbolNameSet &Names);
853
854 /// Returns the given JITDylibs and all of their transitive dependencies in
855 /// DFS order (based on linkage relationships). Each JITDylib will appear
856 /// only once.
857 ///
858 /// If any JITDylib in the order is defunct then this method will return an
859 /// error, otherwise returns the order.
862
863 /// Returns the given JITDylibs and all of their transitive dependencies in
864 /// reverse DFS order (based on linkage relationships). Each JITDylib will
865 /// appear only once.
866 ///
867 /// If any JITDylib in the order is defunct then this method will return an
868 /// error, otherwise returns the order.
871
872 /// Return this JITDylib and its transitive dependencies in DFS order
873 /// based on linkage relationships.
874 ///
875 /// If any JITDylib in the order is defunct then this method will return an
876 /// error, otherwise returns the order.
878
879 /// Rteurn this JITDylib and its transitive dependencies in reverse DFS order
880 /// based on linkage relationships.
881 ///
882 /// If any JITDylib in the order is defunct then this method will return an
883 /// error, otherwise returns the order.
885
886private:
887 using AsynchronousSymbolQuerySet =
888 std::set<std::shared_ptr<AsynchronousSymbolQuery>>;
889
890 using AsynchronousSymbolQueryList =
891 std::vector<std::shared_ptr<AsynchronousSymbolQuery>>;
892
893 struct UnmaterializedInfo {
894 UnmaterializedInfo(std::unique_ptr<MaterializationUnit> MU,
895 ResourceTracker *RT)
896 : MU(std::move(MU)), RT(RT) {}
897
898 std::unique_ptr<MaterializationUnit> MU;
899 ResourceTracker *RT;
900 };
901
902 using UnmaterializedInfosMap =
903 DenseMap<SymbolStringPtr, std::shared_ptr<UnmaterializedInfo>>;
904
905 using UnmaterializedInfosList =
906 std::vector<std::shared_ptr<UnmaterializedInfo>>;
907
908 // Information about not-yet-ready symbol.
909 // * DefiningEDU will point to the EmissionDepUnit that defines the symbol.
910 // * DependantEDUs will hold pointers to any EmissionDepUnits currently
911 // waiting on this symbol.
912 // * Pending queries holds any not-yet-completed queries that include this
913 // symbol.
914 struct MaterializingInfo {
915 friend class ExecutionSession;
916
917 LLVM_ABI void addQuery(std::shared_ptr<AsynchronousSymbolQuery> Q);
918 LLVM_ABI void removeQuery(const AsynchronousSymbolQuery &Q);
919 LLVM_ABI AsynchronousSymbolQueryList
920 takeQueriesMeeting(SymbolState RequiredState);
921 AsynchronousSymbolQueryList takeAllPendingQueries() {
922 return std::move(PendingQueries);
923 }
924 bool hasQueriesPending() const { return !PendingQueries.empty(); }
925 const AsynchronousSymbolQueryList &pendingQueries() const {
926 return PendingQueries;
927 }
928 private:
929 AsynchronousSymbolQueryList PendingQueries;
930 };
931
932 using MaterializingInfosMap = DenseMap<SymbolStringPtr, MaterializingInfo>;
933
934 class SymbolTableEntry {
935 public:
936 SymbolTableEntry() = default;
937 SymbolTableEntry(JITSymbolFlags Flags)
938 : Flags(Flags), State(static_cast<uint8_t>(SymbolState::NeverSearched)),
939 MaterializerAttached(false) {}
940
941 ExecutorAddr getAddress() const { return Addr; }
942 JITSymbolFlags getFlags() const { return Flags; }
943 SymbolState getState() const { return static_cast<SymbolState>(State); }
944
945 bool hasMaterializerAttached() const { return MaterializerAttached; }
946
947 void setAddress(ExecutorAddr Addr) { this->Addr = Addr; }
948 void setFlags(JITSymbolFlags Flags) { this->Flags = Flags; }
949 void setState(SymbolState State) {
950 assert(static_cast<uint8_t>(State) < (1 << 6) &&
951 "State does not fit in bitfield");
952 this->State = static_cast<uint8_t>(State);
953 }
954
955 void setMaterializerAttached(bool MaterializerAttached) {
956 this->MaterializerAttached = MaterializerAttached;
957 }
958
959 ExecutorSymbolDef getSymbol() const { return {Addr, Flags}; }
960
961 private:
962 ExecutorAddr Addr;
963 JITSymbolFlags Flags;
964 uint8_t State : 7;
965 uint8_t MaterializerAttached : 1;
966 };
967
968 using SymbolTable = DenseMap<SymbolStringPtr, SymbolTableEntry>;
969
970 JITDylib(ExecutionSession &ES, std::string Name);
971
972 struct RemoveTrackerResult {
973 AsynchronousSymbolQuerySet QueriesToFail;
974 std::shared_ptr<SymbolDependenceMap> FailedSymbols;
975 std::vector<std::unique_ptr<MaterializationUnit>> DefunctMUs;
976 };
977
978 RemoveTrackerResult IL_removeTracker(ResourceTracker &RT);
979
980 void transferTracker(ResourceTracker &DstRT, ResourceTracker &SrcRT);
981
982 LLVM_ABI Error defineImpl(MaterializationUnit &MU);
983
984 LLVM_ABI void
985 installMaterializationUnit(std::unique_ptr<MaterializationUnit> MU,
986 ResourceTracker &RT);
987
988 void detachQueryHelper(AsynchronousSymbolQuery &Q,
989 const SymbolNameSet &QuerySymbols);
990
991 void transferEmittedNodeDependencies(MaterializingInfo &DependantMI,
992 const SymbolStringPtr &DependantName,
993 MaterializingInfo &EmittedMI);
994
995 Expected<SymbolFlagsMap>
996 defineMaterializing(MaterializationResponsibility &FromMR,
997 SymbolFlagsMap SymbolFlags);
998
999 Error replace(MaterializationResponsibility &FromMR,
1000 std::unique_ptr<MaterializationUnit> MU);
1001
1002 Expected<std::unique_ptr<MaterializationResponsibility>>
1003 delegate(MaterializationResponsibility &FromMR, SymbolFlagsMap SymbolFlags,
1004 SymbolStringPtr InitSymbol);
1005
1006 SymbolNameSet getRequestedSymbols(const SymbolFlagsMap &SymbolFlags) const;
1007
1008 void addDependencies(const SymbolStringPtr &Name,
1009 const SymbolDependenceMap &Dependants);
1010
1012
1013 void unlinkMaterializationResponsibility(MaterializationResponsibility &MR);
1014
1015 /// Attempt to reduce memory usage from empty \c UnmaterializedInfos and
1016 /// \c MaterializingInfos tables.
1017 void shrinkMaterializationInfoMemory();
1018
1019 ExecutionSession &ES;
1020 enum { Open, Closing, Closed } State = Open;
1021 std::mutex GeneratorsMutex;
1022 SymbolTable Symbols;
1023 UnmaterializedInfosMap UnmaterializedInfos;
1024 MaterializingInfosMap MaterializingInfos;
1025 std::vector<std::shared_ptr<DefinitionGenerator>> DefGenerators;
1026 JITDylibSearchOrder LinkOrder;
1027 ResourceTrackerSP DefaultTracker;
1028
1029 // Map trackers to sets of symbols tracked.
1030 DenseMap<ResourceTracker *, SymbolNameVector> TrackerSymbols;
1031 DenseMap<ResourceTracker *, DenseSet<MaterializationResponsibility *>>
1032 TrackerMRs;
1033};
1034
1035/// Platforms set up standard symbols and mediate interactions between dynamic
1036/// initializers (e.g. C++ static constructors) and ExecutionSession state.
1037/// Note that Platforms do not automatically run initializers: clients are still
1038/// responsible for doing this.
1040public:
1041 virtual ~Platform();
1042
1043 /// This method will be called outside the session lock each time a JITDylib
1044 /// is created (unless it is created with EmptyJITDylib set) to allow the
1045 /// Platform to install any JITDylib specific standard symbols (e.g
1046 /// __dso_handle).
1047 virtual Error setupJITDylib(JITDylib &JD) = 0;
1048
1049 /// This method will be called outside the session lock each time a JITDylib
1050 /// is removed to allow the Platform to remove any JITDylib-specific data.
1052
1053 /// This method will be called under the ExecutionSession lock each time a
1054 /// MaterializationUnit is added to a JITDylib.
1056 const MaterializationUnit &MU) = 0;
1057
1058 /// This method will be called under the ExecutionSession lock when a
1059 /// ResourceTracker is removed.
1061
1062 /// A utility function for looking up initializer symbols. Performs a blocking
1063 /// lookup for the given symbols in each of the given JITDylibs.
1064 ///
1065 /// Note: This function is deprecated and will be removed in the near future.
1069
1070 /// Performs an async lookup for the given symbols in each of the given
1071 /// JITDylibs, calling the given handler once all lookups have completed.
1072 static void
1074 ExecutionSession &ES,
1076};
1077
1078/// A materialization task.
1080 : public RTTIExtends<MaterializationTask, Task> {
1081public:
1082 static char ID;
1083
1084 MaterializationTask(std::unique_ptr<MaterializationUnit> MU,
1085 std::unique_ptr<MaterializationResponsibility> MR)
1086 : MU(std::move(MU)), MR(std::move(MR)) {}
1087 ~MaterializationTask() override;
1088 void printDescription(raw_ostream &OS) override;
1089 void run() override;
1090
1091private:
1092 std::unique_ptr<MaterializationUnit> MU;
1093 std::unique_ptr<MaterializationResponsibility> MR;
1094};
1095
1096/// Lookups are usually run on the current thread, but in some cases they may
1097/// be run as tasks, e.g. if the lookup has been continued from a suspended
1098/// state.
1099class LLVM_ABI LookupTask : public RTTIExtends<LookupTask, Task> {
1100public:
1101 static char ID;
1102
1103 LookupTask(LookupState LS) : LS(std::move(LS)) {}
1104 void printDescription(raw_ostream &OS) override;
1105 void run() override;
1106
1107private:
1108 LookupState LS;
1109};
1110
1111/// An ExecutionSession represents a running JIT program.
1115 friend class JITDylib;
1116 friend class LookupState;
1118 friend class ResourceTracker;
1119
1120public:
1121 /// For reporting errors.
1123
1124 /// Function type for returning results from a call-controller handler.
1127
1128 /// A call-controller handler: handles calls from the executor made via the
1129 /// ORC runtime's call-controller mechanism.
1132
1133 /// Associates a call-controller handler with the name of the tag symbol that
1134 /// the executor will use to call it, and the SymbolLookupFlags to use when
1135 /// looking that tag up.
1136 ///
1137 /// The name is not copied (see SymbolNameSpec): the referenced string must
1138 /// outlive the registerCallControllerHandlers call that the binding is
1139 /// passed to. Ordinary string literals have static storage duration, so
1140 /// they are always safe.
1142 public:
1146 : Name(Name), Handler(std::move(Handler)), LF(LF) {}
1147
1148 SymbolNameSpec getName() const { return Name; }
1149
1150 CallControllerHandlerFn takeHandler() { return std::move(Handler); }
1151
1152 SymbolLookupFlags getLookupFlags() const { return LF; }
1153
1154 private:
1155 SymbolNameSpec Name;
1158 };
1159
1160 /// Construct an ExecutionSession with the given ExecutorProcessControl
1161 /// object.
1162 LLVM_ABI ExecutionSession(std::unique_ptr<ExecutorProcessControl> EPC);
1163
1168
1169 /// Destroy an ExecutionSession. Verifies that endSession was called prior to
1170 /// destruction.
1172
1173 /// End the session. Closes all JITDylibs and disconnects from the
1174 /// executor. Clients must call this method before destroying the session.
1176
1177 /// Get the ExecutorProcessControl object associated with this
1178 /// ExecutionSession.
1180
1181 /// Return the triple for the executor.
1182 const Triple &getTargetTriple() const { return EPC->getTargetTriple(); }
1183
1184 // Return the page size for the executor.
1185 size_t getPageSize() const { return EPC->getPageSize(); }
1186
1187 /// Get the SymbolStringPool for this instance.
1188 std::shared_ptr<SymbolStringPool> getSymbolStringPool() {
1189 return EPC->getSymbolStringPool();
1190 }
1191
1192 /// Add a symbol name to the SymbolStringPool and return a pointer to it.
1193 SymbolStringPtr intern(StringRef SymName) { return EPC->intern(SymName); }
1194
1195 /// Returns a reference to the bootstrap JITDylib.
1196 ///
1197 /// This is a bare JITDylib that is created for each ExecutionSession and
1198 /// populated with the bootstrap symbol definitions provided by the
1199 /// ExecutorProcessControl object.
1200 JITDylib &getBootstrapJITDylib() { return BootstrapJD; }
1201
1202 /// Set a WaitingOnGraph::Recorder to capture WaitingOnGraph operations.
1203 ///
1204 /// This method can be called at most once. If called, it should be called
1205 /// before any symbols are materialized.
1206 void setWaitingOnGraphOpRecorder(WaitingOnGraph::OpRecorder &R) {
1207 assert(!GOpRecorder && "WaitingOnGraph recorder already set");
1208 GOpRecorder = &R;
1209 }
1210
1211 /// Set the Platform for this ExecutionSession.
1212 void setPlatform(std::unique_ptr<Platform> P) { this->P = std::move(P); }
1213
1214 /// Get the Platform for this session.
1215 /// Will return null if no Platform has been set for this ExecutionSession.
1216 Platform *getPlatform() { return P.get(); }
1217
1218 /// Run the given lambda with the session mutex locked.
1219 template <typename Func> decltype(auto) runSessionLocked(Func &&F) {
1220 std::lock_guard<std::recursive_mutex> Lock(SessionMutex);
1221 return F();
1222 }
1223
1224 /// Register the given ResourceManager with this ExecutionSession.
1225 /// Managers will be notified of events in reverse order of registration.
1227
1228 /// Deregister the given ResourceManager with this ExecutionSession.
1229 /// Manager must have been previously registered.
1231
1232 /// Return a pointer to the "name" JITDylib.
1233 /// Ownership of JITDylib remains within Execution Session
1235
1236 /// Add a new bare JITDylib to this ExecutionSession.
1237 ///
1238 /// The JITDylib Name is required to be unique. Clients should verify that
1239 /// names are not being re-used (E.g. by calling getJITDylibByName) if names
1240 /// are based on user input.
1241 ///
1242 /// This call does not install any library code or symbols into the newly
1243 /// created JITDylib. The client is responsible for all configuration.
1244 LLVM_ABI JITDylib &createBareJITDylib(std::string Name);
1245
1246 /// Add a new JITDylib to this ExecutionSession.
1247 ///
1248 /// The JITDylib Name is required to be unique. Clients should verify that
1249 /// names are not being re-used (e.g. by calling getJITDylibByName) if names
1250 /// are based on user input.
1251 ///
1252 /// If a Platform is attached then Platform::setupJITDylib will be called to
1253 /// install standard platform symbols (e.g. standard library interposes).
1254 /// If no Platform is attached this call is equivalent to createBareJITDylib.
1256
1257 /// Removes the given JITDylibs from the ExecutionSession.
1258 ///
1259 /// This method clears all resources held for the JITDylibs, puts them in the
1260 /// closed state, and clears all references to them that are held by the
1261 /// ExecutionSession or other JITDylibs. No further code can be added to the
1262 /// removed JITDylibs, and the JITDylib objects will be freed once any
1263 /// remaining JITDylibSPs pointing to them are destroyed.
1264 ///
1265 /// This method does *not* run static destructors for code contained in the
1266 /// JITDylibs, and each JITDylib can only be removed once.
1267 ///
1268 /// JITDylibs will be removed in the order given. Teardown is usually
1269 /// independent for each JITDylib, but not always. In particular, where the
1270 /// ORC runtime is used it is expected that teardown off all JITDylibs will
1271 /// depend on it, so the JITDylib containing the ORC runtime must be removed
1272 /// last. If the client has introduced any other dependencies they should be
1273 /// accounted for in the removal order too.
1274 LLVM_ABI Error removeJITDylibs(std::vector<JITDylibSP> JDsToRemove);
1275
1276 /// Calls removeJTIDylibs on the gives JITDylib.
1278 return removeJITDylibs(std::vector<JITDylibSP>({&JD}));
1279 }
1280
1281 /// Set the error reporter function.
1283 this->ReportError = std::move(ReportError);
1284 return *this;
1285 }
1286
1287 /// Report a error for this execution session.
1288 ///
1289 /// Unhandled errors can be sent here to log them.
1290 void reportError(Error Err) { ReportError(std::move(Err)); }
1291
1292 /// Search the given JITDylibs to find the flags associated with each of the
1293 /// given symbols.
1294 LLVM_ABI void
1296 SymbolLookupSet Symbols,
1297 unique_function<void(Expected<SymbolFlagsMap>)> OnComplete);
1298
1299 /// Blocking version of lookupFlags.
1301 JITDylibSearchOrder SearchOrder,
1302 SymbolLookupSet Symbols);
1303
1304 /// Search the given JITDylibs for the given symbols.
1305 ///
1306 /// SearchOrder lists the JITDylibs to search. For each dylib, the associated
1307 /// boolean indicates whether the search should match against non-exported
1308 /// (hidden visibility) symbols in that dylib (true means match against
1309 /// non-exported symbols, false means do not match).
1310 ///
1311 /// The NotifyComplete callback will be called once all requested symbols
1312 /// reach the required state.
1313 ///
1314 /// If all symbols are found, the RegisterDependencies function will be called
1315 /// while the session lock is held. This gives clients a chance to register
1316 /// dependencies for on the queried symbols for any symbols they are
1317 /// materializing (if a MaterializationResponsibility instance is present,
1318 /// this can be implemented by calling
1319 /// MaterializationResponsibility::addDependencies). If there are no
1320 /// dependenant symbols for this query (e.g. it is being made by a top level
1321 /// client to get an address to call) then the value NoDependenciesToRegister
1322 /// can be used.
1323 LLVM_ABI void lookup(LookupKind K, const JITDylibSearchOrder &SearchOrder,
1324 SymbolLookupSet Symbols, SymbolState RequiredState,
1325 SymbolsResolvedCallback NotifyComplete,
1326 RegisterDependenciesFunction RegisterDependencies);
1327
1328 /// Blocking version of lookup above. Returns the resolved symbol map.
1329 /// If WaitUntilReady is true (the default), will not return until all
1330 /// requested symbols are ready (or an error occurs). If WaitUntilReady is
1331 /// false, will return as soon as all requested symbols are resolved,
1332 /// or an error occurs. If WaitUntilReady is false and an error occurs
1333 /// after resolution, the function will return a success value, but the
1334 /// error will be reported via reportErrors.
1336 lookup(const JITDylibSearchOrder &SearchOrder, SymbolLookupSet Symbols,
1338 SymbolState RequiredState = SymbolState::Ready,
1339 RegisterDependenciesFunction RegisterDependencies =
1341
1342 /// Convenience version of blocking lookup.
1343 /// Searches each of the JITDylibs in the search order in turn for the given
1344 /// symbol.
1346 lookup(const JITDylibSearchOrder &SearchOrder, SymbolStringPtr Symbol,
1347 SymbolState RequiredState = SymbolState::Ready);
1348
1349 /// Convenience version of blocking lookup.
1350 /// Searches each of the JITDylibs in the search order in turn for the given
1351 /// symbol. The search will not find non-exported symbols.
1353 lookup(ArrayRef<JITDylib *> SearchOrder, SymbolStringPtr Symbol,
1354 SymbolState RequiredState = SymbolState::Ready);
1355
1356 /// Convenience version of blocking lookup.
1357 /// Searches each of the JITDylibs in the search order in turn for the given
1358 /// symbol. The search will not find non-exported symbols.
1360 lookup(ArrayRef<JITDylib *> SearchOrder, StringRef Symbol,
1361 SymbolState RequiredState = SymbolState::Ready);
1362
1363 /// Materialize the given unit.
1364 void dispatchTask(std::unique_ptr<Task> T) {
1365 assert(T && "T must be non-null");
1366 DEBUG_WITH_TYPE("orc", dumpDispatchInfo(*T));
1367 EPC->getDispatcher().dispatch(std::move(T));
1368 }
1369
1370 /// Returns the bootstrap map.
1372 return EPC->getBootstrapMap();
1373 }
1374
1375 /// Look up and SPS-deserialize a bootstrap map value.
1376 template <typename T, typename SPSTagT>
1377 Error getBootstrapMapValue(StringRef Key, std::optional<T> &Val) const {
1378 return EPC->getBootstrapMapValue<T, SPSTagT>(Key, Val);
1379 }
1380
1381 /// Returns the bootstrap symbol map.
1383 return EPC->getBootstrapSymbolsMap();
1384 }
1385
1386 /// For each (ExecutorAddr&, StringRef) pair, looks up the string in the
1387 /// bootstrap symbols map and writes its address to the ExecutorAddr if
1388 /// found. If any symbol is not found then the function returns an error.
1390 ArrayRef<std::pair<ExecutorAddr &, StringRef>> Pairs) const {
1391 return EPC->getBootstrapSymbols(Pairs);
1392 }
1393
1394 /// Run a wrapper function in the executor. The given WFRHandler will be
1395 /// called on the result when it is returned.
1396 ///
1397 /// The wrapper function should be callable as:
1398 ///
1399 /// \code{.cpp}
1400 /// CWrapperFunctionBuffer fn(uint8_t *Data, uint64_t Size);
1401 /// \endcode{.cpp}
1402 void callWrapperAsync(ExecutorAddr WrapperFnAddr,
1404 ArrayRef<char> ArgBuffer) {
1405 EPC->callWrapperAsync(WrapperFnAddr, std::move(OnComplete), ArgBuffer);
1406 }
1407
1408 /// Run a wrapper function in the executor using the given Runner to dispatch
1409 /// OnComplete when the result is ready.
1410 template <typename RunPolicyT, typename FnT>
1411 void callWrapperAsync(RunPolicyT &&Runner, ExecutorAddr WrapperFnAddr,
1412 FnT &&OnComplete, ArrayRef<char> ArgBuffer) {
1413 EPC->callWrapperAsync(std::forward<RunPolicyT>(Runner), WrapperFnAddr,
1414 std::forward<FnT>(OnComplete), ArgBuffer);
1415 }
1416
1417 /// Run a wrapper function in the executor. OnComplete will be dispatched
1418 /// as a GenericNamedTask using this instance's TaskDispatch object.
1419 template <typename FnT>
1420 void callWrapperAsync(ExecutorAddr WrapperFnAddr, FnT &&OnComplete,
1421 ArrayRef<char> ArgBuffer) {
1422 EPC->callWrapperAsync(WrapperFnAddr, std::forward<FnT>(OnComplete),
1423 ArgBuffer);
1424 }
1425
1426 /// For each binding, look up its tag symbol in JD and register the binding's
1427 /// call-controller handler for the tag's address. The handler becomes
1428 /// callable from the executor via the ORC runtime's call-controller
1429 /// mechanism, using the tag's address as the handler tag.
1430 ///
1431 /// Tag names are mangled for the session's target triple, then looked up
1432 /// in JD using LookupKind::Static and JITDylibLookupFlags::MatchAllSymbols
1433 /// (hidden tags will be found), with each binding's SymbolLookupFlags. If a
1434 /// weakly referenced tag is not found then its handler is dropped.
1435 ///
1436 /// On failure no handlers are registered.
1438 JITDylib &JD, std::vector<CallControllerHandlerBinding> Hs);
1439
1440 /// Convenience overload of registerCallControllerHandlers that takes the
1441 /// bindings as arguments, so that they can be constructed inline at the
1442 /// call site.
1443 template <typename... BindingTs>
1444 std::enable_if_t<
1445 (std::is_same_v<BindingTs, CallControllerHandlerBinding> && ...), Error>
1447 std::vector<CallControllerHandlerBinding> Bs;
1448 Bs.reserve(sizeof...(Hs));
1449 (Bs.push_back(std::move(Hs)), ...);
1450 return registerCallControllerHandlers(JD, std::move(Bs));
1451 }
1452
1453 /// Run the call-controller handler registered for the given tag address.
1454 /// This should be called by the ExecutorProcessControl instance in response
1455 /// to calls from the executor made via the ORC runtime's call-controller
1456 /// mechanism.
1457 LLVM_ABI void
1459 ExecutorAddr HandlerFnTagAddr,
1461
1462 /// Dump the state of all the JITDylibs in this session.
1463 LLVM_ABI void dump(raw_ostream &OS);
1464
1465 /// Check the internal consistency of ExecutionSession data structures.
1466#ifdef EXPENSIVE_CHECKS
1467 bool verifySessionState(Twine Phase);
1468#endif
1469
1470private:
1471 static void logErrorsToStdErr(Error Err) {
1472 logAllUnhandledErrors(std::move(Err), errs(), "JIT session error: ");
1473 }
1474
1475 void dispatchOutstandingMUs();
1476
1477 static std::unique_ptr<MaterializationResponsibility>
1478 createMaterializationResponsibility(ResourceTracker &RT,
1479 SymbolFlagsMap Symbols,
1480 SymbolStringPtr InitSymbol) {
1481 auto &JD = RT.getJITDylib();
1482 std::unique_ptr<MaterializationResponsibility> MR(
1483 new MaterializationResponsibility(&RT, std::move(Symbols),
1484 std::move(InitSymbol)));
1485 JD.TrackerMRs[&RT].insert(MR.get());
1486 return MR;
1487 }
1488
1489 Error removeResourceTracker(ResourceTracker &RT);
1490 void transferResourceTracker(ResourceTracker &DstRT, ResourceTracker &SrcRT);
1491 void destroyResourceTracker(ResourceTracker &RT);
1492
1493 // State machine functions for query application..
1494
1495 /// IL_updateCandidatesFor is called to remove already-defined symbols that
1496 /// match a given query from the set of candidate symbols to generate
1497 /// definitions for (no need to generate a definition if one already exists).
1498 Error IL_updateCandidatesFor(JITDylib &JD, JITDylibLookupFlags JDLookupFlags,
1499 SymbolLookupSet &Candidates,
1500 SymbolLookupSet *NonCandidates);
1501
1502 /// Handle resumption of a lookup after entering a generator.
1503 void OL_resumeLookupAfterGeneration(InProgressLookupState &IPLS);
1504
1505 /// OL_applyQueryPhase1 is an optionally re-startable loop for triggering
1506 /// definition generation. It is called when a lookup is performed, and again
1507 /// each time that LookupState::continueLookup is called.
1508 void OL_applyQueryPhase1(std::unique_ptr<InProgressLookupState> IPLS,
1509 Error Err);
1510
1511 /// OL_completeLookup is run once phase 1 successfully completes for a lookup
1512 /// call. It attempts to attach the symbol to all symbol table entries and
1513 /// collect all MaterializationUnits to dispatch. If this method fails then
1514 /// all MaterializationUnits will be left un-materialized.
1515 void OL_completeLookup(std::unique_ptr<InProgressLookupState> IPLS,
1516 std::shared_ptr<AsynchronousSymbolQuery> Q,
1517 RegisterDependenciesFunction RegisterDependencies);
1518
1519 /// OL_completeLookupFlags is run once phase 1 successfully completes for a
1520 /// lookupFlags call.
1521 void OL_completeLookupFlags(
1522 std::unique_ptr<InProgressLookupState> IPLS,
1523 unique_function<void(Expected<SymbolFlagsMap>)> OnComplete);
1524
1525 // State machine functions for MaterializationResponsibility.
1526 LLVM_ABI void
1527 OL_destroyMaterializationResponsibility(MaterializationResponsibility &MR);
1529 OL_getRequestedSymbols(const MaterializationResponsibility &MR);
1530 LLVM_ABI Error OL_notifyResolved(MaterializationResponsibility &MR,
1531 const SymbolMap &Symbols);
1532
1533 // FIXME: We should be able to derive FailedSymsForQuery from each query once
1534 // we fix how the detach operation works.
1535 struct EmitQueries {
1536 JITDylib::AsynchronousSymbolQuerySet Completed;
1537 JITDylib::AsynchronousSymbolQuerySet Failed;
1538 DenseMap<AsynchronousSymbolQuery *, std::shared_ptr<SymbolDependenceMap>>
1539 FailedSymsForQuery;
1540 };
1541
1543 IL_getSymbolState(JITDylib *JD, NonOwningSymbolStringPtr Name);
1544
1545 template <typename UpdateSymbolFn, typename UpdateQueryFn>
1546 void IL_collectQueries(JITDylib::AsynchronousSymbolQuerySet &Qs,
1547 WaitingOnGraph::ContainerElementsMap &QualifiedSymbols,
1548 UpdateSymbolFn &&UpdateSymbol,
1549 UpdateQueryFn &&UpdateQuery);
1550
1551 Expected<EmitQueries> IL_emit(MaterializationResponsibility &MR,
1552 WaitingOnGraph::SimplifyResult SR);
1553 LLVM_ABI Error OL_notifyEmitted(MaterializationResponsibility &MR,
1555
1556 LLVM_ABI Error OL_defineMaterializing(MaterializationResponsibility &MR,
1557 SymbolFlagsMap SymbolFlags);
1558
1559 std::pair<JITDylib::AsynchronousSymbolQuerySet,
1560 std::shared_ptr<SymbolDependenceMap>>
1561 IL_failSymbols(JITDylib &JD, const SymbolNameVector &SymbolsToFail);
1562 LLVM_ABI void OL_notifyFailed(MaterializationResponsibility &MR);
1564 std::unique_ptr<MaterializationUnit> MU);
1565 LLVM_ABI Expected<std::unique_ptr<MaterializationResponsibility>>
1566 OL_delegate(MaterializationResponsibility &MR, const SymbolNameSet &Symbols);
1567
1568#ifndef NDEBUG
1569 void dumpDispatchInfo(Task &T);
1570#endif // NDEBUG
1571
1572 mutable std::recursive_mutex SessionMutex;
1573 bool SessionOpen = true;
1574 std::unique_ptr<ExecutorProcessControl> EPC;
1575 std::unique_ptr<Platform> P;
1576 ErrorReporter ReportError = logErrorsToStdErr;
1577
1578 std::vector<ResourceManager *> ResourceManagers;
1579
1580 std::vector<JITDylibSP> JDs;
1581 JITDylib &BootstrapJD;
1583 WaitingOnGraph::OpRecorder *GOpRecorder = nullptr;
1584
1585 // FIXME: Remove this (and runOutstandingMUs) once the linking layer works
1586 // with callbacks from asynchronous queries.
1587 mutable std::recursive_mutex OutstandingMUsMutex;
1588 std::vector<std::pair<std::unique_ptr<MaterializationUnit>,
1589 std::unique_ptr<MaterializationResponsibility>>>
1590 OutstandingMUs;
1591
1592 mutable std::mutex CallControllerHandlersMutex;
1593 DenseMap<ExecutorAddr, std::unique_ptr<CallControllerHandlerFn>>
1594 CallControllerHandlers;
1595};
1596
1597template <typename Func> Error ResourceTracker::withResourceKeyDo(Func &&F) {
1599 if (isDefunct())
1601 F(getKeyUnsafe());
1602 return Error::success();
1603 });
1604}
1605
1606inline ExecutionSession &
1608 return JD.getExecutionSession();
1609}
1610
1611template <typename GeneratorT>
1612GeneratorT &JITDylib::addGenerator(std::unique_ptr<GeneratorT> DefGenerator) {
1613 auto &G = *DefGenerator;
1614 ES.runSessionLocked([&] {
1615 assert(State == Open && "Cannot add generator to closed JITDylib");
1616 DefGenerators.push_back(std::move(DefGenerator));
1617 });
1618 return G;
1619}
1620
1621template <typename Func>
1623 -> decltype(F(std::declval<const JITDylibSearchOrder &>())) {
1624 assert(State == Open && "Cannot use link order of closed JITDylib");
1625 return ES.runSessionLocked([&]() { return F(LinkOrder); });
1626}
1627
1628template <typename MaterializationUnitType>
1629Error JITDylib::define(std::unique_ptr<MaterializationUnitType> &&MU,
1630 ResourceTrackerSP RT) {
1631 assert(MU && "Can not define with a null MU");
1632
1633 if (MU->getSymbols().empty()) {
1634 // Empty MUs are allowable but pathological, so issue a warning.
1635 DEBUG_WITH_TYPE("orc", {
1636 dbgs() << "Warning: Discarding empty MU " << MU->getName() << " for "
1637 << getName() << "\n";
1638 });
1639 return Error::success();
1640 } else
1641 DEBUG_WITH_TYPE("orc", {
1642 dbgs() << "Defining MU " << MU->getName() << " for " << getName()
1643 << " (tracker: ";
1644 if (RT == getDefaultResourceTracker())
1645 dbgs() << "default)";
1646 else if (RT)
1647 dbgs() << RT.get() << ")\n";
1648 else
1649 dbgs() << "0x0, default will be used)\n";
1650 });
1651
1652 return ES.runSessionLocked([&, this]() -> Error {
1653 if (State != Open)
1654 return make_error<JITDylibDefunct>(this);
1655
1656 if (auto Err = defineImpl(*MU))
1657 return Err;
1658
1659 if (!RT)
1661
1662 if (auto *P = ES.getPlatform()) {
1663 if (auto Err = P->notifyAdding(*RT, *MU))
1664 return Err;
1665 }
1666
1667 installMaterializationUnit(std::move(MU), *RT);
1668 return Error::success();
1669 });
1670}
1671
1672template <typename MaterializationUnitType>
1673Error JITDylib::define(std::unique_ptr<MaterializationUnitType> &MU,
1674 ResourceTrackerSP RT) {
1675 assert(MU && "Can not define with a null MU");
1676
1677 if (MU->getSymbols().empty()) {
1678 // Empty MUs are allowable but pathological, so issue a warning.
1679 DEBUG_WITH_TYPE("orc", {
1680 dbgs() << "Warning: Discarding empty MU " << MU->getName() << getName()
1681 << "\n";
1682 });
1683 return Error::success();
1684 } else
1685 DEBUG_WITH_TYPE("orc", {
1686 dbgs() << "Defining MU " << MU->getName() << " for " << getName()
1687 << " (tracker: ";
1688 if (RT == getDefaultResourceTracker())
1689 dbgs() << "default)";
1690 else if (RT)
1691 dbgs() << RT.get() << ")\n";
1692 else
1693 dbgs() << "0x0, default will be used)\n";
1694 });
1695
1696 return ES.runSessionLocked([&, this]() -> Error {
1697 assert(State == Open && "JD is defunct");
1698
1699 if (auto Err = defineImpl(*MU))
1700 return Err;
1701
1702 if (!RT)
1704
1705 if (auto *P = ES.getPlatform()) {
1706 if (auto Err = P->notifyAdding(*RT, *MU))
1707 return Err;
1708 }
1709
1710 installMaterializationUnit(std::move(MU), *RT);
1711 return Error::success();
1712 });
1713}
1714
1715/// ReexportsGenerator can be used with JITDylib::addGenerator to automatically
1716/// re-export a subset of the source JITDylib's symbols in the target.
1718public:
1719 using SymbolPredicate = std::function<bool(SymbolStringPtr)>;
1720
1721 /// Create a reexports generator. If an Allow predicate is passed, only
1722 /// symbols for which the predicate returns true will be reexported. If no
1723 /// Allow predicate is passed, all symbols will be exported.
1724 ReexportsGenerator(JITDylib &SourceJD,
1725 JITDylibLookupFlags SourceJDLookupFlags,
1727
1729 JITDylibLookupFlags JDLookupFlags,
1730 const SymbolLookupSet &LookupSet) override;
1731
1732private:
1733 JITDylib &SourceJD;
1734 JITDylibLookupFlags SourceJDLookupFlags;
1735 SymbolPredicate Allow;
1736};
1737
1738// --------------- IMPLEMENTATION --------------
1739// Implementations for inline functions/methods.
1740// ---------------------------------------------
1741
1743 getExecutionSession().OL_destroyMaterializationResponsibility(*this);
1744}
1745
1747 return getExecutionSession().OL_getRequestedSymbols(*this);
1748}
1749
1751 const SymbolMap &Symbols) {
1752 return getExecutionSession().OL_notifyResolved(*this, Symbols);
1753}
1754
1756 ArrayRef<SymbolDependenceGroup> EmittedDeps) {
1757 return getExecutionSession().OL_notifyEmitted(*this, EmittedDeps);
1758}
1759
1761 SymbolFlagsMap SymbolFlags) {
1762 return getExecutionSession().OL_defineMaterializing(*this,
1763 std::move(SymbolFlags));
1764}
1765
1767 getExecutionSession().OL_notifyFailed(*this);
1768}
1769
1771 std::unique_ptr<MaterializationUnit> MU) {
1772 return getExecutionSession().OL_replace(*this, std::move(MU));
1773}
1774
1777 return getExecutionSession().OL_delegate(*this, Symbols);
1778}
1779
1780} // End namespace orc
1781} // End namespace llvm
1782
1783#endif // LLVM_EXECUTIONENGINE_ORC_CORE_H
aarch64 falkor hwpf fix Falkor HW Prefetch Fix Late Phase
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Function Alias Analysis false
#define LLVM_ABI
Definition Compiler.h:215
This file defines the DenseSet and SmallDenseSet classes.
This file provides a collection of function (or more generally, callable) type erasure utilities supp...
This file defines the RefCountedBase, ThreadSafeRefCountedBase, and IntrusiveRefCntPtr classes.
#define F(x, y, z)
Definition MD5.cpp:54
#define G(x, y, z)
Definition MD5.cpp:55
#define T
#define P(N)
static StringRef getName(Value *V)
#define DEBUG_WITH_TYPE(TYPE,...)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Definition Debug.h:72
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
bool empty() const
Definition DenseMap.h:732
Base class for user error types.
Definition Error.h:354
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...
Flags for symbols in the JIT.
Definition JITSymbol.h:75
Inheritance utility for extensible RTTI.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:129
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
A symbol query that returns results via a callback when results are ready.
Definition Core.h:559
LLVM_ABI AsynchronousSymbolQuery(const SymbolLookupSet &Symbols, SymbolState RequiredState, SymbolsResolvedCallback NotifyComplete)
Create a query for the given symbols.
Definition Core.cpp:192
bool isComplete() const
Returns true if all symbols covered by this query have been resolved.
Definition Core.h:580
friend class InProgressFullLookupState
Definition Core.h:561
LLVM_ABI void notifySymbolMetRequiredState(const SymbolStringPtr &Name, ExecutorSymbolDef Sym)
Notify the query that a requested symbol has reached the required state.
Definition Core.cpp:206
friend class JITSymbolResolverAdapter
Definition Core.h:563
friend class MaterializationResponsibility
Definition Core.h:564
Definition generators can be attached to JITDylibs to generate new definitions for otherwise unresolv...
Definition Core.h:633
virtual Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD, JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &LookupSet)=0
DefinitionGenerators should override this method to insert new definitions into the parent JITDylib.
friend class ExecutionSession
Definition Core.h:634
CallControllerHandlerBinding(SymbolNameSpec Name, CallControllerHandlerFn Handler, SymbolLookupFlags LF=SymbolLookupFlags::RequiredSymbol)
Definition Core.h:1143
An ExecutionSession represents a running JIT program.
Definition Core.h:1112
LLVM_ABI Error endSession()
End the session.
Definition Core.cpp:1572
ExecutorProcessControl & getExecutorProcessControl()
Get the ExecutorProcessControl object associated with this ExecutionSession.
Definition Core.h:1179
unique_function< void(shared::WrapperFunctionBuffer)> CallControllerReturnFn
Function type for returning results from a call-controller handler.
Definition Core.h:1125
void reportError(Error Err)
Report a error for this execution session.
Definition Core.h:1290
void callWrapperAsync(ExecutorAddr WrapperFnAddr, ExecutorProcessControl::IncomingWFRHandler OnComplete, ArrayRef< char > ArgBuffer)
Run a wrapper function in the executor.
Definition Core.h:1402
friend class JITDylib
Definition Core.h:1115
friend class InProgressLookupFlagsState
Definition Core.h:1113
ExecutionSession & operator=(const ExecutionSession &)=delete
const StringMap< ExecutorAddr > & getBootstrapSymbolsMap() const
Returns the bootstrap symbol map.
Definition Core.h:1382
void setPlatform(std::unique_ptr< Platform > P)
Set the Platform for this ExecutionSession.
Definition Core.h:1212
std::enable_if_t<(std::is_same_v< BindingTs, CallControllerHandlerBinding > &&...), Error > registerCallControllerHandlers(JITDylib &JD, BindingTs &&...Hs)
Convenience overload of registerCallControllerHandlers that takes the bindings as arguments,...
Definition Core.h:1446
const Triple & getTargetTriple() const
Return the triple for the executor.
Definition Core.h:1182
ExecutionSession(const ExecutionSession &)=delete
Platform * getPlatform()
Get the Platform for this session.
Definition Core.h:1216
LLVM_ABI void lookupFlags(LookupKind K, JITDylibSearchOrder SearchOrder, SymbolLookupSet Symbols, unique_function< void(Expected< SymbolFlagsMap >)> OnComplete)
Search the given JITDylibs to find the flags associated with each of the given symbols.
Definition Core.cpp:1738
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
Definition Core.h:1193
LLVM_ABI JITDylib * getJITDylibByName(StringRef Name)
Return a pointer to the "name" JITDylib.
Definition Core.cpp:1617
LLVM_ABI void runCallControllerHandler(CallControllerReturnFn Return, ExecutorAddr HandlerFnTagAddr, shared::WrapperFunctionBuffer ArgBytes)
Run the call-controller handler registered for the given tag address.
Definition Core.cpp:1913
friend class LookupState
Definition Core.h:1116
void callWrapperAsync(RunPolicyT &&Runner, ExecutorAddr WrapperFnAddr, FnT &&OnComplete, ArrayRef< char > ArgBuffer)
Run a wrapper function in the executor using the given Runner to dispatch OnComplete when the result ...
Definition Core.h:1411
LLVM_ABI JITDylib & createBareJITDylib(std::string Name)
Add a new bare JITDylib to this ExecutionSession.
Definition Core.cpp:1626
LLVM_ABI Error registerCallControllerHandlers(JITDylib &JD, std::vector< CallControllerHandlerBinding > Hs)
For each binding, look up its tag symbol in JD and register the binding's call-controller handler for...
Definition Core.cpp:1859
JITDylib & getBootstrapJITDylib()
Returns a reference to the bootstrap JITDylib.
Definition Core.h:1200
std::shared_ptr< SymbolStringPool > getSymbolStringPool()
Get the SymbolStringPool for this instance.
Definition Core.h:1188
Error getBootstrapMapValue(StringRef Key, std::optional< T > &Val) const
Look up and SPS-deserialize a bootstrap map value.
Definition Core.h:1377
friend class InProgressFullLookupState
Definition Core.h:1114
Error getBootstrapSymbols(ArrayRef< std::pair< ExecutorAddr &, StringRef > > Pairs) const
For each (ExecutorAddr&, StringRef) pair, looks up the string in the bootstrap symbols map and writes...
Definition Core.h:1389
const StringMap< std::vector< char > > & getBootstrapMap() const
Returns the bootstrap map.
Definition Core.h:1371
LLVM_ABI void lookup(LookupKind K, const JITDylibSearchOrder &SearchOrder, SymbolLookupSet Symbols, SymbolState RequiredState, SymbolsResolvedCallback NotifyComplete, RegisterDependenciesFunction RegisterDependencies)
Search the given JITDylibs for the given symbols.
Definition Core.cpp:1764
friend class MaterializationResponsibility
Definition Core.h:1117
LLVM_ABI void registerResourceManager(ResourceManager &RM)
Register the given ResourceManager with this ExecutionSession.
Definition Core.cpp:1600
LLVM_ABI ~ExecutionSession()
Destroy an ExecutionSession.
Definition Core.cpp:1566
void setWaitingOnGraphOpRecorder(WaitingOnGraph::OpRecorder &R)
Set a WaitingOnGraph::Recorder to capture WaitingOnGraph operations.
Definition Core.h:1206
LLVM_ABI void deregisterResourceManager(ResourceManager &RM)
Deregister the given ResourceManager with this ExecutionSession.
Definition Core.cpp:1604
LLVM_ABI ExecutionSession(std::unique_ptr< ExecutorProcessControl > EPC)
Construct an ExecutionSession with the given ExecutorProcessControl object.
Definition Core.cpp:1553
decltype(auto) runSessionLocked(Func &&F)
Run the given lambda with the session mutex locked.
Definition Core.h:1219
LLVM_ABI void dump(raw_ostream &OS)
Dump the state of all the JITDylibs in this session.
Definition Core.cpp:1936
friend class ResourceTracker
Definition Core.h:1118
ExecutionSession & setErrorReporter(ErrorReporter ReportError)
Set the error reporter function.
Definition Core.h:1282
ExecutionSession(ExecutionSession &&)=delete
LLVM_ABI Error removeJITDylibs(std::vector< JITDylibSP > JDsToRemove)
Removes the given JITDylibs from the ExecutionSession.
Definition Core.cpp:1643
size_t getPageSize() const
Definition Core.h:1185
LLVM_ABI Expected< JITDylib & > createJITDylib(std::string Name)
Add a new JITDylib to this ExecutionSession.
Definition Core.cpp:1635
void dispatchTask(std::unique_ptr< Task > T)
Materialize the given unit.
Definition Core.h:1364
unique_function< void(Error)> ErrorReporter
For reporting errors.
Definition Core.h:1122
ExecutionSession & operator=(ExecutionSession &&)=delete
Error removeJITDylib(JITDylib &JD)
Calls removeJTIDylibs on the gives JITDylib.
Definition Core.h:1277
void callWrapperAsync(ExecutorAddr WrapperFnAddr, FnT &&OnComplete, ArrayRef< char > ArgBuffer)
Run a wrapper function in the executor.
Definition Core.h:1420
unique_function< void( CallControllerReturnFn, shared::WrapperFunctionBuffer)> CallControllerHandlerFn
A call-controller handler: handles calls from the executor made via the ORC runtime's call-controller...
Definition Core.h:1130
Represents an address in the executor process.
A handler or incoming WrapperFunctionBuffers – either return values from callWrapper* calls,...
ExecutorProcessControl supports interaction with a JIT target process.
Represents a defining location for a JIT symbol.
FailedToMaterialize(std::shared_ptr< SymbolStringPool > SSP, std::shared_ptr< SymbolDependenceMap > Symbols)
Definition Core.cpp:94
const SymbolDependenceMap & getSymbols() const
Definition Core.h:224
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition Core.cpp:112
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition Core.cpp:116
JITDylibDefunct(JITDylibSP JD)
Definition Core.h:205
Represents a JIT'd dynamic library.
Definition Core.h:676
LLVM_ABI ~JITDylib()
Definition Core.cpp:643
LLVM_ABI Error remove(const SymbolNameSet &Names)
Tries to remove the given symbols.
Definition Core.cpp:1045
LLVM_ABI Error clear()
Calls remove on all trackers currently associated with this JITDylib.
Definition Core.cpp:647
JITDylib & operator=(JITDylib &&)=delete
LLVM_ABI void dump(raw_ostream &OS)
Dump current JITDylib state to OS.
Definition Core.cpp:1101
friend class AsynchronousSymbolQuery
Definition Core.h:677
LLVM_ABI void replaceInLinkOrder(JITDylib &OldJD, JITDylib &NewJD, JITDylibLookupFlags JDLookupFlags=JITDylibLookupFlags::MatchExportedSymbolsOnly)
Replace OldJD with NewJD in the link order if OldJD is present.
Definition Core.cpp:1021
Error define(std::unique_ptr< MaterializationUnitType > &&MU, ResourceTrackerSP RT=nullptr)
Define all symbols provided by the materialization unit to be part of this JITDylib.
Definition Core.h:1629
ExecutionSession & getExecutionSession() const
Get a reference to the ExecutionSession for this JITDylib.
Definition Core.h:695
LLVM_ABI void addToLinkOrder(const JITDylibSearchOrder &NewLinks)
Append the given JITDylibSearchOrder to the link order for this JITDylib (discarding any elements alr...
Definition Core.cpp:1005
LLVM_ABI ResourceTrackerSP createResourceTracker()
Create a resource tracker for this JITDylib.
Definition Core.cpp:671
auto withLinkOrderDo(Func &&F) -> decltype(F(std::declval< const JITDylibSearchOrder & >()))
Do something with the link order (run under the session lock).
Definition Core.h:1622
friend class MaterializationResponsibility
Definition Core.h:680
friend class Platform
Definition Core.h:679
LLVM_ABI void removeFromLinkOrder(JITDylib &JD)
Remove the given JITDylib from the link order for this JITDylib if it is present.
Definition Core.cpp:1033
LLVM_ABI void setLinkOrder(JITDylibSearchOrder NewSearchOrder, bool LinkAgainstThisJITDylibFirst=true)
Set the link order to be used when fixing up definitions in JITDylib.
Definition Core.cpp:990
LLVM_ABI Expected< std::vector< JITDylibSP > > getReverseDFSLinkOrder()
Rteurn this JITDylib and its transitive dependencies in reverse DFS order based on linkage relationsh...
Definition Core.cpp:1734
friend class ExecutionSession
Definition Core.h:678
LLVM_ABI ResourceTrackerSP getDefaultResourceTracker()
Get the default resource tracker for this JITDylib.
Definition Core.cpp:662
GeneratorT & addGenerator(std::unique_ptr< GeneratorT > DefGenerator)
Adds a definition generator to this JITDylib and returns a referenece to it.
Definition Core.h:1612
JITDylib(const JITDylib &)=delete
JITDylib & operator=(const JITDylib &)=delete
JITDylib(JITDylib &&)=delete
LLVM_ABI void removeGenerator(DefinitionGenerator &G)
Remove a definition generator from this JITDylib.
Definition Core.cpp:679
LLVM_ABI Expected< std::vector< JITDylibSP > > getDFSLinkOrder()
Return this JITDylib and its transitive dependencies in DFS order based on linkage relationships.
Definition Core.cpp:1730
Wraps state for a lookup-in-progress.
Definition Core.h:608
LLVM_ABI void continueLookup(Error Err)
Continue the lookup.
Definition Core.cpp:623
LLVM_ABI LookupState & operator=(LookupState &&)
friend class ExecutionSession
Definition Core.h:610
friend class OrcV2CAPIHelper
Definition Core.h:609
LLVM_ABI LookupState(LookupState &&)
LookupTask(LookupState LS)
Definition Core.h:1103
static char ID
Definition Core.h:1101
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition Core.h:350
MaterializationResponsibility & operator=(MaterializationResponsibility &&)=delete
ExecutionSession & getExecutionSession() const
Returns the ExecutionSession for this instance.
Definition Core.h:1607
Error notifyResolved(const SymbolMap &Symbols)
Notifies the target JITDylib that the given symbols have been resolved.
Definition Core.h:1750
~MaterializationResponsibility()
Destruct a MaterializationResponsibility instance.
Definition Core.h:1742
Error replace(std::unique_ptr< MaterializationUnit > MU)
Transfers responsibility to the given MaterializationUnit for all symbols defined by that Materializa...
Definition Core.h:1770
Error withResourceKeyDo(Func &&F) const
Runs the given callback under the session lock, passing in the associated ResourceKey.
Definition Core.h:369
Error defineMaterializing(SymbolFlagsMap SymbolFlags)
Attempt to claim responsibility for new definitions.
Definition Core.h:1760
SymbolNameSet getRequestedSymbols() const
Returns the names of any symbols covered by this MaterializationResponsibility object that have queri...
Definition Core.h:1746
Expected< std::unique_ptr< MaterializationResponsibility > > delegate(const SymbolNameSet &Symbols)
Delegates responsibility for the given symbols to the returned materialization responsibility.
Definition Core.h:1776
const ResourceTrackerSP & getResourceTracker() const
Return the ResourceTracker associated with this instance.
Definition Core.h:365
const SymbolStringPtr & getInitializerSymbol() const
Returns the initialization pseudo-symbol, if any.
Definition Core.h:389
MaterializationResponsibility(MaterializationResponsibility &&)=delete
Error notifyEmitted(ArrayRef< SymbolDependenceGroup > DepGroups)
Notifies the target JITDylib (and any pending queries on that JITDylib) that all symbols covered by t...
Definition Core.h:1755
void failMaterialization()
Notify all not-yet-emitted covered by this MaterializationResponsibility instance that an error has o...
Definition Core.h:1766
JITDylib & getTargetJITDylib() const
Returns the target JITDylib that these symbols are being materialized into.
Definition Core.h:375
const SymbolFlagsMap & getSymbols() const
Returns the symbol flags map for this responsibility instance.
Definition Core.h:384
A materialization task.
Definition Core.h:1080
MaterializationTask(std::unique_ptr< MaterializationUnit > MU, std::unique_ptr< MaterializationResponsibility > MR)
Definition Core.h:1084
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group,...
const SymbolNameVector & getSymbols() const
Definition Core.h:305
std::shared_ptr< SymbolStringPool > getSymbolStringPool()
Definition Core.h:303
MissingSymbolDefinitions(std::shared_ptr< SymbolStringPool > SSP, std::string ModuleName, SymbolNameVector Symbols)
Definition Core.h:297
const std::string & getModuleName() const
Definition Core.h:304
Platforms set up standard symbols and mediate interactions between dynamic initializers (e....
Definition Core.h:1039
virtual Error teardownJITDylib(JITDylib &JD)=0
This method will be called outside the session lock each time a JITDylib is removed to allow the Plat...
static void lookupInitSymbolsAsync(unique_function< void(Error)> OnComplete, ExecutionSession &ES, const DenseMap< JITDylib *, SymbolLookupSet > &InitSyms)
Performs an async lookup for the given symbols in each of the given JITDylibs, calling the given hand...
Definition Core.cpp:1489
virtual Error notifyRemoving(ResourceTracker &RT)=0
This method will be called under the ExecutionSession lock when a ResourceTracker is removed.
static Expected< DenseMap< JITDylib *, SymbolMap > > lookupInitSymbols(ExecutionSession &ES, const DenseMap< JITDylib *, SymbolLookupSet > &InitSyms)
A utility function for looking up initializer symbols.
Definition Core.cpp:1441
virtual Error notifyAdding(ResourceTracker &RT, const MaterializationUnit &MU)=0
This method will be called under the ExecutionSession lock each time a MaterializationUnit is added t...
virtual Error setupJITDylib(JITDylib &JD)=0
This method will be called outside the session lock each time a JITDylib is created (unless it is cre...
ReExportsMaterializationUnit(JITDylib *SourceJD, JITDylibLookupFlags SourceJDLookupFlags, SymbolAliasMap Aliases)
SourceJD is allowed to be nullptr, in which case the source JITDylib is taken to be whatever JITDylib...
Definition Core.cpp:291
std::function< bool(SymbolStringPtr)> SymbolPredicate
Definition Core.h:1719
Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD, JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &LookupSet) override
DefinitionGenerators should override this method to insert new definitions into the parent JITDylib.
Definition Core.cpp:588
ReexportsGenerator(JITDylib &SourceJD, JITDylibLookupFlags SourceJDLookupFlags, SymbolPredicate Allow=SymbolPredicate())
Create a reexports generator.
Definition Core.cpp:582
Listens for ResourceTracker operations.
Definition Core.h:112
virtual Error handleRemoveResources(JITDylib &JD, ResourceKey K)=0
This function will be called outside the session lock.
virtual void handleTransferResources(JITDylib &JD, ResourceKey DstK, ResourceKey SrcK)=0
This function will be called inside the session lock.
ResourceTrackerDefunct(ResourceTrackerSP RT)
Definition Core.cpp:74
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition Core.cpp:81
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition Core.cpp:77
API to remove / transfer ownership of JIT resources.
Definition Core.h:64
JITDylib & getJITDylib() const
Return the JITDylib targeted by this tracker.
Definition Core.h:79
friend class JITDylib
Definition Core.h:67
ResourceTracker & operator=(const ResourceTracker &)=delete
ResourceKey getKeyUnsafe() const
Returns the key associated with this tracker.
Definition Core.h:101
LLVM_ABI void transferTo(ResourceTracker &DstRT)
Transfer all resources associated with this key to the given tracker, which must target the same JITD...
Definition Core.cpp:62
ResourceTracker & operator=(ResourceTracker &&)=delete
LLVM_ABI ~ResourceTracker()
Definition Core.cpp:53
ResourceTracker(const ResourceTracker &)=delete
friend class MaterializationResponsibility
Definition Core.h:68
bool isDefunct() const
Return true if this tracker has become defunct.
Definition Core.h:96
ResourceTracker(ResourceTracker &&)=delete
Error withResourceKeyDo(Func &&F)
Runs the given callback under the session lock, passing in the associated ResourceKey.
Definition Core.h:1597
friend class ExecutionSession
Definition Core.h:66
LLVM_ABI Error remove()
Remove all resources associated with this key.
Definition Core.cpp:58
A set of symbols to look up, each associated with a SymbolLookupFlags value.
A symbol name together with the naming level (SymbolNameKind) it is expressed in, so that a Mangler /...
Pointer to a pooled string representing a symbol name.
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition Core.cpp:166
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition Core.cpp:170
const SymbolNameSet & getSymbols() const
Definition Core.h:281
SymbolsCouldNotBeRemoved(std::shared_ptr< SymbolStringPool > SSP, SymbolNameSet Symbols)
Definition Core.cpp:160
std::shared_ptr< SymbolStringPool > getSymbolStringPool()
Definition Core.h:280
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition Core.cpp:156
SymbolsNotFound(std::shared_ptr< SymbolStringPool > SSP, SymbolNameSet Symbols)
Definition Core.cpp:139
const SymbolNameVector & getSymbols() const
Definition Core.h:263
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition Core.cpp:152
std::shared_ptr< SymbolStringPool > getSymbolStringPool()
Definition Core.h:262
UnexpectedSymbolDefinitions(std::shared_ptr< SymbolStringPool > SSP, std::string ModuleName, SymbolNameVector Symbols)
Definition Core.h:321
std::shared_ptr< SymbolStringPool > getSymbolStringPool()
Definition Core.h:327
const std::string & getModuleName() const
Definition Core.h:328
const SymbolNameVector & getSymbols() const
Definition Core.h:329
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition Core.cpp:132
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition Core.cpp:128
UnsatisfiedSymbolDependencies(std::shared_ptr< SymbolStringPool > SSP, JITDylibSP JD, SymbolNameSet FailedSymbols, SymbolDependenceMap BadDeps, std::string Explanation)
Definition Core.cpp:120
WaitingOnGraph class template.
C++ wrapper function buffer: Same as CWrapperFunctionBuffer but auto-releases memory.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
unique_function is a type-erasing functor similar to std::function.
JITDylibSearchOrder makeJITDylibSearchOrder(ArrayRef< JITDylib * > JDs, JITDylibLookupFlags Flags=JITDylibLookupFlags::MatchExportedSymbolsOnly)
Convenience function for creating a search order from an ArrayRef of JITDylib*, all with the same fla...
Definition Core.h:154
std::vector< std::pair< JITDylib *, JITDylibLookupFlags > > JITDylibSearchOrder
A list of (JITDylib*, JITDylibLookupFlags) pairs to be used as a search order during symbol lookup.
Definition Core.h:149
IntrusiveRefCntPtr< JITDylib > JITDylibSP
Definition Core.h:59
IntrusiveRefCntPtr< ResourceTracker > ResourceTrackerSP
Definition Core.h:58
std::unique_ptr< ReExportsMaterializationUnit > symbolAliases(SymbolAliasMap Aliases)
Create a ReExportsMaterializationUnit with the given aliases.
Definition Core.h:524
std::function< void(const SymbolDependenceMap &)> RegisterDependenciesFunction
Callback to register the dependencies for a given query.
Definition Core.h:180
uintptr_t ResourceKey
Definition Core.h:61
SymbolLookupFlags
Lookup flags that apply to each symbol in a lookup.
std::unique_ptr< ReExportsMaterializationUnit > reexports(JITDylib &SourceJD, SymbolAliasMap Aliases, JITDylibLookupFlags SourceJDLookupFlags=JITDylibLookupFlags::MatchExportedSymbolsOnly)
Create a materialization unit for re-exporting symbols from another JITDylib with alternative names/f...
Definition Core.h:533
JITDylibLookupFlags
Lookup flags that apply to each dylib in the search order for a lookup.
Definition Core.h:133
detail::WaitingOnGraph< JITDylib *, NonOwningSymbolStringPtr > WaitingOnGraph
Definition Core.h:55
DenseMap< SymbolStringPtr, ExecutorSymbolDef > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
LookupKind
Describes the kind of lookup being performed.
Definition Core.h:145
LLVM_ABI RegisterDependenciesFunction NoDependenciesToRegister
This can be used as the value for a RegisterDependenciesFunction if there are no dependants to regist...
Definition Core.cpp:41
std::vector< SymbolStringPtr > SymbolNameVector
A vector of symbol names.
DenseMap< JITDylib *, SymbolNameSet > SymbolDependenceMap
A map from JITDylibs to sets of symbols.
DenseSet< SymbolStringPtr > SymbolNameSet
A set of symbol names (represented by SymbolStringPtrs for.
SymbolState
Represents the state that a symbol has reached during materialization.
Definition Core.h:546
@ Materializing
Added to the symbol table, never queried.
Definition Core.h:549
@ NeverSearched
No symbol should be in this state.
Definition Core.h:548
@ Ready
Emitted to memory, but waiting on transitive dependencies.
Definition Core.h:552
@ Emitted
Assigned address, still materializing.
Definition Core.h:551
@ Resolved
Queried, materialization begun.
Definition Core.h:550
DenseMap< SymbolStringPtr, SymbolAliasMapEntry > SymbolAliasMap
A map of Symbols to (Symbol, Flags) pairs.
Definition Core.h:174
unique_function< void(Expected< SymbolMap >)> SymbolsResolvedCallback
Callback to notify client that symbols have been resolved.
Definition Core.h:177
LLVM_ABI Expected< SymbolAliasMap > buildSimpleReexportsAliasMap(JITDylib &SourceJD, const SymbolNameSet &Symbols)
Build a SymbolAliasMap for the common case where you want to re-export symbols from another JITDylib ...
Definition Core.cpp:483
DenseMap< SymbolStringPtr, JITSymbolFlags > SymbolFlagsMap
A map from symbol names (as SymbolStringPtrs) to JITSymbolFlags.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition Error.cpp:61
testing::Matcher< const detail::ErrorHolder & > Failed()
Definition Error.h:198
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1933
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
JITSymbolFlags AliasFlags
Definition Core.h:170
SymbolAliasMapEntry(SymbolStringPtr Aliasee, JITSymbolFlags AliasFlags)
Definition Core.h:166
SymbolStringPtr Aliasee
Definition Core.h:169
A set of symbols and the their dependencies.
Definition Core.h:338
SymbolDependenceMap Dependencies
Definition Core.h:340