LLVM  10.0.0
CompileOnDemandLayer.cpp
Go to the documentation of this file.
1 //===----- CompileOnDemandLayer.cpp - Lazily emit IR on first call --------===//
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 
10 #include "llvm/IR/Mangler.h"
11 #include "llvm/IR/Module.h"
12 
13 using namespace llvm;
14 using namespace llvm::orc;
15 
17  StringRef Suffix,
18  GVPredicate ShouldExtract) {
19 
20  auto DeleteExtractedDefs = [](GlobalValue &GV) {
21  // Bump the linkage: this global will be provided by the external module.
22  GV.setLinkage(GlobalValue::ExternalLinkage);
23 
24  // Delete the definition in the source module.
25  if (isa<Function>(GV)) {
26  auto &F = cast<Function>(GV);
27  F.deleteBody();
28  F.setPersonalityFn(nullptr);
29  } else if (isa<GlobalVariable>(GV)) {
30  cast<GlobalVariable>(GV).setInitializer(nullptr);
31  } else if (isa<GlobalAlias>(GV)) {
32  // We need to turn deleted aliases into function or variable decls based
33  // on the type of their aliasee.
34  auto &A = cast<GlobalAlias>(GV);
35  Constant *Aliasee = A.getAliasee();
36  assert(A.hasName() && "Anonymous alias?");
37  assert(Aliasee->hasName() && "Anonymous aliasee");
38  std::string AliasName = A.getName();
39 
40  if (isa<Function>(Aliasee)) {
41  auto *F = cloneFunctionDecl(*A.getParent(), *cast<Function>(Aliasee));
42  A.replaceAllUsesWith(F);
43  A.eraseFromParent();
44  F->setName(AliasName);
45  } else if (isa<GlobalVariable>(Aliasee)) {
46  auto *G = cloneGlobalVariableDecl(*A.getParent(),
47  *cast<GlobalVariable>(Aliasee));
48  A.replaceAllUsesWith(G);
49  A.eraseFromParent();
50  G->setName(AliasName);
51  } else
52  llvm_unreachable("Alias to unsupported type");
53  } else
54  llvm_unreachable("Unsupported global type");
55  };
56 
57  auto NewTSM = cloneToNewContext(TSM, ShouldExtract, DeleteExtractedDefs);
58  NewTSM.withModuleDo([&](Module &M) {
59  M.setModuleIdentifier((M.getModuleIdentifier() + Suffix).str());
60  });
61 
62  return NewTSM;
63 }
64 
65 namespace llvm {
66 namespace orc {
67 
69 public:
71  const ManglingOptions &MO,
73  CompileOnDemandLayer &Parent)
74  : IRMaterializationUnit(ES, MO, std::move(TSM), std::move(K)),
75  Parent(Parent) {}
76 
79  SymbolNameToDefinitionMap SymbolToDefinition,
80  CompileOnDemandLayer &Parent)
81  : IRMaterializationUnit(std::move(TSM), std::move(K),
82  std::move(SymbolFlags),
83  std::move(SymbolToDefinition)),
84  Parent(Parent) {}
85 
86 private:
87  void materialize(MaterializationResponsibility R) override {
88  Parent.emitPartition(std::move(R), std::move(TSM),
89  std::move(SymbolToDefinition));
90  }
91 
92  void discard(const JITDylib &V, const SymbolStringPtr &Name) override {
93  // All original symbols were materialized by the CODLayer and should be
94  // final. The function bodies provided by M should never be overridden.
95  llvm_unreachable("Discard should never be called on an "
96  "ExtractingIRMaterializationUnit");
97  }
98 
99  mutable std::mutex SourceModuleMutex;
100  CompileOnDemandLayer &Parent;
101 };
102 
105  return std::move(Requested);
106 }
107 
110  return None;
111 }
112 
114  ExecutionSession &ES, IRLayer &BaseLayer, LazyCallThroughManager &LCTMgr,
115  IndirectStubsManagerBuilder BuildIndirectStubsManager)
116  : IRLayer(ES, BaseLayer.getManglingOptions()), BaseLayer(BaseLayer),
117  LCTMgr(LCTMgr),
118  BuildIndirectStubsManager(std::move(BuildIndirectStubsManager)) {}
119 
121  this->Partition = std::move(Partition);
122 }
123 
125  this->AliaseeImpls = Imp;
126 }
128  ThreadSafeModule TSM) {
129  assert(TSM && "Null module");
130 
131  auto &ES = getExecutionSession();
132 
133  // Sort the callables and non-callables, build re-exports and lodge the
134  // actual module with the implementation dylib.
135  auto &PDR = getPerDylibResources(R.getTargetJITDylib());
136 
137  SymbolAliasMap NonCallables;
138  SymbolAliasMap Callables;
139  TSM.withModuleDo([&](Module &M) {
140  // First, do some cleanup on the module:
141  cleanUpModule(M);
142  });
143 
144  for (auto &KV : R.getSymbols()) {
145  auto &Name = KV.first;
146  auto &Flags = KV.second;
147  if (Flags.isCallable())
148  Callables[Name] = SymbolAliasMapEntry(Name, Flags);
149  else
150  NonCallables[Name] = SymbolAliasMapEntry(Name, Flags);
151  }
152 
153  // Create a partitioning materialization unit and lodge it with the
154  // implementation dylib.
155  if (auto Err = PDR.getImplDylib().define(
156  std::make_unique<PartitioningIRMaterializationUnit>(
157  ES, *getManglingOptions(), std::move(TSM), R.getVModuleKey(),
158  *this))) {
159  ES.reportError(std::move(Err));
161  return;
162  }
163 
164  R.replace(reexports(PDR.getImplDylib(), std::move(NonCallables),
166  R.replace(lazyReexports(LCTMgr, PDR.getISManager(), PDR.getImplDylib(),
167  std::move(Callables), AliaseeImpls));
168 }
169 
170 CompileOnDemandLayer::PerDylibResources &
171 CompileOnDemandLayer::getPerDylibResources(JITDylib &TargetD) {
172  auto I = DylibResources.find(&TargetD);
173  if (I == DylibResources.end()) {
174  auto &ImplD =
175  getExecutionSession().createJITDylib(TargetD.getName() + ".impl");
176  TargetD.withSearchOrderDo(
177  [&](const JITDylibSearchOrder &TargetSearchOrder) {
178  auto NewSearchOrder = TargetSearchOrder;
179  assert(
180  !NewSearchOrder.empty() &&
181  NewSearchOrder.front().first == &TargetD &&
182  NewSearchOrder.front().second ==
184  "TargetD must be at the front of its own search order and match "
185  "non-exported symbol");
186  NewSearchOrder.insert(std::next(NewSearchOrder.begin()),
188  ImplD.setSearchOrder(std::move(NewSearchOrder), false);
189  });
190  PerDylibResources PDR(ImplD, BuildIndirectStubsManager());
191  I = DylibResources.insert(std::make_pair(&TargetD, std::move(PDR))).first;
192  }
193 
194  return I->second;
195 }
196 
197 void CompileOnDemandLayer::cleanUpModule(Module &M) {
198  for (auto &F : M.functions()) {
199  if (F.isDeclaration())
200  continue;
201 
202  if (F.hasAvailableExternallyLinkage()) {
203  F.deleteBody();
204  F.setPersonalityFn(nullptr);
205  continue;
206  }
207  }
208 }
209 
210 void CompileOnDemandLayer::expandPartition(GlobalValueSet &Partition) {
211  // Expands the partition to ensure the following rules hold:
212  // (1) If any alias is in the partition, its aliasee is also in the partition.
213  // (2) If any aliasee is in the partition, its aliases are also in the
214  // partiton.
215  // (3) If any global variable is in the partition then all global variables
216  // are in the partition.
217  assert(!Partition.empty() && "Unexpected empty partition");
218 
219  const Module &M = *(*Partition.begin())->getParent();
220  bool ContainsGlobalVariables = false;
221  std::vector<const GlobalValue *> GVsToAdd;
222 
223  for (auto *GV : Partition)
224  if (isa<GlobalAlias>(GV))
225  GVsToAdd.push_back(
226  cast<GlobalValue>(cast<GlobalAlias>(GV)->getAliasee()));
227  else if (isa<GlobalVariable>(GV))
228  ContainsGlobalVariables = true;
229 
230  for (auto &A : M.aliases())
231  if (Partition.count(cast<GlobalValue>(A.getAliasee())))
232  GVsToAdd.push_back(&A);
233 
234  if (ContainsGlobalVariables)
235  for (auto &G : M.globals())
236  GVsToAdd.push_back(&G);
237 
238  for (auto *GV : GVsToAdd)
239  Partition.insert(GV);
240 }
241 
242 void CompileOnDemandLayer::emitPartition(
245 
246  // FIXME: Need a 'notify lazy-extracting/emitting' callback to tie the
247  // extracted module key, extracted module, and source module key
248  // together. This could be used, for example, to provide a specific
249  // memory manager instance to the linking layer.
250 
251  auto &ES = getExecutionSession();
252  GlobalValueSet RequestedGVs;
253  for (auto &Name : R.getRequestedSymbols()) {
254  assert(Defs.count(Name) && "No definition for symbol");
255  RequestedGVs.insert(Defs[Name]);
256  }
257 
258  /// Perform partitioning with the context lock held, since the partition
259  /// function is allowed to access the globals to compute the partition.
260  auto GVsToExtract =
261  TSM.withModuleDo([&](Module &M) { return Partition(RequestedGVs); });
262 
263  // Take a 'None' partition to mean the whole module (as opposed to an empty
264  // partition, which means "materialize nothing"). Emit the whole module
265  // unmodified to the base layer.
266  if (GVsToExtract == None) {
267  Defs.clear();
268  BaseLayer.emit(std::move(R), std::move(TSM));
269  return;
270  }
271 
272  // If the partition is empty, return the whole module to the symbol table.
273  if (GVsToExtract->empty()) {
274  R.replace(std::make_unique<PartitioningIRMaterializationUnit>(
275  std::move(TSM), R.getSymbols(), std::move(Defs), *this));
276  return;
277  }
278 
279  // Ok -- we actually need to partition the symbols. Promote the symbol
280  // linkages/names, expand the partition to include any required symbols
281  // (i.e. symbols that can't be separated from our partition), and
282  // then extract the partition.
283  //
284  // FIXME: We apply this promotion once per partitioning. It's safe, but
285  // overkill.
286 
287  auto ExtractedTSM =
289  auto PromotedGlobals = PromoteSymbols(M);
290  if (!PromotedGlobals.empty()) {
291  MangleAndInterner Mangle(ES, M.getDataLayout());
293  for (auto &GV : PromotedGlobals)
294  SymbolFlags[Mangle(GV->getName())] =
296  if (auto Err = R.defineMaterializing(SymbolFlags))
297  return std::move(Err);
298  }
299 
300  expandPartition(*GVsToExtract);
301 
302  // Extract the requested partiton (plus any necessary aliases) and
303  // put the rest back into the impl dylib.
304  auto ShouldExtract = [&](const GlobalValue &GV) -> bool {
305  return GVsToExtract->count(&GV);
306  };
307 
308  return extractSubModule(TSM, ".submodule", ShouldExtract);
309  });
310 
311  if (!ExtractedTSM) {
312  ES.reportError(ExtractedTSM.takeError());
314  return;
315  }
316 
317  R.replace(std::make_unique<PartitioningIRMaterializationUnit>(
318  ES, *getManglingOptions(), std::move(TSM), R.getVModuleKey(), *this));
319  BaseLayer.emit(std::move(R), std::move(*ExtractedTSM));
320 }
321 
322 } // end namespace orc
323 } // end namespace llvm
const NoneType None
Definition: None.h:23
JITDylib & createJITDylib(std::string Name)
Add a new JITDylib to this ExecutionSession.
Definition: Core.cpp:1893
static JITSymbolFlags fromGlobalValue(const GlobalValue &GV)
Construct a JITSymbolFlags value based on the flags of the given global value.
Definition: JITSymbol.cpp:21
static ThreadSafeModule extractSubModule(ThreadSafeModule &TSM, StringRef Suffix, GVPredicate ShouldExtract)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
static Optional< GlobalValueSet > compileRequested(GlobalValueSet Requested)
Off-the-shelf partitioning which compiles all requested symbols (usually a single function at a time)...
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
void setModuleIdentifier(StringRef ID)
Set the module identifier.
Definition: Module.h:274
void replace(std::unique_ptr< MaterializationUnit > MU)
Transfers responsibility to the given MaterializationUnit for all symbols defined by that Materializa...
Definition: Core.cpp:502
auto withModuleDo(Func &&F) -> decltype(F(std::declval< Module &>()))
Locks the associated ThreadSafeContext and calls the given function on the contained Module...
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:94
Externally visible function.
Definition: GlobalValue.h:48
F(f)
std::function< std::unique_ptr< IndirectStubsManager >()> IndirectStubsManagerBuilder
Builder for IndirectStubsManagers.
std::function< bool(const GlobalValue &)> GVPredicate
Definition: BitVector.h:937
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
std::function< Optional< GlobalValueSet >(GlobalValueSet Requested)> PartitionFunction
Partitioning function.
SymbolFlags
Symbol flags.
Definition: Symbol.h:25
Mangles symbol names then uniques them in the context of an ExecutionSession.
Definition: Core.h:1310
std::map< SymbolStringPtr, GlobalValue * > SymbolNameToDefinitionMap
Definition: Layer.h:34
auto withSearchOrderDo(Func &&F) -> decltype(F(std::declval< const JITDylibSearchOrder &>()))
Do something with the search order (run under the session lock).
Definition: Core.h:1247
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:434
IRMaterializationUnit is a convenient base class for MaterializationUnits wrapping LLVM IR...
Definition: Layer.h:28
ExecutionSession & getExecutionSession()
Returns the ExecutionSession for this layer.
Definition: Layer.h:73
JITDylib & getTargetJITDylib() const
Returns the target JITDylib that these symbols are being materialized into.
Definition: Core.h:448
const IRMaterializationUnit::ManglingOptions *& getManglingOptions() const
Get the mangling options for this layer.
Definition: Layer.h:76
GlobalVariable * cloneGlobalVariableDecl(Module &Dst, const GlobalVariable &GV, ValueToValueMapTy *VMap=nullptr)
Clone a global variable declaration into a new module.
Pointer to a pooled string representing a symbol name.
virtual void emit(MaterializationResponsibility R, ThreadSafeModule TSM)=0
Emit should materialize the given IR.
iterator_range< iterator > functions()
Definition: Module.h:610
VModuleKey getVModuleKey() const
Returns the VModuleKey for this instance.
Definition: Core.h:451
This is an important base class in LLVM.
Definition: Constant.h:41
std::unique_ptr< LazyReexportsMaterializationUnit > lazyReexports(LazyCallThroughManager &LCTManager, IndirectStubsManager &ISManager, JITDylib &SourceJD, SymbolAliasMap CallableAliases, ImplSymbolMap *SrcJDLoc=nullptr, VModuleKey K=VModuleKey())
Define lazy-reexports based on the given SymbolAliasMap.
PartitioningIRMaterializationUnit(ThreadSafeModule TSM, SymbolFlagsMap SymbolFlags, SymbolNameToDefinitionMap SymbolToDefinition, CompileOnDemandLayer &Parent)
An LLVM Module together with a shared ThreadSafeContext.
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:211
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setImplMap(ImplSymbolMap *Imp)
Sets the ImplSymbolMap.
Interface for layers that accept LLVM IR.
Definition: Layer.h:64
Function * cloneFunctionDecl(Module &Dst, const Function &F, ValueToValueMapTy *VMap=nullptr)
Clone a function declaration into a new module.
Module.h This file contains the declarations for the Module class.
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
static Optional< GlobalValueSet > compileWholeModule(GlobalValueSet Requested)
Off-the-shelf partitioning which compiles whole modules whenever any symbol in them is requested...
PartitioningIRMaterializationUnit(ExecutionSession &ES, const ManglingOptions &MO, ThreadSafeModule TSM, VModuleKey K, CompileOnDemandLayer &Parent)
SymbolNameSet getRequestedSymbols() const
Returns the names of any symbols covered by this MaterializationResponsibility object that have queri...
Definition: Core.cpp:435
An ExecutionSession represents a running JIT program.
Definition: Core.h:1058
const SymbolFlagsMap & getSymbols() const
Returns the symbol flags map for this responsibility instance.
Definition: Core.h:457
CompileOnDemandLayer(ExecutionSession &ES, IRLayer &BaseLayer, LazyCallThroughManager &LCTMgr, IndirectStubsManagerBuilder BuildIndirectStubsManager)
Construct a CompileOnDemandLayer.
uint8_t uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:42
std::unique_ptr< ReExportsMaterializationUnit > reexports(JITDylib &SourceJD, SymbolAliasMap Aliases, JITDylibLookupFlags SourceJDLookupFlags=JITDylibLookupFlags::MatchExportedSymbolsOnly, VModuleKey K=VModuleKey())
Create a materialization unit for re-exporting symbols from another JITDylib with alternative names/f...
Definition: Core.h:685
void setPartitionFunction(PartitionFunction Partition)
Sets the partition function.
#define I(x, y, z)
Definition: MD5.cpp:58
Error defineMaterializing(SymbolFlagsMap SymbolFlags)
Attempt to claim responsibility for new definitions.
Definition: Core.cpp:470
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
const std::string & getName() const
Get the name for this JITDylib.
Definition: Core.h:805
ThreadSafeModule cloneToNewContext(ThreadSafeModule &TSMW, GVPredicate ShouldCloneDef=GVPredicate(), GVModifier UpdateClonedDefSource=GVModifier())
Clones the given module on to a new context.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void failMaterialization()
Notify all not-yet-emitted covered by this MaterializationResponsibility instance that an error has o...
Definition: Core.cpp:486
Manages a set of &#39;lazy call-through&#39; trampolines.
Definition: LazyReexports.h:37
static const Function * getParent(const Value *V)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
void emit(MaterializationResponsibility R, ThreadSafeModule TSM) override
Emits the given module.
std::set< const GlobalValue * > GlobalValueSet
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:774