Skip to content

Commit ffdaf85

Browse files
authored
[lld][ELF] filter out section symbols when use BP reorder (#151685)
When using Temporal Profiling with the BP algorithm, we encounter an issue with the internal function reorder. In cases where the symbol table contains entries like: ``` Symbol table '.symtab' contains 45 entries: Num: Value Size Type Bind Vis Ndx Name 10: 0000000000000000 0 SECTION LOCAL DEFAULT 18 .text.L1 11: 0000000000000000 24 FUNC LOCAL DEFAULT 18 L1 ```` The zero-sized section symbol .text.L1 gets stored in the secToSym map first. However, when the function lookup searches for L1 (as seen in [BPSectionOrdererBase.inc:191](https://github.com/llvm/llvm-project/blob/main/lld/include/lld/Common/BPSectionOrdererBase.inc#L191)), it fails to find the correct entry in rootSymbolToSectionIdxs because the section symbol has already claimed that slot. This patch fixes the issue by skipping zero-sized symbols during the addSections process, ensuring that function symbols are properly registered for lookup.
1 parent b9ca01b commit ffdaf85

File tree

2 files changed

+73
-27
lines changed

2 files changed

+73
-27
lines changed

lld/ELF/BPSectionOrderer.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@ DenseMap<const InputSectionBase *, int> elf::runBalancedPartitioning(
7676
if (!d)
7777
return;
7878
auto *sec = dyn_cast_or_null<InputSection>(d->section);
79-
// Skip empty, discarded, ICF folded sections, .bss. Skipping ICF folded
80-
// sections reduces duplicate detection work in BPSectionOrderer.
81-
if (!sec || sec->size == 0 || !sec->isLive() || sec->repl != sec ||
82-
!sec->content().data() || !orderer.secToSym.try_emplace(sec, d).second)
79+
// Skip section symbols. Skip empty, discarded, ICF folded sections, .bss.
80+
// Skipping ICF folded sections reduces duplicate detection work in
81+
// BPSectionOrderer.
82+
if (sym.isSection() || !sec || sec->size == 0 || !sec->isLive() ||
83+
sec->repl != sec || !sec->content().data() ||
84+
!orderer.secToSym.try_emplace(sec, d).second)
8385
return;
8486
rootSymbolToSectionIdxs[CachedHashStringRef(
8587
lld::utils::getRootSymbol(sym.getName()))]

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

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,38 @@
2121
# RUN: llvm-profdata merge a.proftext -o a.profdata
2222
# RUN: ld.lld a.o --irpgo-profile=a.profdata --bp-startup-sort=function --verbose-bp-section-orderer --icf=all --gc-sections 2>&1 | FileCheck %s --check-prefix=STARTUP-FUNC-ORDER
2323

24-
# STARTUP-FUNC-ORDER: Ordered 3 sections ([[#]] bytes) using balanced partitioning
25-
# STARTUP-FUNC-ORDER: Total area under the page fault curve: 3.
24+
# STARTUP-FUNC-ORDER: Ordered 4 sections ([[#]] bytes) using balanced partitioning
25+
# STARTUP-FUNC-ORDER: Total area under the page fault curve: 4.
2626

2727
# RUN: ld.lld -o out.s a.o --irpgo-profile=a.profdata --bp-startup-sort=function
2828
# RUN: llvm-nm -jn out.s | tr '\n' , | FileCheck %s --check-prefix=STARTUP
29-
# STARTUP: s5,s4,s3,s2,s1,A,B,C,F,E,D,merged1,merged2,_start,d4,d3,d2,d1,g1,{{$}}
29+
# STARTUP: s5,s4,s3,s2,s1,A,B,C,L1,F,E,D,merged1,merged2,G,_start,d4,d3,d2,d1,g1,{{$}}
3030

3131
# RUN: ld.lld -o out.os a.o --irpgo-profile=a.profdata --bp-startup-sort=function --symbol-ordering-file a.txt
3232
# RUN: llvm-nm -jn out.os | tr '\n' , | FileCheck %s --check-prefix=ORDER-STARTUP
33-
# ORDER-STARTUP: s2,s1,s5,s4,s3,A,F,E,D,B,C,merged1,merged2,_start,d3,d2,d4,d1,g1,{{$}}
33+
# ORDER-STARTUP: s2,s1,s5,s4,s3,A,F,E,D,B,C,L1,merged1,merged2,G,_start,d3,d2,d4,d1,g1,{{$}}
3434

3535
# RUN: ld.lld -o out.cf a.o --verbose-bp-section-orderer --bp-compression-sort=function 2>&1 | FileCheck %s --check-prefix=BP-COMPRESSION-FUNC
3636
# RUN: ld.lld -o out.cf.icf a.o --verbose-bp-section-orderer --bp-compression-sort=function --icf=all --gc-sections 2>&1 | FileCheck %s --check-prefix=BP-COMPRESSION-ICF-FUNC
3737
# RUN: llvm-nm -jn out.cf | tr '\n' , | FileCheck %s --check-prefix=CFUNC
38-
# CFUNC: s5,s4,s3,s2,s1,A,F,merged1,merged2,C,E,D,B,_start,d4,d3,d2,d1,g1,{{$}}
38+
# CFUNC: s5,s4,s3,s2,s1,C,E,D,B,G,F,merged1,merged2,A,_start,L1,d4,d3,d2,d1,g1,{{$}}
3939

4040
# RUN: ld.lld -o out.cd a.o --verbose-bp-section-orderer --bp-compression-sort=data 2>&1 | FileCheck %s --check-prefix=BP-COMPRESSION-DATA
4141
# RUN: llvm-nm -jn out.cd | tr '\n' , | FileCheck %s --check-prefix=CDATA
42-
# CDATA: s5,s3,s4,s2,s1,F,C,E,D,B,A,merged1,merged2,_start,d4,d1,d3,d2,g1,{{$}}
42+
# CDATA: s5,s3,s4,s2,s1,F,C,E,D,B,A,merged1,merged2,L1,G,_start,d4,d1,d3,d2,g1,{{$}}
4343

4444
# RUN: ld.lld -o out.cb a.o --verbose-bp-section-orderer --bp-compression-sort=both 2>&1 | FileCheck %s --check-prefix=BP-COMPRESSION-BOTH
4545
# RUN: llvm-nm -jn out.cb | tr '\n' , | FileCheck %s --check-prefix=CBOTH
46-
# CBOTH: s5,s3,s4,s2,s1,A,F,merged1,merged2,C,E,D,B,_start,d4,d1,d3,d2,g1,{{$}}
46+
# CBOTH: s5,s3,s4,s2,s1,C,E,D,B,G,F,merged1,merged2,A,_start,L1,d4,d1,d3,d2,g1,{{$}}
4747

4848
# RUN: ld.lld -o out.cbs a.o --verbose-bp-section-orderer --bp-compression-sort=both --irpgo-profile=a.profdata --bp-startup-sort=function 2>&1 | FileCheck %s --check-prefix=BP-COMPRESSION-BOTH
4949
# RUN: llvm-nm -jn out.cbs | tr '\n' , | FileCheck %s --check-prefix=CBOTH-STARTUP
50-
# CBOTH-STARTUP: s5,s3,s4,s2,s1,A,B,C,F,E,D,merged1,merged2,_start,d4,d1,d3,d2,g1,{{$}}
50+
# CBOTH-STARTUP: s5,s3,s4,s2,s1,A,B,C,L1,F,E,D,merged1,merged2,G,_start,d4,d1,d3,d2,g1,{{$}}
5151

52-
# BP-COMPRESSION-FUNC: Ordered 9 sections ([[#]] bytes) using balanced partitioning
53-
# BP-COMPRESSION-ICF-FUNC: Ordered 8 sections ([[#]] bytes) using balanced partitioning
52+
# BP-COMPRESSION-FUNC: Ordered 11 sections ([[#]] bytes) using balanced partitioning
53+
# BP-COMPRESSION-ICF-FUNC: Ordered 9 sections ([[#]] bytes) using balanced partitioning
5454
# BP-COMPRESSION-DATA: Ordered 9 sections ([[#]] bytes) using balanced partitioning
55-
# BP-COMPRESSION-BOTH: Ordered 18 sections ([[#]] bytes) using balanced partitioning
55+
# BP-COMPRESSION-BOTH: Ordered 20 sections ([[#]] bytes) using balanced partitioning
5656

5757
#--- a.proftext
5858
:ir
@@ -63,7 +63,7 @@
6363
1
6464
# Weight
6565
1
66-
A, B, C
66+
A, B, C, L1
6767

6868
A
6969
# Func Hash:
@@ -97,6 +97,14 @@ D
9797
# Counter Values:
9898
1
9999

100+
L1
101+
# Func Hash:
102+
5555
103+
# Num Counters:
104+
1
105+
# Counter Values:
106+
1
107+
100108
#--- a.txt
101109
A
102110
F
@@ -137,6 +145,9 @@ void A() {}
137145
RETAIN int merged1(int a) { return F(a + 101); }
138146
int merged2(int a) { return F(a + 101); }
139147

148+
RETAIN static int L1(int a) { return a + 103; }
149+
int G(int a) { return L1(a); }
150+
140151
int _start() { return 0; }
141152

142153
#--- gen
@@ -148,7 +159,7 @@ clang --target=aarch64-linux-gnu -O0 -ffunction-sections -fdata-sections -fno-as
148159
.p2align 2
149160
.type F,@function
150161
F: // @F
151-
// %bb.0: // %entry
162+
// %bb.0:
152163
sub sp, sp, #32
153164
stp x29, x30, [sp, #16] // 16-byte Folded Spill
154165
add x29, sp, #16
@@ -167,7 +178,7 @@ F: // @F
167178
.p2align 2
168179
.type C,@function
169180
C: // @C
170-
// %bb.0: // %entry
181+
// %bb.0:
171182
sub sp, sp, #32
172183
stp x29, x30, [sp, #16] // 16-byte Folded Spill
173184
add x29, sp, #16
@@ -186,7 +197,7 @@ C: // @C
186197
.p2align 2
187198
.type E,@function
188199
E: // @E
189-
// %bb.0: // %entry
200+
// %bb.0:
190201
sub sp, sp, #32
191202
stp x29, x30, [sp, #16] // 16-byte Folded Spill
192203
add x29, sp, #16
@@ -205,7 +216,7 @@ E: // @E
205216
.p2align 2
206217
.type D,@function
207218
D: // @D
208-
// %bb.0: // %entry
219+
// %bb.0:
209220
sub sp, sp, #32
210221
stp x29, x30, [sp, #16] // 16-byte Folded Spill
211222
add x29, sp, #16
@@ -224,7 +235,7 @@ D: // @D
224235
.p2align 2
225236
.type B,@function
226237
B: // @B
227-
// %bb.0: // %entry
238+
// %bb.0:
228239
sub sp, sp, #32
229240
stp x29, x30, [sp, #16] // 16-byte Folded Spill
230241
add x29, sp, #16
@@ -243,7 +254,7 @@ B: // @B
243254
.p2align 2
244255
.type A,@function
245256
A: // @A
246-
// %bb.0: // %entry
257+
// %bb.0:
247258
ret
248259
.Lfunc_end5:
249260
.size A, .Lfunc_end5-A
@@ -253,7 +264,7 @@ A: // @A
253264
.p2align 2
254265
.type merged1,@function
255266
merged1: // @merged1
256-
// %bb.0: // %entry
267+
// %bb.0:
257268
sub sp, sp, #32
258269
stp x29, x30, [sp, #16] // 16-byte Folded Spill
259270
add x29, sp, #16
@@ -272,7 +283,7 @@ merged1: // @merged1
272283
.p2align 2
273284
.type merged2,@function
274285
merged2: // @merged2
275-
// %bb.0: // %entry
286+
// %bb.0:
276287
sub sp, sp, #32
277288
stp x29, x30, [sp, #16] // 16-byte Folded Spill
278289
add x29, sp, #16
@@ -286,16 +297,48 @@ merged2: // @merged2
286297
.Lfunc_end7:
287298
.size merged2, .Lfunc_end7-merged2
288299
// -- End function
300+
.section .text.L1,"axR",@progbits
301+
.p2align 2 // -- Begin function L1
302+
.type L1,@function
303+
L1: // @L1
304+
// %bb.0:
305+
sub sp, sp, #16
306+
str w0, [sp, #12]
307+
ldr w8, [sp, #12]
308+
add w0, w8, #103
309+
add sp, sp, #16
310+
ret
311+
.Lfunc_end8:
312+
.size L1, .Lfunc_end8-L1
313+
// -- End function
314+
.section .text.G,"ax",@progbits
315+
.globl G // -- Begin function G
316+
.p2align 2
317+
.type G,@function
318+
G: // @G
319+
// %bb.0:
320+
sub sp, sp, #32
321+
stp x29, x30, [sp, #16] // 16-byte Folded Spill
322+
add x29, sp, #16
323+
stur w0, [x29, #-4]
324+
ldur w0, [x29, #-4]
325+
bl L1
326+
ldp x29, x30, [sp, #16] // 16-byte Folded Reload
327+
add sp, sp, #32
328+
ret
329+
.Lfunc_end9:
330+
.size G, .Lfunc_end9-G
331+
// -- End function
289332
.section .text._start,"ax",@progbits
290333
.globl _start // -- Begin function _start
291334
.p2align 2
292335
.type _start,@function
293336
_start: // @_start
294-
// %bb.0: // %entry
337+
// %bb.0:
295338
mov w0, wzr
296339
ret
297-
.Lfunc_end8:
298-
.size _start, .Lfunc_end8-_start
340+
.Lfunc_end10:
341+
.size _start, .Lfunc_end10-_start
299342
// -- End function
300343
.type s5,@object // @s5
301344
.section .rodata.s5,"a",@progbits
@@ -395,3 +438,4 @@ g1:
395438
.addrsig_sym B
396439
.addrsig_sym A
397440
.addrsig_sym merged1
441+
.addrsig_sym L1

0 commit comments

Comments
 (0)