-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[MC] Emit a jump table size section #101962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
14f3cb8
[MC] Emit a jump table size section
omern1 450ade1
Fix formatting
omern1 5ae406c
Add more squiggles
omern1 0fe0d9d
Restrict to ELF
omern1 992c71f
Fix formatting
omern1 8084241
Remove `.debug_` prefix
omern1 5d3dd1c
Update test
omern1 d42fedf
Fix formatting
omern1 d71791b
Add support for COFF
omern1 b488ce4
Address review comments
omern1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,6 +99,7 @@ | |
| #include "llvm/MC/MCStreamer.h" | ||
| #include "llvm/MC/MCSubtargetInfo.h" | ||
| #include "llvm/MC/MCSymbol.h" | ||
| #include "llvm/MC/MCSymbolCOFF.h" | ||
| #include "llvm/MC/MCSymbolELF.h" | ||
| #include "llvm/MC/MCTargetOptions.h" | ||
| #include "llvm/MC/MCValue.h" | ||
|
|
@@ -107,6 +108,7 @@ | |
| #include "llvm/Pass.h" | ||
| #include "llvm/Remarks/RemarkStreamer.h" | ||
| #include "llvm/Support/Casting.h" | ||
| #include "llvm/Support/CommandLine.h" | ||
| #include "llvm/Support/Compiler.h" | ||
| #include "llvm/Support/ErrorHandling.h" | ||
| #include "llvm/Support/FileSystem.h" | ||
|
|
@@ -155,6 +157,11 @@ static cl::bits<PGOMapFeaturesEnum> PgoAnalysisMapFeatures( | |
| "Enable extended information within the SHT_LLVM_BB_ADDR_MAP that is " | ||
| "extracted from PGO related analysis.")); | ||
|
|
||
| static cl::opt<bool> EmitJumpTableSizesSection( | ||
| "emit-jump-table-sizes-section", | ||
| cl::desc("Emit a section containing jump table addresses and sizes"), | ||
| cl::Hidden, cl::init(false)); | ||
|
|
||
| STATISTIC(EmittedInsts, "Number of machine instrs printed"); | ||
|
|
||
| char AsmPrinter::ID = 0; | ||
|
|
@@ -2764,10 +2771,59 @@ void AsmPrinter::emitJumpTableInfo() { | |
| for (const MachineBasicBlock *MBB : JTBBs) | ||
| emitJumpTableEntry(MJTI, MBB, JTI); | ||
| } | ||
|
|
||
| if (EmitJumpTableSizesSection) | ||
| emitJumpTableSizesSection(MJTI, F); | ||
|
|
||
| if (!JTInDiffSection) | ||
| OutStreamer->emitDataRegion(MCDR_DataRegionEnd); | ||
| } | ||
|
|
||
| void AsmPrinter::emitJumpTableSizesSection(const MachineJumpTableInfo *MJTI, | ||
| const Function &F) const { | ||
| const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); | ||
|
|
||
| if (JT.empty()) | ||
| return; | ||
|
|
||
| StringRef GroupName = F.hasComdat() ? F.getComdat()->getName() : ""; | ||
| MCSection *JumpTableSizesSection = nullptr; | ||
| StringRef sectionName = ".llvm_jump_table_sizes"; | ||
|
|
||
| if (TM.getTargetTriple().isOSBinFormatELF()) { | ||
| MCSymbolELF *LinkedToSym = dyn_cast<MCSymbolELF>(CurrentFnSym); | ||
| int Flags = F.hasComdat() ? ELF::SHF_GROUP : 0; | ||
|
|
||
| JumpTableSizesSection = OutContext.getELFSection( | ||
| sectionName, ELF::SHT_LLVM_JT_SIZES, Flags, 0, GroupName, F.hasComdat(), | ||
| MCSection::NonUniqueID, LinkedToSym); | ||
| } else if (TM.getTargetTriple().isOSBinFormatCOFF()) { | ||
| if (F.hasComdat()) { | ||
| MCSymbolCOFF *LinkedToSym = dyn_cast<MCSymbolCOFF>(CurrentFnSym); | ||
|
|
||
| JumpTableSizesSection = OutContext.getCOFFSection( | ||
| sectionName, | ||
| COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ | | ||
| COFF::IMAGE_SCN_LNK_COMDAT | COFF::IMAGE_SCN_MEM_DISCARDABLE, | ||
| F.getComdat()->getName(), COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE); | ||
| } else | ||
| JumpTableSizesSection = OutContext.getCOFFSection( | ||
| sectionName, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | | ||
| COFF::IMAGE_SCN_MEM_READ | | ||
| COFF::IMAGE_SCN_MEM_DISCARDABLE); | ||
| } else { | ||
| return; | ||
|
||
| } | ||
|
|
||
| OutStreamer->switchSection(JumpTableSizesSection); | ||
|
|
||
| for (unsigned JTI = 0, E = JT.size(); JTI != E; ++JTI) { | ||
| const std::vector<MachineBasicBlock *> &JTBBs = JT[JTI].MBBs; | ||
| OutStreamer->emitSymbolValue(GetJTISymbol(JTI), TM.getProgramPointerSize()); | ||
| OutStreamer->emitIntValue(JTBBs.size(), TM.getProgramPointerSize()); | ||
| } | ||
| } | ||
|
|
||
| /// EmitJumpTableEntry - Emit a jump table entry for the specified MBB to the | ||
| /// current stream. | ||
| void AsmPrinter::emitJumpTableEntry(const MachineJumpTableInfo *MJTI, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| ; RUN: llc %s -o - -mtriple x86_64-sie-ps5 -emit-jump-table-sizes-section -verify-machineinstrs --relocation-model=pic | FileCheck --check-prefix=PS5-CHECK %s | ||
| ; RUN: llc %s -o - -mtriple x86_64-sie-ps5 -verify-machineinstrs --relocation-model=pic | FileCheck --check-prefix=NOFLAG %s | ||
| ; RUN: llc %s -o - -mtriple x86_64-sie-ps5 -verify-machineinstrs --relocation-model=pic | FileCheck --check-prefix=NOTABLE %s | ||
|
|
||
| ; RUN: llc %s -o - -mtriple x86_64-unknown-linux-gnu -emit-jump-table-sizes-section -verify-machineinstrs --relocation-model=pic | FileCheck --check-prefix=ELF-CHECK %s | ||
| ; RUN: llc %s -o - -mtriple x86_64-unknown-linux-gnu -verify-machineinstrs --relocation-model=pic | FileCheck --check-prefix=NOFLAG %s | ||
| ; RUN: llc %s -o - -mtriple x86_64-unknown-linux-gnu -verify-machineinstrs --relocation-model=pic | FileCheck --check-prefix=NOTABLE %s | ||
|
|
||
| ; RUN: llc %s -o - -mtriple x86_64-pc-windows-msvc -emit-jump-table-sizes-section -verify-machineinstrs --relocation-model=pic | FileCheck --check-prefix=COFF-CHECK %s | ||
| ; RUN: llc %s -o - -mtriple x86_64-pc-windows-msvc -verify-machineinstrs --relocation-model=pic | FileCheck --check-prefix=NOFLAG %s | ||
| ; RUN: llc %s -o - -mtriple x86_64-pc-windows-msvc -verify-machineinstrs --relocation-model=pic | FileCheck --check-prefix=NOTABLE %s | ||
|
|
||
| ; This test verifies the jump table size section. Currently only enabled by default on the PS5 target. | ||
|
|
||
| $foo1 = comdat any | ||
|
|
||
| ; Ensure proper comdat handling. | ||
| define void @foo1(i32 %x, ptr %to) comdat { | ||
|
|
||
| ; PS5-CHECK-LABEL: foo1 | ||
| ; PS5-CHECK: .section .llvm_jump_table_sizes,"G",@llvm_jt_sizes,foo1,comdat | ||
| ; PS5-CHECK-NEXT: .quad .LJTI0_0 | ||
| ; PS5-CHECK-NEXT: .quad 6 | ||
|
|
||
| ; ELF-CHECK-LABEL: foo1 | ||
| ; ELF-CHECK: .section .llvm_jump_table_sizes,"G",@llvm_jt_sizes,foo1,comdat | ||
| ; ELF-CHECK-NEXT: .quad .LJTI0_0 | ||
| ; ELF-CHECK-NEXT: .quad 6 | ||
|
|
||
| ; COFF-CHECK-LABEL: foo1 | ||
| ; COFF-CHECK: .section .llvm_jump_table_sizes,"drD",associative,foo1 | ||
| ; COFF-CHECK-NEXT: .quad .LJTI0_0 | ||
| ; COFF-CHECK-NEXT: .quad 6 | ||
|
|
||
| ; NOFLAG-LABEL: foo1 | ||
| ; NOFLAG-NOT: .section .llvm_jump_table_sizes | ||
|
|
||
| entry: | ||
| switch i32 %x, label %default [ | ||
| i32 0, label %bb0 | ||
| i32 1, label %bb1 | ||
| i32 2, label %bb2 | ||
| i32 3, label %bb3 | ||
| i32 4, label %bb4 | ||
| i32 5, label %bb4 | ||
| ] | ||
| bb0: | ||
| store i32 0, ptr %to | ||
| br label %exit | ||
| bb1: | ||
| store i32 1, ptr %to | ||
| br label %exit | ||
| bb2: | ||
| store i32 2, ptr %to | ||
| br label %exit | ||
| bb3: | ||
| store i32 3, ptr %to | ||
| br label %exit | ||
| bb4: | ||
| store i32 4, ptr %to | ||
| br label %exit | ||
| exit: | ||
| ret void | ||
| default: | ||
| unreachable | ||
| } | ||
|
|
||
| define void @foo2(i32 %x, ptr %to) { | ||
|
|
||
| ; PS5-CHECK-LABEL: foo2 | ||
| ; PS5-CHECK: .section .llvm_jump_table_sizes,"",@llvm_jt_sizes | ||
| ; PS5-CHECK-NEXT: .quad .LJTI1_0 | ||
| ; PS5-CHECK-NEXT: .quad 5 | ||
|
|
||
| ; ELF-CHECK-LABEL: foo2 | ||
| ; ELF-CHECK: .section .llvm_jump_table_sizes,"",@llvm_jt_sizes | ||
| ; ELF-CHECK-NEXT: .quad .LJTI1_0 | ||
| ; ELF-CHECK-NEXT: .quad 5 | ||
|
|
||
| ; COFF-CHECK-LABEL: foo2 | ||
| ; COFF-CHECK: .section .llvm_jump_table_sizes,"drD" | ||
| ; COFF-CHECK-NEXT: .quad .LJTI1_0 | ||
| ; COFF-CHECK-NEXT: .quad 5 | ||
|
|
||
| ; NOFLAG-LABEL: foo1 | ||
| ; NOFLAG-NOT: .section .llvm_jump_table_sizes | ||
|
|
||
| entry: | ||
| switch i32 %x, label %default [ | ||
| i32 0, label %bb0 | ||
| i32 1, label %bb1 | ||
| i32 2, label %bb2 | ||
| i32 3, label %bb3 | ||
| i32 4, label %bb4 | ||
| ] | ||
| bb0: | ||
| store i32 0, ptr %to | ||
| br label %exit | ||
| bb1: | ||
| store i32 1, ptr %to | ||
| br label %exit | ||
| bb2: | ||
| store i32 2, ptr %to | ||
| br label %exit | ||
| bb3: | ||
| store i32 3, ptr %to | ||
| br label %exit | ||
| bb4: | ||
| store i32 4, ptr %to | ||
| br label %exit | ||
| exit: | ||
| ret void | ||
| default: | ||
| unreachable | ||
| } | ||
|
|
||
| ; Ensure that the section isn't produced if there is no jump table. | ||
|
|
||
| define void @foo3(i32 %x, ptr %to) { | ||
|
|
||
| ; NOTABLE-LABEL: foo3 | ||
| ; NOTABLE-NOT: .section .llvm_jump_table_sizes | ||
|
|
||
| exit: | ||
| ret void | ||
| } | ||
|
|
||
| ; Ensure we can deal with nested jump tables. | ||
|
|
||
| define void @nested(i32 %x, i32 %y, ptr %to) { | ||
|
|
||
| ; PS5-CHECK-LABEL: nested | ||
| ; PS5-CHECK: .section .llvm_jump_table_sizes,"",@llvm_jt_sizes | ||
| ; PS5-CHECK-NEXT: .quad .LJTI3_0 | ||
| ; PS5-CHECK-NEXT: .quad 5 | ||
| ; PS5-CHECK-NEXT: .quad .LJTI3_1 | ||
| ; PS5-CHECK-NEXT: .quad 6 | ||
|
|
||
| ; ELF-CHECK-LABEL: nested | ||
| ; ELF-CHECK: .section .llvm_jump_table_sizes,"",@llvm_jt_sizes | ||
| ; ELF-CHECK-NEXT: .quad .LJTI3_0 | ||
| ; ELF-CHECK-NEXT: .quad 5 | ||
| ; ELF-CHECK-NEXT: .quad .LJTI3_1 | ||
| ; ELF-CHECK-NEXT: .quad 6 | ||
|
|
||
| ; COFF-CHECK-LABEL: nested | ||
| ; COFF-CHECK: .section .llvm_jump_table_sizes,"drD" | ||
| ; COFF-CHECK-NEXT: .quad .LJTI3_0 | ||
| ; COFF-CHECK-NEXT: .quad 5 | ||
| ; COFF-CHECK-NEXT: .quad .LJTI3_1 | ||
| ; COFF-CHECK-NEXT: .quad 6 | ||
|
|
||
| ; NOFLAG-LABEL: nested | ||
| ; NOFLAG-NOT: .section .llvm_jump_table_sizes | ||
|
|
||
| entry: | ||
| switch i32 %x, label %default [ | ||
| i32 0, label %bb0 | ||
| i32 1, label %bb1 | ||
| i32 2, label %bb2 | ||
| i32 3, label %bb3 | ||
| i32 4, label %bb4 | ||
| ] | ||
| bb0: | ||
| store i32 0, ptr %to | ||
| br label %exit | ||
| bb1: | ||
| store i32 1, ptr %to | ||
| br label %exit | ||
| bb2: | ||
| store i32 2, ptr %to | ||
| br label %exit | ||
| bb3: | ||
| store i32 3, ptr %to | ||
| br label %exit | ||
| bb4: | ||
| switch i32 %y, label %default [ | ||
| i32 1, label %bb5 | ||
| i32 2, label %bb6 | ||
| i32 3, label %bb7 | ||
| i32 4, label %bb8 | ||
| i32 5, label %bb9 | ||
| i32 6, label %bb10 | ||
| ] | ||
| br label %exit2 | ||
| bb5: | ||
| store i32 4, ptr %to | ||
| br label %exit | ||
| bb6: | ||
| store i32 4, ptr %to | ||
| br label %exit | ||
| bb7: | ||
| store i32 4, ptr %to | ||
| br label %exit | ||
| bb8: | ||
| store i32 4, ptr %to | ||
| br label %exit | ||
| bb9: | ||
| store i32 4, ptr %to | ||
| br label %exit | ||
| bb10: | ||
| store i32 4, ptr %to | ||
| br label %exit | ||
| exit: | ||
| ret void | ||
| exit2: | ||
| ret void | ||
| default: | ||
| unreachable | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Braces here would be nice to match the other side.