llvm.org GIT mirror llvm / 268a755
Linker: Move distinct MDNodes instead of cloning Instead of cloning distinct `MDNode`s when linking in a module, just move them over. The module linker destroys the source module, so the old node would otherwise just be leaked on the context. Create the new node in place. This also reduces the number of cloned uniqued nodes (since it's less likely their operands have changed). This mapping strategy is only correct when we're discarding the source, so the linker turns it on via a ValueMapper flag, `RF_MoveDistinctMDs`. There's nothing observable in terms of `llvm-link` output here: the linked module should be semantically identical. I'll be adding more 'distinct' nodes to the debug info metadata graph in order to break uniquing cycles, so the benefits of this will partly come in future commits. However, we should get some gains immediately, since we have a fair number of 'distinct' `DILocation`s being linked in. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@243883 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 5 years ago
5 changed file(s) with 153 addition(s) and 33 deletion(s). Raw diff Collapse all Expand all
5858 /// RF_IgnoreMissingEntries - If this flag is set, the remapper ignores
5959 /// entries that are not in the value map. If it is unset, it aborts if an
6060 /// operand is asked to be remapped which doesn't exist in the mapping.
61 RF_IgnoreMissingEntries = 2
61 RF_IgnoreMissingEntries = 2,
62
63 /// Instruct the remapper to move distinct metadata instead of duplicating
64 /// it when there are module-level changes.
65 RF_MoveDistinctMDs = 4,
6266 };
6367
6468 static inline RemapFlags operator|(RemapFlags LHS, RemapFlags RHS) {
11551155 continue;
11561156 }
11571157 DstElements.push_back(
1158 MapValue(V, ValueMap, RF_None, &TypeMap, &ValMaterializer));
1158 MapValue(V, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer));
11591159 }
11601160 if (IsNewStructor) {
11611161 NewType = ArrayType::get(NewType->getElementType(), DstElements.size());
11691169 /// referenced are in Dest.
11701170 void ModuleLinker::linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src) {
11711171 // Figure out what the initializer looks like in the dest module.
1172 Dst.setInitializer(MapValue(Src.getInitializer(), ValueMap, RF_None, &TypeMap,
1173 &ValMaterializer));
1172 Dst.setInitializer(MapValue(Src.getInitializer(), ValueMap,
1173 RF_MoveDistinctMDs, &TypeMap, &ValMaterializer));
11741174 }
11751175
11761176 /// Copy the source function over into the dest function and fix up references
11851185
11861186 // Link in the prefix data.
11871187 if (Src.hasPrefixData())
1188 Dst.setPrefixData(MapValue(Src.getPrefixData(), ValueMap, RF_None, &TypeMap,
1189 &ValMaterializer));
1188 Dst.setPrefixData(MapValue(Src.getPrefixData(), ValueMap,
1189 RF_MoveDistinctMDs, &TypeMap, &ValMaterializer));
11901190
11911191 // Link in the prologue data.
11921192 if (Src.hasPrologueData())
1193 Dst.setPrologueData(MapValue(Src.getPrologueData(), ValueMap, RF_None,
1194 &TypeMap, &ValMaterializer));
1193 Dst.setPrologueData(MapValue(Src.getPrologueData(), ValueMap,
1194 RF_MoveDistinctMDs, &TypeMap,
1195 &ValMaterializer));
11951196
11961197 // Link in the personality function.
11971198 if (Src.hasPersonalityFn())
1198 Dst.setPersonalityFn(MapValue(Src.getPersonalityFn(), ValueMap, RF_None,
1199 &TypeMap, &ValMaterializer));
1199 Dst.setPersonalityFn(MapValue(Src.getPersonalityFn(), ValueMap,
1200 RF_MoveDistinctMDs, &TypeMap,
1201 &ValMaterializer));
12001202
12011203 // Go through and convert function arguments over, remembering the mapping.
12021204 Function::arg_iterator DI = Dst.arg_begin();
12121214 SmallVector, 8> MDs;
12131215 Src.getAllMetadata(MDs);
12141216 for (const auto &I : MDs)
1215 Dst.setMetadata(I.first, MapMetadata(I.second, ValueMap, RF_None, &TypeMap,
1216 &ValMaterializer));
1217 Dst.setMetadata(I.first, MapMetadata(I.second, ValueMap, RF_MoveDistinctMDs,
1218 &TypeMap, &ValMaterializer));
12171219
12181220 // Splice the body of the source function into the dest function.
12191221 Dst.getBasicBlockList().splice(Dst.end(), Src.getBasicBlockList());
12241226 // functions and patch them up to point to the local versions.
12251227 for (BasicBlock &BB : Dst)
12261228 for (Instruction &I : BB)
1227 RemapInstruction(&I, ValueMap, RF_IgnoreMissingEntries, &TypeMap,
1229 RemapInstruction(&I, ValueMap,
1230 RF_IgnoreMissingEntries | RF_MoveDistinctMDs, &TypeMap,
12281231 &ValMaterializer);
12291232
12301233 // There is no need to map the arguments anymore.
12371240
12381241 void ModuleLinker::linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src) {
12391242 Constant *Aliasee = Src.getAliasee();
1240 Constant *Val =
1241 MapValue(Aliasee, ValueMap, RF_None, &TypeMap, &ValMaterializer);
1243 Constant *Val = MapValue(Aliasee, ValueMap, RF_MoveDistinctMDs, &TypeMap,
1244 &ValMaterializer);
12421245 Dst.setAliasee(Val);
12431246 }
12441247
12651268 NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(NMD.getName());
12661269 // Add Src elements into Dest node.
12671270 for (const MDNode *op : NMD.operands())
1268 DestNMD->addOperand(
1269 MapMetadata(op, ValueMap, RF_None, &TypeMap, &ValMaterializer));
1271 DestNMD->addOperand(MapMetadata(op, ValueMap, RF_MoveDistinctMDs,
1272 &TypeMap, &ValMaterializer));
12701273 }
12711274 }
12721275
15731576 continue;
15741577 const GlobalValue *GV = SrcM->getNamedValue(C.getName());
15751578 if (GV)
1576 MapValue(GV, ValueMap, RF_None, &TypeMap, &ValMaterializer);
1579 MapValue(GV, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer);
15771580 }
15781581
15791582 // Strip replaced subprograms before mapping any metadata -- so that we're
215215 return AnyChanged;
216216 }
217217
218 /// \brief Map a distinct MDNode.
218 /// Map a distinct MDNode.
219219 ///
220 /// Distinct nodes are not uniqued, so they must always recreated.
220 /// Whether distinct nodes change is independent of their operands. If \a
221 /// RF_MoveDistinctMDs, then they are reused, and their operands remapped in
222 /// place; effectively, they're moved from one graph to another. Otherwise,
223 /// they're cloned/duplicated, and the new copy's operands are remapped.
221224 static Metadata *mapDistinctNode(const MDNode *Node,
222225 SmallVectorImpl &Cycles,
223226 ValueToValueMapTy &VM, RemapFlags Flags,
225228 ValueMaterializer *Materializer) {
226229 assert(Node->isDistinct() && "Expected distinct node");
227230
228 MDNode *NewMD = MDNode::replaceWithDistinct(Node->clone());
231 MDNode *NewMD;
232 if (Flags & RF_MoveDistinctMDs)
233 NewMD = const_cast(Node);
234 else
235 NewMD = MDNode::replaceWithDistinct(Node->clone());
229236
230237 // Remap the operands. If any change, track those that could be involved in
231238 // uniquing cycles.
317324 Metadata *NewMD =
318325 MapMetadataImpl(MD, Cycles, VM, Flags, TypeMapper, Materializer);
319326
327 if ((Flags & RF_NoModuleLevelChanges) ||
328 (MD == NewMD && !(Flags & RF_MoveDistinctMDs))) {
329 assert(Cycles.empty() && "Unresolved cycles without remapping anything?");
330 return NewMD;
331 }
332
333 if (auto *N = dyn_cast(NewMD))
334 if (!N->isResolved())
335 N->resolveCycles();
336
320337 // Resolve cycles underneath MD.
321 if (NewMD && NewMD != MD) {
322 if (auto *N = dyn_cast(NewMD))
323 if (!N->isResolved())
324 N->resolveCycles();
325
326 for (MDNode *N : Cycles)
327 if (!N->isResolved())
328 N->resolveCycles();
329 } else {
330 // Shouldn't get unresolved cycles if nothing was remapped.
331 assert(Cycles.empty() && "Expected no unresolved cycles");
332 }
338 for (MDNode *N : Cycles)
339 if (!N->isResolved())
340 N->resolveCycles();
333341
334342 return NewMD;
335343 }
66 //
77 //===----------------------------------------------------------------------===//
88
9 #include "llvm/ADT/STLExtras.h"
910 #include "llvm/AsmParser/Parser.h"
1011 #include "llvm/IR/BasicBlock.h"
1112 #include "llvm/IR/DataLayout.h"
218219 LLVMDisposeMessage(errout);
219220 }
220221
222 TEST_F(LinkModuleTest, MoveDistinctMDs) {
223 LLVMContext C;
224 SMDiagnostic Err;
225
226 const char *SrcStr = "define void @foo() !attach !0 {\n"
227 "entry:\n"
228 " call void @llvm.md(metadata !1)\n"
229 " ret void, !attach !2\n"
230 "}\n"
231 "declare void @llvm.md(metadata)\n"
232 "!named = !{!3, !4}\n"
233 "!0 = distinct !{}\n"
234 "!1 = distinct !{}\n"
235 "!2 = distinct !{}\n"
236 "!3 = distinct !{}\n"
237 "!4 = !{!3}\n";
238
239 std::unique_ptr Src = parseAssemblyString(SrcStr, Err, C);
240 assert(Src);
241 ASSERT_TRUE(Src.get());
242
243 // Get the addresses of the Metadata before merging.
244 Function *F = &*Src->begin();
245 ASSERT_EQ("foo", F->getName());
246 BasicBlock *BB = &F->getEntryBlock();
247 auto *CI = cast(&BB->front());
248 auto *RI = cast(BB->getTerminator());
249 NamedMDNode *NMD = &*Src->named_metadata_begin();
250
251 MDNode *M0 = F->getMetadata("attach");
252 MDNode *M1 =
253 cast(cast(CI->getArgOperand(0))->getMetadata());
254 MDNode *M2 = RI->getMetadata("attach");
255 MDNode *M3 = NMD->getOperand(0);
256 MDNode *M4 = NMD->getOperand(1);
257
258 // Confirm a few things about the IR.
259 EXPECT_TRUE(M0->isDistinct());
260 EXPECT_TRUE(M1->isDistinct());
261 EXPECT_TRUE(M2->isDistinct());
262 EXPECT_TRUE(M3->isDistinct());
263 EXPECT_TRUE(M4->isUniqued());
264 EXPECT_EQ(M3, M4->getOperand(0));
265
266 // Link into destination module.
267 auto Dst = llvm::make_unique("Linked", C);
268 ASSERT_TRUE(Dst.get());
269 Linker::LinkModules(Dst.get(), Src.get(),
270 [](const llvm::DiagnosticInfo &) {});
271
272 // Check that distinct metadata was moved, not cloned. Even !4, the uniqued
273 // node, should effectively be moved, since its only operand hasn't changed.
274 F = &*Dst->begin();
275 BB = &F->getEntryBlock();
276 CI = cast(&BB->front());
277 RI = cast(BB->getTerminator());
278 NMD = &*Dst->named_metadata_begin();
279
280 EXPECT_EQ(M0, F->getMetadata("attach"));
281 EXPECT_EQ(M1, cast(CI->getArgOperand(0))->getMetadata());
282 EXPECT_EQ(M2, RI->getMetadata("attach"));
283 EXPECT_EQ(M3, NMD->getOperand(0));
284 EXPECT_EQ(M4, NMD->getOperand(1));
285
286 // Confirm a few things about the IR. This shouldn't have changed.
287 EXPECT_TRUE(M0->isDistinct());
288 EXPECT_TRUE(M1->isDistinct());
289 EXPECT_TRUE(M2->isDistinct());
290 EXPECT_TRUE(M3->isDistinct());
291 EXPECT_TRUE(M4->isUniqued());
292 EXPECT_EQ(M3, M4->getOperand(0));
293 }
294
221295 } // end anonymous namespace
2323 EXPECT_EQ(T.get(), MapMetadata(T.get(), VM, RF_NoModuleLevelChanges));
2424 }
2525
26 TEST(ValueMapperTest, MapMetadataDistinct) {
27 LLVMContext Context;
28 auto *D = MDTuple::getDistinct(Context, None);
29
30 {
31 // The node should be cloned.
32 ValueToValueMapTy VM;
33 EXPECT_NE(D, MapMetadata(D, VM, RF_None));
34 }
35 {
36 // The node should be moved.
37 ValueToValueMapTy VM;
38 EXPECT_EQ(D, MapMetadata(D, VM, RF_MoveDistinctMDs));
39 }
2640 }
41
42 TEST(ValueMapperTest, MapMetadataDistinctOperands) {
43 LLVMContext Context;
44 Metadata *Old = MDTuple::getDistinct(Context, None);
45 auto *D = MDTuple::getDistinct(Context, Old);
46 ASSERT_EQ(Old, D->getOperand(0));
47
48 Metadata *New = MDTuple::getDistinct(Context, None);
49 ValueToValueMapTy VM;
50 VM.MD()[Old].reset(New);
51
52 // Make sure operands are updated.
53 EXPECT_EQ(D, MapMetadata(D, VM, RF_MoveDistinctMDs));
54 EXPECT_EQ(New, D->getOperand(0));
55 }
56
57 }