Skip to content

Conversation

kazutakahirata
Copy link
Contributor

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

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

llvmbot commented Jun 11, 2025

@llvm/pr-subscribers-lld-elf

@llvm/pr-subscribers-lld

Author: Kazu Hirata (kazutakahirata)

Changes

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


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

2 Files Affected:

  • (modified) lld/ELF/SyntheticSections.cpp (+2-5)
  • (modified) lld/MachO/UnwindInfoSection.cpp (+3-5)
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index 785a56cdb349e..0a9c7a081eb8b 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -1939,11 +1939,8 @@ bool AndroidPackedRelocationSection<ELFT>::updateAllocSize(Ctx &ctx) {
   // For Rela, we also want to sort by r_addend when r_info is the same. This
   // enables us to group by r_addend as well.
   llvm::sort(nonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) {
-    if (a.r_info != b.r_info)
-      return a.r_info < b.r_info;
-    if (a.r_addend != b.r_addend)
-      return a.r_addend < b.r_addend;
-    return a.r_offset < b.r_offset;
+    return std::tie(a.r_info, a.r_addend, a.r_offset) <
+           std::tie(b.r_info, b.r_addend, b.r_offset);
   });
 
   // Group relocations with the same r_info. Note that each group emits a group
diff --git a/lld/MachO/UnwindInfoSection.cpp b/lld/MachO/UnwindInfoSection.cpp
index 624464e41d77c..6e9f6c2aba749 100644
--- a/lld/MachO/UnwindInfoSection.cpp
+++ b/lld/MachO/UnwindInfoSection.cpp
@@ -535,11 +535,9 @@ void UnwindInfoSectionImpl::finalize() {
   llvm::sort(commonEncodings,
              [](const std::pair<compact_unwind_encoding_t, size_t> &a,
                 const std::pair<compact_unwind_encoding_t, size_t> &b) {
-               if (a.second == b.second)
-                 // When frequencies match, secondarily sort on encoding
-                 // to maintain parity with validate-unwind-info.py
-                 return a.first > b.first;
-               return a.second > b.second;
+               // When frequencies match, secondarily sort on encoding
+               // to maintain parity with validate-unwind-info.py
+               return std::tie(a.second, a.first) > std::tie(b.second, b.first);
              });
 
   // Truncate the vector to 127 elements.

@llvmbot
Copy link
Member

llvmbot commented Jun 11, 2025

@llvm/pr-subscribers-lld-macho

Author: Kazu Hirata (kazutakahirata)

Changes

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


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

2 Files Affected:

  • (modified) lld/ELF/SyntheticSections.cpp (+2-5)
  • (modified) lld/MachO/UnwindInfoSection.cpp (+3-5)
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index 785a56cdb349e..0a9c7a081eb8b 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -1939,11 +1939,8 @@ bool AndroidPackedRelocationSection<ELFT>::updateAllocSize(Ctx &ctx) {
   // For Rela, we also want to sort by r_addend when r_info is the same. This
   // enables us to group by r_addend as well.
   llvm::sort(nonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) {
-    if (a.r_info != b.r_info)
-      return a.r_info < b.r_info;
-    if (a.r_addend != b.r_addend)
-      return a.r_addend < b.r_addend;
-    return a.r_offset < b.r_offset;
+    return std::tie(a.r_info, a.r_addend, a.r_offset) <
+           std::tie(b.r_info, b.r_addend, b.r_offset);
   });
 
   // Group relocations with the same r_info. Note that each group emits a group
diff --git a/lld/MachO/UnwindInfoSection.cpp b/lld/MachO/UnwindInfoSection.cpp
index 624464e41d77c..6e9f6c2aba749 100644
--- a/lld/MachO/UnwindInfoSection.cpp
+++ b/lld/MachO/UnwindInfoSection.cpp
@@ -535,11 +535,9 @@ void UnwindInfoSectionImpl::finalize() {
   llvm::sort(commonEncodings,
              [](const std::pair<compact_unwind_encoding_t, size_t> &a,
                 const std::pair<compact_unwind_encoding_t, size_t> &b) {
-               if (a.second == b.second)
-                 // When frequencies match, secondarily sort on encoding
-                 // to maintain parity with validate-unwind-info.py
-                 return a.first > b.first;
-               return a.second > b.second;
+               // When frequencies match, secondarily sort on encoding
+               // to maintain parity with validate-unwind-info.py
+               return std::tie(a.second, a.first) > std::tie(b.second, b.first);
              });
 
   // Truncate the vector to 127 elements.

@kazutakahirata kazutakahirata merged commit c1d21f4 into llvm:main Jun 11, 2025
11 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20250611_operator_less_lld branch June 11, 2025 19:50
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< and 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< and operator>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants