[llvm] 49deb52 - [MC] Make pseudo-probe divisions ordering stable (#214803)

via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 7 18:21:45 PDT 2026


Author: Navya
Date: 2026-08-07T18:21:36-07:00
New Revision: 49deb5286c91c91155f557255c5be0c18127481e

URL: https://github.com/llvm/llvm-project/commit/49deb5286c91c91155f557255c5be0c18127481e
DIFF: https://github.com/llvm/llvm-project/commit/49deb5286c91c91155f557255c5be0c18127481e.diff

LOG: [MC] Make pseudo-probe divisions ordering stable (#214803)

**Problem**

`MCPseudoProbeSections::emit` sorts probe divisions only by section
ordinal.

Functions sharing a section will have a nondeterministic `unordered_map`
iteration order, producing *different .pseudo_probe bytes for identical
inputs.*

**Solution**


Use the function symbol name as a stable tie-breaker when section
ordinals match.

Add an MC regression test that reverses pseudo-probe insertion order and
requires byte-identical object files.

---------

Co-authored-by: Navya Zaveri <23720432+NavyaZaveri at users.noreply.github.com>
Co-authored-by: Wei Wang <apollo.mobility at gmail.com>

Added: 
    llvm/test/MC/ELF/pseudoprobe-order.s

Modified: 
    llvm/lib/MC/MCPseudoProbe.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/MC/MCPseudoProbe.cpp b/llvm/lib/MC/MCPseudoProbe.cpp
index 99cba8e3d8375..6151abed56db8 100644
--- a/llvm/lib/MC/MCPseudoProbe.cpp
+++ b/llvm/lib/MC/MCPseudoProbe.cpp
@@ -216,9 +216,11 @@ void MCPseudoProbeSections::emit(MCObjectStreamer *MCOS) {
     Vec.emplace_back(ProbeSec.first, &ProbeSec.second);
   for (auto I : llvm::enumerate(MCOS->getAssembler()))
     I.value().setOrdinal(I.index());
-  llvm::sort(Vec, [](auto A, auto B) {
-    return A.first->getSection().getOrdinal() <
-           B.first->getSection().getOrdinal();
+  llvm::sort(Vec, [](const auto &A, const auto &B) {
+    return std::make_pair(A.first->getSection().getOrdinal(),
+                          A.first->getName()) <
+           std::make_pair(B.first->getSection().getOrdinal(),
+                          B.first->getName());
   });
   for (auto [FuncSym, RootPtr] : Vec) {
     const auto &Root = *RootPtr;

diff  --git a/llvm/test/MC/ELF/pseudoprobe-order.s b/llvm/test/MC/ELF/pseudoprobe-order.s
new file mode 100644
index 0000000000000..9f331b52d2960
--- /dev/null
+++ b/llvm/test/MC/ELF/pseudoprobe-order.s
@@ -0,0 +1,31 @@
+# Test that pseudo-probe output does not depend on probe insertion order.
+# RUN: rm -rf %t && split-file %s %t
+# RUN: llvm-mc -triple=x86_64 -filetype=obj %t/forward.s -o %t/forward.o
+# RUN: llvm-mc -triple=x86_64 -filetype=obj %t/reverse.s -o %t/reverse.o
+# RUN: cmp %t/forward.o %t/reverse.o
+
+#--- forward.s
+.text
+.globl z_func
+.type z_func, at function
+z_func:
+  nop
+.globl a_func
+.type a_func, at function
+a_func:
+  nop
+.pseudoprobe 1 1 0 0 z_func
+.pseudoprobe 2 1 0 0 a_func
+
+#--- reverse.s
+.text
+.globl z_func
+.type z_func, at function
+z_func:
+  nop
+.globl a_func
+.type a_func, at function
+a_func:
+  nop
+.pseudoprobe 2 1 0 0 a_func
+.pseudoprobe 1 1 0 0 z_func


        


More information about the llvm-commits mailing list