LLVM  14.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.
49 
50  /// Return the ModuleIdentifier as the name for this MaterializationUnit.
51  StringRef getName() const override;
52 
53  /// Return a reference to the contained ThreadSafeModule.
54  const ThreadSafeModule &getModule() const { return TSM; }
55 
56 protected:
59 
60 private:
61  static SymbolStringPtr getInitSymbol(ExecutionSession &ES,
62  const ThreadSafeModule &TSM);
63 
64  void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
65 };
66 
67 /// Interface for layers that accept LLVM IR.
68 class IRLayer {
69 public:
71  : ES(ES), MO(MO) {}
72 
73  virtual ~IRLayer();
74 
75  /// Returns the ExecutionSession for this layer.
77 
78  /// Get the mangling options for this layer.
80  return MO;
81  }
82 
83  /// Sets the CloneToNewContextOnEmit flag (false by default).
84  ///
85  /// When set, IR modules added to this layer will be cloned on to a new
86  /// context before emit is called. This can be used by clients who want
87  /// to load all IR using one LLVMContext (to save memory via type and
88  /// constant uniquing), but want to move Modules to fresh contexts before
89  /// compiling them to enable concurrent compilation.
90  /// Single threaded clients, or clients who load every module on a new
91  /// context, need not set this.
92  void setCloneToNewContextOnEmit(bool CloneToNewContextOnEmit) {
93  this->CloneToNewContextOnEmit = CloneToNewContextOnEmit;
94  }
95 
96  /// Returns the current value of the CloneToNewContextOnEmit flag.
97  bool getCloneToNewContextOnEmit() const { return CloneToNewContextOnEmit; }
98 
99  /// Add a MaterializatinoUnit representing the given IR to the JITDylib
100  /// targeted by the given tracker.
101  virtual Error add(ResourceTrackerSP RT, ThreadSafeModule TSM);
102 
103  /// Adds a MaterializationUnit representing the given IR to the given
104  /// JITDylib. If RT is not specif
106  return add(JD.getDefaultResourceTracker(), std::move(TSM));
107  }
108 
109  /// Emit should materialize the given IR.
110  virtual void emit(std::unique_ptr<MaterializationResponsibility> R,
111  ThreadSafeModule TSM) = 0;
112 
113 private:
114  bool CloneToNewContextOnEmit = false;
115  ExecutionSession &ES;
117 };
118 
119 /// MaterializationUnit that materializes modules by calling the 'emit' method
120 /// on the given IRLayer.
122 public:
126 
127 private:
128  void materialize(std::unique_ptr<MaterializationResponsibility> R) override;
129 
130  IRLayer &L;
131 };
132 
133 /// Interface for Layers that accept object files.
134 class ObjectLayer : public RTTIExtends<ObjectLayer, RTTIRoot> {
135 public:
136  static char ID;
137 
139  virtual ~ObjectLayer();
140 
141  /// Returns the execution session for this layer.
143 
144  /// Adds a MaterializationUnit representing the given IR to the given
145  /// JITDylib.
146  virtual Error add(ResourceTrackerSP RT, std::unique_ptr<MemoryBuffer> O);
147 
148  Error add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O) {
149  return add(JD.getDefaultResourceTracker(), std::move(O));
150  }
151 
152  /// Emit should materialize the given IR.
153  virtual void emit(std::unique_ptr<MaterializationResponsibility> R,
154  std::unique_ptr<MemoryBuffer> O) = 0;
155 
156 private:
157  ExecutionSession &ES;
158 };
159 
160 /// Materializes the given object file (represented by a MemoryBuffer
161 /// instance) by calling 'emit' on the given ObjectLayer.
163 public:
165  Create(ObjectLayer &L, std::unique_ptr<MemoryBuffer> O);
166 
168  std::unique_ptr<MemoryBuffer> O,
171 
172  /// Return the buffer's identifier as the name for this MaterializationUnit.
173  StringRef getName() const override;
174 
175 private:
176  void materialize(std::unique_ptr<MaterializationResponsibility> R) override;
177  void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
178 
179  ObjectLayer &L;
180  std::unique_ptr<MemoryBuffer> O;
181 };
182 
183 } // End namespace orc
184 } // End namespace llvm
185 
186 #endif // LLVM_EXECUTIONENGINE_ORC_LAYER_H
MemoryBuffer.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::orc::JITDylib
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:922
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:629
llvm::orc::SymbolStringPtr
Pointer to a pooled string representing a symbol name.
Definition: SymbolStringPool.h:50
ThreadSafeModule.h
Module.h
llvm::orc::IRMaterializationUnit::TSM
ThreadSafeModule TSM
Definition: Layer.h:57
llvm::orc::IRLayer::getManglingOptions
const IRSymbolMapper::ManglingOptions *& getManglingOptions() const
Get the mangling options for this layer.
Definition: Layer.h:79
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
ExtensibleRTTI.h
llvm::orc::MaterializationUnit
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group,...
Definition: Core.h:664
llvm::orc::ObjectLayer::add
Error add(JITDylib &JD, std::unique_ptr< MemoryBuffer > O)
Definition: Layer.h:148
llvm::orc::BasicObjectLayerMaterializationUnit::getName
StringRef getName() const override
Return the buffer's identifier as the name for this MaterializationUnit.
Definition: Layer.cpp:196
llvm::orc::IRLayer::setCloneToNewContextOnEmit
void setCloneToNewContextOnEmit(bool CloneToNewContextOnEmit)
Sets the CloneToNewContextOnEmit flag (false by default).
Definition: Layer.h:92
llvm::orc::ObjectLayer::getExecutionSession
ExecutionSession & getExecutionSession()
Returns the execution session for this layer.
Definition: Layer.h:142
llvm::orc::ObjectLayer
Interface for Layers that accept object files.
Definition: Layer.h:134
llvm::orc::ObjectLayer::ID
static char ID
Definition: Layer.h:136
llvm::orc::IRMaterializationUnit::IRMaterializationUnit
IRMaterializationUnit(ExecutionSession &ES, const IRSymbolMapper::ManglingOptions &MO, ThreadSafeModule TSM)
Create an IRMaterializationLayer.
Definition: Layer.cpp:33
llvm::orc::IRMaterializationUnit::getModule
const ThreadSafeModule & getModule() const
Return a reference to the contained ThreadSafeModule.
Definition: Layer.h:54
llvm::RTTIExtends
Inheritance utility for extensible RTTI.
Definition: ExtensibleRTTI.h:65
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:25
llvm::orc::IRLayer::emit
virtual void emit(std::unique_ptr< MaterializationResponsibility > R, ThreadSafeModule TSM)=0
Emit should materialize the given IR.
llvm::orc::ObjectLayer::~ObjectLayer
virtual ~ObjectLayer()
Definition: Layer.cpp:162
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:206
llvm::orc::BasicObjectLayerMaterializationUnit::Create
static Expected< std::unique_ptr< BasicObjectLayerMaterializationUnit > > Create(ObjectLayer &L, std::unique_ptr< MemoryBuffer > O)
Definition: Layer.cpp:174
llvm::orc::BasicObjectLayerMaterializationUnit::BasicObjectLayerMaterializationUnit
BasicObjectLayerMaterializationUnit(ObjectLayer &L, std::unique_ptr< MemoryBuffer > O, SymbolFlagsMap SymbolFlags, SymbolStringPtr InitSymbol)
Definition: Layer.cpp:190
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
llvm::DenseMap< SymbolStringPtr, JITSymbolFlags >
llvm::orc::ResourceTrackerSP
uint8_t IntrusiveRefCntPtr< ResourceTracker > ResourceTrackerSP
Definition: Core.h:47
llvm::orc::ObjectLayer::add
virtual Error add(ResourceTrackerSP RT, std::unique_ptr< MemoryBuffer > O)
Adds a MaterializationUnit representing the given IR to the given JITDylib.
Definition: Layer.cpp:164
llvm::orc::IRLayer::getCloneToNewContextOnEmit
bool getCloneToNewContextOnEmit() const
Returns the current value of the CloneToNewContextOnEmit flag.
Definition: Layer.h:97
llvm::orc::IRLayer::getExecutionSession
ExecutionSession & getExecutionSession()
Returns the ExecutionSession for this layer.
Definition: Layer.h:76
llvm::orc::IRLayer::add
Error add(JITDylib &JD, ThreadSafeModule TSM)
Adds a MaterializationUnit representing the given IR to the given JITDylib.
Definition: Layer.h:105
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:57
llvm::orc::BasicIRLayerMaterializationUnit::BasicIRLayerMaterializationUnit
BasicIRLayerMaterializationUnit(IRLayer &L, const IRSymbolMapper::ManglingOptions &MO, ThreadSafeModule TSM)
Definition: Layer.cpp:129
llvm::orc::BasicObjectLayerMaterializationUnit
Materializes the given object file (represented by a MemoryBuffer instance) by calling 'emit' on the ...
Definition: Layer.h:162
llvm::orc::IRSymbolMapper::ManglingOptions
Definition: Mangling.h:41
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
Mangling.h
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::orc::IRLayer::IRLayer
IRLayer(ExecutionSession &ES, const IRSymbolMapper::ManglingOptions *&MO)
Definition: Layer.h:70
llvm::orc::ExecutionSession
An ExecutionSession represents a running JIT program.
Definition: Core.h:1271
Casting.h
llvm::orc::IRLayer::~IRLayer
virtual ~IRLayer()
Definition: Layer.cpp:23
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:58
llvm::orc::ObjectLayer::ObjectLayer
ObjectLayer(ExecutionSession &ES)
Definition: Layer.cpp:160
llvm::orc::MaterializationUnit::SymbolFlags
SymbolFlagsMap SymbolFlags
Definition: Core.h:705
llvm::orc::BasicIRLayerMaterializationUnit
MaterializationUnit that materializes modules by calling the 'emit' method on the given IRLayer.
Definition: Layer.h:121
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:106
llvm::orc::IRLayer
Interface for layers that accept LLVM IR.
Definition: Layer.h:68
llvm::orc::MaterializationUnit::InitSymbol
SymbolStringPtr InitSymbol
Definition: Core.h:706