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  auto &N = R.getTargetJITDylib().getName();
91 #endif // NDEBUG
92 
93  auto Lock = TSM.getContextLock();
94  LLVM_DEBUG(ES.runSessionLocked(
95  [&]() { dbgs() << "Emitting, for " << N << ", " << *this << "\n"; }););
96  L.emit(std::move(R), std::move(TSM));
97  LLVM_DEBUG(ES.runSessionLocked([&]() {
98  dbgs() << "Finished emitting, for " << N << ", " << *this << "\n";
99  }););
100 }
101 
103 
105 
106 Error ObjectLayer::add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O,
107  VModuleKey K) {
108  auto ObjMU = BasicObjectLayerMaterializationUnit::Create(*this, std::move(K),
109  std::move(O));
110  if (!ObjMU)
111  return ObjMU.takeError();
112  return JD.define(std::move(*ObjMU));
113 }
114 
117  std::unique_ptr<MemoryBuffer> O) {
118  auto SymbolFlags =
119  getObjectSymbolFlags(L.getExecutionSession(), O->getMemBufferRef());
120 
121  if (!SymbolFlags)
122  return SymbolFlags.takeError();
123 
124  return std::unique_ptr<BasicObjectLayerMaterializationUnit>(
125  new BasicObjectLayerMaterializationUnit(L, K, std::move(O),
126  std::move(*SymbolFlags)));
127 }
128 
130  ObjectLayer &L, VModuleKey K, std::unique_ptr<MemoryBuffer> O,
132  : MaterializationUnit(std::move(SymbolFlags), std::move(K)), L(L),
133  O(std::move(O)) {}
134 
136  if (O)
137  return O->getBufferIdentifier();
138  return "<null object>";
139 }
140 
141 void BasicObjectLayerMaterializationUnit::materialize(
143  L.emit(std::move(R), std::move(O));
144 }
145 
146 void BasicObjectLayerMaterializationUnit::discard(const JITDylib &JD,
147  const SymbolStringPtr &Name) {
148  // FIXME: Support object file level discard. This could be done by building a
149  // filter to pass to the object layer along with the object itself.
150 }
151 
153  MemoryBufferRef ObjBuffer) {
154  auto Obj = object::ObjectFile::createObjectFile(ObjBuffer);
155 
156  if (!Obj)
157  return Obj.takeError();
158 
160  for (auto &Sym : (*Obj)->symbols()) {
161  // Skip symbols not defined in this object file.
162  if (Sym.getFlags() & object::BasicSymbolRef::SF_Undefined)
163  continue;
164 
165  // Skip symbols that are not global.
166  if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Global))
167  continue;
168 
169  auto Name = Sym.getName();
170  if (!Name)
171  return Name.takeError();
172  auto InternedName = ES.intern(*Name);
173  auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
174  if (!SymFlags)
175  return SymFlags.takeError();
176  SymbolFlags[InternedName] = std::move(*SymFlags);
177  }
178 
179  return SymbolFlags;
180 }
181 
182 } // End namespace orc.
183 } // 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:161
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:135
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:770
BasicObjectLayerMaterializationUnit(ObjectLayer &L, VModuleKey K, std::unique_ptr< MemoryBuffer > O, SymbolFlagsMap SymbolFlags)
Definition: Layer.cpp:129
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
SymbolFlagsMap SymbolFlags
Definition: Core.h:297
Error define(std::unique_ptr< MaterializationUnitType > &&MU)
Define all symbols provided by the materialization unit to be part of this JITDylib.
Definition: Core.h:942
SymbolFlags
Symbol flags.
Definition: Symbol.h:24
Mangles symbol names then uniques them in the context of an ExecutionSession.
Definition: Core.h:976
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:170
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:184
ExecutionSession & getExecutionSession() const
Get a reference to the ExecutionSession for this JITDylib.
Definition: Core.h:519
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:152
ObjectLayer(ExecutionSession &ES)
Definition: Layer.cpp:102
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:106
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:267
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
ThreadSafeContext::Lock getContextLock()
Take out a lock on the ThreadSafeContext for this module.
An ExecutionSession represents a running JIT program.
Definition: Core.h:752
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:116
uint8_t uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:40
auto runSessionLocked(Func &&F) -> decltype(F())
Run the given lambda with the session mutex locked.
Definition: Core.h:776
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
const std::string & getName() const
Get the name for this JITDylib.
Definition: Core.h:516
ThreadSafeModule cloneToNewContext(ThreadSafeModule &TSMW, GVPredicate ShouldCloneDef=GVPredicate(), GVModifier UpdateClonedDefSource=GVModifier())
Clones the given module on to a new context.
virtual ~ObjectLayer()
Definition: Layer.cpp:104
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:688
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:499