Skip to content

[BOLT] Use std::tie to implement operator< (NFC) #143560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

kazutakahirata
Copy link
Contributor

std::tie facilitates lexicographical comparisons through std::tuple's
built-in operator<.

std::tie facilitates lexicographical comparisons through std::tuple's
built-in operator<.
@llvmbot
Copy link
Member

llvmbot commented Jun 10, 2025

@llvm/pr-subscribers-bolt

Author: Kazu Hirata (kazutakahirata)

Changes

std::tie facilitates lexicographical comparisons through std::tuple's
built-in operator<.


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

3 Files Affected:

  • (modified) bolt/include/bolt/Core/BinaryBasicBlock.h (+2-3)
  • (modified) bolt/include/bolt/Passes/PAuthGadgetScanner.h (+1-3)
  • (modified) bolt/lib/Profile/YAMLProfileWriter.cpp (+2-3)
diff --git a/bolt/include/bolt/Core/BinaryBasicBlock.h b/bolt/include/bolt/Core/BinaryBasicBlock.h
index 25cccc4edecf6..629f0ce8314dc 100644
--- a/bolt/include/bolt/Core/BinaryBasicBlock.h
+++ b/bolt/include/bolt/Core/BinaryBasicBlock.h
@@ -52,9 +52,8 @@ class BinaryBasicBlock {
     uint64_t MispredictedCount; /// number of branches mispredicted
 
     bool operator<(const BinaryBranchInfo &Other) const {
-      return (Count < Other.Count) ||
-             (Count == Other.Count &&
-              MispredictedCount < Other.MispredictedCount);
+      return std::tie(Count, MispredictedCount) <
+             std::tie(Other.Count, Other.MispredictedCount);
     }
   };
 
diff --git a/bolt/include/bolt/Passes/PAuthGadgetScanner.h b/bolt/include/bolt/Passes/PAuthGadgetScanner.h
index 98a49df862ebd..c6b9cc2eb4b9c 100644
--- a/bolt/include/bolt/Passes/PAuthGadgetScanner.h
+++ b/bolt/include/bolt/Passes/PAuthGadgetScanner.h
@@ -77,9 +77,7 @@ struct MCInstInBFReference {
     return BF == RHS.BF && Offset == RHS.Offset;
   }
   bool operator<(const MCInstInBFReference &RHS) const {
-    if (BF != RHS.BF)
-      return BF < RHS.BF;
-    return Offset < RHS.Offset;
+    return std::tie(BF, Offset) < std::tie(RHS.BF, RHS.Offset);
   }
   operator MCInst &() const {
     assert(BF != nullptr);
diff --git a/bolt/lib/Profile/YAMLProfileWriter.cpp b/bolt/lib/Profile/YAMLProfileWriter.cpp
index f1fe45f21a0f6..0ae67a4d35595 100644
--- a/bolt/lib/Profile/YAMLProfileWriter.cpp
+++ b/bolt/lib/Profile/YAMLProfileWriter.cpp
@@ -303,9 +303,8 @@ YAMLProfileWriter::convert(const BinaryFunction &BF, bool UseDFS,
       }
       // Sort targets in a similar way to getBranchData, see Location::operator<
       llvm::sort(CSTargets, [](const auto &RHS, const auto &LHS) {
-        if (RHS.first != LHS.first)
-          return RHS.first < LHS.first;
-        return RHS.second.Offset < LHS.second.Offset;
+        return std::tie(RHS.first, RHS.second.Offset) <
+               std::tie(LHS.first, LHS.second.Offset);
       });
       for (auto &KV : CSTargets)
         YamlBB.CallSites.push_back(KV.second);

@kazutakahirata kazutakahirata merged commit 8da9eb2 into llvm:main Jun 10, 2025
9 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20250610_operator_less_bolt branch June 10, 2025 18:31
rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
std::tie facilitates lexicographical comparisons through std::tuple's
built-in operator<.
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
std::tie facilitates lexicographical comparisons through std::tuple's
built-in operator<.
akuhlens pushed a commit to akuhlens/llvm-project that referenced this pull request Jun 24, 2025
std::tie facilitates lexicographical comparisons through std::tuple's
built-in operator<.
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.

3 participants