-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[lld][LoongArch] Support relaxation during TLSDESC GD/LD to IE/LE conversion #123730
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
Open
ylzsx
wants to merge
37
commits into
users/ylzsx/r-tlsdesc-to-iele-norelax
Choose a base branch
from
users/ylzsx/r-tlsdesc-to-iele-relax
base: users/ylzsx/r-tlsdesc-to-iele-norelax
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[lld][LoongArch] Support relaxation during TLSDESC GD/LD to IE/LE conversion #123730
ylzsx
wants to merge
37
commits into
users/ylzsx/r-tlsdesc-to-iele-norelax
from
users/ylzsx/r-tlsdesc-to-iele-relax
Conversation
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
Support relaxation optimization for two types of code sequences. ``` From: pcalau12i $a0, %pc_hi20(sym) R_LARCH_PCALA_HI20, R_LARCH_RELAX addi.w/d $a0, $a0, %pc_lo12(sym) R_LARCH_PCALA_LO12, R_LARCH_RELAX To: pcaddi $a0, %pc_lo12(sym) R_LARCH_PCREL20_S2 From: pcalau12i $a0, %got_pc_hi20(sym_got) R_LARCH_GOT_PC_HI20, R_LARCH_RELAX ld.w/d $a0, $a0, %got_pc_lo12(sym_got) R_LARCH_GOT_PC_LO12, R_LARCH_RELAX To: pcaddi $a0, %got_pc_hi20(sym_got) R_LARCH_PCREL20_S2 ```
Similar to aarch64-adrp-ldr-got-symbols.s.
Dependency on #123017
Instructions with relocation `R_LARCH_CALL36` may be relax as follows: ``` From: pcaddu18i $dest, %call36(foo) R_LARCH_CALL36, R_LARCH_RELAX jirl $r, $dest, 0 To: b/bl foo # bl if r=$ra, b if r=$zero R_LARCH_B26 ```
In local-exec form, the code sequence is converted as follows: ``` From: lu12i.w $rd, %le_hi20_r(sym) R_LARCH_TLS_LE_HI20_R, R_LARCH_RELAX add.w/d $rd, $rd, $tp, %le_add_r(sym) R_LARCH_TLS_LE_ADD_R, R_LARCH_RELAX addi/ld/st.w/d $rd, $rd, %le_lo12_r(sym) R_LARCH_TLS_LE_LO12_R, R_LARCH_RELAX To: addi/ld/st.w/d $rd, $tp, %le_lo12_r(sym) R_LARCH_TLS_LE_LO12_R ``` In global-dynamic or local-dynamic, the code sequence is converted as follows: ``` From: pcalau12i $a0, %ld_pc_hi20(sym) | %gd_pc_hi20(sym) R_LARCH_TLS_GD_PC_HI20 | R_LARCH_TLS_LD_PC_HI20, R_LARCH_RELAX addi.w/d $a0, $a0, %got_pc_lo12(sym) | %got_pc_lo12(sym) R_LARCH_GOT_PC_LO12, R_LARCH_RELAX To: pcaddi $a0, %got_pc_lo12(sym) | %got_pc_lo12(sym) R_LARCH_TLS_GD_PCREL20_S2 | R_LARCH_TLS_LD_PCREL20_S2 ``` Note: For initial-exec form, since it involves the conversion from IE to LE, we will implement it in a future patch.
Original code sequence: * pcalau12i $a0, %desc_pc_hi20(sym_desc) * addi.d $a0, $a0, %desc_pc_lo12(sym_desc) * ld.d $ra, $a0, %desc_ld(sym_desc) * jirl $ra, $ra, %desc_call(sym_desc) Cannot convert to LE/IE, but relax: * pcaddi $a0, %desc_pcrel_20(sym_desc) * ld.d $ra, $a0, %desc_ld(sym_desc) * jirl $ra, $ra, %desc_call(sym_desc) FIXME: The conversion of TLSDESC GD/LD to LE/IE will implement in a future patch.
llvm-objdump -dr cannot print the relocations, however objudmp works well. It also occurs for RISCV.
Original code sequence: * pcalau12i $a0, %ie_pc_hi20(sym) * ld.d $a0, $a0, %ie_pc_lo12(sym) The code sequence converted is as follows: * lu12i.w $a0, %ie_pc_hi20(sym) # le_hi20 != 0, otherwise NOP * ori $a0 $a0, %ie_pc_lo12(sym) FIXME: When relaxation enables, redundant NOP can be removed. This will be implemented in a future patch. Note: In the normal or medium code model, original code sequence with relocations can appear interleaved, because converted code sequence calculates the absolute offset. However, in extreme code model, to identify the current code model, the first four instructions with relocations must appear consecutively.
Complement https://. When relaxation enable, remove redundant NOPs.
Support TLSDESC to initial-exec or local-exec optimizations. Introduce a new hook RE_LOONGARCH_RELAX_TLS_GD_TO_IE_PAGE_PC and use existing R_RELAX_TLS_GD_TO_IE_ABS to support TLSDESC => IE, while use existing R_RELAX_TLS_GD_TO_LE to support TLSDESC => LE. In normal or medium code model, there are two forms of code sequences: * pcalau12i $a0, %desc_pc_hi20(sym_desc) * addi.d $a0, $a0, %desc_pc_lo12(sym_desc) * ld.d $ra, $a0, %desc_ld(sym_desc) * jirl $ra, $ra, %desc_call(sym_desc) ------ * pcaddi $a0, %desc_pcrel_20(sym_desc) * ld.d $ra, $a0, %desc_ld(sym_desc) * jirl $ra, $ra, %desc_call(sym_desc) The code sequence obtained is as follows: * pcalau12i $a0, %ie_pc_hi20(sym_ie) * ld.[wd] $a0, $a0, %ie_pc_lo12(sym_ie) Simplicity, whether tlsdescToIe or tlsdescToLe, we always tend to convert the preceding instructions to NOPs, due to both forms of code sequence (corresponding to relocation combinations: R_LARCH_TLS_DESC_PC_HI20+R_LARCH_TLS_DESC_PC_LO12 and R_LARCH_TLS_DESC_PCREL20_S2) have same process. FIXME: When relaxation enables, redundant NOPs can be removed. It will be implemented in a future patch. Note: All forms of TLSDESC code sequences should not appear interleaved in the normal, medium or extreme code model, which compilers do not generate and lld is unsupported. This is thanks to the guard in PostRASchedulerList.cpp in llvm. ``` Calls are not scheduling boundaries before register allocation, but post-ra we don't gain anything by scheduling across calls since we don't need to worry about register pressure. ```
@llvm/pr-subscribers-backend-loongarch @llvm/pr-subscribers-lld Author: Zhaoxin Yang (ylzsx) ChangesComplement #123715. When relaxation enable, remove redundant NOPs. Full diff: https://github.com/llvm/llvm-project/pull/123730.diff 2 Files Affected:
diff --git a/lld/ELF/Arch/LoongArch.cpp b/lld/ELF/Arch/LoongArch.cpp
index 37614c3e9615d6..5f49b23e8ffb1a 100644
--- a/lld/ELF/Arch/LoongArch.cpp
+++ b/lld/ELF/Arch/LoongArch.cpp
@@ -965,10 +965,16 @@ static bool relax(Ctx &ctx, InputSection &sec) {
case R_LARCH_GOT_PC_HI20:
case R_LARCH_TLS_GD_PC_HI20:
case R_LARCH_TLS_LD_PC_HI20:
- case R_LARCH_TLS_DESC_PC_HI20:
// The overflow check for i+2 will be carried out in isPairRelaxable.
- if (r.expr != RE_LOONGARCH_RELAX_TLS_GD_TO_IE_PAGE_PC &&
- r.expr != R_RELAX_TLS_GD_TO_LE && isPairRelaxable(relocs, i))
+ if (isPairRelaxable(relocs, i))
+ relaxPCHi20Lo12(ctx, sec, i, loc, r, relocs[i + 2], remove);
+ break;
+ case R_LARCH_TLS_DESC_PC_HI20:
+ if (r.expr == RE_LOONGARCH_RELAX_TLS_GD_TO_IE_PAGE_PC ||
+ r.expr == R_RELAX_TLS_GD_TO_LE) {
+ if (relaxable(relocs, i))
+ remove = 4;
+ } else if (isPairRelaxable(relocs, i))
relaxPCHi20Lo12(ctx, sec, i, loc, r, relocs[i + 2], remove);
break;
case R_LARCH_CALL36:
@@ -986,6 +992,17 @@ static bool relax(Ctx &ctx, InputSection &sec) {
isUInt<12>(r.sym->getVA(ctx, r.addend)))
remove = 4;
break;
+ case R_LARCH_TLS_DESC_PC_LO12:
+ if (relaxable(relocs, i) &&
+ (r.expr == RE_LOONGARCH_RELAX_TLS_GD_TO_IE_PAGE_PC ||
+ r.expr == R_RELAX_TLS_GD_TO_LE))
+ remove = 4;
+ break;
+ case R_LARCH_TLS_DESC_LD:
+ if (relaxable(relocs, i) && r.expr == R_RELAX_TLS_GD_TO_LE &&
+ isUInt<12>(r.sym->getVA(ctx, r.addend)))
+ remove = 4;
+ break;
}
// For all anchors whose offsets are <= r.offset, they are preceded by
@@ -1214,6 +1231,10 @@ void LoongArch::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
bits);
relocateNoSym(loc, rel.type, val);
} else {
+ isRelax = relaxable(relocs, i);
+ if (isRelax && (rel.type == R_LARCH_TLS_DESC_PC_HI20 ||
+ rel.type == R_LARCH_TLS_DESC_PC_LO12))
+ continue;
tlsdescToIe(loc, rel, val);
}
continue;
@@ -1230,6 +1251,11 @@ void LoongArch::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
bits);
relocateNoSym(loc, rel.type, val);
} else {
+ isRelax = relaxable(relocs, i);
+ if (isRelax && (rel.type == R_LARCH_TLS_DESC_PC_HI20 ||
+ rel.type == R_LARCH_TLS_DESC_PC_LO12 ||
+ (rel.type == R_LARCH_TLS_DESC_LD && isUInt<12>(val))))
+ continue;
tlsdescToLe(loc, rel, val);
}
continue;
diff --git a/lld/test/ELF/loongarch-relax-tlsdesc.s b/lld/test/ELF/loongarch-relax-tlsdesc.s
index 5e538985d1402c..666ca6bd1e7243 100644
--- a/lld/test/ELF/loongarch-relax-tlsdesc.s
+++ b/lld/test/ELF/loongarch-relax-tlsdesc.s
@@ -9,7 +9,6 @@
# RUN: llvm-readobj -r -x .got a.64.so | FileCheck --check-prefix=GD64-RELA %s
# RUN: llvm-objdump --no-show-raw-insn -dr -h a.64.so | FileCheck %s --check-prefix=GD64
-## FIXME: IE/LE relaxation have not yet been implemented, --relax/--no-relax obtain the same results.
## Transition from TLSDESC to IE/LE. Also check --emit-relocs.
# RUN: ld.lld --relax -e 0 -z now --emit-relocs a.64.o c.64.o -o a.64.le
# RUN: llvm-readobj -r -x .got a.64.le 2>&1 | FileCheck --check-prefix=LE64-RELA %s
@@ -73,25 +72,21 @@
# LE64-RELA: could not find section '.got'
## a@tprel = 0x8
-# LE64: 20158: nop
+# LE64: 20158: ori $a0, $zero, 8
# LE64-NEXT: R_LARCH_TLS_DESC_PC_HI20 a
# LE64-NEXT: R_LARCH_RELAX *ABS*
-# LE64-NEXT: nop
# LE64-NEXT: R_LARCH_TLS_DESC_PC_LO12 a
# LE64-NEXT: R_LARCH_RELAX *ABS*
-# LE64-NEXT: nop
# LE64-NEXT: R_LARCH_TLS_DESC_LD a
# LE64-NEXT: R_LARCH_RELAX *ABS*
-# LE64-NEXT: ori $a0, $zero, 8
# LE64-NEXT: R_LARCH_TLS_DESC_CALL a
# LE64-NEXT: R_LARCH_RELAX *ABS*
# LE64-NEXT: add.d $a1, $a0, $tp
## b@tprel = 0x7ff
-# LE64: 2016c: nop
+# LE64: 20160: nop
# LE64-NEXT: R_LARCH_TLS_DESC_PC_HI20 b
# LE64-NEXT: R_LARCH_RELAX *ABS*
-# LE64-NEXT: nop
# LE64-NEXT: R_LARCH_TLS_DESC_PC_LO12 b
# LE64-NEXT: nop
# LE64-NEXT: R_LARCH_TLS_DESC_LD b
@@ -101,7 +96,7 @@
## c@tprel = 0x800
## Without R_LARCH_RELAX relocation. No relaxation.
-# LE64: 20180: nop
+# LE64: 20170: nop
# LE64-NEXT: R_LARCH_TLS_DESC_PC_HI20 c
# LE64-NEXT: addi.d $t0, $zero, 0
# LE64-NEXT: nop
@@ -115,13 +110,11 @@
# LE64-NEXT: add.d $a3, $a0, $tp
## d@tprel = 0x1000
-# LE64: 201a0: nop
+# LE64: 20190: lu12i.w $a0, 1
# LE64-NEXT: R_LARCH_TLS_DESC_PC_HI20 d
# LE64-NEXT: R_LARCH_RELAX *ABS*
-# LE64-NEXT: nop
# LE64-NEXT: R_LARCH_TLS_DESC_PC_LO12 d
# LE64-NEXT: R_LARCH_RELAX *ABS*
-# LE64-NEXT: lu12i.w $a0, 1
# LE64-NEXT: R_LARCH_TLS_DESC_LD d
# LE64-NEXT: ori $a0, $a0, 0
# LE64-NEXT: R_LARCH_TLS_DESC_CALL d
@@ -160,35 +153,31 @@
# LE64-NORELAX-NEXT: add.d $a4, $a0, $tp
# IE64-RELA: .rela.dyn {
-# IE64-RELA-NEXT: 0x30408 R_LARCH_TLS_TPREL64 c 0x0
-# IE64-RELA-NEXT: 0x30410 R_LARCH_TLS_TPREL64 d 0x0
+# IE64-RELA-NEXT: 0x303F0 R_LARCH_TLS_TPREL64 c 0x0
+# IE64-RELA-NEXT: 0x303F8 R_LARCH_TLS_TPREL64 d 0x0
# IE64-RELA-NEXT: }
# IE64-RELA: Hex dump of section '.got':
-# IE64-RELA-NEXT: 0x00030408 00000000 00000000 00000000 00000000 .
+# IE64-RELA-NEXT: 0x000303f0 00000000 00000000 00000000 00000000 .
-# IE64: .got 00000010 0000000000030408
+# IE64: .got 00000010 00000000000303f0
## a and b are optimized to use LE. c and d are optimized to IE.
## a@tprel = 0x8
-# IE64: 202c8: nop
+# IE64: 202c8: ori $a0, $zero, 8
# IE64-NEXT: R_LARCH_TLS_DESC_PC_HI20 a
# IE64-NEXT: R_LARCH_RELAX *ABS*
-# IE64-NEXT: nop
# IE64-NEXT: R_LARCH_TLS_DESC_PC_LO12 a
# IE64-NEXT: R_LARCH_RELAX *ABS*
-# IE64-NEXT: nop
# IE64-NEXT: R_LARCH_TLS_DESC_LD a
# IE64-NEXT: R_LARCH_RELAX *ABS*
-# IE64-NEXT: ori $a0, $zero, 8
# IE64-NEXT: R_LARCH_TLS_DESC_CALL a
# IE64-NEXT: R_LARCH_RELAX *ABS*
# IE64-NEXT: add.d $a1, $a0, $tp
## b@tprel = 0x7ff
-# IE64: 202dc: nop
+# IE64: 202d0: nop
# IE64-NEXT: R_LARCH_TLS_DESC_PC_HI20 b
# IE64-NEXT: R_LARCH_RELAX *ABS*
-# IE64-NEXT: nop
# IE64-NEXT: R_LARCH_TLS_DESC_PC_LO12 b
# IE64-NEXT: nop
# IE64-NEXT: R_LARCH_TLS_DESC_LD b
@@ -196,9 +185,9 @@
# IE64-NEXT: R_LARCH_TLS_DESC_CALL b
# IE64-NEXT: add.d $a2, $a0, $tp
-## &.got[c]-. = 0x30408 - 0x20300: 0x10 pages, page offset 0x408
+## &.got[c]-. = 0x303f0 - 0x202f0: 0x10 pages, page offset 0x3f0
## Without R_LARCH_RELAX relocation. No relaxation.
-# IE64: 202f0: nop
+# IE64: 202e0: nop
# IE64-NEXT: R_LARCH_TLS_DESC_PC_HI20 c
# IE64-NEXT: addi.d $t0, $zero, 0
# IE64-NEXT: nop
@@ -207,20 +196,18 @@
# IE64-NEXT: pcalau12i $a0, 16
# IE64-NEXT: R_LARCH_TLS_DESC_LD c
# IE64-NEXT: addi.d $t0, $t0, 1
-# IE64-NEXT: ld.d $a0, $a0, 1032
+# IE64-NEXT: ld.d $a0, $a0, 1008
# IE64-NEXT: R_LARCH_TLS_DESC_CALL c
# IE64-NEXT: add.d $a3, $a0, $tp
-## &.got[d]-. = 0x30408+8 - 0x20318: 0x10 pages, page offset 0x410
-# IE64: 20310: nop
+## &.got[d]-. = 0x303f0+8 - 0x20300: 0x10 pages, page offset 0x3f8
+# IE64: 20300: pcalau12i $a0, 16
# IE64-NEXT: R_LARCH_TLS_DESC_PC_HI20 d
# IE64-NEXT: R_LARCH_RELAX *ABS*
-# IE64-NEXT: nop
# IE64-NEXT: R_LARCH_TLS_DESC_PC_LO12 d
# IE64-NEXT: R_LARCH_RELAX *ABS*
-# IE64-NEXT: pcalau12i $a0, 16
# IE64-NEXT: R_LARCH_TLS_DESC_LD d
-# IE64-NEXT: ld.d $a0, $a0, 1040
+# IE64-NEXT: ld.d $a0, $a0, 1016
# IE64-NEXT: R_LARCH_TLS_DESC_CALL d
# IE64-NEXT: add.d $a4, $a0, $tp
|
This reverts commit 653281c.
ad372af
to
e024b7c
Compare
Complement https://. When relaxation enable, remove redundant NOPs.
e024b7c
to
99a1e07
Compare
cc @xen0n |
0c6ba1d
to
baee006
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Complement #123715. When relaxation enable, remove redundant NOPs.