[DAGCombiner] Eliminate dead stores to stack.
[llvm-complete.git] / lib / ExecutionEngine / Orc / ThreadSafeModule.cpp
blob4cb7376758a71147e1842b61abc76354c1a0a0e5
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 //===----------------------------------------------------------------------===//
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"
15 namespace llvm {
16 namespace orc {
18 ThreadSafeModule cloneToNewContext(ThreadSafeModule &TSM,
19 GVPredicate ShouldCloneDef,
20 GVModifier UpdateClonedDefSource) {
21 assert(TSM && "Can not clone null module");
23 if (!ShouldCloneDef)
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));
36 return true;
38 return false;
39 });
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>());
57 auto ClonedModule =
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