Skip to content

Conversation

@Enna1
Copy link
Contributor

@Enna1 Enna1 commented Nov 14, 2024

This change extracts the comparator for sorting functions by index into a helper function compareBinaryFunctionByIndex()

Not sure why the comparator used in BinaryContext::getSortedFunctions() is not same as the other two places. I think they should use the same comparator, so I also change BinaryContext::getSortedFunctions() to use compareBinaryFunctionByIndex() for sorting functions.

…function

This change extracts the comparator for sorting functions by index into a helper
function `compareBinaryFunctionByIndex()`

Not sure why the comparator used in `BinaryContext::getSortedFunctions()` is not
same as the other two places. I think they should use the same comparator, so I
also change `BinaryContext::getSortedFunctions()` to use
`compareBinaryFunctionByIndex()` for sorting functions.
@Enna1 Enna1 force-pushed the users/Enna1/bolt-extract-function-compare-by-index branch from c2c7b25 to 714df37 Compare November 14, 2024 12:29
@Enna1 Enna1 marked this pull request as ready for review November 14, 2024 13:09
@llvmbot llvmbot added the BOLT label Nov 14, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 14, 2024

@llvm/pr-subscribers-bolt

Author: Enna1 (Enna1)

Changes

This change extracts the comparator for sorting functions by index into a helper function compareBinaryFunctionByIndex()

Not sure why the comparator used in BinaryContext::getSortedFunctions() is not same as the other two places. I think they should use the same comparator, so I also change BinaryContext::getSortedFunctions() to use compareBinaryFunctionByIndex() for sorting functions.


Full diff: https://github.com/llvm/llvm-project/pull/116217.diff

4 Files Affected:

  • (modified) bolt/include/bolt/Core/BinaryFunction.h (+13)
  • (modified) bolt/lib/Core/BinaryContext.cpp (+1-7)
  • (modified) bolt/lib/Core/BinaryFunction.cpp (+1-10)
  • (modified) bolt/lib/Passes/ReorderFunctions.cpp (+1-10)
diff --git a/bolt/include/bolt/Core/BinaryFunction.h b/bolt/include/bolt/Core/BinaryFunction.h
index 0b3682353f736e..4da08c5d662349 100644
--- a/bolt/include/bolt/Core/BinaryFunction.h
+++ b/bolt/include/bolt/Core/BinaryFunction.h
@@ -2366,6 +2366,19 @@ inline raw_ostream &operator<<(raw_ostream &OS,
   return OS;
 }
 
+/// Compare function by index if it is valid, fall back to the original address
+/// otherwise.
+inline bool compareBinaryFunctionByIndex(const BinaryFunction *A,
+                                         const BinaryFunction *B) {
+  if (A->hasValidIndex() && B->hasValidIndex())
+    return A->getIndex() < B->getIndex();
+  if (A->hasValidIndex() && !B->hasValidIndex())
+    return true;
+  if (!A->hasValidIndex() && B->hasValidIndex())
+    return false;
+  return A->getAddress() < B->getAddress();
+}
+
 } // namespace bolt
 
 // GraphTraits specializations for function basic block graphs (CFGs)
diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp
index f246750209d6c4..a808ece12da19c 100644
--- a/bolt/lib/Core/BinaryContext.cpp
+++ b/bolt/lib/Core/BinaryContext.cpp
@@ -1607,13 +1607,7 @@ std::vector<BinaryFunction *> BinaryContext::getSortedFunctions() {
                   SortedFunctions.begin(),
                   [](BinaryFunction &BF) { return &BF; });
 
-  llvm::stable_sort(SortedFunctions,
-                    [](const BinaryFunction *A, const BinaryFunction *B) {
-                      if (A->hasValidIndex() && B->hasValidIndex()) {
-                        return A->getIndex() < B->getIndex();
-                      }
-                      return A->hasValidIndex();
-                    });
+  llvm::stable_sort(SortedFunctions, compareBinaryFunctionByIndex);
   return SortedFunctions;
 }
 
diff --git a/bolt/lib/Core/BinaryFunction.cpp b/bolt/lib/Core/BinaryFunction.cpp
index 5da777411ba7a1..a9ccaea3c43850 100644
--- a/bolt/lib/Core/BinaryFunction.cpp
+++ b/bolt/lib/Core/BinaryFunction.cpp
@@ -385,16 +385,7 @@ bool BinaryFunction::isForwardCall(const MCSymbol *CalleeSymbol) const {
   if (CalleeBF) {
     if (CalleeBF->isInjected())
       return true;
-
-    if (hasValidIndex() && CalleeBF->hasValidIndex()) {
-      return getIndex() < CalleeBF->getIndex();
-    } else if (hasValidIndex() && !CalleeBF->hasValidIndex()) {
-      return true;
-    } else if (!hasValidIndex() && CalleeBF->hasValidIndex()) {
-      return false;
-    } else {
-      return getAddress() < CalleeBF->getAddress();
-    }
+    return compareBinaryFunctionByIndex(this, CalleeBF);
   } else {
     // Absolute symbol.
     ErrorOr<uint64_t> CalleeAddressOrError = BC.getSymbolValue(*CalleeSymbol);
diff --git a/bolt/lib/Passes/ReorderFunctions.cpp b/bolt/lib/Passes/ReorderFunctions.cpp
index c2d540135bdaa1..1256d71342b13b 100644
--- a/bolt/lib/Passes/ReorderFunctions.cpp
+++ b/bolt/lib/Passes/ReorderFunctions.cpp
@@ -473,16 +473,7 @@ Error ReorderFunctions::runOnFunctions(BinaryContext &BC) {
                     [](BinaryFunction &BF) { return &BF; });
 
     // Sort functions by index.
-    llvm::stable_sort(SortedFunctions,
-                      [](const BinaryFunction *A, const BinaryFunction *B) {
-                        if (A->hasValidIndex() && B->hasValidIndex())
-                          return A->getIndex() < B->getIndex();
-                        if (A->hasValidIndex() && !B->hasValidIndex())
-                          return true;
-                        if (!A->hasValidIndex() && B->hasValidIndex())
-                          return false;
-                        return A->getAddress() < B->getAddress();
-                      });
+    llvm::stable_sort(SortedFunctions, compareBinaryFunctionByIndex);
 
     for (const BinaryFunction *Func : SortedFunctions) {
       if (!Func->hasValidIndex())

@Enna1
Copy link
Contributor Author

Enna1 commented Nov 21, 2024

ping:)

Copy link
Contributor

@peterwaller-arm peterwaller-arm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a reasonable change to me.

Copy link
Contributor

@maksfb maksfb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@Enna1 Enna1 merged commit 4d2bc0a into main Nov 27, 2024
10 checks passed
@Enna1 Enna1 deleted the users/Enna1/bolt-extract-function-compare-by-index branch November 27, 2024 01:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants