LLVM  14.0.0git
CompileOnDemandLayer.h
Go to the documentation of this file.
1 //===- CompileOnDemandLayer.h - Compile each function on demand -*- 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 // JIT layer for breaking up modules and inserting callbacks to allow
10 // individual functions to be compiled on demand.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H
16 
17 #include "llvm/ADT/APInt.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/StringRef.h"
28 #include "llvm/IR/Attributes.h"
29 #include "llvm/IR/Constant.h"
30 #include "llvm/IR/Constants.h"
31 #include "llvm/IR/DataLayout.h"
32 #include "llvm/IR/Function.h"
33 #include "llvm/IR/GlobalAlias.h"
34 #include "llvm/IR/GlobalValue.h"
35 #include "llvm/IR/GlobalVariable.h"
36 #include "llvm/IR/Instruction.h"
37 #include "llvm/IR/Mangler.h"
38 #include "llvm/IR/Module.h"
39 #include "llvm/IR/Type.h"
40 #include "llvm/Support/Casting.h"
43 #include <algorithm>
44 #include <cassert>
45 #include <functional>
46 #include <iterator>
47 #include <list>
48 #include <memory>
49 #include <set>
50 #include <utility>
51 #include <vector>
52 
53 namespace llvm {
54 
55 class Value;
56 
57 namespace orc {
58 
59 class ExtractingIRMaterializationUnit;
60 
61 class CompileOnDemandLayer : public IRLayer {
63 
64 public:
65  /// Builder for IndirectStubsManagers.
67  std::function<std::unique_ptr<IndirectStubsManager>()>;
68 
69  using GlobalValueSet = std::set<const GlobalValue *>;
70 
71  /// Partitioning function.
72  using PartitionFunction =
73  std::function<Optional<GlobalValueSet>(GlobalValueSet Requested)>;
74 
75  /// Off-the-shelf partitioning which compiles all requested symbols (usually
76  /// a single function at a time).
78 
79  /// Off-the-shelf partitioning which compiles whole modules whenever any
80  /// symbol in them is requested.
82 
83  /// Construct a CompileOnDemandLayer.
85  LazyCallThroughManager &LCTMgr,
86  IndirectStubsManagerBuilder BuildIndirectStubsManager);
87 
88  /// Sets the partition function.
90 
91  /// Sets the ImplSymbolMap
92  void setImplMap(ImplSymbolMap *Imp);
93 
94  /// Emits the given module. This should not be called by clients: it will be
95  /// called by the JIT when a definition added via the add method is requested.
96  void emit(std::unique_ptr<MaterializationResponsibility> R,
97  ThreadSafeModule TSM) override;
98 
99 private:
100  struct PerDylibResources {
101  public:
102  PerDylibResources(JITDylib &ImplD,
103  std::unique_ptr<IndirectStubsManager> ISMgr)
104  : ImplD(ImplD), ISMgr(std::move(ISMgr)) {}
105  JITDylib &getImplDylib() { return ImplD; }
106  IndirectStubsManager &getISManager() { return *ISMgr; }
107 
108  private:
109  JITDylib &ImplD;
110  std::unique_ptr<IndirectStubsManager> ISMgr;
111  };
112 
113  using PerDylibResourcesMap = std::map<const JITDylib *, PerDylibResources>;
114 
115  PerDylibResources &getPerDylibResources(JITDylib &TargetD);
116 
117  void cleanUpModule(Module &M);
118 
119  void expandPartition(GlobalValueSet &Partition);
120 
121  void emitPartition(std::unique_ptr<MaterializationResponsibility> R,
122  ThreadSafeModule TSM,
124 
125  mutable std::mutex CODLayerMutex;
126 
127  IRLayer &BaseLayer;
128  LazyCallThroughManager &LCTMgr;
129  IndirectStubsManagerBuilder BuildIndirectStubsManager;
130  PerDylibResourcesMap DylibResources;
132  SymbolLinkagePromoter PromoteSymbols;
133  ImplSymbolMap *AliaseeImpls = nullptr;
134 };
135 
136 } // end namespace orc
137 } // end namespace llvm
138 
139 #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::orc::JITDylib
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:907
llvm::orc::CompileOnDemandLayer::setImplMap
void setImplMap(ImplSymbolMap *Imp)
Sets the ImplSymbolMap.
Definition: CompileOnDemandLayer.cpp:127
JITSymbol.h
Optional.h
ValueMapper.h
llvm::orc::ImplSymbolMap
Definition: Speculation.h:35
Speculation.h
StringRef.h
llvm::orc::CompileOnDemandLayer::CompileOnDemandLayer
CompileOnDemandLayer(ExecutionSession &ES, IRLayer &BaseLayer, LazyCallThroughManager &LCTMgr, IndirectStubsManagerBuilder BuildIndirectStubsManager)
Construct a CompileOnDemandLayer.
Definition: CompileOnDemandLayer.cpp:116
llvm::orc::CompileOnDemandLayer::setPartitionFunction
void setPartitionFunction(PartitionFunction Partition)
Sets the partition function.
Definition: CompileOnDemandLayer.cpp:123
LazyReexports.h
llvm::orc::LazyCallThroughManager
Manages a set of 'lazy call-through' trampolines.
Definition: LazyReexports.h:38
APInt.h
Module.h
llvm::orc::CompileOnDemandLayer::compileRequested
static Optional< GlobalValueSet > compileRequested(GlobalValueSet Requested)
Off-the-shelf partitioning which compiles all requested symbols (usually a single function at a time)...
Definition: CompileOnDemandLayer.cpp:107
llvm::Optional
Definition: APInt.h:33
STLExtras.h
RuntimeDyld.h
Layer.h
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:193
Instruction.h
GlobalValue.h
Constants.h
llvm::orc::CompileOnDemandLayer::PartitionFunction
std::function< Optional< GlobalValueSet >(GlobalValueSet Requested)> PartitionFunction
Partitioning function.
Definition: CompileOnDemandLayer.h:73
Type.h
llvm::move
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:1605
llvm::orc::PartitioningIRMaterializationUnit
Definition: CompileOnDemandLayer.cpp:72
llvm::orc::CompileOnDemandLayer::GlobalValueSet
std::set< const GlobalValue * > GlobalValueSet
Definition: CompileOnDemandLayer.h:69
Mangler.h
DataLayout.h
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
Attributes.h
Constant.h
std
Definition: BitVector.h:838
llvm::orc::CompileOnDemandLayer::IndirectStubsManagerBuilder
std::function< std::unique_ptr< IndirectStubsManager >()> IndirectStubsManagerBuilder
Builder for IndirectStubsManagers.
Definition: CompileOnDemandLayer.h:67
llvm::orc::IRLayer::IRLayer
IRLayer(ExecutionSession &ES, const IRSymbolMapper::ManglingOptions *&MO)
Definition: Layer.h:70
GlobalVariable.h
llvm::orc::ExecutionSession
An ExecutionSession represents a running JIT program.
Definition: Core.h:1275
Casting.h
Function.h
llvm::orc::CompileOnDemandLayer::compileWholeModule
static Optional< GlobalValueSet > compileWholeModule(GlobalValueSet Requested)
Off-the-shelf partitioning which compiles whole modules whenever any symbol in them is requested.
Definition: CompileOnDemandLayer.cpp:112
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
GlobalAlias.h
llvm::orc::CompileOnDemandLayer::emit
void emit(std::unique_ptr< MaterializationResponsibility > R, ThreadSafeModule TSM) override
Emits the given module.
Definition: CompileOnDemandLayer.cpp:130
IndirectionUtils.h
llvm::orc::ThreadSafeModule
An LLVM Module together with a shared ThreadSafeContext.
Definition: ThreadSafeModule.h:77
raw_ostream.h
llvm::orc::IRMaterializationUnit::SymbolNameToDefinitionMap
std::map< SymbolStringPtr, GlobalValue * > SymbolNameToDefinitionMap
Definition: Layer.h:33
llvm::orc::IRLayer
Interface for layers that accept LLVM IR.
Definition: Layer.h:68
OrcError.h
llvm::orc::CompileOnDemandLayer
Definition: CompileOnDemandLayer.h:61