[llvm] [FuncComp] Compare MDNodes in cmpMetadata using cmpMDNode. (PR #128878)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 26 13:29:59 PST 2025


================
@@ -207,22 +212,51 @@ int FunctionComparator::cmpMetadata(const Metadata *L,
 
   auto *CL = dyn_cast<ConstantAsMetadata>(L);
   auto *CR = dyn_cast<ConstantAsMetadata>(R);
-  if (CL == CR)
-    return 0;
-  if (!CL)
+  if (CL && CR) {
+    if (!CL)
+      return -1;
+    if (!CR)
+      return 1;
+    return cmpConstants(CL->getValue(), CR->getValue());
+  }
+
+  auto *NodeL = dyn_cast<const MDNode>(L);
+  auto *NodeR = dyn_cast<const MDNode>(R);
+  if (NodeL && NodeR)
+    return cmpMDNode(NodeL, NodeR, SeenL, SeenR);
+
+  if (NodeR)
     return -1;
-  if (!CR)
+
+  if (NodeL)
     return 1;
-  return cmpConstants(CL->getValue(), CR->getValue());
+
+  assert(false);
+
+  return 0;
 }
 
-int FunctionComparator::cmpMDNode(const MDNode *L, const MDNode *R) const {
+int FunctionComparator::cmpMDNode(
+    const MDNode *L, const MDNode *R, SmallPtrSetImpl<const MDNode *> &SeenL,
+    SmallPtrSetImpl<const MDNode *> &SeenR) const {
   if (L == R)
     return 0;
   if (!L)
     return -1;
   if (!R)
     return 1;
+
+  // Check if we already checked either L or R previosuly. This can be the case
+  // for metadata nodes with cycles.
+  bool AlreadySeenL = !SeenL.insert(L).second;
+  bool AlreadySeenR = !SeenR.insert(R).second;
+  if (AlreadySeenL && AlreadySeenR)
+    return 0;
----------------
nikic wrote:

I don't think that both being recursive really guarantees that they are equal, they may recurse in different ways, no?

https://github.com/llvm/llvm-project/pull/128878


More information about the llvm-commits mailing list