|
14 | 14 | namespace llvm { |
15 | 15 | namespace orc { |
16 | 16 |
|
17 | | -ThreadSafeModule cloneToNewContext(const ThreadSafeModule &TSM, |
18 | | - GVPredicate ShouldCloneDef, |
19 | | - GVModifier UpdateClonedDefSource) { |
| 17 | +ThreadSafeModule cloneToContext(const ThreadSafeModule &TSM, |
| 18 | + ThreadSafeContext TSCtx, |
| 19 | + GVPredicate ShouldCloneDef, |
| 20 | + GVModifier UpdateClonedDefSource) { |
20 | 21 | assert(TSM && "Can not clone null module"); |
21 | 22 |
|
22 | 23 | if (!ShouldCloneDef) |
23 | 24 | ShouldCloneDef = [](const GlobalValue &) { return true; }; |
24 | 25 |
|
25 | | - return TSM.withModuleDo([&](Module &M) { |
26 | | - SmallVector<char, 1> ClonedModuleBuffer; |
| 26 | + // First copy the source module into a buffer. |
| 27 | + std::string ModuleName; |
| 28 | + SmallVector<char, 1> ClonedModuleBuffer; |
| 29 | + TSM.withModuleDo([&](Module &M) { |
| 30 | + ModuleName = M.getModuleIdentifier(); |
| 31 | + std::set<GlobalValue *> ClonedDefsInSrc; |
| 32 | + ValueToValueMapTy VMap; |
| 33 | + auto Tmp = CloneModule(M, VMap, [&](const GlobalValue *GV) { |
| 34 | + if (ShouldCloneDef(*GV)) { |
| 35 | + ClonedDefsInSrc.insert(const_cast<GlobalValue *>(GV)); |
| 36 | + return true; |
| 37 | + } |
| 38 | + return false; |
| 39 | + }); |
27 | 40 |
|
28 | | - { |
29 | | - std::set<GlobalValue *> ClonedDefsInSrc; |
30 | | - ValueToValueMapTy VMap; |
31 | | - auto Tmp = CloneModule(M, VMap, [&](const GlobalValue *GV) { |
32 | | - if (ShouldCloneDef(*GV)) { |
33 | | - ClonedDefsInSrc.insert(const_cast<GlobalValue *>(GV)); |
34 | | - return true; |
35 | | - } |
36 | | - return false; |
37 | | - }); |
| 41 | + if (UpdateClonedDefSource) |
| 42 | + for (auto *GV : ClonedDefsInSrc) |
| 43 | + UpdateClonedDefSource(*GV); |
38 | 44 |
|
39 | | - if (UpdateClonedDefSource) |
40 | | - for (auto *GV : ClonedDefsInSrc) |
41 | | - UpdateClonedDefSource(*GV); |
| 45 | + BitcodeWriter BCWriter(ClonedModuleBuffer); |
| 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"); |
42 | 54 |
|
43 | | - BitcodeWriter BCWriter(ClonedModuleBuffer); |
| 55 | + // Then parse the buffer into the new Module. |
| 56 | + auto M = TSCtx.withContextDo([&](LLVMContext *Ctx) { |
| 57 | + assert(Ctx && "No LLVMContext provided"); |
| 58 | + auto TmpM = cantFail(parseBitcodeFile(ClonedModuleBufferRef, *Ctx)); |
| 59 | + TmpM->setModuleIdentifier(ModuleName); |
| 60 | + return TmpM; |
| 61 | + }); |
44 | 62 |
|
45 | | - BCWriter.writeModule(*Tmp); |
46 | | - BCWriter.writeSymtab(); |
47 | | - BCWriter.writeStrtab(); |
48 | | - } |
| 63 | + return ThreadSafeModule(std::move(M), std::move(TSCtx)); |
| 64 | +} |
49 | 65 |
|
50 | | - MemoryBufferRef ClonedModuleBufferRef( |
51 | | - StringRef(ClonedModuleBuffer.data(), ClonedModuleBuffer.size()), |
52 | | - "cloned module buffer"); |
53 | | - ThreadSafeContext NewTSCtx(std::make_unique<LLVMContext>()); |
| 66 | +ThreadSafeModule cloneToNewContext(const ThreadSafeModule &TSM, |
| 67 | + GVPredicate ShouldCloneDef, |
| 68 | + GVModifier UpdateClonedDefSource) { |
| 69 | + assert(TSM && "Can not clone null module"); |
54 | 70 |
|
55 | | - auto ClonedModule = NewTSCtx.withContextDo([&](LLVMContext *Ctx) { |
56 | | - auto TmpM = cantFail(parseBitcodeFile(ClonedModuleBufferRef, *Ctx)); |
57 | | - TmpM->setModuleIdentifier(M.getName()); |
58 | | - return TmpM; |
59 | | - }); |
60 | | - return ThreadSafeModule(std::move(ClonedModule), std::move(NewTSCtx)); |
61 | | - }); |
| 71 | + ThreadSafeContext TSCtx(std::make_unique<LLVMContext>()); |
| 72 | + return cloneToContext(TSM, std::move(TSCtx), std::move(ShouldCloneDef), |
| 73 | + std::move(UpdateClonedDefSource)); |
62 | 74 | } |
63 | 75 |
|
64 | 76 | } // end namespace orc |
|
0 commit comments