1 //===- LinkGraphLayer.h - Add LinkGraphs to an ExecutionSession -*- 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 // LinkGraphLayer and associated utilities.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_EXECUTIONENGINE_ORC_LINKGRAPHLAYER_H
14 #define LLVM_EXECUTIONENGINE_ORC_LINKGRAPHLAYER_H
15
16 #include "llvm/ExecutionEngine/JITLink/JITLink.h"
17 #include "llvm/ExecutionEngine/Orc/Core.h"
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/Error.h"
20 #include "llvm/Support/MemoryBuffer.h"
21
22 #include <atomic>
23 #include <memory>
24
25 namespace llvm::orc {
26
27 class LLVM_ABI LinkGraphLayer {
28 public:
LinkGraphLayer(ExecutionSession & ES)29 LinkGraphLayer(ExecutionSession &ES) : ES(ES) {}
30
31 virtual ~LinkGraphLayer();
32
getExecutionSession()33 ExecutionSession &getExecutionSession() { return ES; }
34
35 /// Adds a LinkGraph to the JITDylib for the given ResourceTracker.
36 virtual Error add(ResourceTrackerSP RT, std::unique_ptr<jitlink::LinkGraph> G,
37 MaterializationUnit::Interface I);
38
39 /// Adds a LinkGraph to the JITDylib for the given ResourceTracker. The
40 /// interface for the graph will be built using getLinkGraphInterface.
add(ResourceTrackerSP RT,std::unique_ptr<jitlink::LinkGraph> G)41 Error add(ResourceTrackerSP RT, std::unique_ptr<jitlink::LinkGraph> G) {
42 auto LGI = getInterface(*G);
43 return add(std::move(RT), std::move(G), std::move(LGI));
44 }
45
46 /// Adds a LinkGraph to the given JITDylib.
add(JITDylib & JD,std::unique_ptr<jitlink::LinkGraph> G,MaterializationUnit::Interface I)47 Error add(JITDylib &JD, std::unique_ptr<jitlink::LinkGraph> G,
48 MaterializationUnit::Interface I) {
49 return add(JD.getDefaultResourceTracker(), std::move(G), std::move(I));
50 }
51
52 /// Adds a LinkGraph to the given JITDylib. The interface for the object will
53 /// be built using getLinkGraphInterface.
add(JITDylib & JD,std::unique_ptr<jitlink::LinkGraph> G)54 Error add(JITDylib &JD, std::unique_ptr<jitlink::LinkGraph> G) {
55 return add(JD.getDefaultResourceTracker(), std::move(G));
56 }
57
58 /// Emit should materialize the given IR.
59 virtual void emit(std::unique_ptr<MaterializationResponsibility> R,
60 std::unique_ptr<jitlink::LinkGraph> G) = 0;
61
62 /// Get the interface for the given LinkGraph.
63 MaterializationUnit::Interface getInterface(jitlink::LinkGraph &G);
64
65 /// Get the JITSymbolFlags for the given symbol.
66 static JITSymbolFlags getJITSymbolFlagsForSymbol(jitlink::Symbol &Sym);
67
68 private:
69 ExecutionSession &ES;
70 std::atomic<uint64_t> Counter{0};
71 };
72
73 /// MaterializationUnit for wrapping LinkGraphs.
74 class LLVM_ABI LinkGraphMaterializationUnit : public MaterializationUnit {
75 public:
LinkGraphMaterializationUnit(LinkGraphLayer & LGLayer,std::unique_ptr<jitlink::LinkGraph> G,Interface I)76 LinkGraphMaterializationUnit(LinkGraphLayer &LGLayer,
77 std::unique_ptr<jitlink::LinkGraph> G,
78 Interface I)
79 : MaterializationUnit(I), LGLayer(LGLayer), G(std::move(G)) {}
80
LinkGraphMaterializationUnit(LinkGraphLayer & LGLayer,std::unique_ptr<jitlink::LinkGraph> G)81 LinkGraphMaterializationUnit(LinkGraphLayer &LGLayer,
82 std::unique_ptr<jitlink::LinkGraph> G)
83 : MaterializationUnit(LGLayer.getInterface(*G)), LGLayer(LGLayer),
84 G(std::move(G)) {}
85
86 StringRef getName() const override;
87
materialize(std::unique_ptr<MaterializationResponsibility> MR)88 void materialize(std::unique_ptr<MaterializationResponsibility> MR) override {
89 LGLayer.emit(std::move(MR), std::move(G));
90 }
91
92 private:
93 void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
94
95 LinkGraphLayer &LGLayer;
96 std::unique_ptr<jitlink::LinkGraph> G;
97 };
98
add(ResourceTrackerSP RT,std::unique_ptr<jitlink::LinkGraph> G,MaterializationUnit::Interface I)99 inline Error LinkGraphLayer::add(ResourceTrackerSP RT,
100 std::unique_ptr<jitlink::LinkGraph> G,
101 MaterializationUnit::Interface I) {
102 auto &JD = RT->getJITDylib();
103
104 return JD.define(std::make_unique<LinkGraphMaterializationUnit>(
105 *this, std::move(G), std::move(I)),
106 std::move(RT));
107 }
108
109 } // end namespace llvm::orc
110
111 #endif // LLVM_EXECUTIONENGINE_ORC_LINKGRAPHLAYER_H
112