LLVM  10.0.0
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 /// IRMaterializationUnit is a convenient base class for MaterializationUnits
25 /// wrapping LLVM IR. Represents materialization responsibility for all symbols
26 /// in the given module. If symbols are overridden by other definitions, then
27 /// their linkage is changed to available-externally.
29 public:
30  struct ManglingOptions {
31  bool EmulatedTLS = false;
32  };
33 
34  using SymbolNameToDefinitionMap = std::map<SymbolStringPtr, GlobalValue *>;
35 
36  /// Create an IRMaterializationLayer. Scans the module to build the
37  /// 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  const ThreadSafeModule &getModule() const { return TSM; }
54 
55 protected:
58 
59 private:
60  void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
61 };
62 
63 /// Interface for layers that accept LLVM IR.
64 class IRLayer {
65 public:
68  : ES(ES), MO(MO) {}
69 
70  virtual ~IRLayer();
71 
72  /// Returns the ExecutionSession for this layer.
74 
75  /// Get the mangling options for this layer.
77  return MO;
78  }
79 
80  /// Sets the CloneToNewContextOnEmit flag (false by default).
81  ///
82  /// When set, IR modules added to this layer will be cloned on to a new
83  /// context before emit is called. This can be used by clients who want
84  /// to load all IR using one LLVMContext (to save memory via type and
85  /// constant uniquing), but want to move Modules to fresh contexts before
86  /// compiling them to enable concurrent compilation.
87  /// Single threaded clients, or clients who load every module on a new
88  /// context, need not set this.
89  void setCloneToNewContextOnEmit(bool CloneToNewContextOnEmit) {
90  this->CloneToNewContextOnEmit = CloneToNewContextOnEmit;
91  }
92 
93  /// Returns the current value of the CloneToNewContextOnEmit flag.
94  bool getCloneToNewContextOnEmit() const { return CloneToNewContextOnEmit; }
95 
96  /// Adds a MaterializationUnit representing the given IR to the given
97  /// JITDylib.
98  virtual Error add(JITDylib &JD, ThreadSafeModule TSM,
99  VModuleKey K = VModuleKey());
100 
101  /// Emit should materialize the given IR.
102  virtual void emit(MaterializationResponsibility R, ThreadSafeModule TSM) = 0;
103 
104 private:
105  bool CloneToNewContextOnEmit = false;
106  ExecutionSession &ES;
108 };
109 
110 /// MaterializationUnit that materializes modules by calling the 'emit' method
111 /// on the given IRLayer.
113 public:
116 
117 private:
118 
119  void materialize(MaterializationResponsibility R) override;
120 
121  IRLayer &L;
122  VModuleKey K;
123 };
124 
125 /// Interface for Layers that accept object files.
126 class ObjectLayer {
127 public:
129  virtual ~ObjectLayer();
130 
131  /// Returns the execution session for this layer.
133 
134  /// Adds a MaterializationUnit representing the given IR to the given
135  /// JITDylib.
136  virtual Error add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O,
137  VModuleKey K = VModuleKey());
138 
139  /// Emit should materialize the given IR.
140  virtual void emit(MaterializationResponsibility R,
141  std::unique_ptr<MemoryBuffer> O) = 0;
142 
143 private:
144  ExecutionSession &ES;
145 };
146 
147 /// Materializes the given object file (represented by a MemoryBuffer
148 /// instance) by calling 'emit' on the given ObjectLayer.
150 public:
152  Create(ObjectLayer &L, VModuleKey K, std::unique_ptr<MemoryBuffer> O);
153 
155  std::unique_ptr<MemoryBuffer> O,
157 
158  /// Return the buffer's identifier as the name for this MaterializationUnit.
159  StringRef getName() const override;
160 
161 private:
162 
163  void materialize(MaterializationResponsibility R) override;
164  void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
165 
166  ObjectLayer &L;
167  std::unique_ptr<MemoryBuffer> O;
168 };
169 
170 /// Returns a SymbolFlagsMap for the object file represented by the given
171 /// buffer, or an error if the buffer does not contain a valid object file.
172 // FIXME: Maybe move to Core.h?
174  MemoryBufferRef ObjBuffer);
175 
176 } // End namespace orc
177 } // End namespace llvm
178 
179 #endif // LLVM_EXECUTIONENGINE_ORC_LAYER_H
bool getCloneToNewContextOnEmit() const
Returns the current value of the CloneToNewContextOnEmit flag.
Definition: Layer.h:94
This class represents lattice values for constants.
Definition: AllocatorList.h:23
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
StringRef getName() const override
Return the ModuleIdentifier as the name for this MaterializationUnit.
Definition: Layer.cpp:84
SymbolNameToDefinitionMap SymbolToDefinition
Definition: Layer.h:57
Materializes the given object file (represented by a MemoryBuffer instance) by calling &#39;emit&#39; on the ...
Definition: Layer.h:149
SymbolFlagsMap SymbolFlags
Definition: Core.h:580
std::map< SymbolStringPtr, GlobalValue * > SymbolNameToDefinitionMap
Definition: Layer.h:34
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:434
IRMaterializationUnit is a convenient base class for MaterializationUnits wrapping LLVM IR...
Definition: Layer.h:28
ExecutionSession & getExecutionSession()
Returns the execution session for this layer.
Definition: Layer.h:132
MaterializationUnit that materializes modules by calling the &#39;emit&#39; method on the given IRLayer...
Definition: Layer.h:112
ExecutionSession & getExecutionSession()
Returns the ExecutionSession for this layer.
Definition: Layer.h:73
const IRMaterializationUnit::ManglingOptions *& getManglingOptions() const
Get the mangling options for this layer.
Definition: Layer.h:76
Pointer to a pooled string representing a symbol name.
ThreadSafeModule TSM
Definition: Layer.h:56
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:187
IRMaterializationUnit(ExecutionSession &ES, const ManglingOptions &MO, ThreadSafeModule TSM, VModuleKey K)
Create an IRMaterializationLayer.
Definition: Layer.cpp:26
void setCloneToNewContextOnEmit(bool CloneToNewContextOnEmit)
Sets the CloneToNewContextOnEmit flag (false by default).
Definition: Layer.h:89
An LLVM Module together with a shared ThreadSafeContext.
Interface for layers that accept LLVM IR.
Definition: Layer.h:64
IRLayer(ExecutionSession &ES, const IRMaterializationUnit::ManglingOptions *&MO)
Definition: Layer.h:66
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group...
Definition: Core.h:550
const ThreadSafeModule & getModule() const
Definition: Layer.h:53
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
Definition: FileCheck.cpp:208
Module.h This file contains the declarations for the Module class.
An ExecutionSession represents a running JIT program.
Definition: Core.h:1058
uint8_t uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:42
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
Interface for Layers that accept object files.
Definition: Layer.h:126
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:774