LLVM  17.0.0git
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 
14 #include "llvm/IR/Constants.h"
15 #include "llvm/Support/Debug.h"
16 
17 #define DEBUG_TYPE "orc"
18 
19 namespace llvm {
20 namespace orc {
21 
22 IRLayer::~IRLayer() = default;
23 
25  assert(RT && "RT can not be null");
26  auto &JD = RT->getJITDylib();
27  return JD.define(std::make_unique<BasicIRLayerMaterializationUnit>(
28  *this, *getManglingOptions(), std::move(TSM)),
29  std::move(RT));
30 }
31 
34  ThreadSafeModule TSM)
35  : MaterializationUnit(Interface()), TSM(std::move(TSM)) {
36 
37  assert(this->TSM && "Module must not be null");
38 
39  MangleAndInterner Mangle(ES, this->TSM.getModuleUnlocked()->getDataLayout());
40  this->TSM.withModuleDo([&](Module &M) {
41  for (auto &G : M.global_values()) {
42  // Skip globals that don't generate symbols.
43 
44  if (!G.hasName() || G.isDeclaration() || G.hasLocalLinkage() ||
45  G.hasAvailableExternallyLinkage() || G.hasAppendingLinkage())
46  continue;
47 
48  // thread locals generate different symbols depending on whether or not
49  // emulated TLS is enabled.
50  if (G.isThreadLocal() && MO.EmulatedTLS) {
51  auto &GV = cast<GlobalVariable>(G);
52 
53  auto Flags = JITSymbolFlags::fromGlobalValue(GV);
54 
55  auto EmuTLSV = Mangle(("__emutls_v." + GV.getName()).str());
56  SymbolFlags[EmuTLSV] = Flags;
57  SymbolToDefinition[EmuTLSV] = &GV;
58 
59  // If this GV has a non-zero initializer we'll need to emit an
60  // __emutls.t symbol too.
61  if (GV.hasInitializer()) {
62  const auto *InitVal = GV.getInitializer();
63 
64  // Skip zero-initializers.
65  if (isa<ConstantAggregateZero>(InitVal))
66  continue;
67  const auto *InitIntValue = dyn_cast<ConstantInt>(InitVal);
68  if (InitIntValue && InitIntValue->isZero())
69  continue;
70 
71  auto EmuTLST = Mangle(("__emutls_t." + GV.getName()).str());
72  SymbolFlags[EmuTLST] = Flags;
73  }
74  continue;
75  }
76 
77  // Otherwise we just need a normal linker mangling.
78  auto MangledName = Mangle(G.getName());
80  if (G.getComdat() &&
81  G.getComdat()->getSelectionKind() != Comdat::NoDeduplicate)
82  SymbolFlags[MangledName] |= JITSymbolFlags::Weak;
83  SymbolToDefinition[MangledName] = &G;
84  }
85 
86  // If we need an init symbol for this module then create one.
87  if (!getStaticInitGVs(M).empty()) {
88  size_t Counter = 0;
89 
90  do {
91  std::string InitSymbolName;
92  raw_string_ostream(InitSymbolName)
93  << "$." << M.getModuleIdentifier() << ".__inits." << Counter++;
94  InitSymbol = ES.intern(InitSymbolName);
95  } while (SymbolFlags.count(InitSymbol));
96 
98  }
99  });
100 }
101 
102 IRMaterializationUnit::IRMaterializationUnit(
104  SymbolNameToDefinitionMap SymbolToDefinition)
105  : MaterializationUnit(std::move(I)), TSM(std::move(TSM)),
106  SymbolToDefinition(std::move(SymbolToDefinition)) {}
107 
109  if (TSM)
110  return TSM.withModuleDo(
111  [](const Module &M) -> StringRef { return M.getModuleIdentifier(); });
112  return "<null module>";
113 }
114 
115 void IRMaterializationUnit::discard(const JITDylib &JD,
116  const SymbolStringPtr &Name) {
118  dbgs() << "In " << JD.getName() << " discarding " << *Name << " from MU@"
119  << this << " (" << getName() << ")\n";
120  }););
121 
122  auto I = SymbolToDefinition.find(Name);
123  assert(I != SymbolToDefinition.end() &&
124  "Symbol not provided by this MU, or previously discarded");
125  assert(!I->second->isDeclaration() &&
126  "Discard should only apply to definitions");
127  I->second->setLinkage(GlobalValue::AvailableExternallyLinkage);
128  SymbolToDefinition.erase(I);
129 }
130 
133  : IRMaterializationUnit(L.getExecutionSession(), MO, std::move(TSM)), L(L) {
134 }
135 
136 void BasicIRLayerMaterializationUnit::materialize(
137  std::unique_ptr<MaterializationResponsibility> R) {
138 
139  // Throw away the SymbolToDefinition map: it's not usable after we hand
140  // off the module.
141  SymbolToDefinition.clear();
142 
143  // If cloneToNewContextOnEmit is set, clone the module now.
146 
147 #ifndef NDEBUG
148  auto &ES = R->getTargetJITDylib().getExecutionSession();
149  auto &N = R->getTargetJITDylib().getName();
150 #endif // NDEBUG
151 
152  LLVM_DEBUG(ES.runSessionLocked(
153  [&]() { dbgs() << "Emitting, for " << N << ", " << *this << "\n"; }););
154  L.emit(std::move(R), std::move(TSM));
155  LLVM_DEBUG(ES.runSessionLocked([&]() {
156  dbgs() << "Finished emitting, for " << N << ", " << *this << "\n";
157  }););
158 }
159 
160 char ObjectLayer::ID;
161 
163 
164 ObjectLayer::~ObjectLayer() = default;
165 
166 Error ObjectLayer::add(ResourceTrackerSP RT, std::unique_ptr<MemoryBuffer> O,
168  assert(RT && "RT can not be null");
169  auto &JD = RT->getJITDylib();
170  return JD.define(std::make_unique<BasicObjectLayerMaterializationUnit>(
171  *this, std::move(O), std::move(I)),
172  std::move(RT));
173 }
174 
175 Error ObjectLayer::add(ResourceTrackerSP RT, std::unique_ptr<MemoryBuffer> O) {
176  auto I = getObjectFileInterface(getExecutionSession(), O->getMemBufferRef());
177  if (!I)
178  return I.takeError();
179  return add(std::move(RT), std::move(O), std::move(*I));
180 }
181 
182 Error ObjectLayer::add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O) {
183  auto I = getObjectFileInterface(getExecutionSession(), O->getMemBufferRef());
184  if (!I)
185  return I.takeError();
186  return add(JD, std::move(O), std::move(*I));
187 }
188 
191  std::unique_ptr<MemoryBuffer> O) {
192 
193  auto ObjInterface =
194  getObjectFileInterface(L.getExecutionSession(), O->getMemBufferRef());
195 
196  if (!ObjInterface)
197  return ObjInterface.takeError();
198 
199  return std::unique_ptr<BasicObjectLayerMaterializationUnit>(
201  std::move(*ObjInterface)));
202 }
203 
205  ObjectLayer &L, std::unique_ptr<MemoryBuffer> O, Interface I)
206  : MaterializationUnit(std::move(I)), L(L), O(std::move(O)) {}
207 
209  if (O)
210  return O->getBufferIdentifier();
211  return "<null object>";
212 }
213 
214 void BasicObjectLayerMaterializationUnit::materialize(
215  std::unique_ptr<MaterializationResponsibility> R) {
216  L.emit(std::move(R), std::move(O));
217 }
218 
219 void BasicObjectLayerMaterializationUnit::discard(const JITDylib &JD,
220  const SymbolStringPtr &Name) {
221  // This is a no-op for object files: Having removed 'Name' from SymbolFlags
222  // the symbol will be dead-stripped by the JIT linker.
223 }
224 
225 } // End namespace orc.
226 } // End namespace llvm.
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::orc::JITDylib
Represents a JIT'd dynamic library.
Definition: Core.h:961
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::orc::ObjectLayer::~ObjectLayer
virtual ~ObjectLayer()
llvm::orc::IRMaterializationUnit
IRMaterializationUnit is a convenient base class for MaterializationUnits wrapping LLVM IR.
Definition: Layer.h:31
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:642
llvm::Comdat::NoDeduplicate
@ NoDeduplicate
No deduplication is performed.
Definition: Comdat.h:39
llvm::orc::SymbolStringPtr
Pointer to a pooled string representing a symbol name.
Definition: SymbolStringPool.h:57
llvm::orc::ObjectLayer::add
virtual Error add(ResourceTrackerSP RT, std::unique_ptr< MemoryBuffer > O, MaterializationUnit::Interface I)
Adds a MaterializationUnit for the object file in the given memory buffer to the JITDylib for the giv...
Definition: Layer.cpp:166
llvm::orc::IRMaterializationUnit::TSM
ThreadSafeModule TSM
Definition: Layer.h:56
llvm::DenseMapBase::count
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:145
llvm::orc::IRLayer::getManglingOptions
const IRSymbolMapper::ManglingOptions *& getManglingOptions() const
Get the mangling options for this layer.
Definition: Layer.h:78
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::orc::ThreadSafeModule::getModuleUnlocked
Module * getModuleUnlocked()
Get a raw pointer to the contained module without locking the context.
Definition: ThreadSafeModule.h:156
Layer.h
llvm::orc::MaterializationUnit
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group,...
Definition: Core.h:673
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
ExecutionUtils.h
DebugUtils.h
Constants.h
llvm::JITSymbolFlags::MaterializationSideEffectsOnly
@ MaterializationSideEffectsOnly
Definition: JITSymbol.h:87
llvm::orc::BasicObjectLayerMaterializationUnit::getName
StringRef getName() const override
Return the buffer's identifier as the name for this MaterializationUnit.
Definition: Layer.cpp:208
llvm::orc::ExecutionSession::intern
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
Definition: Core.h:1424
llvm::orc::ObjectLayer::getExecutionSession
ExecutionSession & getExecutionSession()
Returns the execution session for this layer.
Definition: Layer.h:141
llvm::orc::ExecutionSession::runSessionLocked
decltype(auto) runSessionLocked(Func &&F)
Run the given lambda with the session mutex locked.
Definition: Core.h:1434
llvm::orc::ObjectLayer
Interface for Layers that accept object files.
Definition: Layer.h:133
llvm::orc::MaterializationUnit::Interface
Definition: Core.h:680
llvm::orc::IRMaterializationUnit::IRMaterializationUnit
IRMaterializationUnit(ExecutionSession &ES, const IRSymbolMapper::ManglingOptions &MO, ThreadSafeModule TSM)
Create an IRMaterializationLayer.
Definition: Layer.cpp:32
llvm::orc::getStaticInitGVs
iterator_range< StaticInitGVIterator > getStaticInitGVs(Module &M)
Create an iterator range over the GlobalValues that contribute to static initialization.
Definition: ExecutionUtils.h:139
llvm::orc::ObjectLayer::ID
static char ID
Definition: Layer.h:135
llvm::orc::IRLayer::add
virtual Error add(ResourceTrackerSP RT, ThreadSafeModule TSM)
Add a MaterializatinoUnit representing the given IR to the JITDylib targeted by the given tracker.
Definition: Layer.cpp:24
llvm::orc::IRLayer::emit
virtual void emit(std::unique_ptr< MaterializationResponsibility > R, ThreadSafeModule TSM)=0
Emit should materialize the given IR.
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:200
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:274
llvm::orc::BasicObjectLayerMaterializationUnit::Create
static Expected< std::unique_ptr< BasicObjectLayerMaterializationUnit > > Create(ObjectLayer &L, std::unique_ptr< MemoryBuffer > O)
Create using the default object interface builder function.
Definition: Layer.cpp:190
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::orc::ResourceTrackerSP
uint8_t IntrusiveRefCntPtr< ResourceTracker > ResourceTrackerSP
Definition: Core.h:47
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1862
llvm::orc::IRLayer::getCloneToNewContextOnEmit
bool getCloneToNewContextOnEmit() const
Returns the current value of the CloneToNewContextOnEmit flag.
Definition: Layer.h:96
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::orc::BasicObjectLayerMaterializationUnit::BasicObjectLayerMaterializationUnit
BasicObjectLayerMaterializationUnit(ObjectLayer &L, std::unique_ptr< MemoryBuffer > O, Interface I)
Definition: Layer.cpp:204
llvm::orc::ThreadSafeModule::withModuleDo
decltype(auto) withModuleDo(Func &&F)
Locks the associated ThreadSafeContext and calls the given function on the contained Module.
Definition: ThreadSafeModule.h:133
llvm::orc::ObjectLayer::emit
virtual void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< MemoryBuffer > O)=0
Emit should materialize the given IR.
llvm::JITSymbolFlags::Weak
@ Weak
Definition: JITSymbol.h:82
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::orc::BasicIRLayerMaterializationUnit::BasicIRLayerMaterializationUnit
BasicIRLayerMaterializationUnit(IRLayer &L, const IRSymbolMapper::ManglingOptions &MO, ThreadSafeModule TSM)
Definition: Layer.cpp:131
if
if(llvm_vc STREQUAL "") set(fake_version_inc "$
Definition: CMakeLists.txt:14
llvm::GlobalValue::AvailableExternallyLinkage
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition: GlobalValue.h:49
llvm::orc::MangleAndInterner
Mangles symbol names then uniques them in the context of an ExecutionSession.
Definition: Mangling.h:26
llvm::orc::IRSymbolMapper::ManglingOptions
Definition: Mangling.h:41
std
Definition: BitVector.h:851
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
llvm::orc::ExecutionSession
An ExecutionSession represents a running JIT program.
Definition: Core.h:1373
llvm::orc::IRLayer::~IRLayer
virtual ~IRLayer()
llvm::JITSymbolFlags::fromGlobalValue
static JITSymbolFlags fromGlobalValue(const GlobalValue &GV)
Construct a JITSymbolFlags value based on the flags of the given global value.
Definition: JITSymbol.cpp:22
llvm::orc::JITDylib::define
Error define(std::unique_ptr< MaterializationUnitType > &&MU, ResourceTrackerSP RT=nullptr)
Define all symbols provided by the materialization unit to be part of this JITDylib.
Definition: Core.h:1813
N
#define N
llvm::orc::getObjectFileInterface
Expected< MaterializationUnit::Interface > getObjectFileInterface(ExecutionSession &ES, MemoryBufferRef ObjBuffer)
Returns a MaterializationUnit::Interface for the object file contained in the given buffer,...
Definition: ObjectFileInterface.cpp:274
llvm::orc::JITDylib::getExecutionSession
ExecutionSession & getExecutionSession() const
Get a reference to the ExecutionSession for this JITDylib.
Definition: Core.h:981
llvm::Module::getDataLayout
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:398
llvm::orc::ThreadSafeModule
An LLVM Module together with a shared ThreadSafeContext.
Definition: ThreadSafeModule.h:77
llvm::orc::IRMaterializationUnit::SymbolToDefinition
SymbolNameToDefinitionMap SymbolToDefinition
Definition: Layer.h:57
llvm::orc::MaterializationUnit::SymbolFlags
SymbolFlagsMap SymbolFlags
Definition: Core.h:729
llvm::orc::ObjectLayer::ObjectLayer
ObjectLayer(ExecutionSession &ES)
Definition: Layer.cpp:162
llvm::orc::IRMaterializationUnit::SymbolNameToDefinitionMap
std::map< SymbolStringPtr, GlobalValue * > SymbolNameToDefinitionMap
Definition: Layer.h:33
llvm::orc::IRMaterializationUnit::getName
StringRef getName() const override
Return the ModuleIdentifier as the name for this MaterializationUnit.
Definition: Layer.cpp:108
llvm::orc::IRLayer
Interface for layers that accept LLVM IR.
Definition: Layer.h:67
Debug.h
ObjectFileInterface.h
llvm::orc::cloneToNewContext
ThreadSafeModule cloneToNewContext(const ThreadSafeModule &TSMW, GVPredicate ShouldCloneDef=GVPredicate(), GVModifier UpdateClonedDefSource=GVModifier())
Clones the given module on to a new context.
Definition: ThreadSafeModule.cpp:18
llvm::orc::MaterializationUnit::InitSymbol
SymbolStringPtr InitSymbol
Definition: Core.h:730