1 //===-- ThreadSafeModule.cpp - Thread safe Module, Context, and Utilities
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
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
11 #include "llvm/Bitcode/BitcodeReader.h"
12 #include "llvm/Bitcode/BitcodeWriter.h"
13 #include "llvm/Transforms/Utils/Cloning.h"
18 ThreadSafeModule
cloneToNewContext(ThreadSafeModule
&TSM
,
19 GVPredicate ShouldCloneDef
,
20 GVModifier UpdateClonedDefSource
) {
21 assert(TSM
&& "Can not clone null module");
24 ShouldCloneDef
= [](const GlobalValue
&) { return true; };
26 auto Lock
= TSM
.getContextLock();
28 SmallVector
<char, 1> ClonedModuleBuffer
;
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
));
41 if (UpdateClonedDefSource
)
42 for (auto *GV
: ClonedDefsInSrc
)
43 UpdateClonedDefSource(*GV
);
45 BitcodeWriter
BCWriter(ClonedModuleBuffer
);
47 BCWriter
.writeModule(*Tmp
);
48 BCWriter
.writeSymtab();
49 BCWriter
.writeStrtab();
52 MemoryBufferRef
ClonedModuleBufferRef(
53 StringRef(ClonedModuleBuffer
.data(), ClonedModuleBuffer
.size()),
54 "cloned module buffer");
55 ThreadSafeContext
NewTSCtx(llvm::make_unique
<LLVMContext
>());
58 cantFail(parseBitcodeFile(ClonedModuleBufferRef
, *NewTSCtx
.getContext()));
59 ClonedModule
->setModuleIdentifier(TSM
.getModule()->getName());
60 return ThreadSafeModule(std::move(ClonedModule
), std::move(NewTSCtx
));
63 } // end namespace orc
64 } // end namespace llvm