[PATCH] D149045: [Assignment Tracking] Fix faulty assertion inside std::sort predicate

Orlando Cazalet-Hyams via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 24 02:30:57 PDT 2023


Orlando created this revision.
Orlando added reviewers: jmorse, mstorsjo, srj.
Herald added subscribers: mgrang, hiraditya.
Herald added a project: All.
Orlando requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The vectors being sorted here shouldn't contain duplicate entries. Prior to this patch this was checked with an assert within the `std::sort` predicate. However, `std::sort` may compare an element against itself which causes the assert to fire (false positive). Move the assert outside of the sort predicate to avoid such issues.


https://reviews.llvm.org/D149045

Files:
  llvm/lib/CodeGen/AssignmentTrackingAnalysis.cpp


Index: llvm/lib/CodeGen/AssignmentTrackingAnalysis.cpp
===================================================================
--- llvm/lib/CodeGen/AssignmentTrackingAnalysis.cpp
+++ llvm/lib/CodeGen/AssignmentTrackingAnalysis.cpp
@@ -2011,16 +2011,17 @@
     }
   }
 
-  // Sort the fragment map for each DebugAggregate in non-descending
-  // order of fragment size. Assert no entries are duplicates.
+  // Sort the fragment map for each DebugAggregate in ascending
+  // order of fragment size - there should be no duplicates.
   for (auto &Pair : FragmentMap) {
     SmallVector<DebugVariable, 8> &Frags = Pair.second;
-    std::sort(
-        Frags.begin(), Frags.end(), [](DebugVariable Next, DebugVariable Elmt) {
-          assert(!(Elmt.getFragmentOrDefault() == Next.getFragmentOrDefault()));
-          return Elmt.getFragmentOrDefault().SizeInBits >
-                 Next.getFragmentOrDefault().SizeInBits;
-        });
+    std::sort(Frags.begin(), Frags.end(),
+              [](const DebugVariable &Next, const DebugVariable &Elmt) {
+                return Elmt.getFragmentOrDefault().SizeInBits >
+                       Next.getFragmentOrDefault().SizeInBits;
+              });
+    // Check for duplicates.
+    assert(std::adjacent_find(Frags.begin(), Frags.end()) == Frags.end());
   }
 
   // Build the map.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149045.516322.patch
Type: text/x-patch
Size: 1328 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230424/f02da22b/attachment.bin>


More information about the llvm-commits mailing list