[PATCH] Improve debug message of the layout pass.

Rui Ueyama ruiu at google.com
Thu Oct 17 15:38:38 PDT 2013


Instead of showing multiple lines of debug messages, show only one message
by CompareAtoms::operator(). Here is an example.

Before:
  Sorting _main .text
  Sorting by sectionPos(2,2)
  Sorting by override
  Sorting _main .text
  Sorting by sectionPos(2,2)
  Sorting by override

After:
  Layout: '_main' > '.text' (override (1, 0))
  Layout: '_main' > '.text' (override (1, 0))

http://llvm-reviews.chandlerc.com/D1964

Files:
  include/lld/Passes/LayoutPass.h
  lib/Passes/LayoutPass.cpp

Index: include/lld/Passes/LayoutPass.h
===================================================================
--- include/lld/Passes/LayoutPass.h
+++ include/lld/Passes/LayoutPass.h
@@ -19,6 +19,7 @@
 #include "llvm/ADT/DenseMap.h"
 
 #include <map>
+#include <string>
 #include <vector>
 
 namespace lld {
@@ -39,6 +40,8 @@
     explicit CompareAtoms(const LayoutPass &pass) : _layout(pass) {}
     bool operator()(const DefinedAtom *left, const DefinedAtom *right) const;
   private:
+    bool compare(const DefinedAtom *left, const DefinedAtom *right,
+                 std::string &reason) const;
     const LayoutPass &_layout;
   };
 
Index: lib/Passes/LayoutPass.cpp
===================================================================
--- lib/Passes/LayoutPass.cpp
+++ lib/Passes/LayoutPass.cpp
@@ -20,36 +20,44 @@
 
 using namespace lld;
 
+namespace {
+// Return "reason (leftval, rightval)"
+std::string formatReason(StringRef reason, int leftVal, int rightVal) {
+  Twine msg = Twine(reason) + " (" + Twine(leftVal) + ", " + Twine(rightVal)
+      + ")";
+  return std::move(msg.str());
+}
+}
+
 /// The function compares atoms by sorting atoms in the following order
 /// a) Sorts atoms by Section position preference
 /// b) Sorts atoms by their ordinal overrides
 ///    (layout-after/layout-before/ingroup)
 /// c) Sorts atoms by their permissions
 /// d) Sorts atoms by their content
 /// e) Sorts atoms on how they appear using File Ordinality
 /// f) Sorts atoms on how they appear within the File
-bool LayoutPass::CompareAtoms::operator()(const DefinedAtom *left,
-                                          const DefinedAtom *right) const {
-  DEBUG(llvm::dbgs() << "Sorting " << left->name() << " " << right->name() << "\n");
-  if (left == right)
+bool LayoutPass::CompareAtoms::compare(
+    const DefinedAtom *left, const DefinedAtom *right, std::string &reason)
+    const {
+  if (left == right) {
+    reason = "same";
     return false;
+  }
 
   // Sort by section position preference.
   DefinedAtom::SectionPosition leftPos = left->sectionPosition();
   DefinedAtom::SectionPosition rightPos = right->sectionPosition();
 
-  DEBUG(llvm::dbgs() << "Sorting by sectionPos"
-                     << "(" << leftPos << "," << rightPos << ")\n");
-
   bool leftSpecialPos = (leftPos != DefinedAtom::sectionPositionAny);
   bool rightSpecialPos = (rightPos != DefinedAtom::sectionPositionAny);
   if (leftSpecialPos || rightSpecialPos) {
-    if (leftPos != rightPos)
+    if (leftPos != rightPos) {
+      DEBUG(reason = formatReason("sectionPos", (int)leftPos, (int)rightPos));
       return leftPos < rightPos;
+    }
   }
 
-  DEBUG(llvm::dbgs() << "Sorting by override\n");
-
   AtomToOrdinalT::const_iterator lPos = _layout._ordinalOverrideMap.find(left);
   AtomToOrdinalT::const_iterator rPos = _layout._ordinalOverrideMap.find(right);
   AtomToOrdinalT::const_iterator end = _layout._ordinalOverrideMap.end();
@@ -63,57 +71,66 @@
       rightAtom != _layout._followOnRoots.end() &&
       leftAtom->second == rightAtom->second) {
     if ((lPos != end) && (rPos != end)) {
+      DEBUG(reason = formatReason("override", lPos->second, rPos->second));
       return lPos->second < rPos->second;
     }
   }
 
   // Sort same permissions together.
   DefinedAtom::ContentPermissions leftPerms = left->permissions();
   DefinedAtom::ContentPermissions rightPerms = right->permissions();
 
-  DEBUG(llvm::dbgs() << "Sorting by contentPerms"
-                     << "(" << leftPerms << "," << rightPerms << ")\n");
-
-  if (leftPerms != rightPerms)
+  if (leftPerms != rightPerms) {
+    DEBUG(reason = formatReason("contentPerms", (int)leftPerms,
+                                (int)rightPerms));
     return leftPerms < rightPerms;
+  }
 
   // Sort same content types together.
   DefinedAtom::ContentType leftType = left->contentType();
   DefinedAtom::ContentType rightType = right->contentType();
 
-  DEBUG(llvm::dbgs() << "Sorting by contentType"
-                     << "(" << leftType << "," << rightType << ")\n");
-
-  if (leftType != rightType)
+  if (leftType != rightType) {
+    DEBUG(reason = formatReason("contentType", (int)leftType, (int)rightType));
     return leftType < rightType;
+  }
 
   // Sort by .o order.
   const File *leftFile = &left->file();
   const File *rightFile = &right->file();
 
-  DEBUG(llvm::dbgs()
-        << "Sorting by .o order("
-        << "(" << leftFile->ordinal() << "," << rightFile->ordinal() << ")"
-        << "[" << leftFile->path() << "," << rightFile->path() << "]\n");
-
-  if (leftFile != rightFile)
+  if (leftFile != rightFile) {
+    DEBUG(reason = formatReason(".o order", (int)leftFile->ordinal(),
+                                (int)rightFile->ordinal()));
     return leftFile->ordinal() < rightFile->ordinal();
+  }
 
   // Sort by atom order with .o file.
   uint64_t leftOrdinal = left->ordinal();
   uint64_t rightOrdinal = right->ordinal();
 
-  DEBUG(llvm::dbgs() << "Sorting by ordinal(" << left->ordinal() << ","
-                     << right->ordinal() << ")\n");
-
-  if (leftOrdinal != rightOrdinal)
+  if (leftOrdinal != rightOrdinal) {
+    DEBUG(reason = formatReason("ordinal", (int)left->ordinal(),
+                                (int)right->ordinal()));
     return leftOrdinal < rightOrdinal;
+  }
 
   DEBUG(llvm::dbgs() << "Unordered\n");
-
   llvm_unreachable("Atoms with Same Ordinal!");
 }
 
+bool LayoutPass::CompareAtoms::operator()(const DefinedAtom *left,
+                                          const DefinedAtom *right) const {
+  std::string reason;
+  bool result = compare(left, right, reason);
+  DEBUG({
+    StringRef comp = result ? "<" : ">";
+    llvm::dbgs() << "Layout: '" << left->name() << "' " << comp << " '" << right->name()
+                 << "' (" << reason << ")\n";
+  });
+  return result;
+}
+
 // Returns the atom immediately followed by the given atom in the followon
 // chain.
 const DefinedAtom *LayoutPass::findAtomFollowedBy(
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1964.1.patch
Type: text/x-patch
Size: 5992 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20131017/d073f167/attachment.bin>


More information about the llvm-commits mailing list