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