xref: /llvm-project/llvm/include/llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h (revision b3d2548d5b04ed3b7aaedfd22e62da40875c0f31)
1 //===-- ObjectLinkingLayer.h - JITLink-based jit linking layer --*- 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 // Contains the definition for an JITLink-based, in-process object linking
10 // layer.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
16 
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ExecutionEngine/Orc/Core.h"
20 #include "llvm/ExecutionEngine/Orc/Layer.h"
21 #include "llvm/ExecutionEngine/Orc/LinkGraphLinkingLayer.h"
22 #include "llvm/Support/Error.h"
23 
24 #include <memory>
25 
26 namespace llvm {
27 
28 namespace jitlink {
29 class EHFrameRegistrar;
30 class LinkGraph;
31 class Symbol;
32 } // namespace jitlink
33 
34 namespace orc {
35 
36 /// An ObjectLayer implementation built on JITLink.
37 ///
38 /// Clients can use this class to add relocatable object files to an
39 /// ExecutionSession, and it typically serves as the base layer (underneath
40 /// a compiling layer like IRCompileLayer) for the rest of the JIT.
41 class ObjectLinkingLayer : public LinkGraphLinkingLayer,
42                            public RTTIExtends<ObjectLinkingLayer, ObjectLayer> {
43 private:
44   using BaseObjectLayer = RTTIExtends<ObjectLinkingLayer, ObjectLayer>;
45 
46 public:
47   static char ID;
48 
49   using ReturnObjectBufferFunction =
50       std::function<void(std::unique_ptr<MemoryBuffer>)>;
51 
52   /// Construct an ObjectLinkingLayer using the ExecutorProcessControl
53   /// instance's memory manager.
54   ObjectLinkingLayer(ExecutionSession &ES)
55       : LinkGraphLinkingLayer(ES), BaseObjectLayer(ES) {}
56 
57   /// Construct an ObjectLinkingLayer using a custom memory manager.
58   ObjectLinkingLayer(ExecutionSession &ES,
59                      jitlink::JITLinkMemoryManager &MemMgr)
60       : LinkGraphLinkingLayer(ES, MemMgr), BaseObjectLayer(ES) {}
61 
62   /// Construct an ObjectLinkingLayer. Takes ownership of the given
63   /// JITLinkMemoryManager. This method is a temporary hack to simplify
64   /// co-existence with RTDyldObjectLinkingLayer (which also owns its
65   /// allocators).
66   ObjectLinkingLayer(ExecutionSession &ES,
67                      std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr)
68       : LinkGraphLinkingLayer(ES, std::move(MemMgr)), BaseObjectLayer(ES) {}
69 
70   using LinkGraphLinkingLayer::getExecutionSession;
71 
72   /// Set an object buffer return function. By default object buffers are
73   /// deleted once the JIT has linked them. If a return function is set then
74   /// it will be called to transfer ownership of the buffer instead.
75   void setReturnObjectBuffer(ReturnObjectBufferFunction ReturnObjectBuffer) {
76     this->ReturnObjectBuffer = std::move(ReturnObjectBuffer);
77   }
78 
79   using LinkGraphLinkingLayer::add;
80   using LinkGraphLinkingLayer::emit;
81 
82   using ObjectLayer::add;
83 
84   /// Emit an object file.
85   void emit(std::unique_ptr<MaterializationResponsibility> R,
86             std::unique_ptr<MemoryBuffer> O) override;
87 };
88 
89 } // end namespace orc
90 } // end namespace llvm
91 
92 #endif // LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
93