Skip to content

Commit fcf877e

Browse files
committed
Optimize getSymbols
1 parent 0d30017 commit fcf877e

File tree

2 files changed

+72
-78
lines changed

2 files changed

+72
-78
lines changed

lld/ELF/BPSectionOrderer.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ template <> struct lld::BPOrdererTraits<struct BPOrdererELF> {
2626
};
2727
namespace {
2828
struct BPOrdererELF : lld::BPOrderer<BPOrdererELF> {
29+
DenseMap<const InputSectionBase *, Defined *> secToSym;
30+
2931
static uint64_t getSize(const Section &sec) { return sec.getSize(); }
3032
static bool isCodeSection(const Section &sec) {
3133
return sec.flags & llvm::ELF::SHF_EXECINSTR;
3234
}
33-
static SmallVector<Defined *, 0> getSymbols(const Section &sec) {
34-
SmallVector<Defined *, 0> symbols;
35-
for (auto *sym : sec.file->getSymbols())
36-
if (auto *d = llvm::dyn_cast_or_null<Defined>(sym))
37-
if (d->size > 0 && d->section == &sec)
38-
symbols.emplace_back(d);
39-
return symbols;
35+
ArrayRef<Defined *> getSymbols(const Section &sec) {
36+
auto it = secToSym.find(&sec);
37+
if (it == secToSym.end())
38+
return {};
39+
return ArrayRef(it->second);
4040
}
4141

4242
static void
@@ -69,28 +69,27 @@ DenseMap<const InputSectionBase *, int> elf::runBalancedPartitioning(
6969
// Collect candidate sections and associated symbols.
7070
SmallVector<InputSectionBase *> sections;
7171
DenseMap<CachedHashStringRef, DenseSet<unsigned>> rootSymbolToSectionIdxs;
72-
DenseSet<const InputSectionBase *> seenSections;
72+
BPOrdererELF orderer;
7373

7474
auto addSection = [&](Symbol &sym) {
7575
auto *d = dyn_cast<Defined>(&sym);
76-
if (!d || d->size == 0)
76+
if (!d)
7777
return;
7878
auto *sec = dyn_cast_or_null<InputSectionBase>(d->section);
79-
if (sec && seenSections.insert(sec).second) {
80-
rootSymbolToSectionIdxs[CachedHashStringRef(getRootSymbol(sym.getName()))]
81-
.insert(sections.size());
82-
sections.emplace_back(sec);
83-
}
79+
if (!sec || sec->size == 0 || !orderer.secToSym.try_emplace(sec, d).second)
80+
return;
81+
rootSymbolToSectionIdxs[CachedHashStringRef(getRootSymbol(sym.getName()))]
82+
.insert(sections.size());
83+
sections.emplace_back(sec);
8484
};
8585

8686
for (Symbol *sym : ctx.symtab->getSymbols())
8787
addSection(*sym);
8888
for (ELFFileBase *file : ctx.objectFiles)
8989
for (Symbol *sym : file->getLocalSymbols())
9090
addSection(*sym);
91-
92-
return BPOrdererELF::computeOrder(profilePath, forFunctionCompression,
93-
forDataCompression,
94-
compressionSortStartupFunctions, verbose,
95-
sections, rootSymbolToSectionIdxs);
91+
return orderer.computeOrder(profilePath, forFunctionCompression,
92+
forDataCompression,
93+
compressionSortStartupFunctions, verbose,
94+
sections, rootSymbolToSectionIdxs);
9695
}

lld/test/ELF/bp-section-orderer.s

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
# RUN: ld.lld a.o --irpgo-profile=a.profdata --bp-startup-sort=function --verbose-bp-section-orderer --icf=all 2>&1 | FileCheck %s --check-prefix=STARTUP-FUNC-ORDER
2222

2323
# STARTUP-FUNC-ORDER: Ordered 3 sections using balanced partitioning
24+
# STARTUP-FUNC-ORDER: Total area under the page fault curve: 3.
2425

25-
# RUN: ld.lld -o - a.o --symbol-ordering-file a.orderfile --irpgo-profile=a.profdata --bp-startup-sort=function | llvm-nm --numeric-sort --format=just-symbols - | FileCheck %s --check-prefix=ORDERFILE
26-
# RUN: ld.lld -o - a.o --symbol-ordering-file a.orderfile --bp-compression-sort=both | llvm-nm --numeric-sort --format=just-symbols - | FileCheck %s --check-prefix=ORDERFILE
26+
# RUN: ld.lld -o - a.o --symbol-ordering-file a.txt --irpgo-profile=a.profdata --bp-startup-sort=function | llvm-nm --numeric-sort --format=just-symbols - | FileCheck %s --check-prefix=ORDERFILE
27+
# RUN: ld.lld -o - a.o --symbol-ordering-file a.txt --bp-compression-sort=both | llvm-nm --numeric-sort --format=just-symbols - | FileCheck %s --check-prefix=ORDERFILE
2728

2829
## Rodata
2930
# ORDERFILE: s2
@@ -35,8 +36,8 @@
3536
# ORDERFILE-NEXT: F
3637
# ORDERFILE-NEXT: E
3738
# ORDERFILE-NEXT: D
38-
# ORDERFILE-NEXT: B
39-
# ORDERFILE-NEXT: C
39+
# ORDERFILE-DAG: B
40+
# ORDERFILE-DAG: C
4041
# ORDERFILE-NEXT: _start
4142

4243
## Data
@@ -97,7 +98,7 @@ D
9798
# Counter Values:
9899
1
99100

100-
#--- a.orderfile
101+
#--- a.txt
101102
A
102103
F
103104
E
@@ -115,77 +116,80 @@ const char *r1 = s1;
115116
const char **r2 = &r1;
116117
const char ***r3 = &r2;
117118
const char *r4 = s2;
118-
void A() { return; }
119119

120-
int B(int a) {
121-
A();
122-
return a + 1;
123-
}
124-
125-
int C(int a) {
126-
A();
127-
return a + 2;
128-
}
129-
130-
int D(int a) { return B(a + 2); }
131-
132-
int E(int a) { return C(a + 2); }
120+
int C(int a);
121+
int B(int a);
122+
void A();
133123

134124
int F(int a) { return C(a + 3); }
125+
int E(int a) { return C(a + 2); }
126+
int D(int a) { return B(a + 2); }
127+
int C(int a) { A(); return a + 2; }
128+
int B(int a) { A(); return a + 1; }
129+
void A() {}
135130

136131
int _start() { return 0; }
137132

138133
#--- gen
139134
clang --target=aarch64-linux-gnu -O0 -ffunction-sections -fdata-sections -fno-asynchronous-unwind-tables -S a.c -o -
140135
;--- a.s
141136
.file "a.c"
142-
.section .text.A,"ax",@progbits
143-
.globl A // -- Begin function A
137+
.section .text.F,"ax",@progbits
138+
.globl F // -- Begin function F
144139
.p2align 2
145-
.type A,@function
146-
A: // @A
140+
.type F,@function
141+
F: // @F
147142
// %bb.0: // %entry
143+
sub sp, sp, #32
144+
stp x29, x30, [sp, #16] // 16-byte Folded Spill
145+
add x29, sp, #16
146+
stur w0, [x29, #-4]
147+
ldur w8, [x29, #-4]
148+
add w0, w8, #3
149+
bl C
150+
ldp x29, x30, [sp, #16] // 16-byte Folded Reload
151+
add sp, sp, #32
148152
ret
149153
.Lfunc_end0:
150-
.size A, .Lfunc_end0-A
154+
.size F, .Lfunc_end0-F
151155
// -- End function
152-
.section .text.B,"ax",@progbits
153-
.globl B // -- Begin function B
156+
.section .text.C,"ax",@progbits
157+
.globl C // -- Begin function C
154158
.p2align 2
155-
.type B,@function
156-
B: // @B
159+
.type C,@function
160+
C: // @C
157161
// %bb.0: // %entry
158162
sub sp, sp, #32
159163
stp x29, x30, [sp, #16] // 16-byte Folded Spill
160164
add x29, sp, #16
161165
stur w0, [x29, #-4]
162166
bl A
163167
ldur w8, [x29, #-4]
164-
add w0, w8, #1
168+
add w0, w8, #2
165169
ldp x29, x30, [sp, #16] // 16-byte Folded Reload
166170
add sp, sp, #32
167171
ret
168172
.Lfunc_end1:
169-
.size B, .Lfunc_end1-B
173+
.size C, .Lfunc_end1-C
170174
// -- End function
171-
.section .text.C,"ax",@progbits
172-
.globl C // -- Begin function C
175+
.section .text.E,"ax",@progbits
176+
.globl E // -- Begin function E
173177
.p2align 2
174-
.type C,@function
175-
C: // @C
178+
.type E,@function
179+
E: // @E
176180
// %bb.0: // %entry
177181
sub sp, sp, #32
178182
stp x29, x30, [sp, #16] // 16-byte Folded Spill
179183
add x29, sp, #16
180184
stur w0, [x29, #-4]
181-
bl A
182185
ldur w8, [x29, #-4]
183186
add w0, w8, #2
187+
bl C
184188
ldp x29, x30, [sp, #16] // 16-byte Folded Reload
185189
add sp, sp, #32
186190
ret
187191
.Lfunc_end2:
188-
.size C, .Lfunc_end2-C
192+
.size E, .Lfunc_end2-E
189193
// -- End function
190194
.section .text.D,"ax",@progbits
191195
.globl D // -- Begin function D
@@ -206,43 +210,34 @@ D: // @D
206210
.Lfunc_end3:
207211
.size D, .Lfunc_end3-D
208212
// -- End function
209-
.section .text.E,"ax",@progbits
210-
.globl E // -- Begin function E
213+
.section .text.B,"ax",@progbits
214+
.globl B // -- Begin function B
211215
.p2align 2
212-
.type E,@function
213-
E: // @E
216+
.type B,@function
217+
B: // @B
214218
// %bb.0: // %entry
215219
sub sp, sp, #32
216220
stp x29, x30, [sp, #16] // 16-byte Folded Spill
217221
add x29, sp, #16
218222
stur w0, [x29, #-4]
223+
bl A
219224
ldur w8, [x29, #-4]
220-
add w0, w8, #2
221-
bl C
225+
add w0, w8, #1
222226
ldp x29, x30, [sp, #16] // 16-byte Folded Reload
223227
add sp, sp, #32
224228
ret
225229
.Lfunc_end4:
226-
.size E, .Lfunc_end4-E
230+
.size B, .Lfunc_end4-B
227231
// -- End function
228-
.section .text.F,"ax",@progbits
229-
.globl F // -- Begin function F
232+
.section .text.A,"ax",@progbits
233+
.globl A // -- Begin function A
230234
.p2align 2
231-
.type F,@function
232-
F: // @F
235+
.type A,@function
236+
A: // @A
233237
// %bb.0: // %entry
234-
sub sp, sp, #32
235-
stp x29, x30, [sp, #16] // 16-byte Folded Spill
236-
add x29, sp, #16
237-
stur w0, [x29, #-4]
238-
ldur w8, [x29, #-4]
239-
add w0, w8, #3
240-
bl C
241-
ldp x29, x30, [sp, #16] // 16-byte Folded Reload
242-
add sp, sp, #32
243238
ret
244239
.Lfunc_end5:
245-
.size F, .Lfunc_end5-F
240+
.size A, .Lfunc_end5-A
246241
// -- End function
247242
.section .text._start,"ax",@progbits
248243
.globl _start // -- Begin function _start
@@ -310,9 +305,9 @@ r4:
310305

311306
.section ".note.GNU-stack","",@progbits
312307
.addrsig
313-
.addrsig_sym A
314-
.addrsig_sym B
315308
.addrsig_sym C
309+
.addrsig_sym B
310+
.addrsig_sym A
316311
.addrsig_sym s1
317312
.addrsig_sym s2
318313
.addrsig_sym r1

0 commit comments

Comments
 (0)