[lld] [lld-macho] Choose ICF root function deterministically based on symbol names (PR #158157)
Kyungwoo Lee via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 12 11:55:11 PDT 2025
================
@@ -408,9 +408,40 @@ void ICF::run() {
// When using safe_thunks, ensure that we first sort by icfEqClass and
// then by keepUnique (descending). This guarantees that within an
// equivalence class, the keepUnique inputs are always first.
- if (config->icfLevel == ICFLevel::safe_thunks)
- if (a->icfEqClass[0] == b->icfEqClass[0])
- return a->keepUnique > b->keepUnique;
+ if (a->icfEqClass[0] == b->icfEqClass[0]) {
+ if (config->icfLevel == ICFLevel::safe_thunks) {
+ if (a->keepUnique != b->keepUnique)
+ return a->keepUnique > b->keepUnique;
+ }
+ // Within each equivalence class, sort by primary (user) symbol name
+ // for determinism. Prefer the first non-local (external) symbol at
+ // offset 0.
+ auto getPrimarySymbolName = [](const ConcatInputSection *isec) {
+ const Symbol *firstLocalSymAtZero = nullptr;
+
+ // Single pass through symbols to find the best candidate
+ for (const Symbol *sym : isec->symbols) {
+ if (auto *d = dyn_cast<Defined>(sym)) {
+ if (d->value == 0) {
+ // If we find an external symbol at offset 0, return it
+ // immediately
+ if (d->isExternal())
+ return d->getName();
+ // Otherwise, remember the first local symbol at offset 0
+ if (!firstLocalSymAtZero)
+ firstLocalSymAtZero = sym;
----------------
kyulee-com wrote:
Simplified the logic to rely solely on external user symbol names to ensure determinism. Using local symbols or section names seems ineffective for this purpose and added unnecessary code complexity
https://github.com/llvm/llvm-project/pull/158157
More information about the llvm-commits
mailing list