[lld] [lld][BP] Avoid ordering ICF'ed sections (PR #126327)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 7 20:15:36 PST 2025
MaskRay wrote:
Thanks for updating the ELF test! If you want to update the ELF code, you could do the following.
```
--- i/lld/ELF/BPSectionOrderer.cpp
+++ w/lld/ELF/BPSectionOrderer.cpp
@@ -75,8 +75,11 @@ DenseMap<const InputSectionBase *, int> elf::runBalancedPartitioning(
auto *d = dyn_cast<Defined>(&sym);
if (!d)
return;
- auto *sec = dyn_cast_or_null<InputSectionBase>(d->section);
- if (!sec || sec->size == 0 || !orderer.secToSym.try_emplace(sec, d).second)
+ auto *sec = dyn_cast_or_null<InputSection>(d->section);
+ // Skip empty, discarded, ICF folded sections. Skipping ICF folded sections
+ // reduces duplicate detection work in BPSectionOrderer.
+ if (!sec || sec->size == 0 || !sec->isLive() || sec->repl != sec ||
+ !orderer.secToSym.try_emplace(sec, d).second)
return;
rootSymbolToSectionIdxs[CachedHashStringRef(getRootSymbol(sym.getName()))]
.insert(sections.size());
```
In ELF, `InputSectionBase` can be InputSection (regular sections from .o), EhInputSection (.eh_frame, already merged), MergeInputSection (mergeable string sections like .rodata.str1 .debug.str, similar to Mach-O `__TEXT,__cstring`; already merged into a MergeSyntheticSection). We only need to handle InputSection.
`!sec->isLive()` skips unreachable input sections due to --gc-sections (like -dead_strip).
While writing this comment, I realize that the tests probably should be enhanced to test section garbage collection (dead stripping).
Since many functions are actually unreachable from main, you will probably need to add this attribute
```c
#define RETAIN [[gnu::used,gnu::retain]]
RETAIN void A() {}
RETAIN void B() {}
```
https://github.com/llvm/llvm-project/pull/126327
More information about the llvm-commits
mailing list