1 //===- IRTransformLayer.h - Run all IR through a functor --------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Run all IR passed in through a user supplied functor.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
14 #define LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
16 #include "llvm/ExecutionEngine/JITSymbol.h"
17 #include "llvm/ExecutionEngine/Orc/Layer.h"
25 class IRTransformLayer
: public IRLayer
{
27 using TransformFunction
= std::function
<Expected
<ThreadSafeModule
>(
28 ThreadSafeModule
, const MaterializationResponsibility
&R
)>;
30 IRTransformLayer(ExecutionSession
&ES
, IRLayer
&BaseLayer
,
31 TransformFunction Transform
= identityTransform
);
33 void setTransform(TransformFunction Transform
) {
34 this->Transform
= std::move(Transform
);
37 void emit(MaterializationResponsibility R
, ThreadSafeModule TSM
) override
;
39 static ThreadSafeModule
40 identityTransform(ThreadSafeModule TSM
,
41 const MaterializationResponsibility
&R
) {
47 TransformFunction Transform
;
50 /// IR mutating layer.
52 /// This layer applies a user supplied transform to each module that is added,
53 /// then adds the transformed module to the layer below.
54 template <typename BaseLayerT
, typename TransformFtor
>
55 class LegacyIRTransformLayer
{
58 /// Construct an LegacyIRTransformLayer with the given BaseLayer
59 LegacyIRTransformLayer(BaseLayerT
&BaseLayer
,
60 TransformFtor Transform
= TransformFtor())
61 : BaseLayer(BaseLayer
), Transform(std::move(Transform
)) {}
63 /// Apply the transform functor to the module, then add the module to
64 /// the layer below, along with the memory manager and symbol resolver.
66 /// @return A handle for the added modules.
67 Error
addModule(VModuleKey K
, std::unique_ptr
<Module
> M
) {
68 return BaseLayer
.addModule(std::move(K
), Transform(std::move(M
)));
71 /// Remove the module associated with the VModuleKey K.
72 Error
removeModule(VModuleKey K
) { return BaseLayer
.removeModule(K
); }
74 /// Search for the given named symbol.
75 /// @param Name The name of the symbol to search for.
76 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
77 /// @return A handle for the given named symbol, if it exists.
78 JITSymbol
findSymbol(const std::string
&Name
, bool ExportedSymbolsOnly
) {
79 return BaseLayer
.findSymbol(Name
, ExportedSymbolsOnly
);
82 /// Get the address of the given symbol in the context of the module
83 /// represented by the VModuleKey K. This call is forwarded to the base
84 /// layer's implementation.
85 /// @param K The VModuleKey for the module to search in.
86 /// @param Name The name of the symbol to search for.
87 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
88 /// @return A handle for the given named symbol, if it is found in the
90 JITSymbol
findSymbolIn(VModuleKey K
, const std::string
&Name
,
91 bool ExportedSymbolsOnly
) {
92 return BaseLayer
.findSymbolIn(K
, Name
, ExportedSymbolsOnly
);
95 /// Immediately emit and finalize the module represented by the given
97 /// @param K The VModuleKey for the module to emit/finalize.
98 Error
emitAndFinalize(VModuleKey K
) { return BaseLayer
.emitAndFinalize(K
); }
100 /// Access the transform functor directly.
101 TransformFtor
& getTransform() { return Transform
; }
103 /// Access the mumate functor directly.
104 const TransformFtor
& getTransform() const { return Transform
; }
107 BaseLayerT
&BaseLayer
;
108 TransformFtor Transform
;
111 } // end namespace orc
112 } // end namespace llvm
114 #endif // LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H