1414namespace llvm {
1515namespace orc {
1616
17- ThreadSafeModule cloneToContext (const ThreadSafeModule &TSM,
18- ThreadSafeContext TSCtx,
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- // First copy the source module into a buffer.
17+ static std::pair<std::string, SmallVector<char , 1 >>
18+ serializeModule (const Module &M, GVPredicate ShouldCloneDef,
19+ GVModifier UpdateClonedDefSource) {
2720 std::string ModuleName;
2821 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- });
40-
41- if (UpdateClonedDefSource)
42- for (auto *GV : ClonedDefsInSrc)
43- UpdateClonedDefSource (*GV);
44-
45- BitcodeWriter BCWriter (ClonedModuleBuffer);
46- BCWriter.writeModule (*Tmp);
47- BCWriter.writeSymtab ();
48- BCWriter.writeStrtab ();
22+
23+ ModuleName = M.getModuleIdentifier ();
24+ std::set<GlobalValue *> ClonedDefsInSrc;
25+ ValueToValueMapTy VMap;
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 ;
4932 });
5033
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+
46+ ThreadSafeModule
47+ deserializeModule (std::string ModuleName,
48+ const SmallVector<char , 1 > &ClonedModuleBuffer,
49+ ThreadSafeContext TSCtx) {
5150 MemoryBufferRef ClonedModuleBufferRef (
5251 StringRef (ClonedModuleBuffer.data (), ClonedModuleBuffer.size ()),
5352 " cloned module buffer" );
@@ -63,6 +62,40 @@ ThreadSafeModule cloneToContext(const ThreadSafeModule &TSM,
6362 return ThreadSafeModule (std::move (M), std::move (TSCtx));
6463}
6564
65+ ThreadSafeModule
66+ cloneExternalModuleToContext (const Module &M, ThreadSafeContext TSCtx,
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+
80+ ThreadSafeModule cloneToContext (const ThreadSafeModule &TSM,
81+ ThreadSafeContext TSCtx,
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+
6699ThreadSafeModule cloneToNewContext (const ThreadSafeModule &TSM,
67100 GVPredicate ShouldCloneDef,
68101 GVModifier UpdateClonedDefSource) {
0 commit comments