LLVM 20.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/STLExtras.h"
19#include "llvm/ADT/StringRef.h"
27#include "llvm/IR/Attributes.h"
28#include "llvm/IR/Constant.h"
29#include "llvm/IR/Constants.h"
30#include "llvm/IR/DataLayout.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/GlobalAlias.h"
33#include "llvm/IR/GlobalValue.h"
35#include "llvm/IR/Instruction.h"
36#include "llvm/IR/Mangler.h"
37#include "llvm/IR/Module.h"
38#include "llvm/IR/Type.h"
42#include <algorithm>
43#include <cassert>
44#include <functional>
45#include <iterator>
46#include <list>
47#include <memory>
48#include <optional>
49#include <set>
50#include <utility>
51
52namespace llvm {
53namespace orc {
54
57
58public:
59 /// Builder for IndirectStubsManagers.
61 std::function<std::unique_ptr<IndirectStubsManager>()>;
62
63 using GlobalValueSet = std::set<const GlobalValue *>;
64
65 /// Partitioning function.
67 std::function<std::optional<GlobalValueSet>(GlobalValueSet Requested)>;
68
69 /// Off-the-shelf partitioning which compiles all requested symbols (usually
70 /// a single function at a time).
71 static std::optional<GlobalValueSet>
73
74 /// Off-the-shelf partitioning which compiles whole modules whenever any
75 /// symbol in them is requested.
76 static std::optional<GlobalValueSet>
78
79 /// Construct a CompileOnDemandLayer.
82 IndirectStubsManagerBuilder BuildIndirectStubsManager);
83
84 /// Sets the partition function.
86
87 /// Sets the ImplSymbolMap
88 void setImplMap(ImplSymbolMap *Imp);
89
90 /// Emits the given module. This should not be called by clients: it will be
91 /// called by the JIT when a definition added via the add method is requested.
92 void emit(std::unique_ptr<MaterializationResponsibility> R,
93 ThreadSafeModule TSM) override;
94
95private:
96 struct PerDylibResources {
97 public:
98 PerDylibResources(JITDylib &ImplD,
99 std::unique_ptr<IndirectStubsManager> ISMgr)
100 : ImplD(ImplD), ISMgr(std::move(ISMgr)) {}
101 JITDylib &getImplDylib() { return ImplD; }
102 IndirectStubsManager &getISManager() { return *ISMgr; }
103
104 private:
105 JITDylib &ImplD;
106 std::unique_ptr<IndirectStubsManager> ISMgr;
107 };
108
109 using PerDylibResourcesMap = std::map<const JITDylib *, PerDylibResources>;
110
111 PerDylibResources &getPerDylibResources(JITDylib &TargetD);
112
113 void cleanUpModule(Module &M);
114
115 void expandPartition(GlobalValueSet &Partition);
116
117 void emitPartition(std::unique_ptr<MaterializationResponsibility> R,
118 ThreadSafeModule TSM,
120
121 mutable std::mutex CODLayerMutex;
122
123 IRLayer &BaseLayer;
124 LazyCallThroughManager &LCTMgr;
125 IndirectStubsManagerBuilder BuildIndirectStubsManager;
126 PerDylibResourcesMap DylibResources;
128 SymbolLinkagePromoter PromoteSymbols;
129 ImplSymbolMap *AliaseeImpls = nullptr;
130};
131
132} // end namespace orc
133} // end namespace llvm
134
135#endif // LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H
This file implements a class to represent arbitrary precision integral constant values and operations...
This file contains the simple types necessary to represent the attributes associated with functions a...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
This file contains some templates that are useful if you are working with the STL at all.
std::function< std::unique_ptr< IndirectStubsManager >()> IndirectStubsManagerBuilder
Builder for IndirectStubsManagers.
static std::optional< GlobalValueSet > compileWholeModule(GlobalValueSet Requested)
Off-the-shelf partitioning which compiles whole modules whenever any symbol in them is requested.
void emit(std::unique_ptr< MaterializationResponsibility > R, ThreadSafeModule TSM) override
Emits the given module.
std::set< const GlobalValue * > GlobalValueSet
void setImplMap(ImplSymbolMap *Imp)
Sets the ImplSymbolMap.
static std::optional< GlobalValueSet > compileRequested(GlobalValueSet Requested)
Off-the-shelf partitioning which compiles all requested symbols (usually a single function at a time)...
std::function< std::optional< GlobalValueSet >(GlobalValueSet Requested)> PartitionFunction
Partitioning function.
void setPartitionFunction(PartitionFunction Partition)
Sets the partition function.
An ExecutionSession represents a running JIT program.
Definition: Core.h:1431
Interface for layers that accept LLVM IR.
Definition: Layer.h:67
IRLayer(ExecutionSession &ES, const IRSymbolMapper::ManglingOptions *&MO)
Definition: Layer.h:69
std::map< SymbolStringPtr, GlobalValue * > SymbolNameToDefinitionMap
Definition: Layer.h:33
Represents a JIT'd dynamic library.
Definition: Core.h:989
Manages a set of 'lazy call-through' trampolines.
Definition: LazyReexports.h:38
An LLVM Module together with a shared ThreadSafeContext.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1856
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858