-
Notifications
You must be signed in to change notification settings - Fork 14.5k
Use COFF image-base-relative jump tables on AMD64 #147625
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
; RUN: llc < %s -relocation-model=static | FileCheck %s | ||
; RUN: llc < %s -relocation-model=pic | FileCheck %s --check-prefix=PIC | ||
; RUN: llc < %s -relocation-model=pic -code-model=large | FileCheck %s --check-prefix=PIC | ||
; RUN: llc < %s -relocation-model=pic | FileCheck %s --check-prefixes=CHECK,PIC | ||
; RUN: llc < %s -relocation-model=pic -code-model=large | FileCheck %s --check-prefixes=CHECK,LARGE | ||
|
||
; FIXME: Remove '-relocation-model=static' when it is no longer necessary to | ||
; trigger the separate .rdata section. | ||
|
@@ -43,25 +43,39 @@ declare void @g(i32) | |
; CHECK: .text | ||
; CHECK: f: | ||
; CHECK: .seh_proc f | ||
; CHECK: jmpq *.LJTI0_0 | ||
; CHECK: .seh_endprologue | ||
|
||
; STATIC: movl .LJTI0_0(,%rax,4), %eax | ||
; STATIC: leaq __ImageBase(%rax), %rax | ||
; STATIC: jmpq *%rax | ||
|
||
; PIC: movl %ecx, %eax | ||
; PIC: leaq .LJTI0_0(%rip), %rcx | ||
; PIC: movl (%rcx,%rax,4), %eax | ||
; PIC: leaq __ImageBase(%rip), %rcx | ||
; PIC: addq %rax, %rcx | ||
; PIC: jmpq *%rcx | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The existing code is:
The proposed sequence is 5 bytes longer, and the benefit is... compatibility with Microsoft-internal tools that don't understand the jump-table debug info format defined by Microsoft? That seems a little dubious; why can't these tools understand the existing debug info? The difference on AArch64 is likely even larger because you're essentially proposing we disable jump table compression. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I agree; this isn't worth it for AMD64, unless I can get the code sequence down to the same idiom that MSVC uses. MSVC uses a single RIP-relative load for COFF image base, then a single register-relative load that has an SIB that handles the offset from the image base to the lookup table start, as well as scaling by the table offset. I tried several different ways to generate the same code and could not find an exact equivalent, i.e. one that generated the right SIB idiom. Ignore my comments are ARM64. I see now that S_ARMSWITCHTABLE covers the layouts that LLVM already uses for switch tables. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. llvm-mc gives the following representation for the load from the jump table; does this help?
In any case, I computed the actual numbers... the sequence in the current patch is actually 6 bytes longer than what LLVM currently generates. The MSVC-equivalent sequence is 3 bytes shorter... but still 3 bytes longer longer than LLVM's current sequence. Which is slightly better than what I expected, but still not great. (The immediate is 4 bytes. Zero-extending instead of sign-extending the table entry saves a byte.) |
||
|
||
; LARGE: movl %ecx, %eax | ||
; LARGE-NEXT: movabsq $.LJTI0_0, %rcx | ||
; LARGE-NEXT: movl (%rcx,%rax,4), %eax | ||
; LARGE-NEXT: movabsq $__ImageBase, %rcx | ||
; LARGE-NEXT: addq %rax, %rcx | ||
; LARGE-NEXT: jmpq *%rcx | ||
|
||
; CHECK: .LBB0_{{.*}}: # %sw.bb | ||
; CHECK: .LBB0_{{.*}}: # %sw.bb2 | ||
; CHECK: .LBB0_{{.*}}: # %sw.bb3 | ||
; CHECK: .LBB0_{{.*}}: # %sw.bb1 | ||
; CHECK: callq g | ||
; CHECK: jmp g # TAILCALL | ||
; STATIC: callq g | ||
; STATIC: jmp g # TAILCALL | ||
; CHECK: .section .rdata,"dr" | ||
; CHECK: .quad .LBB0_ | ||
; CHECK: .quad .LBB0_ | ||
; CHECK: .quad .LBB0_ | ||
; CHECK: .quad .LBB0_ | ||
; CHECK: .LJTI0_0: | ||
; CHECK: .long .LBB0_{{[0-9]+}}@IMGREL | ||
; CHECK: .long .LBB0_{{[0-9]+}}@IMGREL | ||
; CHECK: .long .LBB0_{{[0-9]+}}@IMGREL | ||
; CHECK: .long .LBB0_{{[0-9]+}}@IMGREL | ||
|
||
; It's important that we switch back to .text here, not .rdata. | ||
; CHECK: .text | ||
; CHECK: .seh_endproc | ||
|
||
; Windows PIC code should use 32-bit entries | ||
; PIC: .long .LBB0_2-.LJTI0_0 | ||
; PIC: .long .LBB0_3-.LJTI0_0 | ||
; PIC: .long .LBB0_4-.LJTI0_0 | ||
; PIC: .long .LBB0_5-.LJTI0_0 |
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.
Needs mir support tests in test/CodeGen/MIR