[PATCH] D38830: [DWARF] Fix bad comparator in sortGlobalExprs.

Eli Friedman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 11 16:45:05 PDT 2017


efriedma created this revision.
Herald added a subscriber: JDevlieghere.

The comparator passed to std::sort must provide a strict weak ordering; otherwise, the behavior is undefined.

Fixes an assertion failure generating debug info for globals optimized by GlobalOpt. I have a testcase, but not sure how to reduce it, so not included here.

(I think this issue was exposed by r312318.)


Repository:
  rL LLVM

https://reviews.llvm.org/D38830

Files:
  lib/CodeGen/AsmPrinter/DwarfDebug.cpp


Index: lib/CodeGen/AsmPrinter/DwarfDebug.cpp
===================================================================
--- lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -508,13 +508,15 @@
 sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &GVEs) {
   std::sort(GVEs.begin(), GVEs.end(),
             [](DwarfCompileUnit::GlobalExpr A, DwarfCompileUnit::GlobalExpr B) {
-              if (A.Expr != B.Expr && A.Expr && B.Expr) {
-                auto FragmentA = A.Expr->getFragmentInfo();
-                auto FragmentB = B.Expr->getFragmentInfo();
-                if (FragmentA && FragmentB)
-                  return FragmentA->OffsetInBits < FragmentB->OffsetInBits;
-              }
-              return false;
+              // First null exprs, then exprs without fragment info, then sort
+              // by fragment offset in bits.
+              if (!A.Expr || !B.Expr)
+                return !!B.Expr;
+              auto FragmentA = A.Expr->getFragmentInfo();
+              auto FragmentB = B.Expr->getFragmentInfo();
+              if (!FragmentA || !FragmentB)
+                return !!FragmentB;
+              return FragmentA->OffsetInBits < FragmentB->OffsetInBits;
             });
   GVEs.erase(std::unique(GVEs.begin(), GVEs.end(),
                          [](DwarfCompileUnit::GlobalExpr A,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38830.118725.patch
Type: text/x-patch
Size: 1363 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171011/6d528d04/attachment.bin>


More information about the llvm-commits mailing list