[llvm] [cmake] Add possibility to use ChooseMSVCCRT.cmake when include LLVM library
[llvm-core.git] / include / llvm / ExecutionEngine / Orc / ObjectTransformLayer.h
blobeac1cc3e097a6b4cf5ba966d53a16b028bf09a39
1 //===- ObjectTransformLayer.h - Run all objects through functor -*- 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 // Run all objects passed in through a user supplied functor.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
14 #define LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
16 #include "llvm/ExecutionEngine/JITSymbol.h"
17 #include "llvm/ExecutionEngine/Orc/Layer.h"
18 #include <algorithm>
19 #include <memory>
20 #include <string>
22 namespace llvm {
23 namespace orc {
25 class ObjectTransformLayer : public ObjectLayer {
26 public:
27 using TransformFunction =
28 std::function<Expected<std::unique_ptr<MemoryBuffer>>(
29 std::unique_ptr<MemoryBuffer>)>;
31 ObjectTransformLayer(ExecutionSession &ES, ObjectLayer &BaseLayer,
32 TransformFunction Transform);
34 void emit(MaterializationResponsibility R,
35 std::unique_ptr<MemoryBuffer> O) override;
37 private:
38 ObjectLayer &BaseLayer;
39 TransformFunction Transform;
42 /// Object mutating layer.
43 ///
44 /// This layer accepts sets of ObjectFiles (via addObject). It
45 /// immediately applies the user supplied functor to each object, then adds
46 /// the set of transformed objects to the layer below.
47 template <typename BaseLayerT, typename TransformFtor>
48 class LegacyObjectTransformLayer {
49 public:
50 /// Construct an ObjectTransformLayer with the given BaseLayer
51 LLVM_ATTRIBUTE_DEPRECATED(
52 LegacyObjectTransformLayer(BaseLayerT &BaseLayer,
53 TransformFtor Transform = TransformFtor()),
54 "ORCv1 layers (layers with the 'Legacy' prefix) are deprecated. Please "
55 "use "
56 "the ORCv2 ObjectTransformLayer instead");
58 /// Legacy layer constructor with deprecation acknowledgement.
59 LegacyObjectTransformLayer(ORCv1DeprecationAcknowledgement,
60 BaseLayerT &BaseLayer,
61 TransformFtor Transform = TransformFtor())
62 : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
64 /// Apply the transform functor to each object in the object set, then
65 /// add the resulting set of objects to the base layer, along with the
66 /// memory manager and symbol resolver.
67 ///
68 /// @return A handle for the added objects.
69 template <typename ObjectPtr> Error addObject(VModuleKey K, ObjectPtr Obj) {
70 return BaseLayer.addObject(std::move(K), Transform(std::move(Obj)));
73 /// Remove the object set associated with the VModuleKey K.
74 Error removeObject(VModuleKey K) { return BaseLayer.removeObject(K); }
76 /// Search for the given named symbol.
77 /// @param Name The name of the symbol to search for.
78 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
79 /// @return A handle for the given named symbol, if it exists.
80 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
81 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
84 /// Get the address of the given symbol in the context of the set of
85 /// objects represented by the VModuleKey K. This call is forwarded to
86 /// the base layer's implementation.
87 /// @param K The VModuleKey associated with the object set to search in.
88 /// @param Name The name of the symbol to search for.
89 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
90 /// @return A handle for the given named symbol, if it is found in the
91 /// given object set.
92 JITSymbol findSymbolIn(VModuleKey K, const std::string &Name,
93 bool ExportedSymbolsOnly) {
94 return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly);
97 /// Immediately emit and finalize the object set represented by the
98 /// given VModuleKey K.
99 Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); }
101 /// Map section addresses for the objects associated with the
102 /// VModuleKey K.
103 void mapSectionAddress(VModuleKey K, const void *LocalAddress,
104 JITTargetAddress TargetAddr) {
105 BaseLayer.mapSectionAddress(K, LocalAddress, TargetAddr);
108 /// Access the transform functor directly.
109 TransformFtor &getTransform() { return Transform; }
111 /// Access the mumate functor directly.
112 const TransformFtor &getTransform() const { return Transform; }
114 private:
115 BaseLayerT &BaseLayer;
116 TransformFtor Transform;
119 template <typename BaseLayerT, typename TransformFtor>
120 LegacyObjectTransformLayer<BaseLayerT, TransformFtor>::
121 LegacyObjectTransformLayer(BaseLayerT &BaseLayer, TransformFtor Transform)
122 : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
124 } // end namespace orc
125 } // end namespace llvm
127 #endif // LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H