Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:53

0001 //===- LinkGraphLayer.h - Add LinkGraphs to an ExecutionSession -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // LinkGraphLayer and associated utilities.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_EXECUTIONENGINE_ORC_LINKGRAPHLAYER_H
0014 #define LLVM_EXECUTIONENGINE_ORC_LINKGRAPHLAYER_H
0015 
0016 #include "llvm/ExecutionEngine/JITLink/JITLink.h"
0017 #include "llvm/ExecutionEngine/Orc/Core.h"
0018 #include "llvm/Support/Error.h"
0019 #include "llvm/Support/MemoryBuffer.h"
0020 
0021 #include <atomic>
0022 #include <memory>
0023 
0024 namespace llvm::orc {
0025 
0026 class LinkGraphLayer {
0027 public:
0028   LinkGraphLayer(ExecutionSession &ES) : ES(ES) {}
0029 
0030   virtual ~LinkGraphLayer();
0031 
0032   ExecutionSession &getExecutionSession() { return ES; }
0033 
0034   /// Adds a LinkGraph to the JITDylib for the given ResourceTracker.
0035   virtual Error add(ResourceTrackerSP RT, std::unique_ptr<jitlink::LinkGraph> G,
0036                     MaterializationUnit::Interface I);
0037 
0038   /// Adds a LinkGraph to the JITDylib for the given ResourceTracker. The
0039   /// interface for the graph will be built using getLinkGraphInterface.
0040   Error add(ResourceTrackerSP RT, std::unique_ptr<jitlink::LinkGraph> G) {
0041     auto LGI = getInterface(*G);
0042     return add(std::move(RT), std::move(G), std::move(LGI));
0043   }
0044 
0045   /// Adds a LinkGraph to the given JITDylib.
0046   Error add(JITDylib &JD, std::unique_ptr<jitlink::LinkGraph> G,
0047             MaterializationUnit::Interface I) {
0048     return add(JD.getDefaultResourceTracker(), std::move(G), std::move(I));
0049   }
0050 
0051   /// Adds a LinkGraph to the given JITDylib. The interface for the object will
0052   /// be built using getLinkGraphInterface.
0053   Error add(JITDylib &JD, std::unique_ptr<jitlink::LinkGraph> G) {
0054     return add(JD.getDefaultResourceTracker(), std::move(G));
0055   }
0056 
0057   /// Emit should materialize the given IR.
0058   virtual void emit(std::unique_ptr<MaterializationResponsibility> R,
0059                     std::unique_ptr<jitlink::LinkGraph> G) = 0;
0060 
0061   /// Get the interface for the given LinkGraph.
0062   MaterializationUnit::Interface getInterface(jitlink::LinkGraph &G);
0063 
0064   /// Get the JITSymbolFlags for the given symbol.
0065   static JITSymbolFlags getJITSymbolFlagsForSymbol(jitlink::Symbol &Sym);
0066 
0067 private:
0068   ExecutionSession &ES;
0069   std::atomic<uint64_t> Counter{0};
0070 };
0071 
0072 /// MaterializationUnit for wrapping LinkGraphs.
0073 class LinkGraphMaterializationUnit : public MaterializationUnit {
0074 public:
0075   LinkGraphMaterializationUnit(LinkGraphLayer &LGLayer,
0076                                std::unique_ptr<jitlink::LinkGraph> G,
0077                                Interface I)
0078       : MaterializationUnit(I), LGLayer(LGLayer), G(std::move(G)) {}
0079 
0080   LinkGraphMaterializationUnit(LinkGraphLayer &LGLayer,
0081                                std::unique_ptr<jitlink::LinkGraph> G)
0082       : MaterializationUnit(LGLayer.getInterface(*G)), LGLayer(LGLayer),
0083         G(std::move(G)) {}
0084 
0085   StringRef getName() const override;
0086 
0087   void materialize(std::unique_ptr<MaterializationResponsibility> MR) override {
0088     LGLayer.emit(std::move(MR), std::move(G));
0089   }
0090 
0091 private:
0092   void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
0093 
0094   LinkGraphLayer &LGLayer;
0095   std::unique_ptr<jitlink::LinkGraph> G;
0096 };
0097 
0098 inline Error LinkGraphLayer::add(ResourceTrackerSP RT,
0099                                  std::unique_ptr<jitlink::LinkGraph> G,
0100                                  MaterializationUnit::Interface I) {
0101   auto &JD = RT->getJITDylib();
0102 
0103   return JD.define(std::make_unique<LinkGraphMaterializationUnit>(
0104                        *this, std::move(G), std::move(I)),
0105                    std::move(RT));
0106 }
0107 
0108 } // end namespace llvm::orc
0109 
0110 #endif // LLVM_EXECUTIONENGINE_ORC_LINKGRAPHLAYER_H