Skip to content

Commit 8a351f1

Browse files
authored
[lld][LoongArch] Support relaxation during IE to LE conversion (llvm#123702)
Complement llvm#123680. When relaxation enable, remove redundant NOPs.
1 parent fafeaab commit 8a351f1

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

lld/ELF/Arch/LoongArch.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,11 @@ static bool relax(Ctx &ctx, InputSection &sec) {
974974
if (relaxable(relocs, i))
975975
relaxTlsLe(ctx, sec, i, loc, r, remove);
976976
break;
977+
case R_LARCH_TLS_IE_PC_HI20:
978+
if (relaxable(relocs, i) && r.expr == R_RELAX_TLS_IE_TO_LE &&
979+
isUInt<12>(r.sym->getVA(ctx, r.addend)))
980+
remove = 4;
981+
break;
977982
}
978983

979984
// For all anchors whose offsets are <= r.offset, they are preceded by
@@ -1048,7 +1053,7 @@ void LoongArch::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
10481053
secAddr += s->outSecOff;
10491054
else if (auto *ehIn = dyn_cast<EhInputSection>(&sec))
10501055
secAddr += ehIn->getParent()->outSecOff;
1051-
bool isExtreme = false;
1056+
bool isExtreme = false, isRelax = false;
10521057
const MutableArrayRef<Relocation> relocs = sec.relocs();
10531058
for (size_t i = 0, size = relocs.size(); i != size; ++i) {
10541059
Relocation &rel = relocs[i];
@@ -1077,6 +1082,9 @@ void LoongArch::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
10771082
bits);
10781083
relocateNoSym(loc, rel.type, val);
10791084
} else {
1085+
isRelax = relaxable(relocs, i);
1086+
if (isRelax && rel.type == R_LARCH_TLS_IE_PC_HI20 && isUInt<12>(val))
1087+
continue;
10801088
tlsIeToLe(loc, rel, val);
10811089
}
10821090
continue;

lld/test/ELF/loongarch-relax-tls-ie.s

+25-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33

44
# RUN: llvm-mc --filetype=obj --triple=loongarch64 -mattr=+relax %s -o %t.o
55

6-
## FIXME: IE relaxation has not yet been implemented.
7-
## --relax/--no-relax has the same result. Also check --emit-relocs.
6+
## Also check --emit-relocs.
87
# RUN: ld.lld --emit-relocs %t.o -o %t
98
# RUN: llvm-readelf -x .got %t 2>&1 | FileCheck --check-prefix=LE-GOT %s
10-
# RUN: llvm-objdump -dr --no-show-raw-insn %t | FileCheck --check-prefixes=LE %s
9+
# RUN: llvm-objdump -dr --no-show-raw-insn %t | FileCheck --check-prefixes=LER %s
1110

1211
# RUN: ld.lld --emit-relocs --no-relax %t.o -o %t.norelax
1312
# RUN: llvm-readelf -x .got %t.norelax 2>&1 | FileCheck --check-prefix=LE-GOT %s
@@ -42,6 +41,29 @@
4241
# LE-NEXT: add.d $a2, $a2, $tp
4342
# LE-NEXT: add.d $a3, $a3, $tp
4443

44+
# LER: 20158: ori $a0, $zero, 4095
45+
# LER-NEXT: R_LARCH_TLS_IE_PC_HI20 a
46+
# LER-NEXT: R_LARCH_RELAX *ABS*
47+
# LER-NEXT: R_LARCH_TLS_IE_PC_LO12 a
48+
# LER-NEXT: R_LARCH_RELAX *ABS*
49+
# LER-NEXT: add.d $a0, $a0, $tp
50+
# LER-NEXT: 20160: lu12i.w $a1, 1
51+
# LER-NEXT: R_LARCH_TLS_IE_PC_HI20 b
52+
# LER-NEXT: ori $a1, $a1, 0
53+
# LER-NEXT: R_LARCH_TLS_IE_PC_LO12 b
54+
# LER-NEXT: add.d $a1, $a1, $tp
55+
# LER-NEXT: 2016c: lu12i.w $a3, 1
56+
# LER-NEXT: R_LARCH_TLS_IE_PC_HI20 a
57+
# LER-NEXT: R_LARCH_RELAX *ABS*
58+
# LER-NEXT: R_LARCH_TLS_IE_PC_HI20 b
59+
# LER-NEXT: R_LARCH_RELAX *ABS*
60+
# LER-NEXT: ori $a2, $zero, 4095
61+
# LER-NEXT: R_LARCH_TLS_IE_PC_LO12 a
62+
# LER-NEXT: ori $a3, $a3, 0
63+
# LER-NEXT: R_LARCH_TLS_IE_PC_LO12 b
64+
# LER-NEXT: add.d $a2, $a2, $tp
65+
# LER-NEXT: add.d $a3, $a3, $tp
66+
4567
la.tls.ie $a0, a # relax
4668
add.d $a0, $a0, $tp
4769

0 commit comments

Comments
 (0)