Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- CompileOnDemandLayer.h - Compile each function on demand -*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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" |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Optional.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
| 20 | #include "llvm/ADT/StringRef.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 21 | #include "llvm/ExecutionEngine/JITSymbol.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 22 | #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h" |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 23 | #include "llvm/ExecutionEngine/Orc/Layer.h" |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 24 | #include "llvm/ExecutionEngine/Orc/LazyReexports.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 25 | #include "llvm/ExecutionEngine/Orc/OrcError.h" |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 26 | #include "llvm/ExecutionEngine/Orc/Speculation.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 27 | #include "llvm/ExecutionEngine/RuntimeDyld.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" |
| 41 | #include "llvm/Support/raw_ostream.h" |
| 42 | #include "llvm/Transforms/Utils/ValueMapper.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 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 60 | class ExtractingIRMaterializationUnit; |
| 61 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 62 | class CompileOnDemandLayer : public IRLayer { |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 63 | friend class PartitioningIRMaterializationUnit; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 64 | |
| 65 | public: |
| 66 | /// Builder for IndirectStubsManagers. |
| 67 | using IndirectStubsManagerBuilder = |
| 68 | std::function<std::unique_ptr<IndirectStubsManager>()>; |
| 69 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 70 | using GlobalValueSet = std::set<const GlobalValue *>; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 71 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 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). |
| 78 | static Optional<GlobalValueSet> compileRequested(GlobalValueSet Requested); |
| 79 | |
| 80 | /// Off-the-shelf partitioning which compiles whole modules whenever any |
| 81 | /// symbol in them is requested. |
| 82 | static Optional<GlobalValueSet> compileWholeModule(GlobalValueSet Requested); |
| 83 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 84 | /// Construct a CompileOnDemandLayer. |
| 85 | CompileOnDemandLayer(ExecutionSession &ES, IRLayer &BaseLayer, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 86 | LazyCallThroughManager &LCTMgr, |
| 87 | IndirectStubsManagerBuilder BuildIndirectStubsManager); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 88 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 89 | /// Sets the partition function. |
| 90 | void setPartitionFunction(PartitionFunction Partition); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 91 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 92 | /// Sets the ImplSymbolMap |
| 93 | void setImplMap(ImplSymbolMap *Imp); |
| 94 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 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. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 97 | void emit(std::unique_ptr<MaterializationResponsibility> R, |
| 98 | ThreadSafeModule TSM) override; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 99 | |
| 100 | private: |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 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; } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 108 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 109 | private: |
| 110 | JITDylib &ImplD; |
| 111 | std::unique_ptr<IndirectStubsManager> ISMgr; |
| 112 | }; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 113 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 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 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 122 | void emitPartition(std::unique_ptr<MaterializationResponsibility> R, |
| 123 | ThreadSafeModule TSM, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 124 | IRMaterializationUnit::SymbolNameToDefinitionMap Defs); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 125 | |
| 126 | mutable std::mutex CODLayerMutex; |
| 127 | |
| 128 | IRLayer &BaseLayer; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 129 | LazyCallThroughManager &LCTMgr; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 130 | IndirectStubsManagerBuilder BuildIndirectStubsManager; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 131 | PerDylibResourcesMap DylibResources; |
| 132 | PartitionFunction Partition = compileRequested; |
| 133 | SymbolLinkagePromoter PromoteSymbols; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 134 | ImplSymbolMap *AliaseeImpls = nullptr; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | } // end namespace orc |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 138 | } // end namespace llvm |
| 139 | |
| 140 | #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H |