LLVM  13.0.0git
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  return TSM.withModuleDo([&](Module &M) {
27  SmallVector<char, 1> ClonedModuleBuffer;
28 
29  {
30  std::set<GlobalValue *> ClonedDefsInSrc;
31  ValueToValueMapTy VMap;
32  auto Tmp = CloneModule(M, VMap, [&](const GlobalValue *GV) {
33  if (ShouldCloneDef(*GV)) {
34  ClonedDefsInSrc.insert(const_cast<GlobalValue *>(GV));
35  return true;
36  }
37  return false;
38  });
39 
40  if (UpdateClonedDefSource)
41  for (auto *GV : ClonedDefsInSrc)
42  UpdateClonedDefSource(*GV);
43 
44  BitcodeWriter BCWriter(ClonedModuleBuffer);
45 
46  BCWriter.writeModule(*Tmp);
47  BCWriter.writeSymtab();
48  BCWriter.writeStrtab();
49  }
50 
51  MemoryBufferRef ClonedModuleBufferRef(
52  StringRef(ClonedModuleBuffer.data(), ClonedModuleBuffer.size()),
53  "cloned module buffer");
54  ThreadSafeContext NewTSCtx(std::make_unique<LLVMContext>());
55 
56  auto ClonedModule = cantFail(
57  parseBitcodeFile(ClonedModuleBufferRef, *NewTSCtx.getContext()));
58  ClonedModule->setModuleIdentifier(M.getName());
60  });
61 }
62 
63 } // end namespace orc
64 } // end namespace llvm
llvm
Definition: AllocatorList.h:23
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::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1167
llvm::CloneFunctionChangeType::ClonedModule
@ ClonedModule
ThreadSafeModule.h
llvm::BitcodeWriter::writeModule
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.
Definition: BitcodeWriter.cpp:4602
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
BitcodeWriter.h
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::orc::ThreadSafeContext::getContext
LLVMContext * getContext()
Returns a pointer to the LLVMContext that was used to construct this instance, or null if the instanc...
Definition: ThreadSafeModule.h:61
llvm::parseBitcodeFile
Expected< std::unique_ptr< Module > > parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, DataLayoutCallbackTy DataLayoutCallback=[](StringRef) { return None;})
Read the specified bitcode file, returning the module.
Definition: BitcodeReader.cpp:7060
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
Cloning.h
llvm::BitcodeWriter
Definition: BitcodeWriter.h:32
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::orc::ThreadSafeContext
An LLVMContext together with an associated mutex that can be used to lock the context to prevent conc...
Definition: ThreadSafeModule.h:29
llvm::ValueMap::insert
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: ValueMap.h:173
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
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::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::cantFail
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:708
llvm::ValueMap< const Value *, WeakTrackingVH >
llvm::BitcodeWriter::writeStrtab
void writeStrtab()
Write the bitcode file's string table.
Definition: BitcodeWriter.cpp:4583
llvm::orc::GVPredicate
std::function< bool(const GlobalValue &)> GVPredicate
Definition: ThreadSafeModule.h:160
llvm::CloneModule
std::unique_ptr< Module > CloneModule(const Module &M)
Return an exact copy of the specified module.
Definition: CloneModule.cpp:34
llvm::BitcodeWriter::writeSymtab
void writeSymtab()
Attempt to write a symbol table to the bitcode file.
Definition: BitcodeWriter.cpp:4551
llvm::orc::GVModifier
std::function< void(GlobalValue &)> GVModifier
Definition: ThreadSafeModule.h:161
llvm::orc::ThreadSafeModule
An LLVM Module together with a shared ThreadSafeContext.
Definition: ThreadSafeModule.h:77
BitcodeReader.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