LLVM  9.0.0svn
ThreadSafeModule.cpp
Go to the documentation of this file.
1 //===-- ThreadSafeModule.cpp - Thread safe Module, Context, and Utilities
2 //h-===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
14 
15 namespace llvm {
16 namespace orc {
17 
19  GVPredicate ShouldCloneDef,
20  GVModifier UpdateClonedDefSource) {
21  assert(TSM && "Can not clone null module");
22 
23  if (!ShouldCloneDef)
24  ShouldCloneDef = [](const GlobalValue &) { return true; };
25 
26  auto Lock = TSM.getContextLock();
27 
28  SmallVector<char, 1> ClonedModuleBuffer;
29 
30  {
31  std::set<GlobalValue *> ClonedDefsInSrc;
32  ValueToValueMapTy VMap;
33  auto Tmp = CloneModule(*TSM.getModule(), VMap, [&](const GlobalValue *GV) {
34  if (ShouldCloneDef(*GV)) {
35  ClonedDefsInSrc.insert(const_cast<GlobalValue *>(GV));
36  return true;
37  }
38  return false;
39  });
40 
41  if (UpdateClonedDefSource)
42  for (auto *GV : ClonedDefsInSrc)
43  UpdateClonedDefSource(*GV);
44 
45  BitcodeWriter BCWriter(ClonedModuleBuffer);
46 
47  BCWriter.writeModule(*Tmp);
48  BCWriter.writeSymtab();
49  BCWriter.writeStrtab();
50  }
51 
52  MemoryBufferRef ClonedModuleBufferRef(
53  StringRef(ClonedModuleBuffer.data(), ClonedModuleBuffer.size()),
54  "cloned module buffer");
55  ThreadSafeContext NewTSCtx(llvm::make_unique<LLVMContext>());
56 
57  auto ClonedModule =
58  cantFail(parseBitcodeFile(ClonedModuleBufferRef, *NewTSCtx.getContext()));
59  ClonedModule->setModuleIdentifier(TSM.getModule()->getName());
60  return ThreadSafeModule(std::move(ClonedModule), std::move(NewTSCtx));
61 }
62 
63 } // end namespace orc
64 } // end namespace llvm
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:703
This class represents lattice values for constants.
Definition: AllocatorList.h:23
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: ValueMap.h:178
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:226
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:34
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:52
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:839
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:144
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:48
void writeStrtab()
Write the bitcode file&#39;s string table.