[llvm] ff87ee4 - [Metadata] Utilize the resizing capability of MDNodes in Moduleflag processing.
Wolfgang Pieb via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 6 10:19:02 PDT 2022
Author: Wolfgang Pieb
Date: 2022-07-06T10:18:33-07:00
New Revision: ff87ee4dee9c01d841182d5959cec3a998115d34
URL: https://github.com/llvm/llvm-project/commit/ff87ee4dee9c01d841182d5959cec3a998115d34
DIFF: https://github.com/llvm/llvm-project/commit/ff87ee4dee9c01d841182d5959cec3a998115d34.diff
LOG: [Metadata] Utilize the resizing capability of MDNodes in Moduleflag processing.
This mostly affects PGO/LTO builds which use module flags describing the call
graph. Fixes Issue #51893.
Reviewed By: dexonsmith
Differential Revision: https://reviews.llvm.org/D125999
Added:
Modified:
llvm/lib/Linker/IRMover.cpp
llvm/lib/Transforms/Instrumentation/CGProfile.cpp
llvm/test/Instrumentation/cgprofile.ll
llvm/test/Linker/module-flags-8-a.ll
llvm/test/Linker/module-flags-dont-change-others.ll
llvm/test/Transforms/FunctionImport/cg_profile.ll
Removed:
################################################################################
diff --git a/llvm/lib/Linker/IRMover.cpp b/llvm/lib/Linker/IRMover.cpp
index 5a819e2d736c2..9e89cce8312e3 100644
--- a/llvm/lib/Linker/IRMover.cpp
+++ b/llvm/lib/Linker/IRMover.cpp
@@ -1357,11 +1357,18 @@ Error IRLinker::linkModuleFlagsMetadata() {
DstM.getModuleIdentifier() + "'");
}
- auto replaceDstValue = [&](MDNode *New) {
+ auto ensureDistinctOp = [&](MDNode *DstValue) {
+ assert(isa<MDTuple>(DstValue) &&
+ "Expected MDTuple when appending module flags");
+ if (DstValue->isDistinct())
+ return dyn_cast<MDTuple>(DstValue);
+ MDTuple *New = MDTuple::getDistinct(
+ DstM.getContext(), SmallVector<Metadata *, 4>(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
diff er and either source or destination
@@ -1438,25 +1445,20 @@ Error IRLinker::linkModuleFlagsMetadata() {
break;
}
case Module::Append: {
- MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
+ MDTuple *DstValue = ensureDistinctOp(cast<MDNode>(DstOp->getOperand(2)));
MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
- SmallVector<Metadata *, 8> 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<Metadata *, 16> Elts;
- MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
+ MDTuple *DstValue = ensureDistinctOp(cast<MDNode>(DstOp->getOperand(2)));
MDNode *SrcValue = cast<MDNode>(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
index b11b84d65d23b..57c491436b934 100644
--- a/llvm/lib/Transforms/Instrumentation/CGProfile.cpp
+++ b/llvm/lib/Transforms/Instrumentation/CGProfile.cpp
@@ -39,7 +39,8 @@ addModuleFlags(Module &M,
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
index 5e2f2665ab0f4..81df2ebeafdc9 100644
--- a/llvm/test/Instrumentation/cgprofile.ll
+++ b/llvm/test/Instrumentation/cgprofile.ll
@@ -34,7 +34,7 @@ B:
; 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
index b65a50fda8ed1..66d5998879cb4 100644
--- 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
index 4cc9f39027a33..aff3f8df47bd4 100644
--- 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
index 2cf920c2a5ddc..b61faa5e5ae77 100644
--- 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"
More information about the llvm-commits
mailing list