[PATCH] D158875: [ADT] Fix IntEqClasses::join to return the leader in all cases.

Joshua Cranmer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 25 12:05:29 PDT 2023


jcranmer-intel created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
jcranmer-intel requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The method is specified to return the leader of the result of a join. If
the two members were already in an equivalence class, there is a chance
that it might fail to return the leader. For example, if the array looks
like this:

EC[0] = 0
EC[1] = 0
EC[2] = 1
EC[3] = 1

Then join(2, 3) would have returned 1, which is not the same as the
actual leader 0.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158875

Files:
  llvm/lib/Support/IntEqClasses.cpp
  llvm/unittests/ADT/IntEqClassesTest.cpp


Index: llvm/unittests/ADT/IntEqClassesTest.cpp
===================================================================
--- llvm/unittests/ADT/IntEqClassesTest.cpp
+++ llvm/unittests/ADT/IntEqClassesTest.cpp
@@ -103,4 +103,17 @@
   EXPECT_EQ(0u, ec.findLeader(9));
 }
 
+TEST(IntEqClasses, JoinReturnsLeader) {
+  IntEqClasses ec(10);
+  EXPECT_EQ(1u, ec.join(1, 2));
+  EXPECT_EQ(1u, ec.join(1, 3));
+  EXPECT_EQ(0u, ec.join(0, 1));
+  EXPECT_EQ(0u, ec.findLeader(1));
+  // At this point, EC[2] = EC[3] = 1, but EC[1] = 0. Be sure that trying to
+  // join EC[2] and EC[3] returns the correct leader, 0, and not their common
+  // node 1.
+  EXPECT_EQ(0u, ec.join(2, 3));
+  EXPECT_EQ(0u, ec.findLeader(2));
+}
+
 } // end anonymous namespace
Index: llvm/lib/Support/IntEqClasses.cpp
===================================================================
--- llvm/lib/Support/IntEqClasses.cpp
+++ llvm/lib/Support/IntEqClasses.cpp
@@ -47,6 +47,9 @@
       eca = EC[a];
     }
 
+  // Make sure that eca is actually the leader of the equivalence class (this
+  // can happen if a and b were already in the same equivalence class).
+  eca = findLeader(eca);
   return eca;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158875.553569.patch
Type: text/x-patch
Size: 1170 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230825/add1a3a5/attachment.bin>


More information about the llvm-commits mailing list