LLVM  14.0.0git
LazyReexports.h
Go to the documentation of this file.
1 //===------ LazyReexports.h -- Utilities for lazy reexports -----*- 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 // Lazy re-exports are similar to normal re-exports, except that for callable
10 // symbols the definitions are replaced with trampolines that will look up and
11 // call through to the re-exported symbol at runtime. This can be used to
12 // enable lazy compilation.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_EXECUTIONENGINE_ORC_LAZYREEXPORTS_H
17 #define LLVM_EXECUTIONENGINE_ORC_LAZYREEXPORTS_H
18 
19 #include "llvm/ADT/STLExtras.h"
23 
24 namespace llvm {
25 
26 class Triple;
27 
28 namespace orc {
29 
30 /// Manages a set of 'lazy call-through' trampolines. These are compiler
31 /// re-entry trampolines that are pre-bound to look up a given symbol in a given
32 /// JITDylib, then jump to that address. Since compilation of symbols is
33 /// triggered on first lookup, these call-through trampolines can be used to
34 /// implement lazy compilation.
35 ///
36 /// The easiest way to construct these call-throughs is using the lazyReexport
37 /// function.
39 public:
42 
44  JITTargetAddress ErrorHandlerAddr, TrampolinePool *TP);
45 
46  // Return a free call-through trampoline and bind it to look up and call
47  // through to the given symbol.
50  NotifyResolvedFunction NotifyResolved);
51 
53  JITTargetAddress TrampolineAddr,
54  TrampolinePool::NotifyLandingResolvedFunction NotifyLandingResolved);
55 
56  virtual ~LazyCallThroughManager() = default;
57 
58 protected:
61 
62  struct ReexportsEntry {
65  };
66 
69  Error notifyResolved(JITTargetAddress TrampolineAddr,
70  JITTargetAddress ResolvedAddr);
71  void setTrampolinePool(TrampolinePool &TP) { this->TP = &TP; }
72 
73 private:
74  using ReexportsMap = std::map<JITTargetAddress, ReexportsEntry>;
75 
76  using NotifiersMap = std::map<JITTargetAddress, NotifyResolvedFunction>;
77 
78  std::mutex LCTMMutex;
79  ExecutionSession &ES;
80  JITTargetAddress ErrorHandlerAddr;
81  TrampolinePool *TP = nullptr;
82  ReexportsMap Reexports;
83  NotifiersMap Notifiers;
84 };
85 
86 /// A lazy call-through manager that builds trampolines in the current process.
88 private:
90 
92  JITTargetAddress ErrorHandlerAddr)
93  : LazyCallThroughManager(ES, ErrorHandlerAddr, nullptr) {}
94 
95  template <typename ORCABI> Error init() {
97  [this](JITTargetAddress TrampolineAddr,
99  NotifyLandingResolved) {
100  resolveTrampolineLandingAddress(TrampolineAddr,
101  std::move(NotifyLandingResolved));
102  });
103 
104  if (!TP)
105  return TP.takeError();
106 
107  this->TP = std::move(*TP);
108  setTrampolinePool(*this->TP);
109  return Error::success();
110  }
111 
112  std::unique_ptr<TrampolinePool> TP;
113 
114 public:
115  /// Create a LocalLazyCallThroughManager using the given ABI. See
116  /// createLocalLazyCallThroughManager.
117  template <typename ORCABI>
119  Create(ExecutionSession &ES, JITTargetAddress ErrorHandlerAddr) {
120  auto LLCTM = std::unique_ptr<LocalLazyCallThroughManager>(
121  new LocalLazyCallThroughManager(ES, ErrorHandlerAddr));
122 
123  if (auto Err = LLCTM->init<ORCABI>())
124  return std::move(Err);
125 
126  return std::move(LLCTM);
127  }
128 };
129 
130 /// Create a LocalLazyCallThroughManager from the given triple and execution
131 /// session.
133 createLocalLazyCallThroughManager(const Triple &T, ExecutionSession &ES,
134  JITTargetAddress ErrorHandlerAddr);
135 
136 /// A materialization unit that builds lazy re-exports. These are callable
137 /// entry points that call through to the given symbols.
138 /// Unlike a 'true' re-export, the address of the lazy re-export will not
139 /// match the address of the re-exported symbol, but calling it will behave
140 /// the same as calling the re-exported symbol.
142 public:
144  IndirectStubsManager &ISManager,
145  JITDylib &SourceJD,
146  SymbolAliasMap CallableAliases,
147  ImplSymbolMap *SrcJDLoc);
148 
149  StringRef getName() const override;
150 
151 private:
152  void materialize(std::unique_ptr<MaterializationResponsibility> R) override;
153  void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
154  static SymbolFlagsMap extractFlags(const SymbolAliasMap &Aliases);
155 
156  LazyCallThroughManager &LCTManager;
157  IndirectStubsManager &ISManager;
158  JITDylib &SourceJD;
159  SymbolAliasMap CallableAliases;
160  ImplSymbolMap *AliaseeTable;
161 };
162 
163 /// Define lazy-reexports based on the given SymbolAliasMap. Each lazy re-export
164 /// is a callable symbol that will look up and dispatch to the given aliasee on
165 /// first call. All subsequent calls will go directly to the aliasee.
166 inline std::unique_ptr<LazyReexportsMaterializationUnit>
168  IndirectStubsManager &ISManager, JITDylib &SourceJD,
169  SymbolAliasMap CallableAliases,
170  ImplSymbolMap *SrcJDLoc = nullptr) {
171  return std::make_unique<LazyReexportsMaterializationUnit>(
172  LCTManager, ISManager, SourceJD, std::move(CallableAliases), SrcJDLoc);
173 }
174 
175 } // End namespace orc
176 } // End namespace llvm
177 
178 #endif // LLVM_EXECUTIONENGINE_ORC_LAZYREEXPORTS_H
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::orc::JITDylib
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:922
llvm::orc::ImplSymbolMap
Definition: Speculation.h:35
Speculation.h
llvm::orc::LazyCallThroughManager::ReexportsEntry::SymbolName
SymbolStringPtr SymbolName
Definition: LazyReexports.h:64
llvm::unique_function
unique_function is a type-erasing functor similar to std::function.
Definition: FunctionExtras.h:56
llvm::orc::IndirectStubsManager
Base class for managing collections of named indirect stubs.
Definition: IndirectionUtils.h:275
llvm::orc::SymbolStringPtr
Pointer to a pooled string representing a symbol name.
Definition: SymbolStringPool.h:50
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
llvm::orc::LazyCallThroughManager::setTrampolinePool
void setTrampolinePool(TrampolinePool &TP)
Definition: LazyReexports.h:71
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
llvm::orc::LocalLazyCallThroughManager
A lazy call-through manager that builds trampolines in the current process.
Definition: LazyReexports.h:87
llvm::orc::LazyCallThroughManager
Manages a set of 'lazy call-through' trampolines.
Definition: LazyReexports.h:38
llvm::orc::lazyReexports
std::unique_ptr< LazyReexportsMaterializationUnit > lazyReexports(LazyCallThroughManager &LCTManager, IndirectStubsManager &ISManager, JITDylib &SourceJD, SymbolAliasMap CallableAliases, ImplSymbolMap *SrcJDLoc=nullptr)
Define lazy-reexports based on the given SymbolAliasMap.
Definition: LazyReexports.h:167
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
STLExtras.h
llvm::orc::LazyReexportsMaterializationUnit::LazyReexportsMaterializationUnit
LazyReexportsMaterializationUnit(LazyCallThroughManager &LCTManager, IndirectStubsManager &ISManager, JITDylib &SourceJD, SymbolAliasMap CallableAliases, ImplSymbolMap *SrcJDLoc)
Definition: LazyReexports.cpp:144
llvm::orc::MaterializationUnit
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group,...
Definition: Core.h:664
llvm::orc::LazyCallThroughManager::~LazyCallThroughManager
virtual ~LazyCallThroughManager()=default
llvm::orc::LocalLazyCallThroughManager::Create
static Expected< std::unique_ptr< LocalLazyCallThroughManager > > Create(ExecutionSession &ES, JITTargetAddress ErrorHandlerAddr)
Create a LocalLazyCallThroughManager using the given ABI.
Definition: LazyReexports.h:119
llvm::orc::LazyCallThroughManager::findReexport
Expected< ReexportsEntry > findReexport(JITTargetAddress TrampolineAddr)
Definition: LazyReexports.cpp:45
llvm::orc::TrampolinePool
Base class for pools of compiler re-entry trampolines.
Definition: IndirectionUtils.h:56
llvm::orc::LazyReexportsMaterializationUnit::getName
StringRef getName() const override
Return the name of this materialization unit.
Definition: LazyReexports.cpp:151
llvm::orc::LazyCallThroughManager::getCallThroughTrampoline
Expected< JITTargetAddress > getCallThroughTrampoline(JITDylib &SourceJD, SymbolStringPtr SymbolName, NotifyResolvedFunction NotifyResolved)
Definition: LazyReexports.cpp:23
llvm::orc::LazyCallThroughManager::ReexportsEntry::SourceJD
JITDylib * SourceJD
Definition: LazyReexports.h:63
uint64_t
llvm::orc::LazyCallThroughManager::reportCallThroughError
JITTargetAddress reportCallThroughError(Error Err)
Definition: LazyReexports.cpp:39
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::DenseMap< SymbolStringPtr, SymbolAliasMapEntry >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::orc::createLocalLazyCallThroughManager
Expected< std::unique_ptr< LazyCallThroughManager > > createLocalLazyCallThroughManager(const Triple &T, ExecutionSession &ES, JITTargetAddress ErrorHandlerAddr)
Create a LocalLazyCallThroughManager from the given triple and execution session.
Definition: LazyReexports.cpp:106
llvm::orc::LazyReexportsMaterializationUnit
A materialization unit that builds lazy re-exports.
Definition: LazyReexports.h:141
llvm::AMDGPU::HSAMD::Kernel::Key::SymbolName
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
Definition: AMDGPUMetadata.h:381
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::orc::LazyCallThroughManager::notifyResolved
Error notifyResolved(JITTargetAddress TrampolineAddr, JITTargetAddress ResolvedAddr)
Definition: LazyReexports.cpp:55
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::orc::LazyCallThroughManager::ReexportsEntry
Definition: LazyReexports.h:62
llvm::orc::ExecutionSession
An ExecutionSession represents a running JIT program.
Definition: Core.h:1275
llvm::JITTargetAddress
uint64_t JITTargetAddress
Represents an address in the target process's address space.
Definition: JITSymbol.h:42
llvm::orc::LazyCallThroughManager::LazyCallThroughManager
LazyCallThroughManager(ExecutionSession &ES, JITTargetAddress ErrorHandlerAddr, TrampolinePool *TP)
Definition: LazyReexports.cpp:19
Core.h
llvm::orc::LocalTrampolinePool::Create
static Expected< std::unique_ptr< LocalTrampolinePool > > Create(ResolveLandingFunction ResolveLanding)
Creates a LocalTrampolinePool with the given RunCallback function.
Definition: IndirectionUtils.h:101
IndirectionUtils.h
llvm::orc::TrampolinePool::NotifyLandingResolvedFunction
unique_function< void(JITTargetAddress) const > NotifyLandingResolvedFunction
Definition: IndirectionUtils.h:59
llvm::orc::LazyCallThroughManager::resolveTrampolineLandingAddress
void resolveTrampolineLandingAddress(JITTargetAddress TrampolineAddr, TrampolinePool::NotifyLandingResolvedFunction NotifyLandingResolved)
Definition: LazyReexports.cpp:70