xref: /netbsd-src/external/apache2/llvm/dist/llvm/include/llvm/ExecutionEngine/Orc/IRTransformLayer.h (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 //===- IRTransformLayer.h - Run all IR through a 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 IR passed in through a user supplied functor.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
14 #define LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
15 
16 #include "llvm/ADT/FunctionExtras.h"
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 /// A layer that applies a transform to emitted modules.
27 /// The transform function is responsible for locking the ThreadSafeContext
28 /// before operating on the module.
29 class IRTransformLayer : public IRLayer {
30 public:
31   using TransformFunction = unique_function<Expected<ThreadSafeModule>(
32       ThreadSafeModule, MaterializationResponsibility &R)>;
33 
34   IRTransformLayer(ExecutionSession &ES, IRLayer &BaseLayer,
35                    TransformFunction Transform = identityTransform);
36 
setTransform(TransformFunction Transform)37   void setTransform(TransformFunction Transform) {
38     this->Transform = std::move(Transform);
39   }
40 
41   void emit(std::unique_ptr<MaterializationResponsibility> R,
42             ThreadSafeModule TSM) override;
43 
identityTransform(ThreadSafeModule TSM,MaterializationResponsibility & R)44   static ThreadSafeModule identityTransform(ThreadSafeModule TSM,
45                                             MaterializationResponsibility &R) {
46     return TSM;
47   }
48 
49 private:
50   IRLayer &BaseLayer;
51   TransformFunction Transform;
52 };
53 
54 } // end namespace orc
55 } // end namespace llvm
56 
57 #endif // LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
58