Skip to content

Commit cd33ece

Browse files
mingmingl-llvmvar-const
authored andcommitted
[NFC][AsmPrinter] Refactor AsmPrinter and AArch64AsmPrinter to prepare for jump table partitions on aarch64 (llvm#125993)
With llvm@3feb724, AsmPrinter can place jump table entries into `.hot` or `.unlikely` prefixed data sections. This change refactors AsmPrinter and AArch64AsmPrinter to prepare for the aarch64 port. * Before this patch, the AsmPrinter class exposes `emitJumpTableInfo` as a virtual method, and AArch64AsmPrinter overrides `emitJumpTableInfo` for jump table emission. * After this patch, both AsmPrinter and AArch64AsmPrinter shares `AsmPrinter::emitJumpTableInfo`, and class-specific code are moved inside each class's `emitJumpTableImpl` respectively. This is a follow-up of llvm#125987, and llvm#126018 implements the port for aarch64.
1 parent ffb4c2e commit cd33ece

File tree

4 files changed

+73
-71
lines changed

4 files changed

+73
-71
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -908,9 +908,8 @@ class AsmPrinter : public MachineFunctionPass {
908908
// Internal Implementation Details
909909
//===------------------------------------------------------------------===//
910910

911-
void emitJumpTableImpl(const MachineJumpTableInfo &MJTI,
912-
ArrayRef<unsigned> JumpTableIndices,
913-
bool JTInDiffSection);
911+
virtual void emitJumpTableImpl(const MachineJumpTableInfo &MJTI,
912+
ArrayRef<unsigned> JumpTableIndices);
914913

915914
void emitJumpTableSizesSection(const MachineJumpTableInfo &MJTI,
916915
const Function &F) const;

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2860,22 +2860,12 @@ void AsmPrinter::emitConstantPool() {
28602860
void AsmPrinter::emitJumpTableInfo() {
28612861
const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
28622862
if (!MJTI) return;
2863-
if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_Inline) return;
2863+
28642864
const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
28652865
if (JT.empty()) return;
28662866

2867-
// Pick the directive to use to print the jump table entries, and switch to
2868-
// the appropriate section.
2869-
const Function &F = MF->getFunction();
2870-
const TargetLoweringObjectFile &TLOF = getObjFileLowering();
2871-
bool JTInDiffSection = !TLOF.shouldPutJumpTableInFunctionSection(
2872-
MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 ||
2873-
MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference64,
2874-
F);
2875-
28762867
if (!TM.Options.EnableStaticDataPartitioning) {
2877-
emitJumpTableImpl(*MJTI, llvm::to_vector(llvm::seq<unsigned>(JT.size())),
2878-
JTInDiffSection);
2868+
emitJumpTableImpl(*MJTI, llvm::to_vector(llvm::seq<unsigned>(JT.size())));
28792869
return;
28802870
}
28812871

@@ -2891,33 +2881,40 @@ void AsmPrinter::emitJumpTableInfo() {
28912881
}
28922882
}
28932883

2894-
emitJumpTableImpl(*MJTI, HotJumpTableIndices, JTInDiffSection);
2895-
emitJumpTableImpl(*MJTI, ColdJumpTableIndices, JTInDiffSection);
2884+
emitJumpTableImpl(*MJTI, HotJumpTableIndices);
2885+
emitJumpTableImpl(*MJTI, ColdJumpTableIndices);
28962886
}
28972887

28982888
void AsmPrinter::emitJumpTableImpl(const MachineJumpTableInfo &MJTI,
2899-
ArrayRef<unsigned> JumpTableIndices,
2900-
bool JTInDiffSection) {
2901-
if (JumpTableIndices.empty())
2889+
ArrayRef<unsigned> JumpTableIndices) {
2890+
if (MJTI.getEntryKind() == MachineJumpTableInfo::EK_Inline ||
2891+
JumpTableIndices.empty())
29022892
return;
29032893

29042894
const TargetLoweringObjectFile &TLOF = getObjFileLowering();
29052895
const Function &F = MF->getFunction();
29062896
const std::vector<MachineJumpTableEntry> &JT = MJTI.getJumpTables();
29072897
MCSection *JumpTableSection = nullptr;
2908-
if (TM.Options.EnableStaticDataPartitioning) {
2909-
JumpTableSection =
2910-
TLOF.getSectionForJumpTable(F, TM, &JT[JumpTableIndices.front()]);
2911-
} else {
2912-
JumpTableSection = TLOF.getSectionForJumpTable(F, TM);
2913-
}
29142898

2915-
const DataLayout &DL = MF->getDataLayout();
2899+
const bool UseLabelDifference =
2900+
MJTI.getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 ||
2901+
MJTI.getEntryKind() == MachineJumpTableInfo::EK_LabelDifference64;
2902+
// Pick the directive to use to print the jump table entries, and switch to
2903+
// the appropriate section.
2904+
const bool JTInDiffSection =
2905+
!TLOF.shouldPutJumpTableInFunctionSection(UseLabelDifference, F);
29162906
if (JTInDiffSection) {
2907+
if (TM.Options.EnableStaticDataPartitioning) {
2908+
JumpTableSection =
2909+
TLOF.getSectionForJumpTable(F, TM, &JT[JumpTableIndices.front()]);
2910+
} else {
2911+
JumpTableSection = TLOF.getSectionForJumpTable(F, TM);
2912+
}
29172913
OutStreamer->switchSection(JumpTableSection);
29182914
}
29192915

2920-
emitAlignment(Align(MJTI.getEntryAlignment(MF->getDataLayout())));
2916+
const DataLayout &DL = MF->getDataLayout();
2917+
emitAlignment(Align(MJTI.getEntryAlignment(DL)));
29212918

29222919
// Jump tables in code sections are marked with a data_region directive
29232920
// where that's supported.

llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ class AArch64AsmPrinter : public AsmPrinter {
113113
const MCExpr *lowerBlockAddressConstant(const BlockAddress &BA) override;
114114

115115
void emitStartOfAsmFile(Module &M) override;
116-
void emitJumpTableInfo() override;
116+
void emitJumpTableImpl(const MachineJumpTableInfo &MJTI,
117+
ArrayRef<unsigned> JumpTableIndices) override;
117118
std::tuple<const MCSymbol *, uint64_t, const MCSymbol *,
118119
codeview::JumpTableEntrySize>
119120
getCodeViewJumpTableInfo(int JTI, const MachineInstr *BranchInstr,
@@ -1290,19 +1291,18 @@ void AArch64AsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
12901291
printOperand(MI, NOps - 2, OS);
12911292
}
12921293

1293-
void AArch64AsmPrinter::emitJumpTableInfo() {
1294-
const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1295-
if (!MJTI) return;
1296-
1297-
const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1298-
if (JT.empty()) return;
1299-
1294+
void AArch64AsmPrinter::emitJumpTableImpl(const MachineJumpTableInfo &MJTI,
1295+
ArrayRef<unsigned> JumpTableIndices) {
1296+
// Fast return if there is nothing to emit to avoid creating empty sections.
1297+
if (JumpTableIndices.empty())
1298+
return;
13001299
const TargetLoweringObjectFile &TLOF = getObjFileLowering();
13011300
MCSection *ReadOnlySec = TLOF.getSectionForJumpTable(MF->getFunction(), TM);
13021301
OutStreamer->switchSection(ReadOnlySec);
13031302

1303+
const std::vector<MachineJumpTableEntry> &JT = MJTI.getJumpTables();
13041304
auto AFI = MF->getInfo<AArch64FunctionInfo>();
1305-
for (unsigned JTI = 0, e = JT.size(); JTI != e; ++JTI) {
1305+
for (unsigned JTI : JumpTableIndices) {
13061306
const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
13071307

13081308
// If this jump table was deleted, ignore it.

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

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,29 @@
1717
; DEFAULT: .section .rodata.func_without_profile,"a",@progbits
1818
; DEFAULT: .LJTI1_0:
1919
; DEFAULT: .section .rodata.bar_prefix.bar,"a",@progbits
20-
; DEFAULT: .LJTI2_0
21-
22-
; RUN: llc -mtriple=aarch64-unknown-linux-gnu -enable-split-machine-functions \
23-
; RUN: -partition-static-data-sections=true -function-sections=true \
20+
; DEFAULT: .LJTI2_0
21+
22+
; Test that section names are uniqufied by numbers but not function names with
23+
; {-function-sections, -unique-section-names=false}. Specifically, @foo jump
24+
; tables are emitted in two sections, one with unique ID 2 and the other with
25+
; unique ID 3.
26+
; RUN: llc -mtriple=aarch64-unknown-linux-gnu -partition-static-data-sections \
27+
; RUN: -function-sections -unique-section-names=false \
2428
; RUN: -aarch64-enable-atomic-cfg-tidy=false -aarch64-min-jump-table-entries=2 \
25-
; RUN: -unique-section-names=false %s -o - 2>&1 | FileCheck %s --check-prefixes=NUM,JT
29+
; RUN: %s -o - 2>&1 | FileCheck %s --check-prefixes=NUM,JT
2630

27-
; Section names will optionally have `.<func>` if -function-sections is enabled.
28-
; RUN: llc -mtriple=aarch64-unknown-linux-gnu -enable-split-machine-functions \
29-
; RUN: -partition-static-data-sections=true -function-sections=true \
31+
; Section names will optionally have `.<func>` with {-function-sections, -unique-section-names}.
32+
; RUN: llc -mtriple=aarch64-unknown-linux-gnu -partition-static-data-sections \
33+
; RUN: -function-sections -unique-section-names \
3034
; RUN: -aarch64-enable-atomic-cfg-tidy=false -aarch64-min-jump-table-entries=2 \
3135
; RUN: %s -o - 2>&1 | FileCheck %s --check-prefixes=FUNC,JT
3236

33-
; RUN: llc -mtriple=aarch64-unknown-linux-gnu -enable-split-machine-functions \
34-
; RUN: -partition-static-data-sections=true -function-sections=false \
37+
; Test that section names won't have `.<func>` with -function-sections=false.
38+
; RUN: llc -mtriple=aarch64-unknown-linux-gnu -partition-static-data-sections \
39+
; RUN: -function-sections=false \
3540
; RUN: -aarch64-enable-atomic-cfg-tidy=false -aarch64-min-jump-table-entries=2 \
3641
; RUN: %s -o - 2>&1 | FileCheck %s --check-prefixes=FUNCLESS,JT
3742

38-
; A function's section prefix is used for all jump tables of this function.
39-
; @foo is hot so its jump table data section has a hot prefix.
40-
; NUM: .section .rodata.hot.,"a",@progbits,unique,2
41-
; FUNC: .section .rodata.hot.foo,"a",@progbits
42-
; FUNCLESS: .section .rodata.hot.,"a",@progbits
43-
; JT: .LJTI0_0:
44-
; JT: .LJTI0_1:
45-
; JT: .LJTI0_2:
46-
; JT: .LJTI0_3:
47-
48-
; func_without_profile doesn't have profiles, so its jumptable doesn't have
49-
; hotness-based prefix.
50-
; NUM: .section .rodata,"a",@progbits,unique,4
51-
; FUNC: .section .rodata.func_without_profile,"a",@progbits
52-
; FUNCLESS: .section .rodata,"a",@progbits
53-
; JT: .LJTI1_0:
54-
55-
; @bar doesn't have profile information and it has a section prefix.
56-
; Tests that its jump tables are placed in sections with function prefixes.
57-
; NUM: .section .rodata.bar_prefix.,"a",@progbits,unique,
58-
; FUNC: .section .rodata.bar_prefix.bar
59-
; FUNCLESS: .section .rodata.bar_prefix.,"a"
60-
; JT: .LJTI2_0
61-
6243
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
6344
target triple = "aarch64-unknown-linux-gnu"
6445

@@ -71,6 +52,19 @@ target triple = "aarch64-unknown-linux-gnu"
7152
@default = private constant [8 x i8] c"default\00"
7253
@jt3 = private constant [4 x i8] c"jt3\00"
7354

55+
; A function's section prefix is used for all jump tables of this function.
56+
; @foo is hot so its jump table data section has a hot prefix.
57+
; NUM: .section .rodata.hot.,"a",@progbits,unique,2
58+
; FUNC: .section .rodata.hot.foo,"a",@progbits
59+
; FUNCLESS: .section .rodata.hot.,"a",@progbits
60+
; JT: .LJTI0_0:
61+
; JT: .LJTI0_2:
62+
; NUM: .section .rodata.hot.,"a",@progbits,unique,3
63+
; FUNC-NOT: .section .rodata.hot.foo
64+
; FUNCLESS-NOT: .section .rodata.hot.,"a",@progbits
65+
; JT: .LJTI0_1:
66+
; JT: .LJTI0_3:
67+
7468
; jt0 and jt2 are hot. jt1 and jt3 are cold.
7569
define i32 @foo(i32 %num) !prof !13 {
7670
entry:
@@ -168,6 +162,12 @@ return:
168162
ret i32 %mod3
169163
}
170164

165+
; @func_without_profile doesn't have profiles, so its jumptable doesn't have
166+
; hotness-based prefix.
167+
; NUM: .section .rodata,"a",@progbits,unique,5
168+
; FUNC: .section .rodata.func_without_profile,"a",@progbits
169+
; FUNCLESS: .section .rodata,"a",@progbits
170+
; JT: .LJTI1_0:
171171
define void @func_without_profile(i32 %num) {
172172
entry:
173173
switch i32 %num, label %sw.default [
@@ -191,6 +191,12 @@ sw.epilog:
191191
ret void
192192
}
193193

194+
; @bar doesn't have profile information and it has a section prefix.
195+
; Tests that its jump tables are placed in sections with function prefixes.
196+
; NUM: .section .rodata.bar_prefix.,"a",@progbits,unique,7
197+
; FUNC: .section .rodata.bar_prefix.bar
198+
; FUNCLESS: .section .rodata.bar_prefix.,"a"
199+
; JT: .LJTI2_0
194200
define void @bar(i32 %num) !section_prefix !20 {
195201
entry:
196202
switch i32 %num, label %sw.default [

0 commit comments

Comments
 (0)