Skip to content

Commit e816def

Browse files
Emit jump tables into .hot and .unlikely sections respectively
1 parent 27ef86d commit e816def

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2885,8 +2885,14 @@ void AsmPrinter::emitJumpTableInfo() {
28852885
return;
28862886
}
28872887

2888+
// When static data partitioning is enabled, collect jump table entries that
2889+
// go into the same section together to reduce the amount of section switch
2890+
// statements.
2891+
//
28882892
// Iterate all jump tables, put hot jump table indices towards the beginning
2889-
// of the vector, and cold jump table indices towards the end.
2893+
// of the vector, and cold jump table indices towards the end. Meanwhile
2894+
// retain the relative orders of original jump tables within a hot or unlikely
2895+
// section by reversing the cold jump table indices.
28902896
int NextHotJumpTableIndex = 0, NextColdJumpTableIndex = JT.size() - 1;
28912897
JumpTableIndices.resize(JT.size());
28922898
for (unsigned JTI = 0, JTSize = JT.size(); JTI < JTSize; ++JTI) {
@@ -2902,8 +2908,8 @@ void AsmPrinter::emitJumpTableInfo() {
29022908
TLOF.getSectionForJumpTable(F, TM, &JT[0]), JTInDiffSection, *MJTI);
29032909
}
29042910

2905-
if (NextHotJumpTableIndex != (int)JT.size()) {
2906-
// Retain the relative orders of original jump tables.
2911+
if (NextHotJumpTableIndex < (int)JT.size()) {
2912+
// Reverse the order of cold jump tables indices.
29072913
for (int L = NextHotJumpTableIndex, R = JT.size() - 1; L < R; ++L, --R)
29082914
std::swap(JumpTableIndices[L], JumpTableIndices[R]);
29092915

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,8 @@ getELFSectionNameForGlobal(const GlobalObject *GO, SectionKind Kind,
671671
if (JTE) {
672672
if (JTE->Hotness == MachineFunctionDataHotness::Hot)
673673
raw_svector_ostream(Name) << ".hot";
674+
else if (JTE->Hotness == MachineFunctionDataHotness::Cold)
675+
raw_svector_ostream(Name) << ".unlikely";
674676
} else if (std::optional<StringRef> Prefix = F->getSectionPrefix()) {
675677
raw_svector_ostream(Name) << '.' << *Prefix;
676678
HasPrefix = true;

llvm/test/CodeGen/X86/jump-table-partition.ll

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88

99
; When 'partition-static-data-sections' is enabled, static data splitter pass will
1010
; categorize jump tables and assembly printer will place hot jump tables in the
11-
; `.hot`-suffixed read only section, and cold ones in the `.rodata` sections.
11+
; `.rodata.hot`-prefixed section, and cold ones in the `.rodata.unlikely`-prefixed section.
1212
; Section names will optionally have `.<func>` if -function-sections is enabled.
13-
; RUN: llc -enable-split-machine-functions -partition-static-data-sections=true -function-sections=true -min-jump-table-entries=2 %s -o - 2>&1 | FileCheck %s --check-prefixes=FUNC,JT,HOT
13+
; RUN: llc -enable-split-machine-functions -partition-static-data-sections=true -function-sections=true -min-jump-table-entries=2 %s -o - 2>&1 | FileCheck %s --check-prefixes=FUNC,JT,DEFAULTHOT
1414
; RUN: llc -enable-split-machine-functions -partition-static-data-sections=true -function-sections=false -min-jump-table-entries=2 %s -o - 2>&1 | FileCheck %s --check-prefixes=FUNCLESS,JT
1515

16-
; Tests that jump tables with unknown hotness are categorized as cold if `-static-data-default-hotness` specifies so.
17-
; RUN: llc -enable-split-machine-functions -partition-static-data-sections=true -min-jump-table-entries=2 -static-data-default-hotness=cold -function-sections=true %s -o - 2>&1 | FileCheck %s --check-prefixes=FUNC,JT,DEFAULT
16+
; RUN: llc -enable-split-machine-functions -partition-static-data-sections=true -min-jump-table-entries=2 -static-data-default-hotness=cold -function-sections=true %s -o - 2>&1 | FileCheck %s --check-prefixes=FUNC,JT,DEFAULTCOLD
1817

1918
; Tests stat messages are expected.
2019
; STAT-DAG: 2 static-data-splitter - Number of cold jump tables seen
@@ -27,15 +26,17 @@
2726
; FUNCLESS: .section .rodata.hot,"a",@progbits
2827
; JT: .LJTI0_0:
2928
; JT: .LJTI0_2:
30-
; FUNC: .section .rodata.foo,"a",@progbits
31-
; FUNCLESS: .section .rodata,"a",@progbits
29+
; FUNC: .section .rodata.unlikely.foo,"a",@progbits
30+
; FUNCLESS: .section .rodata.unlikely,"a",@progbits
3231
; JT: .LJTI0_1:
3332
; JT: .LJTI0_3:
34-
; HOT: .section .rodata.hot.func_without_entry_count,"a",@progbits
35-
; HOT: .LJTI1_0:
33+
; DEFAULTHOT: .section .rodata.hot.func_without_entry_count,"a",@progbits
34+
; DEFAULTHOT: .LJTI1_0:
35+
; FUNCLESS: .section .rodata.hot,"a",@progbits
36+
; FUNCLESS: .LJTI1_0:
3637

37-
; DEFAULT: .section .rodata.func_without_entry_count,"a",@progbits
38-
; DEFAULT: .LJTI1_0:
38+
; DEFAULTCOLD: .section .rodata.unlikely.func_without_entry_count,"a",@progbits
39+
; DEFAULTCOLD: .LJTI1_0:
3940

4041
; @foo has four jump tables, jt0, jt1, jt2 and jt3 in the input basic block
4142
; order; jt0 and jt2 are hot, and jt1 and jt3 are cold.

0 commit comments

Comments
 (0)