diff --git a/llvm/lib/Linker/IRMover.cpp b/llvm/lib/Linker/IRMover.cpp --- a/llvm/lib/Linker/IRMover.cpp +++ b/llvm/lib/Linker/IRMover.cpp @@ -1357,11 +1357,18 @@ DstM.getModuleIdentifier() + "'"); } - auto replaceDstValue = [&](MDNode *New) { + auto ensureDistinctOp = [&](MDNode *DstValue) { + assert(isa(DstValue) && + "Expected MDTuple when appending module flags"); + if (DstValue->isDistinct()) + return dyn_cast(DstValue); + MDTuple *New = MDTuple::getDistinct( + DstM.getContext(), SmallVector(DstValue->operands())); Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New}; - MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps); + MDNode *Flag = MDTuple::getDistinct(DstM.getContext(), FlagOps); DstModFlags->setOperand(DstIndex, Flag); Flags[ID].first = Flag; + return New; }; // Emit a warning if the values differ and either source or destination @@ -1438,25 +1445,20 @@ break; } case Module::Append: { - MDNode *DstValue = cast(DstOp->getOperand(2)); + MDTuple *DstValue = ensureDistinctOp(cast(DstOp->getOperand(2))); MDNode *SrcValue = cast(SrcOp->getOperand(2)); - SmallVector MDs; - MDs.reserve(DstValue->getNumOperands() + SrcValue->getNumOperands()); - MDs.append(DstValue->op_begin(), DstValue->op_end()); - MDs.append(SrcValue->op_begin(), SrcValue->op_end()); - - replaceDstValue(MDNode::get(DstM.getContext(), MDs)); + for (const auto &O : SrcValue->operands()) + DstValue->push_back(O); break; } case Module::AppendUnique: { SmallSetVector Elts; - MDNode *DstValue = cast(DstOp->getOperand(2)); + MDTuple *DstValue = ensureDistinctOp(cast(DstOp->getOperand(2))); MDNode *SrcValue = cast(SrcOp->getOperand(2)); Elts.insert(DstValue->op_begin(), DstValue->op_end()); Elts.insert(SrcValue->op_begin(), SrcValue->op_end()); - - replaceDstValue(MDNode::get(DstM.getContext(), - makeArrayRef(Elts.begin(), Elts.end()))); + for (auto I = DstValue->getNumOperands(); I < Elts.size(); I++) + DstValue->push_back(Elts[I]); break; } } diff --git a/llvm/lib/Transforms/Instrumentation/CGProfile.cpp b/llvm/lib/Transforms/Instrumentation/CGProfile.cpp --- a/llvm/lib/Transforms/Instrumentation/CGProfile.cpp +++ b/llvm/lib/Transforms/Instrumentation/CGProfile.cpp @@ -39,7 +39,8 @@ Nodes.push_back(MDNode::get(Context, Vals)); } - M.addModuleFlag(Module::Append, "CG Profile", MDNode::get(Context, Nodes)); + M.addModuleFlag(Module::Append, "CG Profile", + MDTuple::getDistinct(Context, Nodes)); return true; } diff --git a/llvm/test/Instrumentation/cgprofile.ll b/llvm/test/Instrumentation/cgprofile.ll --- a/llvm/test/Instrumentation/cgprofile.ll +++ b/llvm/test/Instrumentation/cgprofile.ll @@ -34,7 +34,7 @@ ; CHECK: !llvm.module.flags = !{![[cgprof:[0-9]+]]} ; CHECK: ![[cgprof]] = !{i32 5, !"CG Profile", ![[prof:[0-9]+]]} -; CHECK: ![[prof]] = !{![[e0:[0-9]+]], ![[e1:[0-9]+]], ![[e2:[0-9]+]], ![[e3:[0-9]+]], ![[e4:[0-9]+]], ![[e5:[0-9]+]], ![[e6:[0-9]+]]} +; CHECK: ![[prof]] = distinct !{![[e0:[0-9]+]], ![[e1:[0-9]+]], ![[e2:[0-9]+]], ![[e3:[0-9]+]], ![[e4:[0-9]+]], ![[e5:[0-9]+]], ![[e6:[0-9]+]]} ; CHECK: ![[e0]] = !{void ()* @a, void ()* @b, i64 32} ; CHECK: ![[e1]] = !{void (i1)* @freq, i32 ()* @func4, i64 1030} ; CHECK: ![[e2]] = !{void (i1)* @freq, i32 ()* @func2, i64 410} diff --git a/llvm/test/Linker/module-flags-8-a.ll b/llvm/test/Linker/module-flags-8-a.ll --- a/llvm/test/Linker/module-flags-8-a.ll +++ b/llvm/test/Linker/module-flags-8-a.ll @@ -2,10 +2,10 @@ ; Test append-type module flags. -; CHECK: !0 = !{i32 5, !"flag-0", !1} -; CHECK: !1 = !{i32 0, i32 0, i32 1} -; CHECK: !2 = !{i32 6, !"flag-1", !3} -; CHECK: !3 = !{i32 0, i32 1, i32 2} +; CHECK: !0 = distinct !{i32 5, !"flag-0", !1} +; CHECK: !1 = distinct !{i32 0, i32 0, i32 1} +; CHECK: !2 = distinct !{i32 6, !"flag-1", !3} +; CHECK: !3 = distinct !{i32 0, i32 1, i32 2} ; CHECK: !llvm.module.flags = !{!0, !2} !0 = !{ i32 5, !"flag-0", !{ i32 0 } } diff --git a/llvm/test/Linker/module-flags-dont-change-others.ll b/llvm/test/Linker/module-flags-dont-change-others.ll --- a/llvm/test/Linker/module-flags-dont-change-others.ll +++ b/llvm/test/Linker/module-flags-dont-change-others.ll @@ -5,7 +5,7 @@ ; happen to also serve as module flags. ; CHECK: !named = !{!0, !1, !2, !3, !4, !5} -; CHECK: !llvm.module.flags = !{!6, !7, !8} +; CHECK: !llvm.module.flags = !{!6, !7, !9} !named = !{!0, !1, !2, !3, !4, !5} !llvm.module.flags = !{!3, !4, !5} @@ -16,8 +16,10 @@ ; CHECK: !4 = !{i32 5, !"bar", !0} ; CHECK: !5 = !{i32 6, !"baz", !1} ; CHECK: !6 = !{i32 4, !"foo", i32 37} -; CHECK: !7 = !{i32 5, !"bar", !1} -; CHECK: !8 = !{i32 6, !"baz", !2} +; CHECK: !7 = distinct !{i32 5, !"bar", !8} +; CHECK: !8 = distinct !{!0} +; CHECK: !9 = distinct !{i32 6, !"baz", !10} +; CHECK: !10 = distinct !{!0, !1} !0 = !{} !1 = !{!0} !2 = !{!0, !1} diff --git a/llvm/test/Transforms/FunctionImport/cg_profile.ll b/llvm/test/Transforms/FunctionImport/cg_profile.ll --- a/llvm/test/Transforms/FunctionImport/cg_profile.ll +++ b/llvm/test/Transforms/FunctionImport/cg_profile.ll @@ -9,7 +9,7 @@ ; CHECK: !0 = !{i32 1, !"EnableSplitLTOUnit", i32 0} ; CHECK-NEXT: !1 = !{i32 5, !"CG Profile", !2} -; CHECK-NEXT: !2 = !{!3} +; CHECK-NEXT: !2 = distinct !{!3} ; CHECK-NEXT: !3 = !{void ()* @foo, void (%class.A*)* bitcast (void (%class.A.0*)* @bar to void (%class.A*)*), i64 2753} target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"