LLVM  8.0.1
Layer.cpp
Go to the documentation of this file.
1 //===-------------------- Layer.cpp - Layer interfaces --------------------===//
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 
11 #include "llvm/Object/ObjectFile.h"
12 #include "llvm/Support/Debug.h"
13 
14 #define DEBUG_TYPE "orc"
15 
16 namespace llvm {
17 namespace orc {
18 
21 
23  return JD.define(llvm::make_unique<BasicIRLayerMaterializationUnit>(
24  *this, std::move(K), std::move(TSM)));
25 }
26 
29  : MaterializationUnit(SymbolFlagsMap(), std::move(K)), TSM(std::move(TSM)) {
30 
31  assert(this->TSM && "Module must not be null");
32 
33  MangleAndInterner Mangle(ES, this->TSM.getModule()->getDataLayout());
34  for (auto &G : this->TSM.getModule()->global_values()) {
35  if (G.hasName() && !G.isDeclaration() && !G.hasLocalLinkage() &&
36  !G.hasAvailableExternallyLinkage() && !G.hasAppendingLinkage()) {
37  auto MangledName = Mangle(G.getName());
39  SymbolToDefinition[MangledName] = &G;
40  }
41  }
42 }
43 
47  : MaterializationUnit(std::move(SymbolFlags), std::move(K)),
48  TSM(std::move(TSM)), SymbolToDefinition(std::move(SymbolToDefinition)) {}
49 
51  if (TSM.getModule())
52  return TSM.getModule()->getModuleIdentifier();
53  return "<null module>";
54 }
55 
56 void IRMaterializationUnit::discard(const JITDylib &JD,
57  const SymbolStringPtr &Name) {
59  dbgs() << "In " << JD.getName() << " discarding " << *Name << " from MU@"
60  << this << " (" << getName() << ")\n";
61  }););
62 
63  auto I = SymbolToDefinition.find(Name);
64  assert(I != SymbolToDefinition.end() &&
65  "Symbol not provided by this MU, or previously discarded");
66  assert(!I->second->isDeclaration() &&
67  "Discard should only apply to definitions");
68  I->second->setLinkage(GlobalValue::AvailableExternallyLinkage);
69  SymbolToDefinition.erase(I);
70 }
71 
74  : IRMaterializationUnit(L.getExecutionSession(), std::move(TSM),
75  std::move(K)),
76  L(L), K(std::move(K)) {}
77 
78 void BasicIRLayerMaterializationUnit::materialize(
80 
81  // Throw away the SymbolToDefinition map: it's not usable after we hand
82  // off the module.
83  SymbolToDefinition.clear();
84 
85  // If cloneToNewContextOnEmit is set, clone the module now.
88 
89 #ifndef NDEBUG
90  auto &ES = R.getTargetJITDylib().getExecutionSession();
91 #endif // NDEBUG
92 
93  auto Lock = TSM.getContextLock();
94  LLVM_DEBUG(ES.runSessionLocked([&]() {
95  dbgs() << "Emitting, for " << R.getTargetJITDylib().getName() << ", "
96  << *this << "\n";
97  }););
98  L.emit(std::move(R), std::move(TSM));
99  LLVM_DEBUG(ES.runSessionLocked([&]() {
100  dbgs() << "Finished emitting, for " << R.getTargetJITDylib().getName()
101  << ", " << *this << "\n";
102  }););
103 }
104 
106 
108 
109 Error ObjectLayer::add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O,
110  VModuleKey K) {
111  auto ObjMU = BasicObjectLayerMaterializationUnit::Create(*this, std::move(K),
112  std::move(O));
113  if (!ObjMU)
114  return ObjMU.takeError();
115  return JD.define(std::move(*ObjMU));
116 }
117 
120  std::unique_ptr<MemoryBuffer> O) {
121  auto SymbolFlags =
122  getObjectSymbolFlags(L.getExecutionSession(), O->getMemBufferRef());
123 
124  if (!SymbolFlags)
125  return SymbolFlags.takeError();
126 
127  return std::unique_ptr<BasicObjectLayerMaterializationUnit>(
128  new BasicObjectLayerMaterializationUnit(L, K, std::move(O),
129  std::move(*SymbolFlags)));
130 }
131 
133  ObjectLayer &L, VModuleKey K, std::unique_ptr<MemoryBuffer> O,
134  SymbolFlagsMap SymbolFlags)
135  : MaterializationUnit(std::move(SymbolFlags), std::move(K)), L(L),
136  O(std::move(O)) {}
137 
139  if (O)
140  return O->getBufferIdentifier();
141  return "<null object>";
142 }
143 
144 void BasicObjectLayerMaterializationUnit::materialize(
146  L.emit(std::move(R), std::move(O));
147 }
148 
149 void BasicObjectLayerMaterializationUnit::discard(const JITDylib &JD,
150  const SymbolStringPtr &Name) {
151  // FIXME: Support object file level discard. This could be done by building a
152  // filter to pass to the object layer along with the object itself.
153 }
154 
156  MemoryBufferRef ObjBuffer) {
157  auto Obj = object::ObjectFile::createObjectFile(ObjBuffer);
158 
159  if (!Obj)
160  return Obj.takeError();
161 
163  for (auto &Sym : (*Obj)->symbols()) {
164  // Skip symbols not defined in this object file.
165  if (Sym.getFlags() & object::BasicSymbolRef::SF_Undefined)
166  continue;
167 
168  // Skip symbols that are not global.
169  if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Global))
170  continue;
171 
172  auto Name = Sym.getName();
173  if (!Name)
174  return Name.takeError();
175  auto InternedName = ES.intern(*Name);
176  auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
177  if (!SymFlags)
178  return SymFlags.takeError();
179  SymbolFlags[InternedName] = std::move(*SymFlags);
180  }
181 
182  return SymbolFlags;
183 }
184 
185 } // End namespace orc.
186 } // End namespace llvm.
bool getCloneToNewContextOnEmit() const
Returns the current value of the CloneToNewContextOnEmit flag.
Definition: Layer.h:48
IRLayer(ExecutionSession &ES)
Definition: Layer.cpp:19
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:161
static JITSymbolFlags fromGlobalValue(const GlobalValue &GV)
Construct a JITSymbolFlags value based on the flags of the given global value.
Definition: JITSymbol.cpp:22
This class represents lattice values for constants.
Definition: AllocatorList.h:24
StringRef getName() const override
Return the ModuleIdentifier as the name for this MaterializationUnit.
Definition: Layer.cpp:50
SymbolNameToDefinitionMap SymbolToDefinition
Definition: Layer.h:92
Available for inspection, not emission.
Definition: GlobalValue.h:50
static sys::Mutex Lock
static Expected< JITSymbolFlags > fromObjectSymbol(const object::SymbolRef &Symbol)
Construct a JITSymbolFlags value based on the flags of the given libobject symbol.
Definition: JITSymbol.cpp:41
StringRef getName() const override
Return the buffer&#39;s identifier as the name for this MaterializationUnit.
Definition: Layer.cpp:138
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
IRMaterializationUnit(ExecutionSession &ES, ThreadSafeModule TSM, VModuleKey K)
Create an IRMaterializationLayer.
Definition: Layer.cpp:27
amdgpu Simplify well known AMD library false Value Value const Twine & Name
Definition: BitVector.h:938
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
Definition: Core.h:715
BasicObjectLayerMaterializationUnit(ObjectLayer &L, VModuleKey K, std::unique_ptr< MemoryBuffer > O, SymbolFlagsMap SymbolFlags)
Definition: Layer.cpp:132
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:371
SymbolFlagsMap SymbolFlags
Definition: Core.h:282
Error define(std::unique_ptr< MaterializationUnitType > &&MU)
Define all symbols provided by the materialization unit to be part of this JITDylib.
Definition: Core.h:881
Mangles symbol names then uniques them in the context of an ExecutionSession.
Definition: Core.h:915
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
JITDylib & getTargetJITDylib() const
Returns the target JITDylib that these symbols are being materialized into.
Definition: Core.h:169
ExecutionSession & getExecutionSession() const
Get a reference to the ExecutionSession for this JITDylib.
Definition: Core.h:516
Pointer to a pooled string representing a symbol name.
virtual void emit(MaterializationResponsibility R, ThreadSafeModule TSM)=0
Emit should materialize the given IR.
Module * getModule()
Get the module wrapped by this ThreadSafeModule.
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
ObjectLayer(ExecutionSession &ES)
Definition: Layer.cpp:105
An LLVM Module together with a shared ThreadSafeContext.
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:210
virtual void emit(MaterializationResponsibility R, std::unique_ptr< MemoryBuffer > O)=0
Emit should materialize the given IR.
virtual Error add(JITDylib &JD, std::unique_ptr< MemoryBuffer > O, VModuleKey K=VModuleKey())
Adds a MaterializationUnit representing the given IR to the given JITDylib.
Definition: Layer.cpp:109
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 DataFlowGraph & G
Definition: RDFGraph.cpp:211
ThreadSafeContext::Lock getContextLock()
Take out a lock on the ThreadSafeContext for this module.
An ExecutionSession represents a running JIT program.
Definition: Core.h:697
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
BasicIRLayerMaterializationUnit(IRLayer &L, VModuleKey K, ThreadSafeModule TSM)
Definition: Layer.cpp:72
static Expected< std::unique_ptr< BasicObjectLayerMaterializationUnit > > Create(ObjectLayer &L, VModuleKey K, std::unique_ptr< MemoryBuffer > O)
Definition: Layer.cpp:119
auto runSessionLocked(Func &&F) -> decltype(F())
Run the given lambda with the session mutex locked.
Definition: Core.h:721
#define I(x, y, z)
Definition: MD5.cpp:58
const std::string & getName() const
Get the name for this JITDylib.
Definition: Core.h:513
ThreadSafeModule cloneToNewContext(ThreadSafeModule &TSMW, GVPredicate ShouldCloneDef=GVPredicate(), GVModifier UpdateClonedDefSource=GVModifier())
Clones the given module on to a new context.
virtual ~ObjectLayer()
Definition: Layer.cpp:107
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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
#define LLVM_DEBUG(X)
Definition: Debug.h:123
iterator_range< global_value_iterator > global_values()
Definition: Module.h:685
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:496