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