Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===---------------- Layer.h -- Layer interfaces --------------*- 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 // Layer interfaces.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_EXECUTIONENGINE_ORC_LAYER_H
0014 #define LLVM_EXECUTIONENGINE_ORC_LAYER_H
0015 
0016 #include "llvm/ExecutionEngine/Orc/Core.h"
0017 #include "llvm/ExecutionEngine/Orc/Mangling.h"
0018 #include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
0019 #include "llvm/IR/Module.h"
0020 #include "llvm/Support/Casting.h"
0021 #include "llvm/Support/ExtensibleRTTI.h"
0022 #include "llvm/Support/MemoryBuffer.h"
0023 
0024 namespace llvm {
0025 namespace orc {
0026 
0027 /// IRMaterializationUnit is a convenient base class for MaterializationUnits
0028 /// wrapping LLVM IR. Represents materialization responsibility for all symbols
0029 /// in the given module. If symbols are overridden by other definitions, then
0030 /// their linkage is changed to available-externally.
0031 class IRMaterializationUnit : public MaterializationUnit {
0032 public:
0033   using SymbolNameToDefinitionMap = std::map<SymbolStringPtr, GlobalValue *>;
0034 
0035   /// Create an IRMaterializationLayer. Scans the module to build the
0036   /// SymbolFlags and SymbolToDefinition maps.
0037   IRMaterializationUnit(ExecutionSession &ES,
0038                         const IRSymbolMapper::ManglingOptions &MO,
0039                         ThreadSafeModule TSM);
0040 
0041   /// Create an IRMaterializationLayer from a module, and pre-existing
0042   /// SymbolFlags and SymbolToDefinition maps. The maps must provide
0043   /// entries for each definition in M.
0044   /// This constructor is useful for delegating work from one
0045   /// IRMaterializationUnit to another.
0046   IRMaterializationUnit(ThreadSafeModule TSM, Interface I,
0047                         SymbolNameToDefinitionMap SymbolToDefinition);
0048 
0049   /// Return the ModuleIdentifier as the name for this MaterializationUnit.
0050   StringRef getName() const override;
0051 
0052   /// Return a reference to the contained ThreadSafeModule.
0053   const ThreadSafeModule &getModule() const { return TSM; }
0054 
0055 protected:
0056   ThreadSafeModule TSM;
0057   SymbolNameToDefinitionMap SymbolToDefinition;
0058 
0059 private:
0060   static SymbolStringPtr getInitSymbol(ExecutionSession &ES,
0061                                        const ThreadSafeModule &TSM);
0062 
0063   void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
0064 };
0065 
0066 /// Interface for layers that accept LLVM IR.
0067 class IRLayer {
0068 public:
0069   IRLayer(ExecutionSession &ES, const IRSymbolMapper::ManglingOptions *&MO)
0070       : ES(ES), MO(MO) {}
0071 
0072   virtual ~IRLayer();
0073 
0074   /// Returns the ExecutionSession for this layer.
0075   ExecutionSession &getExecutionSession() { return ES; }
0076 
0077   /// Get the mangling options for this layer.
0078   const IRSymbolMapper::ManglingOptions *&getManglingOptions() const {
0079     return MO;
0080   }
0081 
0082   /// Sets the CloneToNewContextOnEmit flag (false by default).
0083   ///
0084   /// When set, IR modules added to this layer will be cloned on to a new
0085   /// context before emit is called. This can be used by clients who want
0086   /// to load all IR using one LLVMContext (to save memory via type and
0087   /// constant uniquing), but want to move Modules to fresh contexts before
0088   /// compiling them to enable concurrent compilation.
0089   /// Single threaded clients, or clients who load every module on a new
0090   /// context, need not set this.
0091   void setCloneToNewContextOnEmit(bool CloneToNewContextOnEmit) {
0092     this->CloneToNewContextOnEmit = CloneToNewContextOnEmit;
0093   }
0094 
0095   /// Returns the current value of the CloneToNewContextOnEmit flag.
0096   bool getCloneToNewContextOnEmit() const { return CloneToNewContextOnEmit; }
0097 
0098   /// Add a MaterializatinoUnit representing the given IR to the JITDylib
0099   /// targeted by the given tracker.
0100   virtual Error add(ResourceTrackerSP RT, ThreadSafeModule TSM);
0101 
0102   /// Adds a MaterializationUnit representing the given IR to the given
0103   /// JITDylib. If RT is not specif
0104   Error add(JITDylib &JD, ThreadSafeModule TSM) {
0105     return add(JD.getDefaultResourceTracker(), std::move(TSM));
0106   }
0107 
0108   /// Emit should materialize the given IR.
0109   virtual void emit(std::unique_ptr<MaterializationResponsibility> R,
0110                     ThreadSafeModule TSM) = 0;
0111 
0112 private:
0113   bool CloneToNewContextOnEmit = false;
0114   ExecutionSession &ES;
0115   const IRSymbolMapper::ManglingOptions *&MO;
0116 };
0117 
0118 /// MaterializationUnit that materializes modules by calling the 'emit' method
0119 /// on the given IRLayer.
0120 class BasicIRLayerMaterializationUnit : public IRMaterializationUnit {
0121 public:
0122   BasicIRLayerMaterializationUnit(IRLayer &L,
0123                                   const IRSymbolMapper::ManglingOptions &MO,
0124                                   ThreadSafeModule TSM);
0125 
0126 private:
0127   void materialize(std::unique_ptr<MaterializationResponsibility> R) override;
0128 
0129   IRLayer &L;
0130 };
0131 
0132 /// Interface for Layers that accept object files.
0133 class ObjectLayer : public RTTIExtends<ObjectLayer, RTTIRoot> {
0134 public:
0135   static char ID;
0136 
0137   ObjectLayer(ExecutionSession &ES);
0138   virtual ~ObjectLayer();
0139 
0140   /// Returns the execution session for this layer.
0141   ExecutionSession &getExecutionSession() { return ES; }
0142 
0143   /// Adds a MaterializationUnit for the object file in the given memory buffer
0144   /// to the JITDylib for the given ResourceTracker.
0145   virtual Error add(ResourceTrackerSP RT, std::unique_ptr<MemoryBuffer> O,
0146                     MaterializationUnit::Interface I);
0147 
0148   /// Adds a MaterializationUnit for the object file in the given memory buffer
0149   /// to the JITDylib for the given ResourceTracker. The interface for the
0150   /// object will be built using the default object interface builder.
0151   Error add(ResourceTrackerSP RT, std::unique_ptr<MemoryBuffer> O);
0152 
0153   /// Adds a MaterializationUnit for the object file in the given memory buffer
0154   /// to the given JITDylib.
0155   Error add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O,
0156             MaterializationUnit::Interface I) {
0157     return add(JD.getDefaultResourceTracker(), std::move(O), std::move(I));
0158   }
0159 
0160   /// Adds a MaterializationUnit for the object file in the given memory buffer
0161   /// to the given JITDylib. The interface for the object will be built using
0162   /// the default object interface builder.
0163   Error add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O);
0164 
0165   /// Emit should materialize the given IR.
0166   virtual void emit(std::unique_ptr<MaterializationResponsibility> R,
0167                     std::unique_ptr<MemoryBuffer> O) = 0;
0168 
0169 private:
0170   ExecutionSession &ES;
0171 };
0172 
0173 /// Materializes the given object file (represented by a MemoryBuffer
0174 /// instance) by calling 'emit' on the given ObjectLayer.
0175 class BasicObjectLayerMaterializationUnit : public MaterializationUnit {
0176 public:
0177   /// Create using the default object interface builder function.
0178   static Expected<std::unique_ptr<BasicObjectLayerMaterializationUnit>>
0179   Create(ObjectLayer &L, std::unique_ptr<MemoryBuffer> O);
0180 
0181   BasicObjectLayerMaterializationUnit(ObjectLayer &L,
0182                                       std::unique_ptr<MemoryBuffer> O,
0183                                       Interface I);
0184 
0185   /// Return the buffer's identifier as the name for this MaterializationUnit.
0186   StringRef getName() const override;
0187 
0188 private:
0189   void materialize(std::unique_ptr<MaterializationResponsibility> R) override;
0190   void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
0191 
0192   ObjectLayer &L;
0193   std::unique_ptr<MemoryBuffer> O;
0194 };
0195 
0196 } // End namespace orc
0197 } // End namespace llvm
0198 
0199 #endif // LLVM_EXECUTIONENGINE_ORC_LAYER_H