Line data Source code
1 : //===- IRTransformLayer.h - Run all IR through a functor --------*- C++ -*-===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 : //
10 : // Run all IR passed in through a user supplied functor.
11 : //
12 : //===----------------------------------------------------------------------===//
13 :
14 : #ifndef LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
15 : #define LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
16 :
17 : #include "llvm/ExecutionEngine/JITSymbol.h"
18 : #include "llvm/ExecutionEngine/Orc/Layer.h"
19 : #include <memory>
20 : #include <string>
21 :
22 : namespace llvm {
23 : class Module;
24 : namespace orc {
25 :
26 : class IRTransformLayer : public IRLayer {
27 : public:
28 : using TransformFunction = std::function<Expected<ThreadSafeModule>(
29 : ThreadSafeModule, const MaterializationResponsibility &R)>;
30 :
31 : IRTransformLayer(ExecutionSession &ES, IRLayer &BaseLayer,
32 : TransformFunction Transform = identityTransform);
33 :
34 : void setTransform(TransformFunction Transform) {
35 13 : this->Transform = std::move(Transform);
36 : }
37 :
38 : void emit(MaterializationResponsibility R, ThreadSafeModule TSM) override;
39 :
40 : static ThreadSafeModule
41 0 : identityTransform(ThreadSafeModule TSM,
42 : const MaterializationResponsibility &R) {
43 0 : return TSM;
44 : }
45 :
46 : private:
47 : IRLayer &BaseLayer;
48 : TransformFunction Transform;
49 : };
50 :
51 : /// IR mutating layer.
52 : ///
53 : /// This layer applies a user supplied transform to each module that is added,
54 : /// then adds the transformed module to the layer below.
55 : template <typename BaseLayerT, typename TransformFtor>
56 : class LegacyIRTransformLayer {
57 : public:
58 :
59 : /// Construct an LegacyIRTransformLayer with the given BaseLayer
60 : LegacyIRTransformLayer(BaseLayerT &BaseLayer,
61 : TransformFtor Transform = TransformFtor())
62 : : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
63 :
64 : /// Apply the transform functor to the module, then add the module to
65 : /// the layer below, along with the memory manager and symbol resolver.
66 : ///
67 : /// @return A handle for the added modules.
68 : Error addModule(VModuleKey K, std::unique_ptr<Module> M) {
69 : return BaseLayer.addModule(std::move(K), Transform(std::move(M)));
70 : }
71 :
72 : /// Remove the module associated with the VModuleKey K.
73 : Error removeModule(VModuleKey K) { return BaseLayer.removeModule(K); }
74 :
75 : /// Search for the given named symbol.
76 : /// @param Name The name of the symbol to search for.
77 : /// @param ExportedSymbolsOnly If true, search only for exported symbols.
78 : /// @return A handle for the given named symbol, if it exists.
79 : JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
80 : return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
81 : }
82 :
83 : /// Get the address of the given symbol in the context of the module
84 : /// represented by the VModuleKey K. This call is forwarded to the base
85 : /// layer's implementation.
86 : /// @param K The VModuleKey for the module to search in.
87 : /// @param Name The name of the symbol to search for.
88 : /// @param ExportedSymbolsOnly If true, search only for exported symbols.
89 : /// @return A handle for the given named symbol, if it is found in the
90 : /// given module.
91 : JITSymbol findSymbolIn(VModuleKey K, const std::string &Name,
92 : bool ExportedSymbolsOnly) {
93 : return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly);
94 : }
95 :
96 : /// Immediately emit and finalize the module represented by the given
97 : /// VModuleKey.
98 : /// @param K The VModuleKey for the module to emit/finalize.
99 : Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); }
100 :
101 : /// Access the transform functor directly.
102 : TransformFtor& getTransform() { return Transform; }
103 :
104 : /// Access the mumate functor directly.
105 : const TransformFtor& getTransform() const { return Transform; }
106 :
107 : private:
108 : BaseLayerT &BaseLayer;
109 : TransformFtor Transform;
110 : };
111 :
112 : } // end namespace orc
113 : } // end namespace llvm
114 :
115 : #endif // LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
|