-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[lld][LoongArch] GOT indirection to PC relative optimization #123743
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5e854ab
[lld][LoongArch] GOT indirection to PC relative optimization.
ylzsx c18b57d
Add check for register.
ylzsx 9b06f46
Delete FIXME according to review.
ylzsx 2fa2d5c
fix code style
ylzsx ae010c8
Merge branch 'main' into users/ylzsx/r-got-to-pcrel
zhaoqi5 0ff13d8
split got optimization and relax and fix the range of tryGotToPCRel
ylzsx 3cb21ed
some fixes accroding to SixWeining's review.
ylzsx 99b1aa6
Merge branch 'main' into users/ylzsx/r-got-to-pcrel
ylzsx 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# REQUIRES: loongarch | ||
# RUN: rm -rf %t && split-file %s %t && cd %t | ||
|
||
# RUN: llvm-mc --filetype=obj --triple=loongarch64 a.s -o a.o | ||
# RUN: llvm-mc --filetype=obj --triple=loongarch64 unpaired.s -o unpaired.o | ||
# RUN: llvm-mc --filetype=obj --triple=loongarch64 lone-ldr.s -o lone-ldr.o | ||
|
||
# RUN: ld.lld a.o -T within-range.t -o a | ||
# RUN: llvm-objdump -d --no-show-raw-insn a | FileCheck %s | ||
|
||
## This test verifies the encoding when the register $a0 is used. | ||
# CHECK: pcalau12i $a0, 0 | ||
# CHECK-NEXT: addi.d $a0, $a0, -2048 | ||
|
||
## PCALAU12I contains a nonzero addend, no relaxations should be applied. | ||
# CHECK-NEXT: pcalau12i $a1, 2 | ||
# CHECK-NEXT: ld.d $a1, $a1, -2048 | ||
|
||
## LD contains a nonzero addend, no relaxations should be applied. | ||
# CHECK-NEXT: pcalau12i $a2, 2 | ||
# CHECK-NEXT: ld.d $a2, $a2, -2040 | ||
|
||
## PCALAU12I and LD use different registers, no relaxations should be applied. | ||
# CHECK-NEXT: pcalau12i $a3, 2 | ||
# CHECK-NEXT: ld.d $a4, $a3, -2048 | ||
|
||
## PCALAU12I and LD use different registers, no relaxations should be applied. | ||
# CHECK-NEXT: pcalau12i $a5, 2 | ||
# CHECK-NEXT: ld.d $a5, $a6, -2048 | ||
|
||
# RUN: ld.lld a.o -T underflow-range.t -o a-underflow | ||
# RUN: llvm-objdump -d --no-show-raw-insn a-underflow | FileCheck --check-prefix=OUTRANGE %s | ||
|
||
# RUN: ld.lld a.o -T overflow-range.t -o a-overflow | ||
# RUN: llvm-objdump -d --no-show-raw-insn a-overflow | FileCheck --check-prefix=OUTRANGE %s | ||
|
||
# OUTRANGE: pcalau12i $a0, 1 | ||
# OUTRANGE-NEXT: ld.d $a0, $a0, 0 | ||
|
||
## Relocations do not appear in pairs, no relaxations should be applied. | ||
# RUN: ld.lld unpaired.o -T within-range.t -o unpaired | ||
# RUN: llvm-objdump --no-show-raw-insn -d unpaired | FileCheck --check-prefix=UNPAIRED %s | ||
|
||
# UNPAIRED: pcalau12i $a0, 2 | ||
# UNPAIRED-NEXT: b 8 | ||
# UNPAIRED-NEXT: pcalau12i $a0, 2 | ||
# UNPAIRED: ld.d $a0, $a0, -2048 | ||
|
||
## Relocations do not appear in pairs, no relaxations should be applied. | ||
# RUN: ld.lld lone-ldr.o -T within-range.t -o lone-ldr | ||
# RUN: llvm-objdump --no-show-raw-insn -d lone-ldr | FileCheck --check-prefix=LONE-LDR %s | ||
|
||
# LONE-LDR: ld.d $a0, $a0, -2048 | ||
|
||
## 32-bit code is mostly the same. We only test a few variants. | ||
# RUN: llvm-mc --filetype=obj --triple=loongarch32 a.32.s -o a.32.o | ||
# RUN: ld.lld a.32.o -T within-range.t -o a32 | ||
# RUN: llvm-objdump -d --no-show-raw-insn a32 | FileCheck --check-prefix=CHECK32 %s | ||
|
||
## This test verifies the encoding when the register $a0 is used. | ||
# CHECK32: pcalau12i $a0, 0 | ||
# CHECK32-NEXT: addi.w $a0, $a0, -2048 | ||
|
||
|
||
## This linker script ensures that .rodata and .text are sufficiently close to | ||
## each other so that the pcalau12i + ld pair can be relaxed to pcalau12i + add. | ||
#--- within-range.t | ||
SECTIONS { | ||
.rodata 0x1800: { *(.rodata) } | ||
.text 0x2800: { *(.text) } | ||
.got 0x3800: { *(.got) } | ||
} | ||
|
||
## This linker script ensures that .rodata and .text are sufficiently far apart | ||
## so that the pcalau12i + ld pair cannot be relaxed to pcalau12i + add. | ||
#--- underflow-range.t | ||
SECTIONS { | ||
.rodata 0x800-4: { *(.rodata) } | ||
.got 0x80002000: { *(.got) } | ||
.text 0x80001000: { *(.text) } /* (0x800-4)+2GB+0x800+4 */ | ||
} | ||
|
||
#--- overflow-range.t | ||
SECTIONS { | ||
.text 0x1000: { *(.text) } | ||
.got 0x2000: { *(.got) } | ||
.rodata 0x80000800 : { *(.rodata) } /* 0x1000+2GB-0x800 */ | ||
} | ||
|
||
#--- a.s | ||
## Symbol 'x' is nonpreemptible, the optimization should be applied. | ||
.rodata | ||
.hidden x | ||
x: | ||
.word 10 | ||
|
||
.text | ||
.global _start | ||
_start: | ||
pcalau12i $a0, %got_pc_hi20(x) | ||
ld.d $a0, $a0, %got_pc_lo12(x) | ||
pcalau12i $a1, %got_pc_hi20(x+1) | ||
ld.d $a1, $a1, %got_pc_lo12(x) | ||
pcalau12i $a2, %got_pc_hi20(x) | ||
ld.d $a2, $a2, %got_pc_lo12(x+8) | ||
pcalau12i $a3, %got_pc_hi20(x) | ||
ld.d $a4, $a3, %got_pc_lo12(x) | ||
pcalau12i $a5, %got_pc_hi20(x) | ||
ld.d $a5, $a6, %got_pc_lo12(x) | ||
|
||
#--- unpaired.s | ||
.text | ||
.hidden x | ||
x: | ||
nop | ||
.global _start | ||
_start: | ||
pcalau12i $a0, %got_pc_hi20(x) | ||
b L | ||
pcalau12i $a0, %got_pc_hi20(x) | ||
L: | ||
ld.d $a0, $a0, %got_pc_lo12(x) | ||
|
||
#--- lone-ldr.s | ||
.text | ||
.hidden x | ||
x: | ||
nop | ||
.global _start | ||
_start: | ||
ld.d $a0, $a0, %got_pc_lo12(x) | ||
|
||
|
||
#--- a.32.s | ||
## Symbol 'x' is nonpreemptible, the optimization should be applied. | ||
.rodata | ||
.hidden x | ||
x: | ||
.word 10 | ||
|
||
.text | ||
.global _start | ||
_start: | ||
pcalau12i $a0, %got_pc_hi20(x) | ||
ld.w $a0, $a0, %got_pc_lo12(x) |
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
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.
Only apply this relax when
--relax
is enabled forlld
.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.
Clarification: After careful consideration, I think we do not need to check the
--relax
option becauselinker relaxation
is about to reduce the number of instructions while this pr is not.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.
While x86-64 and s390x don't have linker relaxation, they do support --no-relax. --no-relax is useful to disable this optimization.