[llvm] [BOLT] Make ICF bucket iteration order deterministic for single-threaded mode (PR #200706)
Kito Cheng via llvm-commits
llvm-commits at lists.llvm.org
Sun Jun 28 20:20:18 PDT 2026
https://github.com/kito-cheng updated https://github.com/llvm/llvm-project/pull/200706
>From d3be607c35d8ff473ba2eb680d6a9a9ed2139a2b Mon Sep 17 00:00:00 2001
From: Kito Cheng <kito.cheng at sifive.com>
Date: Fri, 29 May 2026 03:00:20 -0700
Subject: [PATCH] [BOLT] Make ICF bucket iteration order deterministic for
single-threaded mode
`CongruentBuckets` is an unordered_map, so iterating it directly produces
non-deterministic folding order across different build environments. Sort
buckets by the binary address of the representative function before iterating
to guarantee a stable debug output order.
That order isn't matter for the output binary, but it cause randomly different
debug output across different build environments, which makes it hard to write
stable tests for ICF.
Also the debug output of multi-threaded ICF is already non-deterministic even
we sort that by address, so we only try to stabilize the single-threaded ICF
output.
Update the icf-safe tests to reflect the new deterministic ordering.
Assisted-by: Sonnet 4.6
---
bolt/lib/Passes/IdenticalCodeFolding.cpp | 34 ++++++++++++-------
bolt/test/X86/icf-safe-icp.test | 2 +-
bolt/test/X86/icf-safe-test1.test | 2 +-
.../icf-safe-test2GlobalConstPtrNoPic.test | 2 +-
4 files changed, 25 insertions(+), 15 deletions(-)
diff --git a/bolt/lib/Passes/IdenticalCodeFolding.cpp b/bolt/lib/Passes/IdenticalCodeFolding.cpp
index e412bc1530219..cebb7638fa7c6 100644
--- a/bolt/lib/Passes/IdenticalCodeFolding.cpp
+++ b/bolt/lib/Passes/IdenticalCodeFolding.cpp
@@ -570,20 +570,30 @@ Error IdenticalCodeFolding::runOnFunctions(BinaryContext &BC) {
LLVM_DEBUG(T.stopTimer());
};
- // Create a task for each congruent bucket
- for (auto &Entry : CongruentBuckets) {
- std::set<BinaryFunction *> &Bucket = Entry.second;
- if (Bucket.size() < 2)
- continue;
-
- if (opts::NoThreads)
- processSingleBucket(Bucket);
- else
- ThPool->async(processSingleBucket, std::ref(Bucket));
- }
+ if (opts::NoThreads) {
+ // Sort buckets by address for deterministic folding order when running
+ // single-threaded.
+ SmallVector<std::pair<uint64_t, std::set<BinaryFunction *> *>>
+ SortedBuckets;
+ for (auto &Entry : CongruentBuckets) {
+ if (Entry.second.size() >= 2)
+ SortedBuckets.push_back({Entry.first->getAddress(), &Entry.second});
+ }
+ llvm::sort(SortedBuckets, [](const auto &A, const auto &B) {
+ return A.first < B.first;
+ });
+ for (auto &[Addr, Bucket] : SortedBuckets)
+ processSingleBucket(*Bucket);
+ } else {
+ for (auto &Entry : CongruentBuckets) {
+ std::set<BinaryFunction *> &Bucket = Entry.second;
+ if (Bucket.size() < 2)
+ continue;
- if (!opts::NoThreads)
+ ThPool->async(processSingleBucket, std::ref(Entry.second));
+ }
ThPool->wait();
+ }
LLVM_DEBUG(SinglePass.stopTimer());
};
diff --git a/bolt/test/X86/icf-safe-icp.test b/bolt/test/X86/icf-safe-icp.test
index 9a302c8ac8192..6ead68afe41d4 100644
--- a/bolt/test/X86/icf-safe-icp.test
+++ b/bolt/test/X86/icf-safe-icp.test
@@ -9,8 +9,8 @@
# RUN: llvm-bolt --no-threads %t.exe --icf=safe -debug-only=bolt-icf -o %t.bolt 2>&1 | FileCheck --check-prefix=SAFEICFCHECK %s
# ICFCHECK: ICF iteration 1
-# ICFCHECK-NEXT: folding Derived3Destructor into Derived2Destructor
# ICFCHECK-NEXT: folding Derived3Func into Derived2Func
+# ICFCHECK-NEXT: folding Derived3Destructor into Derived2Destructor
# SAFEICFCHECK: skipping function with reference taken Derived3Func
# SAFEICFCHECK-NEXT: ICF iteration 1
diff --git a/bolt/test/X86/icf-safe-test1.test b/bolt/test/X86/icf-safe-test1.test
index 45e23d1e40ea5..73e40a69159fb 100644
--- a/bolt/test/X86/icf-safe-test1.test
+++ b/bolt/test/X86/icf-safe-test1.test
@@ -12,8 +12,8 @@
# RUN: --skip-funcs=helper1Func,main -o %t.bolt 2>&1 | FileCheck --check-prefix=SAFEICFCHECKNOCFG %s
# ICFCHECK: ICF iteration 1
-# ICFCHECK-NEXT: folding barAddFunc into fooAddFunc
# ICFCHECK-NEXT: folding barSubFunc into fooSubFunc
+# ICFCHECK-NEXT: folding barAddFunc into fooAddFunc
# SAFEICFCHECK: skipping function with reference taken barAddFunc
# SAFEICFCHECK-NEXT: ICF iteration 1
diff --git a/bolt/test/X86/icf-safe-test2GlobalConstPtrNoPic.test b/bolt/test/X86/icf-safe-test2GlobalConstPtrNoPic.test
index cb720f096fd40..e74ef774bb57c 100644
--- a/bolt/test/X86/icf-safe-test2GlobalConstPtrNoPic.test
+++ b/bolt/test/X86/icf-safe-test2GlobalConstPtrNoPic.test
@@ -8,8 +8,8 @@
# RUN: llvm-bolt --no-threads %t.exe --icf=safe -debug-only=bolt-icf -o %t.bolt 2>&1 | FileCheck --check-prefix=SAFEICFCHECK %s
# ICFCHECK: ICF iteration 1
-# ICFCHECK-NEXT: folding barAddFunc into fooAddFunc
# ICFCHECK-NEXT: folding barMulFunc into fooMulFunc
+# ICFCHECK-NEXT: folding barAddFunc into fooAddFunc
# SAFEICFCHECK: skipping function with reference taken fooMulFunc
# SAFEICFCHECK-NEXT: skipping function with reference taken barMulFunc
More information about the llvm-commits
mailing list