LLVM 19.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
15namespace llvm {
16namespace 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;
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
57 parseBitcodeFile(ClonedModuleBufferRef, *NewTSCtx.getContext()));
58 ClonedModule->setModuleIdentifier(M.getName());
59 return ThreadSafeModule(std::move(ClonedModule), std::move(NewTSCtx));
60 });
61}
62
63} // end namespace orc
64} // end namespace llvm
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void writeStrtab()
Write the bitcode file's string table.
void writeSymtab()
Attempt to write a symbol table to the bitcode file.
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.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
size_t size() const
Definition: SmallVector.h:91
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:299
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: ValueMap.h:172
An LLVMContext together with an associated mutex that can be used to lock the context to prevent conc...
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.
decltype(auto) withModuleDo(Func &&F)
Locks the associated ThreadSafeContext and calls the given function on the contained Module.
std::function< bool(const GlobalValue &)> GVPredicate
std::function< void(GlobalValue &)> GVModifier
ThreadSafeModule cloneToNewContext(const ThreadSafeModule &TSMW, GVPredicate ShouldCloneDef=GVPredicate(), GVModifier UpdateClonedDefSource=GVModifier())
Clones the given module on to a new context.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Expected< std::unique_ptr< Module > > parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, ParserCallbacks Callbacks={})
Read the specified bitcode file, returning the module.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:749
std::unique_ptr< Module > CloneModule(const Module &M)
Return an exact copy of the specified module.
Definition: CloneModule.cpp:39