LLVM  16.0.0git
Layer.h
Go to the documentation of this file.
1 //===---------------- Layer.h -- Layer interfaces --------------*- 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 // Layer interfaces.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_ORC_LAYER_H
14 #define LLVM_EXECUTIONENGINE_ORC_LAYER_H
15 
19 #include "llvm/IR/Module.h"
20 #include "llvm/Support/Casting.h"
23 
24 namespace llvm {
25 namespace orc {
26 
27 /// IRMaterializationUnit is a convenient base class for MaterializationUnits
28 /// wrapping LLVM IR. Represents materialization responsibility for all symbols
29 /// in the given module. If symbols are overridden by other definitions, then
30 /// their linkage is changed to available-externally.
32 public:
33  using SymbolNameToDefinitionMap = std::map<SymbolStringPtr, GlobalValue *>;
34 
35  /// Create an IRMaterializationLayer. Scans the module to build the
36  /// SymbolFlags and SymbolToDefinition maps.
40 
41  /// Create an IRMaterializationLayer from a module, and pre-existing
42  /// SymbolFlags and SymbolToDefinition maps. The maps must provide
43  /// entries for each definition in M.
44  /// This constructor is useful for delegating work from one
45  /// IRMaterializationUnit to another.
48 
49  /// Return the ModuleIdentifier as the name for this MaterializationUnit.
50  StringRef getName() const override;
51 
52  /// Return a reference to the contained ThreadSafeModule.
53  const ThreadSafeModule &getModule() const { return TSM; }
54 
55 protected:
58 
59 private:
60  static SymbolStringPtr getInitSymbol(ExecutionSession &ES,
61  const ThreadSafeModule &TSM);
62 
63  void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
64 };
65 
66 /// Interface for layers that accept LLVM IR.
67 class IRLayer {
68 public:
70  : ES(ES), MO(MO) {}
71 
72  virtual ~IRLayer();
73 
74  /// Returns the ExecutionSession for this layer.
76 
77  /// Get the mangling options for this layer.
79  return MO;
80  }
81 
82  /// Sets the CloneToNewContextOnEmit flag (false by default).
83  ///
84  /// When set, IR modules added to this layer will be cloned on to a new
85  /// context before emit is called. This can be used by clients who want
86  /// to load all IR using one LLVMContext (to save memory via type and
87  /// constant uniquing), but want to move Modules to fresh contexts before
88  /// compiling them to enable concurrent compilation.
89  /// Single threaded clients, or clients who load every module on a new
90  /// context, need not set this.
91  void setCloneToNewContextOnEmit(bool CloneToNewContextOnEmit) {
92  this->CloneToNewContextOnEmit = CloneToNewContextOnEmit;
93  }
94 
95  /// Returns the current value of the CloneToNewContextOnEmit flag.
96  bool getCloneToNewContextOnEmit() const { return CloneToNewContextOnEmit; }
97 
98  /// Add a MaterializatinoUnit representing the given IR to the JITDylib
99  /// targeted by the given tracker.
100  virtual Error add(ResourceTrackerSP RT, ThreadSafeModule TSM);
101 
102  /// Adds a MaterializationUnit representing the given IR to the given
103  /// JITDylib. If RT is not specif
105  return add(JD.getDefaultResourceTracker(), std::move(TSM));
106  }
107 
108  /// Emit should materialize the given IR.
109  virtual void emit(std::unique_ptr<MaterializationResponsibility> R,
110  ThreadSafeModule TSM) = 0;
111 
112 private:
113  bool CloneToNewContextOnEmit = false;
114  ExecutionSession &ES;
116 };
117 
118 /// MaterializationUnit that materializes modules by calling the 'emit' method
119 /// on the given IRLayer.
121 public:
125 
126 private:
127  void materialize(std::unique_ptr<MaterializationResponsibility> R) override;
128 
129  IRLayer &L;
130 };
131 
132 /// Interface for Layers that accept object files.
133 class ObjectLayer : public RTTIExtends<ObjectLayer, RTTIRoot> {
134 public:
135  static char ID;
136 
138  virtual ~ObjectLayer();
139 
140  /// Returns the execution session for this layer.
142 
143  /// Adds a MaterializationUnit for the object file in the given memory buffer
144  /// to the JITDylib for the given ResourceTracker.
145  virtual Error add(ResourceTrackerSP RT, std::unique_ptr<MemoryBuffer> O,
147 
148  /// Adds a MaterializationUnit for the object file in the given memory buffer
149  /// to the JITDylib for the given ResourceTracker. The interface for the
150  /// object will be built using the default object interface builder.
151  Error add(ResourceTrackerSP RT, std::unique_ptr<MemoryBuffer> O);
152 
153  /// Adds a MaterializationUnit for the object file in the given memory buffer
154  /// to the given JITDylib.
155  Error add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O,
158  }
159 
160  /// Adds a MaterializationUnit for the object file in the given memory buffer
161  /// to the given JITDylib. The interface for the object will be built using
162  /// the default object interface builder.
163  Error add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O);
164 
165  /// Emit should materialize the given IR.
166  virtual void emit(std::unique_ptr<MaterializationResponsibility> R,
167  std::unique_ptr<MemoryBuffer> O) = 0;
168 
169 private:
170  ExecutionSession &ES;
171 };
172 
173 /// Materializes the given object file (represented by a MemoryBuffer
174 /// instance) by calling 'emit' on the given ObjectLayer.
176 public:
177  /// Create using the default object interface builder function.
179  Create(ObjectLayer &L, std::unique_ptr<MemoryBuffer> O);
180 
182  std::unique_ptr<MemoryBuffer> O,
183  Interface I);
184 
185  /// Return the buffer's identifier as the name for this MaterializationUnit.
186  StringRef getName() const override;
187 
188 private:
189  void materialize(std::unique_ptr<MaterializationResponsibility> R) override;
190  void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
191 
192  ObjectLayer &L;
193  std::unique_ptr<MemoryBuffer> O;
194 };
195 
196 } // End namespace orc
197 } // End namespace llvm
198 
199 #endif // LLVM_EXECUTIONENGINE_ORC_LAYER_H
MemoryBuffer.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::orc::JITDylib
Represents a JIT'd dynamic library.
Definition: Core.h:953
llvm::orc::ObjectLayer::~ObjectLayer
virtual ~ObjectLayer()
llvm::orc::IRMaterializationUnit
IRMaterializationUnit is a convenient base class for MaterializationUnits wrapping LLVM IR.
Definition: Layer.h:31
llvm::orc::JITDylib::getDefaultResourceTracker
ResourceTrackerSP getDefaultResourceTracker()
Get the default resource tracker for this JITDylib.
Definition: Core.cpp:661
llvm::orc::SymbolStringPtr
Pointer to a pooled string representing a symbol name.
Definition: SymbolStringPool.h:57
llvm::orc::ObjectLayer::add
virtual Error add(ResourceTrackerSP RT, std::unique_ptr< MemoryBuffer > O, MaterializationUnit::Interface I)
Adds a MaterializationUnit for the object file in the given memory buffer to the JITDylib for the giv...
Definition: Layer.cpp:163
ThreadSafeModule.h
Module.h
llvm::orc::IRMaterializationUnit::TSM
ThreadSafeModule TSM
Definition: Layer.h:56
llvm::orc::IRLayer::getManglingOptions
const IRSymbolMapper::ManglingOptions *& getManglingOptions() const
Get the mangling options for this layer.
Definition: Layer.h:78
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
ExtensibleRTTI.h
llvm::orc::MaterializationUnit
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group,...
Definition: Core.h:665
llvm::orc::BasicObjectLayerMaterializationUnit::getName
StringRef getName() const override
Return the buffer's identifier as the name for this MaterializationUnit.
Definition: Layer.cpp:205
llvm::orc::IRLayer::setCloneToNewContextOnEmit
void setCloneToNewContextOnEmit(bool CloneToNewContextOnEmit)
Sets the CloneToNewContextOnEmit flag (false by default).
Definition: Layer.h:91
llvm::orc::ObjectLayer::getExecutionSession
ExecutionSession & getExecutionSession()
Returns the execution session for this layer.
Definition: Layer.h:141
llvm::orc::ObjectLayer
Interface for Layers that accept object files.
Definition: Layer.h:133
llvm::orc::MaterializationUnit::Interface
Definition: Core.h:672
llvm::orc::IRMaterializationUnit::IRMaterializationUnit
IRMaterializationUnit(ExecutionSession &ES, const IRSymbolMapper::ManglingOptions &MO, ThreadSafeModule TSM)
Create an IRMaterializationLayer.
Definition: Layer.cpp:32
llvm::orc::IRMaterializationUnit::getModule
const ThreadSafeModule & getModule() const
Return a reference to the contained ThreadSafeModule.
Definition: Layer.h:53
llvm::RTTIExtends
Inheritance utility for extensible RTTI.
Definition: ExtensibleRTTI.h:114
llvm::orc::ObjectLayer::ID
static char ID
Definition: Layer.h:135
llvm::orc::IRLayer::add
virtual Error add(ResourceTrackerSP RT, ThreadSafeModule TSM)
Add a MaterializatinoUnit representing the given IR to the JITDylib targeted by the given tracker.
Definition: Layer.cpp:24
llvm::orc::IRLayer::emit
virtual void emit(std::unique_ptr< MaterializationResponsibility > R, ThreadSafeModule TSM)=0
Emit should materialize the given IR.
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:264
llvm::orc::BasicObjectLayerMaterializationUnit::Create
static Expected< std::unique_ptr< BasicObjectLayerMaterializationUnit > > Create(ObjectLayer &L, std::unique_ptr< MemoryBuffer > O)
Create using the default object interface builder function.
Definition: Layer.cpp:187
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::orc::ResourceTrackerSP
uint8_t IntrusiveRefCntPtr< ResourceTracker > ResourceTrackerSP
Definition: Core.h:47
llvm::orc::IRLayer::getCloneToNewContextOnEmit
bool getCloneToNewContextOnEmit() const
Returns the current value of the CloneToNewContextOnEmit flag.
Definition: Layer.h:96
llvm::orc::IRLayer::getExecutionSession
ExecutionSession & getExecutionSession()
Returns the ExecutionSession for this layer.
Definition: Layer.h:75
llvm::orc::IRLayer::add
Error add(JITDylib &JD, ThreadSafeModule TSM)
Adds a MaterializationUnit representing the given IR to the given JITDylib.
Definition: Layer.h:104
llvm::orc::BasicObjectLayerMaterializationUnit::BasicObjectLayerMaterializationUnit
BasicObjectLayerMaterializationUnit(ObjectLayer &L, std::unique_ptr< MemoryBuffer > O, Interface I)
Definition: Layer.cpp:201
llvm::orc::ObjectLayer::emit
virtual void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< MemoryBuffer > O)=0
Emit should materialize the given IR.
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::orc::BasicIRLayerMaterializationUnit::BasicIRLayerMaterializationUnit
BasicIRLayerMaterializationUnit(IRLayer &L, const IRSymbolMapper::ManglingOptions &MO, ThreadSafeModule TSM)
Definition: Layer.cpp:128
llvm::orc::BasicObjectLayerMaterializationUnit
Materializes the given object file (represented by a MemoryBuffer instance) by calling 'emit' on the ...
Definition: Layer.h:175
llvm::orc::IRSymbolMapper::ManglingOptions
Definition: Mangling.h:41
Mangling.h
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::orc::IRLayer::IRLayer
IRLayer(ExecutionSession &ES, const IRSymbolMapper::ManglingOptions *&MO)
Definition: Layer.h:69
llvm::orc::ExecutionSession
An ExecutionSession represents a running JIT program.
Definition: Core.h:1365
Casting.h
llvm::orc::IRLayer::~IRLayer
virtual ~IRLayer()
Core.h
llvm::orc::ThreadSafeModule
An LLVM Module together with a shared ThreadSafeContext.
Definition: ThreadSafeModule.h:77
llvm::orc::IRMaterializationUnit::SymbolToDefinition
SymbolNameToDefinitionMap SymbolToDefinition
Definition: Layer.h:57
llvm::orc::ObjectLayer::ObjectLayer
ObjectLayer(ExecutionSession &ES)
Definition: Layer.cpp:159
llvm::orc::BasicIRLayerMaterializationUnit
MaterializationUnit that materializes modules by calling the 'emit' method on the given IRLayer.
Definition: Layer.h:120
llvm::orc::IRMaterializationUnit::SymbolNameToDefinitionMap
std::map< SymbolStringPtr, GlobalValue * > SymbolNameToDefinitionMap
Definition: Layer.h:33
llvm::orc::IRMaterializationUnit::getName
StringRef getName() const override
Return the ModuleIdentifier as the name for this MaterializationUnit.
Definition: Layer.cpp:105
llvm::orc::IRLayer
Interface for layers that accept LLVM IR.
Definition: Layer.h:67
llvm::orc::ObjectLayer::add
Error add(JITDylib &JD, std::unique_ptr< MemoryBuffer > O, MaterializationUnit::Interface I)
Adds a MaterializationUnit for the object file in the given memory buffer to the given JITDylib.
Definition: Layer.h:155