LLVM  8.0.1
ThreadSafeModule.cpp
Go to the documentation of this file.
1 //===-- ThreadSafeModule.cpp - Thread safe Module, Context, and Utilities
2 //h-===//
3 //
4 // The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
15 
16 namespace llvm {
17 namespace orc {
18 
20  GVPredicate ShouldCloneDef,
21  GVModifier UpdateClonedDefSource) {
22  assert(TSM && "Can not clone null module");
23 
24  if (!ShouldCloneDef)
25  ShouldCloneDef = [](const GlobalValue &) { return true; };
26 
27  auto Lock = TSM.getContextLock();
28 
29  SmallVector<char, 1> ClonedModuleBuffer;
30 
31  {
32  std::set<GlobalValue *> ClonedDefsInSrc;
33  ValueToValueMapTy VMap;
34  auto Tmp = CloneModule(*TSM.getModule(), VMap, [&](const GlobalValue *GV) {
35  if (ShouldCloneDef(*GV)) {
36  ClonedDefsInSrc.insert(const_cast<GlobalValue *>(GV));
37  return true;
38  }
39  return false;
40  });
41 
42  if (UpdateClonedDefSource)
43  for (auto *GV : ClonedDefsInSrc)
44  UpdateClonedDefSource(*GV);
45 
46  BitcodeWriter BCWriter(ClonedModuleBuffer);
47 
48  BCWriter.writeModule(*Tmp);
49  BCWriter.writeSymtab();
50  BCWriter.writeStrtab();
51  }
52 
53  MemoryBufferRef ClonedModuleBufferRef(
54  StringRef(ClonedModuleBuffer.data(), ClonedModuleBuffer.size()),
55  "cloned module buffer");
56  ThreadSafeContext NewTSCtx(llvm::make_unique<LLVMContext>());
57 
58  auto ClonedModule =
59  cantFail(parseBitcodeFile(ClonedModuleBufferRef, *NewTSCtx.getContext()));
60  ClonedModule->setModuleIdentifier(TSM.getModule()->getName());
61  return ThreadSafeModule(std::move(ClonedModule), std::move(NewTSCtx));
62 }
63 
64 } // end namespace orc
65 } // end namespace llvm
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:704
This class represents lattice values for constants.
Definition: AllocatorList.h:24
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: ValueMap.h:179
An LLVMContext together with an associated mutex that can be used to lock the context to prevent conc...
void writeSymtab()
Attempt to write a symbol table to the bitcode file.
static sys::Mutex Lock
StringRef getName() const
Get a short "name" for the module.
Definition: Module.h:227
std::function< bool(const GlobalValue &)> GVPredicate
void writeModule(const Module &M, bool ShouldPreserveUseListOrder=false, const ModuleSummaryIndex *Index=nullptr, bool GenerateHash=false, ModuleHash *ModHash=nullptr)
Write the specified module to the buffer specified at construction time.
std::unique_ptr< Module > CloneModule(const Module &M)
Return an exact copy of the specified module.
Definition: CloneModule.cpp:35
Module * getModule()
Get the module wrapped by this ThreadSafeModule.
LLVMContext * getContext()
Returns a pointer to the LLVMContext that was used to construct this instance, or null if the instanc...
An LLVM Module together with a shared ThreadSafeContext.
size_t size() const
Definition: SmallVector.h:53
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
ThreadSafeContext::Lock getContextLock()
Take out a lock on the ThreadSafeContext for this module.
Expected< std::unique_ptr< Module > > parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context)
Read the specified bitcode file, returning the module.
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:149
ThreadSafeModule cloneToNewContext(ThreadSafeModule &TSMW, GVPredicate ShouldCloneDef=GVPredicate(), GVModifier UpdateClonedDefSource=GVModifier())
Clones the given module on to a new context.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::function< void(GlobalValue &)> GVModifier
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void writeStrtab()
Write the bitcode file&#39;s string table.