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

via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 7 10:53:27 PDT 2026


https://github.com/NavyaZaveri created https://github.com/llvm/llvm-project/pull/214803

**Problem**

MCPseudoProbeSections::emit sorted probe divisions only by section ordinal. 

Functions sharing a section therefore retained 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.

>From 239cecf45e440af58e51a97a64f56ce13b46efc7 Mon Sep 17 00:00:00 2001
From: Navya Zaveri <23720432+NavyaZaveri at users.noreply.github.com>
Date: Fri, 7 Aug 2026 10:51:10 -0700
Subject: [PATCH] [MC] Make pseudo-probe emission deterministic

---
 llvm/lib/MC/MCPseudoProbe.cpp        |  9 ++++++---
 llvm/test/MC/ELF/pseudoprobe-order.s | 30 ++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/MC/ELF/pseudoprobe-order.s

diff --git a/llvm/lib/MC/MCPseudoProbe.cpp b/llvm/lib/MC/MCPseudoProbe.cpp
index 99cba8e3d8375..c226a7706c049 100644
--- a/llvm/lib/MC/MCPseudoProbe.cpp
+++ b/llvm/lib/MC/MCPseudoProbe.cpp
@@ -216,9 +216,12 @@ 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) {
+    const auto AOrdinal = A.first->getSection().getOrdinal();
+    const auto BOrdinal = B.first->getSection().getOrdinal();
+    if (AOrdinal != BOrdinal)
+      return AOrdinal < BOrdinal;
+    return A.first->getName() < 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..a13177872dd75
--- /dev/null
+++ b/llvm/test/MC/ELF/pseudoprobe-order.s
@@ -0,0 +1,30 @@
+# 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