LLVM  7.0.0
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"
13 
14 namespace llvm {
15 namespace orc {
16 
19 
20 Error IRLayer::add(VSO &V, VModuleKey K, std::unique_ptr<Module> M) {
21  return V.define(llvm::make_unique<BasicIRLayerMaterializationUnit>(
22  *this, std::move(K), std::move(M)));
23 }
24 
26  std::unique_ptr<Module> M)
27  : MaterializationUnit(SymbolFlagsMap()), M(std::move(M)) {
28 
29  MangleAndInterner Mangle(ES, this->M->getDataLayout());
30  for (auto &G : this->M->global_values()) {
31  if (G.hasName() && !G.isDeclaration() && !G.hasLocalLinkage() &&
32  !G.hasAvailableExternallyLinkage() && !G.hasAppendingLinkage()) {
33  auto MangledName = Mangle(G.getName());
35  SymbolToDefinition[MangledName] = &G;
36  }
37  }
38 }
39 
41  std::unique_ptr<Module> M, SymbolFlagsMap SymbolFlags,
43  : MaterializationUnit(std::move(SymbolFlags)), M(std::move(M)),
44  SymbolToDefinition(std::move(SymbolToDefinition)) {}
45 
46 void IRMaterializationUnit::discard(const VSO &V, SymbolStringPtr Name) {
47  auto I = SymbolToDefinition.find(Name);
48  assert(I != SymbolToDefinition.end() &&
49  "Symbol not provided by this MU, or previously discarded");
50  assert(!I->second->isDeclaration() &&
51  "Discard should only apply to definitions");
52  I->second->setLinkage(GlobalValue::AvailableExternallyLinkage);
53  SymbolToDefinition.erase(I);
54 }
55 
57  IRLayer &L, VModuleKey K, std::unique_ptr<Module> M)
58  : IRMaterializationUnit(L.getExecutionSession(), std::move(M)),
59  L(L), K(std::move(K)) {}
60 
61 void BasicIRLayerMaterializationUnit::materialize(
63  L.emit(std::move(R), std::move(K), std::move(M));
64 }
65 
67 
69 
70 Error ObjectLayer::add(VSO &V, VModuleKey K, std::unique_ptr<MemoryBuffer> O) {
71  return V.define(llvm::make_unique<BasicObjectLayerMaterializationUnit>(
72  *this, std::move(K), std::move(O)));
73 }
74 
76  ObjectLayer &L, VModuleKey K, std::unique_ptr<MemoryBuffer> O)
77  : MaterializationUnit(SymbolFlagsMap()), L(L), K(std::move(K)),
78  O(std::move(O)) {
79 
80  auto &ES = L.getExecutionSession();
81  auto Obj = cantFail(
82  object::ObjectFile::createObjectFile(this->O->getMemBufferRef()));
83 
84  for (auto &Sym : Obj->symbols()) {
85  if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Undefined) &&
86  (Sym.getFlags() & object::BasicSymbolRef::SF_Exported)) {
87  auto InternedName =
88  ES.getSymbolStringPool().intern(cantFail(Sym.getName()));
89  SymbolFlags[InternedName] = JITSymbolFlags::fromObjectSymbol(Sym);
90  }
91  }
92 }
93 
94 void BasicObjectLayerMaterializationUnit::materialize(
96  L.emit(std::move(R), std::move(K), std::move(O));
97 }
98 
99 void BasicObjectLayerMaterializationUnit::discard(const VSO &V,
100  SymbolStringPtr Name) {
101  // FIXME: Support object file level discard. This could be done by building a
102  // filter to pass to the object layer along with the object itself.
103 }
104 
105 } // End namespace orc.
106 } // End namespace llvm.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:697
IRLayer(ExecutionSession &ES)
Definition: Layer.cpp:17
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:153
virtual Error add(VSO &V, VModuleKey K, std::unique_ptr< Module > M)
Adds a MaterializationUnit representing the given IR to the given VSO.
Definition: Layer.cpp:20
std::enable_if< std::is_convertible< typename std::decay< UniquePtrToMaterializationUnit >::type, std::unique_ptr< MaterializationUnit > >::value, Error >::type define(UniquePtrToMaterializationUnit &&MU)
Define all symbols provided by the materialization unit to be part of the given VSO.
Definition: Core.h:621
static JITSymbolFlags fromGlobalValue(const GlobalValue &GV)
Construct a JITSymbolFlags value based on the flags of the given global value.
Definition: JITSymbol.cpp:20
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
BasicIRLayerMaterializationUnit(IRLayer &L, VModuleKey K, std::unique_ptr< Module > M)
Definition: Layer.cpp:56
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:544
SymbolNameToDefinitionMap SymbolToDefinition
Definition: Layer.h:65
Available for inspection, not emission.
Definition: GlobalValue.h:50
std::unique_ptr< Module > M
Definition: Layer.h:64
uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:40
Definition: BitVector.h:921
std::map< SymbolStringPtr, JITSymbolFlags > SymbolFlagsMap
A map from symbol names (as SymbolStringPtrs) to JITSymbolFlags.
Definition: Core.h:57
SymbolFlagsMap SymbolFlags
Definition: Core.h:230
Mangles symbol names then uniques them in the context of an ExecutionSession.
Definition: Core.h:766
std::map< SymbolStringPtr, GlobalValue * > SymbolNameToDefinitionMap
Definition: Layer.h:49
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:124
virtual void emit(MaterializationResponsibility R, VModuleKey K, std::unique_ptr< Module > M)=0
Emit should materialize the given IR.
IRMaterializationUnit is a convenient base class for MaterializationUnits wrapping LLVM IR...
Definition: Layer.h:47
ExecutionSession & getExecutionSession()
Returns the execution session for this layer.
Definition: Layer.h:92
IRMaterializationUnit(ExecutionSession &ES, std::unique_ptr< Module > M)
Create an IRMaterializationLayer.
Definition: Layer.cpp:25
Pointer to a pooled string representing a symbol name.
ObjectLayer(ExecutionSession &ES)
Definition: Layer.cpp:66
static JITSymbolFlags fromObjectSymbol(const object::BasicSymbolRef &Symbol)
Construct a JITSymbolFlags value based on the flags of the given libobject symbol.
Definition: JITSymbol.cpp:32
virtual Error add(VSO &V, VModuleKey K, std::unique_ptr< MemoryBuffer > O)
Adds a MaterializationUnit representing the given IR to the given VSO.
Definition: Layer.cpp:70
Interface for layers that accept LLVM IR.
Definition: Layer.h:24
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group...
Definition: Core.h:205
virtual void emit(MaterializationResponsibility R, VModuleKey K, std::unique_ptr< MemoryBuffer > O)=0
Emit should materialize the given IR.
const DataFlowGraph & G
Definition: RDFGraph.cpp:211
An ExecutionSession represents a running JIT program.
Definition: Core.h:735
BasicObjectLayerMaterializationUnit(ObjectLayer &L, VModuleKey K, std::unique_ptr< MemoryBuffer > O)
The MemoryBuffer should represent a valid object file.
Definition: Layer.cpp:75
#define I(x, y, z)
Definition: MD5.cpp:58
virtual ~ObjectLayer()
Definition: Layer.cpp:68
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:18
Interface for Layers that accept object files.
Definition: Layer.h:86