LLVM  12.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 <string>
51 #include <utility>
52 #include <vector>
53 
54 namespace llvm {
55 
56 class Value;
57 
58 namespace orc {
59 
60 class ExtractingIRMaterializationUnit;
61 
62 class CompileOnDemandLayer : public IRLayer {
64 
65 public:
66  /// Builder for IndirectStubsManagers.
68  std::function<std::unique_ptr<IndirectStubsManager>()>;
69 
70  using GlobalValueSet = std::set<const GlobalValue *>;
71 
72  /// Partitioning function.
73  using PartitionFunction =
74  std::function<Optional<GlobalValueSet>(GlobalValueSet Requested)>;
75 
76  /// Off-the-shelf partitioning which compiles all requested symbols (usually
77  /// a single function at a time).
79 
80  /// Off-the-shelf partitioning which compiles whole modules whenever any
81  /// symbol in them is requested.
83 
84  /// Construct a CompileOnDemandLayer.
86  LazyCallThroughManager &LCTMgr,
87  IndirectStubsManagerBuilder BuildIndirectStubsManager);
88 
89  /// Sets the partition function.
91 
92  /// Sets the ImplSymbolMap
93  void setImplMap(ImplSymbolMap *Imp);
94 
95  /// Emits the given module. This should not be called by clients: it will be
96  /// called by the JIT when a definition added via the add method is requested.
97  void emit(std::unique_ptr<MaterializationResponsibility> R,
98  ThreadSafeModule TSM) override;
99 
100 private:
101  struct PerDylibResources {
102  public:
103  PerDylibResources(JITDylib &ImplD,
104  std::unique_ptr<IndirectStubsManager> ISMgr)
105  : ImplD(ImplD), ISMgr(std::move(ISMgr)) {}
106  JITDylib &getImplDylib() { return ImplD; }
107  IndirectStubsManager &getISManager() { return *ISMgr; }
108 
109  private:
110  JITDylib &ImplD;
111  std::unique_ptr<IndirectStubsManager> ISMgr;
112  };
113 
114  using PerDylibResourcesMap = std::map<const JITDylib *, PerDylibResources>;
115 
116  PerDylibResources &getPerDylibResources(JITDylib &TargetD);
117 
118  void cleanUpModule(Module &M);
119 
120  void expandPartition(GlobalValueSet &Partition);
121 
122  void emitPartition(std::unique_ptr<MaterializationResponsibility> R,
123  ThreadSafeModule TSM,
125 
126  mutable std::mutex CODLayerMutex;
127 
128  IRLayer &BaseLayer;
129  LazyCallThroughManager &LCTMgr;
130  IndirectStubsManagerBuilder BuildIndirectStubsManager;
131  PerDylibResourcesMap DylibResources;
133  SymbolLinkagePromoter PromoteSymbols;
134  ImplSymbolMap *AliaseeImpls = nullptr;
135 };
136 
137 } // end namespace orc
138 } // end namespace llvm
139 
140 #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static Optional< GlobalValueSet > compileRequested(GlobalValueSet Requested)
Off-the-shelf partitioning which compiles all requested symbols (usually a single function at a time)...
std::function< std::unique_ptr< IndirectStubsManager >()> IndirectStubsManagerBuilder
Builder for IndirectStubsManagers.
Definition: BitVector.h:941
std::function< Optional< GlobalValueSet >(GlobalValueSet Requested)> PartitionFunction
Partitioning function.
This file contains the simple types necessary to represent the attributes associated with functions a...
std::map< SymbolStringPtr, GlobalValue * > SymbolNameToDefinitionMap
Definition: Layer.h:31
This file implements a class to represent arbitrary precision integral constant values and operations...
IRLayer(ExecutionSession &ES, const IRSymbolMapper::ManglingOptions *&MO)
Definition: Layer.h:68
This file contains the declarations for the subclasses of Constant, which represent the different fla...
An LLVM Module together with a shared ThreadSafeContext.
void setImplMap(ImplSymbolMap *Imp)
Sets the ImplSymbolMap.
Interface for layers that accept LLVM IR.
Definition: Layer.h:66
Machine Check Debug Module
Module.h This file contains the declarations for the Module class.
static Optional< GlobalValueSet > compileWholeModule(GlobalValueSet Requested)
Off-the-shelf partitioning which compiles whole modules whenever any symbol in them is requested.
An ExecutionSession represents a running JIT program.
Definition: Core.h:1221
CompileOnDemandLayer(ExecutionSession &ES, IRLayer &BaseLayer, LazyCallThroughManager &LCTMgr, IndirectStubsManagerBuilder BuildIndirectStubsManager)
Construct a CompileOnDemandLayer.
void setPartitionFunction(PartitionFunction Partition)
Sets the partition function.
Manages a set of 'lazy call-through' trampolines.
Definition: LazyReexports.h:38
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:1556
std::set< const GlobalValue * > GlobalValueSet
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:894
void emit(std::unique_ptr< MaterializationResponsibility > R, ThreadSafeModule TSM) override
Emits the given module.