LLVM 22.0.0git
ThreadSafeModule.cpp
Go to the documentation of this file.
1//===-- ThreadSafeModule.cpp - Thread safe Module, Context, and Utilities -===//
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
13
14namespace llvm {
15namespace orc {
16
17static std::pair<std::string, SmallVector<char, 1>>
18serializeModule(const Module &M, GVPredicate ShouldCloneDef,
19 GVModifier UpdateClonedDefSource) {
20 std::string ModuleName;
21 SmallVector<char, 1> ClonedModuleBuffer;
22
23 ModuleName = M.getModuleIdentifier();
24 std::set<GlobalValue *> ClonedDefsInSrc;
26 auto Tmp = CloneModule(M, VMap, [&](const GlobalValue *GV) {
27 if (ShouldCloneDef(*GV)) {
28 ClonedDefsInSrc.insert(const_cast<GlobalValue *>(GV));
29 return true;
30 }
31 return false;
32 });
33
34 if (UpdateClonedDefSource)
35 for (auto *GV : ClonedDefsInSrc)
36 UpdateClonedDefSource(*GV);
37
38 BitcodeWriter BCWriter(ClonedModuleBuffer);
39 BCWriter.writeModule(*Tmp);
40 BCWriter.writeSymtab();
41 BCWriter.writeStrtab();
42
43 return {std::move(ModuleName), std::move(ClonedModuleBuffer)};
44}
45
46ThreadSafeModule
48 const SmallVector<char, 1> &ClonedModuleBuffer,
49 ThreadSafeContext TSCtx) {
50 MemoryBufferRef ClonedModuleBufferRef(
51 StringRef(ClonedModuleBuffer.data(), ClonedModuleBuffer.size()),
52 "cloned module buffer");
53
54 // Then parse the buffer into the new Module.
55 auto M = TSCtx.withContextDo([&](LLVMContext *Ctx) {
56 assert(Ctx && "No LLVMContext provided");
57 auto TmpM = cantFail(parseBitcodeFile(ClonedModuleBufferRef, *Ctx));
58 TmpM->setModuleIdentifier(ModuleName);
59 return TmpM;
60 });
61
62 return ThreadSafeModule(std::move(M), std::move(TSCtx));
63}
64
65ThreadSafeModule
67 GVPredicate ShouldCloneDef,
68 GVModifier UpdateClonedDefSource) {
69
70 if (!ShouldCloneDef)
71 ShouldCloneDef = [](const GlobalValue &) { return true; };
72
73 auto [ModuleName, ClonedModuleBuffer] = serializeModule(
74 M, std::move(ShouldCloneDef), std::move(UpdateClonedDefSource));
75
76 return deserializeModule(std::move(ModuleName), ClonedModuleBuffer,
77 std::move(TSCtx));
78}
79
82 GVPredicate ShouldCloneDef,
83 GVModifier UpdateClonedDefSource) {
84 assert(TSM && "Can not clone null module");
85
86 if (!ShouldCloneDef)
87 ShouldCloneDef = [](const GlobalValue &) { return true; };
88
89 // First copy the source module into a buffer.
90 auto [ModuleName, ClonedModuleBuffer] = TSM.withModuleDo([&](Module &M) {
91 return serializeModule(M, std::move(ShouldCloneDef),
92 std::move(UpdateClonedDefSource));
93 });
94
95 return deserializeModule(std::move(ModuleName), ClonedModuleBuffer,
96 std::move(TSCtx));
97}
98
100 GVPredicate ShouldCloneDef,
101 GVModifier UpdateClonedDefSource) {
102 assert(TSM && "Can not clone null module");
103
104 ThreadSafeContext TSCtx(std::make_unique<LLVMContext>());
105 return cloneToContext(TSM, std::move(TSCtx), std::move(ShouldCloneDef),
106 std::move(UpdateClonedDefSource));
107}
108
109} // end namespace orc
110} // end namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
LLVM_ABI void writeStrtab()
Write the bitcode file's string table.
LLVM_ABI void writeSymtab()
Attempt to write a symbol table to the bitcode file.
LLVM_ABI 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.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
size_t size() const
Definition: SmallVector.h:79
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:287
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: ValueMap.h:177
An LLVMContext together with an associated mutex that can be used to lock the context to prevent conc...
decltype(auto) withContextDo(Func &&F)
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.
ThreadSafeModule deserializeModule(std::string ModuleName, const SmallVector< char, 1 > &ClonedModuleBuffer, ThreadSafeContext TSCtx)
std::function< bool(const GlobalValue &)> GVPredicate
std::function< void(GlobalValue &)> GVModifier
LLVM_ABI ThreadSafeModule cloneToContext(const ThreadSafeModule &TSMW, ThreadSafeContext TSCtx, GVPredicate ShouldCloneDef=GVPredicate(), GVModifier UpdateClonedDefSource=GVModifier())
Clones the given module onto the given context.
static std::pair< std::string, SmallVector< char, 1 > > serializeModule(const Module &M, GVPredicate ShouldCloneDef, GVModifier UpdateClonedDefSource)
LLVM_ABI ThreadSafeModule cloneExternalModuleToContext(const Module &M, ThreadSafeContext TSCtx, GVPredicate ShouldCloneDef=GVPredicate(), GVModifier UpdateClonedDefSource=GVModifier())
Clone the given module onto the given context.
LLVM_ABI 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
LLVM_ABI 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:769
LLVM_ABI std::unique_ptr< Module > CloneModule(const Module &M)
Return an exact copy of the specified module.
Definition: CloneModule.cpp:40