LLVM  8.0.1
Layer.h
Go to the documentation of this file.
1 //===---------------- Layer.h -- Layer interfaces --------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Layer interfaces.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_LAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_LAYER_H
16 
19 #include "llvm/IR/Module.h"
21 
22 namespace llvm {
23 namespace orc {
24 
25 /// Interface for layers that accept LLVM IR.
26 class IRLayer {
27 public:
29  virtual ~IRLayer();
30 
31  /// Returns the ExecutionSession for this layer.
33 
34  /// Sets the CloneToNewContextOnEmit flag (false by default).
35  ///
36  /// When set, IR modules added to this layer will be cloned on to a new
37  /// context before emit is called. This can be used by clients who want
38  /// to load all IR using one LLVMContext (to save memory via type and
39  /// constant uniquing), but want to move Modules to fresh contexts before
40  /// compiling them to enable concurrent compilation.
41  /// Single threaded clients, or clients who load every module on a new
42  /// context, need not set this.
43  void setCloneToNewContextOnEmit(bool CloneToNewContextOnEmit) {
44  this->CloneToNewContextOnEmit = CloneToNewContextOnEmit;
45  }
46 
47  /// Returns the current value of the CloneToNewContextOnEmit flag.
48  bool getCloneToNewContextOnEmit() const { return CloneToNewContextOnEmit; }
49 
50  /// Adds a MaterializationUnit representing the given IR to the given
51  /// JITDylib.
52  virtual Error add(JITDylib &JD, ThreadSafeModule TSM,
53  VModuleKey K = VModuleKey());
54 
55  /// Emit should materialize the given IR.
56  virtual void emit(MaterializationResponsibility R, ThreadSafeModule TSM) = 0;
57 
58 private:
59  bool CloneToNewContextOnEmit = false;
60  ExecutionSession &ES;
61 };
62 
63 /// IRMaterializationUnit is a convenient base class for MaterializationUnits
64 /// wrapping LLVM IR. Represents materialization responsibility for all symbols
65 /// in the given module. If symbols are overridden by other definitions, then
66 /// their linkage is changed to available-externally.
68 public:
69  using SymbolNameToDefinitionMap = std::map<SymbolStringPtr, GlobalValue *>;
70 
71  /// Create an IRMaterializationLayer. Scans the module to build the
72  /// SymbolFlags and SymbolToDefinition maps.
74  VModuleKey K);
75 
76  /// Create an IRMaterializationLayer from a module, and pre-existing
77  /// SymbolFlags and SymbolToDefinition maps. The maps must provide
78  /// entries for each definition in M.
79  /// This constructor is useful for delegating work from one
80  /// IRMaterializationUnit to another.
82  SymbolFlagsMap SymbolFlags,
83  SymbolNameToDefinitionMap SymbolToDefinition);
84 
85  /// Return the ModuleIdentifier as the name for this MaterializationUnit.
86  StringRef getName() const override;
87 
88  const ThreadSafeModule &getModule() const { return TSM; }
89 
90 protected:
93 
94 private:
95  void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
96 };
97 
98 /// MaterializationUnit that materializes modules by calling the 'emit' method
99 /// on the given IRLayer.
101 public:
103  ThreadSafeModule TSM);
104 
105 private:
106 
107  void materialize(MaterializationResponsibility R) override;
108 
109  IRLayer &L;
110  VModuleKey K;
111 };
112 
113 /// Interface for Layers that accept object files.
114 class ObjectLayer {
115 public:
117  virtual ~ObjectLayer();
118 
119  /// Returns the execution session for this layer.
121 
122  /// Adds a MaterializationUnit representing the given IR to the given
123  /// JITDylib.
124  virtual Error add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O,
125  VModuleKey K = VModuleKey());
126 
127  /// Emit should materialize the given IR.
128  virtual void emit(MaterializationResponsibility R,
129  std::unique_ptr<MemoryBuffer> O) = 0;
130 
131 private:
132  ExecutionSession &ES;
133 };
134 
135 /// Materializes the given object file (represented by a MemoryBuffer
136 /// instance) by calling 'emit' on the given ObjectLayer.
138 public:
140  Create(ObjectLayer &L, VModuleKey K, std::unique_ptr<MemoryBuffer> O);
141 
143  std::unique_ptr<MemoryBuffer> O,
144  SymbolFlagsMap SymbolFlags);
145 
146  /// Return the buffer's identifier as the name for this MaterializationUnit.
147  StringRef getName() const override;
148 
149 private:
150 
151  void materialize(MaterializationResponsibility R) override;
152  void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
153 
154  ObjectLayer &L;
155  std::unique_ptr<MemoryBuffer> O;
156 };
157 
158 /// Returns a SymbolFlagsMap for the object file represented by the given
159 /// buffer, or an error if the buffer does not contain a valid object file.
160 // FIXME: Maybe move to Core.h?
162  MemoryBufferRef ObjBuffer);
163 
164 } // End namespace orc
165 } // End namespace llvm
166 
167 #endif // LLVM_EXECUTIONENGINE_ORC_LAYER_H
bool getCloneToNewContextOnEmit() const
Returns the current value of the CloneToNewContextOnEmit flag.
Definition: Layer.h:48
IRLayer(ExecutionSession &ES)
Definition: Layer.cpp:19
This class represents lattice values for constants.
Definition: AllocatorList.h:24
SymbolNameToDefinitionMap SymbolToDefinition
Definition: Layer.h:92
uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:40
Materializes the given object file (represented by a MemoryBuffer instance) by calling &#39;emit&#39; on the ...
Definition: Layer.h:137
amdgpu Simplify well known AMD library false Value Value const Twine & Name
static StringRef getName(Value *V)
std::map< SymbolStringPtr, GlobalValue * > SymbolNameToDefinitionMap
Definition: Layer.h:69
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
virtual Error add(JITDylib &JD, ThreadSafeModule TSM, VModuleKey K=VModuleKey())
Adds a MaterializationUnit representing the given IR to the given JITDylib.
Definition: Layer.cpp:22
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:155
IRMaterializationUnit is a convenient base class for MaterializationUnits wrapping LLVM IR...
Definition: Layer.h:67
ExecutionSession & getExecutionSession()
Returns the execution session for this layer.
Definition: Layer.h:120
MaterializationUnit that materializes modules by calling the &#39;emit&#39; method on the given IRLayer...
Definition: Layer.h:100
ExecutionSession & getExecutionSession()
Returns the ExecutionSession for this layer.
Definition: Layer.h:32
Pointer to a pooled string representing a symbol name.
virtual void emit(MaterializationResponsibility R, ThreadSafeModule TSM)=0
Emit should materialize the given IR.
ThreadSafeModule TSM
Definition: Layer.h:91
Expected< SymbolFlagsMap > getObjectSymbolFlags(ExecutionSession &ES, MemoryBufferRef ObjBuffer)
Returns a SymbolFlagsMap for the object file represented by the given buffer, or an error if the buff...
Definition: Layer.cpp:155
void setCloneToNewContextOnEmit(bool CloneToNewContextOnEmit)
Sets the CloneToNewContextOnEmit flag (false by default).
Definition: Layer.h:43
An LLVM Module together with a shared ThreadSafeContext.
Interface for layers that accept LLVM IR.
Definition: Layer.h:26
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group...
Definition: Core.h:252
const ThreadSafeModule & getModule() const
Definition: Layer.h:88
Module.h This file contains the declarations for the Module class.
An ExecutionSession represents a running JIT program.
Definition: Core.h:697
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
virtual ~IRLayer()
Definition: Layer.cpp:20
Interface for Layers that accept object files.
Definition: Layer.h:114
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:496