Skip to content

Commit 31b62e2

Browse files
authored
[LoongArch] Add relax relocations for tls_le code sequence (#121329)
This commit add relax relocations for `tls_le` code sequence. Handwritten assembly and generating source code by clang are both affected. Scheduled `tls_le` code sequence can be relaxed normally and we can add relax relocs when code emitting according to their relocs. Other relaxable macros' code sequence cannot simply add relax relocs according to their relocs, such as `PCALA_{HI20/LO12}`, we do not want to add relax relocs when code model is large. This will be implemented in later commit.
1 parent 89e3a64 commit 31b62e2

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCCodeEmitter.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,11 @@ LoongArchMCCodeEmitter::getExprOpValue(const MCInst &MI, const MCOperand &MO,
282282
break;
283283
case LoongArchMCExpr::VK_LoongArch_TLS_LE_HI20_R:
284284
FixupKind = LoongArch::fixup_loongarch_tls_le_hi20_r;
285+
RelaxCandidate = true;
285286
break;
286287
case LoongArchMCExpr::VK_LoongArch_TLS_LE_LO12_R:
287288
FixupKind = LoongArch::fixup_loongarch_tls_le_lo12_r;
289+
RelaxCandidate = true;
288290
break;
289291
case LoongArchMCExpr::VK_LoongArch_PCREL20_S2:
290292
FixupKind = LoongArch::fixup_loongarch_pcrel20_s2;
@@ -387,11 +389,17 @@ void LoongArchMCCodeEmitter::expandAddTPRel(const MCInst &MI,
387389
"Expected %le_add_r relocation on TP-relative symbol");
388390

389391
// Emit the correct %le_add_r relocation for the symbol.
390-
// TODO: Emit R_LARCH_RELAX for %le_add_r where the relax feature is enabled.
391392
Fixups.push_back(MCFixup::create(
392393
0, Expr, MCFixupKind(LoongArch::fixup_loongarch_tls_le_add_r),
393394
MI.getLoc()));
394395

396+
// Emit R_LARCH_RELAX for %le_add_r when the relax feature is enabled.
397+
if (STI.hasFeature(LoongArch::FeatureRelax)) {
398+
const MCConstantExpr *Dummy = MCConstantExpr::create(0, Ctx);
399+
Fixups.push_back(MCFixup::create(
400+
0, Dummy, MCFixupKind(LoongArch::fixup_loongarch_relax), MI.getLoc()));
401+
}
402+
395403
// Emit a normal ADD instruction with the given operands.
396404
unsigned ADD = MI.getOpcode() == LoongArch::PseudoAddTPRel_D
397405
? LoongArch::ADD_D
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# RUN: llvm-mc --filetype=obj --triple=loongarch32 --mattr=+relax < %s \
2+
# RUN: | llvm-readobj -r - | FileCheck --check-prefix=LA32-RELAX-RELOC %s
3+
# RUN: llvm-mc --filetype=obj --triple=loongarch32 --mattr=-relax < %s \
4+
# RUN: | llvm-readobj -r - | FileCheck --check-prefix=LA32-NORELAX-RELOC %s
5+
# RUN: llvm-mc --triple=loongarch32 --mattr=+relax < %s --show-encoding \
6+
# RUN: | FileCheck --check-prefix=LA32-RELAX-FIXUP %s
7+
8+
# RUN: llvm-mc --filetype=obj --triple=loongarch64 --mattr=+relax --defsym=LA64=1 < %s \
9+
# RUN: | llvm-readobj -r - | FileCheck --check-prefix=LA64-RELAX-RELOC %s
10+
# RUN: llvm-mc --filetype=obj --triple=loongarch64 --mattr=-relax --defsym=LA64=1 < %s \
11+
# RUN: | llvm-readobj -r - | FileCheck --check-prefix=LA64-NORELAX-RELOC %s
12+
# RUN: llvm-mc --triple=loongarch64 --mattr=+relax --defsym=LA64=1 < %s --show-encoding \
13+
# RUN: | FileCheck --check-prefix=LA64-RELAX-FIXUP %s
14+
15+
.long foo
16+
17+
.ifndef LA64
18+
19+
lu12i.w $a0, %le_hi20_r(foo)
20+
# LA32-NORELAX-RELOC: R_LARCH_TLS_LE_HI20_R foo 0x0
21+
# LA32-NORELAX-RELOC-NOT: R_LARCH_RELAX - 0x0
22+
# LA32-RELAX-RELOC: R_LARCH_TLS_LE_HI20_R foo 0x0
23+
# LA32-RELAX-RELOC: R_LARCH_RELAX - 0x0
24+
# LA32-RELAX-FIXUP: fixup A - offset: 0, value: %le_hi20_r(foo), kind: FK_NONE
25+
# LA32-RELAX-FIXUP: fixup B - offset: 0, value: 0, kind: FK_NONE
26+
27+
add.w $a0, $a0, $tp, %le_add_r(foo)
28+
# LA32-NORELAX-RELOC: R_LARCH_TLS_LE_ADD_R foo 0x0
29+
# LA32-NORELAX-RELOC-NOT: R_LARCH_RELAX - 0x0
30+
# LA32-RELAX-RELOC: R_LARCH_TLS_LE_ADD_R foo 0x0
31+
# LA32-RELAX-RELOC: R_LARCH_RELAX - 0x0
32+
# LA32-RELAX-FIXUP: fixup A - offset: 0, value: %le_add_r(foo), kind: FK_NONE
33+
# LA32-RELAX-FIXUP: fixup B - offset: 0, value: 0, kind: FK_NONE
34+
35+
addi.w $a0, $a0, %le_lo12_r(foo)
36+
# LA32-NORELAX-RELOC: R_LARCH_TLS_LE_LO12_R foo 0x0
37+
# LA32-NORELAX-RELOC-NOT: R_LARCH_RELAX - 0x0
38+
# LA32-RELAX-RELOC: R_LARCH_TLS_LE_LO12_R foo 0x0
39+
# LA32-RELAX-RELOC: R_LARCH_RELAX - 0x0
40+
# LA32-RELAX-FIXUP: fixup A - offset: 0, value: %le_lo12_r(foo), kind: FK_NONE
41+
# LA32-RELAX-FIXUP: fixup B - offset: 0, value: 0, kind: FK_NONE
42+
43+
.else
44+
45+
lu12i.w $a0, %le_hi20_r(foo)
46+
# LA64-NORELAX-RELOC: R_LARCH_TLS_LE_HI20_R foo 0x0
47+
# LA64-NORELAX-RELOC-NOT: R_LARCH_RELAX - 0x0
48+
# LA64-RELAX-RELOC: R_LARCH_TLS_LE_HI20_R foo 0x0
49+
# LA64-RELAX-RELOC: R_LARCH_RELAX - 0x0
50+
# LA64-RELAX-FIXUP: fixup A - offset: 0, value: %le_hi20_r(foo), kind: FK_NONE
51+
# LA64-RELAX-FIXUP: fixup B - offset: 0, value: 0, kind: FK_NONE
52+
53+
add.d $a0, $a0, $tp, %le_add_r(foo)
54+
# LA64-NORELAX-RELOC: R_LARCH_TLS_LE_ADD_R foo 0x0
55+
# LA64-NORELAX-RELOC-NOT: R_LARCH_RELAX - 0x0
56+
# LA64-RELAX-RELOC: R_LARCH_TLS_LE_ADD_R foo 0x0
57+
# LA64-RELAX-RELOC: R_LARCH_RELAX - 0x0
58+
# LA64-RELAX-FIXUP: fixup A - offset: 0, value: %le_add_r(foo), kind: FK_NONE
59+
# LA64-RELAX-FIXUP: fixup B - offset: 0, value: 0, kind: FK_NONE
60+
61+
addi.d $a0, $a0, %le_lo12_r(foo)
62+
# LA64-NORELAX-RELOC: R_LARCH_TLS_LE_LO12_R foo 0x0
63+
# LA64-NORELAX-RELOC-NOT: R_LARCH_RELAX - 0x0
64+
# LA64-RELAX-RELOC: R_LARCH_TLS_LE_LO12_R foo 0x0
65+
# LA64-RELAX-RELOC: R_LARCH_RELAX - 0x0
66+
# LA64-RELAX-FIXUP: fixup A - offset: 0, value: %le_lo12_r(foo), kind: FK_NONE
67+
# LA64-RELAX-FIXUP: fixup B - offset: 0, value: 0, kind: FK_NONE
68+
69+
.endif
70+

0 commit comments

Comments
 (0)