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 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" 14 15 namespace llvm { 16 namespace orc { 17 18 ThreadSafeModule cloneToNewContext(ThreadSafeModule &TSM, 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 auto Lock = TSM.getContextLock(); 27 28 SmallVector<char, 1> ClonedModuleBuffer; 29 30 { 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; 37 } 38 return false; 39 }); 40 41 if (UpdateClonedDefSource) 42 for (auto *GV : ClonedDefsInSrc) 43 UpdateClonedDefSource(*GV); 44 45 BitcodeWriter BCWriter(ClonedModuleBuffer); 46 47 BCWriter.writeModule(*Tmp); 48 BCWriter.writeSymtab(); 49 BCWriter.writeStrtab(); 50 } 51 52 MemoryBufferRef ClonedModuleBufferRef( 53 StringRef(ClonedModuleBuffer.data(), ClonedModuleBuffer.size()), 54 "cloned module buffer"); 55 ThreadSafeContext NewTSCtx(llvm::make_unique<LLVMContext>()); 56 57 auto ClonedModule = 58 cantFail(parseBitcodeFile(ClonedModuleBufferRef, *NewTSCtx.getContext())); 59 ClonedModule->setModuleIdentifier(TSM.getModule()->getName()); 60 return ThreadSafeModule(std::move(ClonedModule), std::move(NewTSCtx)); 61 } 62 63 } // end namespace orc 64 } // end namespace llvm 65