[PATCH] D38586: SLPVectorizer.cpp: Avoid std::stable_sort() due to asymmetric comparator

NAKAMURA Takumi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 5 07:45:56 PDT 2017


chapuni created this revision.
Herald added a subscriber: rengolin.

This causes different output between libstdc++ and libc++.

Consider A,B,C,D,E, and F.
DT->properlyDominates() says;

- D dominates A
- E dominates C
- F dominates B

libstdc++'s stable_sort did;

1. D (dominates A)
2. A
3. B
4. E (dominates C)
5. C
6. F (oughta dominate B...)

F was not hoisted.

libc++ didn't modify order, oops.

I suggest to introduce **stupid** sort here. Please suggest me if you had better algorithm.


Repository:
  rL LLVM

https://reviews.llvm.org/D38586

Files:
  llvm/trunk/lib/Transforms/Vectorize/SLPVectorizer.cpp


Index: llvm/trunk/lib/Transforms/Vectorize/SLPVectorizer.cpp
===================================================================
--- llvm/trunk/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ llvm/trunk/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -3344,10 +3344,11 @@
 
   // Sort blocks by domination. This ensures we visit a block after all blocks
   // dominating it are visited.
-  std::stable_sort(CSEWorkList.begin(), CSEWorkList.end(),
-                   [this](const DomTreeNode *A, const DomTreeNode *B) {
-    return DT->properlyDominates(A, B);
-  });
+  // std::sort is unavailable since the comparator is asymmetric.
+  for (auto A = CSEWorkList.begin(), E = CSEWorkList.end(); A != E; ++A)
+    for (auto B = std::next(A); B != E; ++B)
+      if (DT->properlyDominates(*B, *A))
+        std::swap(*A, *B);
 
   // Perform O(N^2) search over the gather sequences and merge identical
   // instructions. TODO: We can further optimize this scan if we split the


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38586.117816.patch
Type: text/x-patch
Size: 974 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171005/170b0b11/attachment.bin>


More information about the llvm-commits mailing list