Skip to content

Commit 89e3a64

Browse files
authored
[LoongArch] Emit R_LARCH_RELAX when expanding some macros (#120067)
Emit `R_LARCH_RELAX` relocations when expanding some macros, including: - `la.tls.ie`, `la.tls.ld`, `la.tls.gd`, `la.tls.desc`, - `call36`, `tail36`. Other macros that need to emit `R_LARCH_RELAX` relocations was implemented in llvm/llvm-project#72961, including: - `la.local`, `la.pcrel`, `la.pcrel` expanded as `la.abs`, `la`, `la.global`, `la/la.global` expanded as `la.pcrel`, `la.got`. Note: `la.tls.le` macro can be relaxed when expanded with `R_LARCH_TLS_LE_{HI20/ADD/LO12}_R` relocations. But if we do so, previously handwritten assembly code will occur error due to the redundant `add.{w/d}` followed by `la.tls.le`. So `la.tls.le` keeps to expands with `R_LARCH_TLS_LE_{HI20/LO12}`.
1 parent 7347870 commit 89e3a64

File tree

4 files changed

+104
-8
lines changed

4 files changed

+104
-8
lines changed

llvm/lib/Target/LoongArch/AsmParser/LoongArchAsmParser.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,8 @@ void LoongArchAsmParser::emitLoadAddressPcrel(MCInst &Inst, SMLoc IDLoc,
10091009
Insts.push_back(
10101010
LoongArchAsmParser::Inst(ADDI, LoongArchMCExpr::VK_LoongArch_PCALA_LO12));
10111011

1012-
emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out, true);
1012+
emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out,
1013+
/*RelaxHint=*/true);
10131014
}
10141015

10151016
void LoongArchAsmParser::emitLoadAddressPcrelLarge(MCInst &Inst, SMLoc IDLoc,
@@ -1083,7 +1084,8 @@ void LoongArchAsmParser::emitLoadAddressGot(MCInst &Inst, SMLoc IDLoc,
10831084
Insts.push_back(
10841085
LoongArchAsmParser::Inst(LD, LoongArchMCExpr::VK_LoongArch_GOT_PC_LO12));
10851086

1086-
emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out, true);
1087+
emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out,
1088+
/*RelaxHint=*/true);
10871089
}
10881090

10891091
void LoongArchAsmParser::emitLoadAddressGotLarge(MCInst &Inst, SMLoc IDLoc,
@@ -1176,7 +1178,8 @@ void LoongArchAsmParser::emitLoadAddressTLSIE(MCInst &Inst, SMLoc IDLoc,
11761178
Insts.push_back(LoongArchAsmParser::Inst(
11771179
LD, LoongArchMCExpr::VK_LoongArch_TLS_IE_PC_LO12));
11781180

1179-
emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out);
1181+
emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out,
1182+
/*RelaxHint=*/true);
11801183
}
11811184

11821185
void LoongArchAsmParser::emitLoadAddressTLSIELarge(MCInst &Inst, SMLoc IDLoc,
@@ -1248,7 +1251,8 @@ void LoongArchAsmParser::emitLoadAddressTLSLD(MCInst &Inst, SMLoc IDLoc,
12481251
Insts.push_back(LoongArchAsmParser::Inst(
12491252
ADDI, LoongArchMCExpr::VK_LoongArch_GOT_PC_LO12));
12501253

1251-
emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out);
1254+
emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out,
1255+
/*RelaxHint=*/true);
12521256
}
12531257

12541258
void LoongArchAsmParser::emitLoadAddressTLSLDLarge(MCInst &Inst, SMLoc IDLoc,
@@ -1320,7 +1324,8 @@ void LoongArchAsmParser::emitLoadAddressTLSGD(MCInst &Inst, SMLoc IDLoc,
13201324
Insts.push_back(LoongArchAsmParser::Inst(
13211325
ADDI, LoongArchMCExpr::VK_LoongArch_GOT_PC_LO12));
13221326

1323-
emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out);
1327+
emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out,
1328+
/*RelaxHint=*/true);
13241329
}
13251330

13261331
void LoongArchAsmParser::emitLoadAddressTLSGDLarge(MCInst &Inst, SMLoc IDLoc,
@@ -1409,7 +1414,8 @@ void LoongArchAsmParser::emitLoadAddressTLSDesc(MCInst &Inst, SMLoc IDLoc,
14091414
Insts.push_back(LoongArchAsmParser::Inst(
14101415
LoongArch::JIRL, LoongArchMCExpr::VK_LoongArch_TLS_DESC_CALL));
14111416

1412-
emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out);
1417+
emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out,
1418+
/*RelaxHint=*/true);
14131419
}
14141420

14151421
void LoongArchAsmParser::emitLoadAddressTLSDescLarge(MCInst &Inst, SMLoc IDLoc,
@@ -1500,8 +1506,9 @@ void LoongArchAsmParser::emitFuncCall36(MCInst &Inst, SMLoc IDLoc,
15001506
IsTailCall ? Inst.getOperand(0).getReg() : MCRegister(LoongArch::R1);
15011507
const MCExpr *Sym =
15021508
IsTailCall ? Inst.getOperand(1).getExpr() : Inst.getOperand(0).getExpr();
1503-
const LoongArchMCExpr *LE = LoongArchMCExpr::create(
1504-
Sym, llvm::LoongArchMCExpr::VK_LoongArch_CALL36, getContext());
1509+
const LoongArchMCExpr *LE =
1510+
LoongArchMCExpr::create(Sym, llvm::LoongArchMCExpr::VK_LoongArch_CALL36,
1511+
getContext(), /*RelaxHint=*/true);
15051512

15061513
Out.emitInstruction(
15071514
MCInstBuilder(LoongArch::PCADDU18I).addReg(ScratchReg).addExpr(LE),

llvm/test/MC/LoongArch/Macros/aliases-la.s

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,26 @@
33

44
# RUN: llvm-mc --triple=loongarch64 %s \
55
# RUN: | FileCheck %s --check-prefix=NORMAL
6+
# RUN: llvm-mc --filetype=obj --triple=loongarch64 --mattr=-relax %s -o %t
7+
# RUN: llvm-readobj -r %t | FileCheck %s --check-prefix=RELOC
8+
# RUN: llvm-mc --filetype=obj --triple=loongarch64 --mattr=+relax %s -o %t.relax
9+
# RUN: llvm-readobj -r %t.relax | FileCheck %s --check-prefixes=RELOC,RELAX
610
# RUN: llvm-mc --triple=loongarch64 --mattr=+la-global-with-pcrel < %s \
711
# RUN: | FileCheck %s --check-prefix=GTOPCR
12+
# RUN: llvm-mc --filetype=obj --triple=loongarch64 --mattr=+la-global-with-pcrel \
13+
# RUN: --mattr=-relax %s -o %t
14+
# RUN: llvm-readobj -r %t | FileCheck %s --check-prefix=GTOPCR-RELOC
15+
# RUN: llvm-mc --filetype=obj --triple=loongarch64 --mattr=+la-global-with-pcrel \
16+
# RUN: --mattr=+relax %s -o %t.relax
17+
# RUN: llvm-readobj -r %t.relax | FileCheck %s --check-prefixes=GTOPCR-RELOC,GTOPCR-RELAX
818
# RUN: llvm-mc --triple=loongarch64 --mattr=+la-global-with-abs < %s \
919
# RUN: | FileCheck %s --check-prefix=GTOABS
1020
# RUN: llvm-mc --triple=loongarch64 --mattr=+la-local-with-abs < %s \
1121
# RUN: | FileCheck %s --check-prefix=LTOABS
1222

23+
# RELOC: Relocations [
24+
# RELOC-NEXT: Section ({{.*}}) .rela.text {
25+
1326
la $a0, sym
1427
# NORMAL: pcalau12i $a0, %got_pc_hi20(sym)
1528
# NORMAL-NEXT: ld.d $a0, $a0, %got_pc_lo12(sym)
@@ -22,6 +35,16 @@ la $a0, sym
2235
# GTOABS-NEXT: lu32i.d $a0, %abs64_lo20(sym)
2336
# GTOABS-NEXT: lu52i.d $a0, $a0, %abs64_hi12(sym)
2437

38+
# RELOC-NEXT: R_LARCH_GOT_PC_HI20 sym 0x0
39+
# RELAX-NEXT: R_LARCH_RELAX - 0x0
40+
# RELOC-NEXT: R_LARCH_GOT_PC_LO12 sym 0x0
41+
# RELAX-NEXT: R_LARCH_RELAX - 0x0
42+
43+
# GTOPCR-RELOC: R_LARCH_PCALA_HI20 sym 0x0
44+
# GTOPCR-RELAX: R_LARCH_RELAX - 0x0
45+
# GTOPCR-RELOC-NEXT: R_LARCH_PCALA_LO12 sym 0x0
46+
# GTOPCR-RELAX-NEXT: R_LARCH_RELAX - 0x0
47+
2548
la.global $a0, sym_global
2649
# NORMAL: pcalau12i $a0, %got_pc_hi20(sym_global)
2750
# NORMAL-NEXT: ld.d $a0, $a0, %got_pc_lo12(sym_global)
@@ -34,6 +57,16 @@ la.global $a0, sym_global
3457
# GTOABS-NEXT: lu32i.d $a0, %abs64_lo20(sym_global)
3558
# GTOABS-NEXT: lu52i.d $a0, $a0, %abs64_hi12(sym_global)
3659

60+
# RELOC-NEXT: R_LARCH_GOT_PC_HI20 sym_global 0x0
61+
# RELAX-NEXT: R_LARCH_RELAX - 0x0
62+
# RELOC-NEXT: R_LARCH_GOT_PC_LO12 sym_global 0x0
63+
# RELAX-NEXT: R_LARCH_RELAX - 0x0
64+
65+
# GTOPCR-RELOC-NEXT: R_LARCH_PCALA_HI20 sym_global 0x0
66+
# GTOPCR-RELAX-NEXT: R_LARCH_RELAX - 0x0
67+
# GTOPCR-RELOC-NEXT: R_LARCH_PCALA_LO12 sym_global 0x0
68+
# GTOPCR-RELAX-NEXT: R_LARCH_RELAX - 0x0
69+
3770
la.global $a0, $a1, sym_global_large
3871
# NORMAL: pcalau12i $a0, %got_pc_hi20(sym_global_large)
3972
# NORMAL-NEXT: addi.d $a1, $zero, %got_pc_lo12(sym_global_large)
@@ -52,6 +85,11 @@ la.global $a0, $a1, sym_global_large
5285
# GTOABS-NEXT: lu32i.d $a0, %abs64_lo20(sym_global_large)
5386
# GTOABS-NEXT: lu52i.d $a0, $a0, %abs64_hi12(sym_global_large)
5487

88+
# RELOC-NEXT: R_LARCH_GOT_PC_HI20 sym_global_large 0x0
89+
# RELOC-NEXT: R_LARCH_GOT_PC_LO12 sym_global_large 0x0
90+
# RELOC-NEXT: R_LARCH_GOT64_PC_LO20 sym_global_large 0x0
91+
# RELOC-NEXT: R_LARCH_GOT64_PC_HI12 sym_global_large 0x0
92+
5593
la.local $a0, sym_local
5694
# NORMAL: pcalau12i $a0, %pc_hi20(sym_local)
5795
# NORMAL-NEXT: addi.d $a0, $a0, %pc_lo12(sym_local)
@@ -61,6 +99,11 @@ la.local $a0, sym_local
6199
# LTOABS-NEXT: lu32i.d $a0, %abs64_lo20(sym_local)
62100
# LTOABS-NEXT: lu52i.d $a0, $a0, %abs64_hi12(sym_local)
63101

102+
# RELOC-NEXT: R_LARCH_PCALA_HI20 sym_local 0x0
103+
# RELAX-NEXT: R_LARCH_RELAX - 0x0
104+
# RELOC-NEXT: R_LARCH_PCALA_LO12 sym_local 0x0
105+
# RELAX-NEXT: R_LARCH_RELAX - 0x0
106+
64107
la.local $a0, $a1, sym_local_large
65108
# NORMAL: pcalau12i $a0, %pc_hi20(sym_local_large)
66109
# NORMAL-NEXT: addi.d $a1, $zero, %pc_lo12(sym_local_large)
@@ -72,3 +115,12 @@ la.local $a0, $a1, sym_local_large
72115
# LTOABS-NEXT: ori $a0, $a0, %abs_lo12(sym_local_large)
73116
# LTOABS-NEXT: lu32i.d $a0, %abs64_lo20(sym_local_large)
74117
# LTOABS-NEXT: lu52i.d $a0, $a0, %abs64_hi12(sym_local_large)
118+
119+
# RELOC-NEXT: R_LARCH_PCALA_HI20 sym_local_large 0x0
120+
# RELOC-NEXT: R_LARCH_PCALA_LO12 sym_local_large 0x0
121+
# RELOC-NEXT: R_LARCH_PCALA64_LO20 sym_local_large 0x0
122+
# RELOC-NEXT: R_LARCH_PCALA64_HI12 sym_local_large 0x0
123+
124+
125+
# RELOC-NEXT: }
126+
# RELOC-NEXT: ]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
# RUN: llvm-mc --triple=loongarch64 %s | FileCheck %s
2+
# RUN: llvm-mc --filetype=obj --triple=loongarch64 --mattr=-relax %s -o %t
3+
# RUN: llvm-readobj -r %t | FileCheck %s --check-prefix=RELOC
4+
# RUN: llvm-mc --filetype=obj --triple=loongarch64 --mattr=+relax %s -o %t.relax
5+
# RUN: llvm-readobj -r %t.relax | FileCheck %s --check-prefixes=RELOC,RELAX
6+
7+
# RELOC: Relocations [
8+
# RELOC-NEXT: Section ({{.*}}) .rela.text {
29

310
call36 sym_call
411
# CHECK: pcaddu18i $ra, %call36(sym_call)
512
# CHECK-NEXT: jirl $ra, $ra, 0
613

14+
# RELOC-NEXT: R_LARCH_CALL36 sym_call 0x0
15+
# RELAX-NEXT: R_LARCH_RELAX - 0x0
16+
717
tail36 $t0, sym_tail
818
# CHECK: pcaddu18i $t0, %call36(sym_tail)
919
# CHECK-NEXT: jr $t0
20+
21+
# RELOC-NEXT: R_LARCH_CALL36 sym_tail 0x0
22+
# RELAX-NEXT: R_LARCH_RELAX - 0x0
23+
24+
25+
# RELOC-NEXT: }
26+
# RELOC-NEXT: ]

llvm/test/MC/LoongArch/Macros/macros-la.s

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
# RUN: llvm-readobj -r %t.relax | FileCheck %s --check-prefixes=RELOC,RELAX
66
# RUN: llvm-mc --triple=loongarch64 --mattr=+la-global-with-abs \
77
# RUN: %s | FileCheck %s --check-prefix=ABS
8+
# RUN: llvm-mc --filetype=obj --triple=loongarch64 --mattr=+la-global-with-abs \
9+
# RUN: --mattr=-relax %s -o %t
10+
# RUN: llvm-readobj -r %t | FileCheck %s --check-prefix=GTOABS-RELOC
11+
# RUN: llvm-mc --filetype=obj --triple=loongarch64 --mattr=+la-global-with-abs \
12+
# RUN: --mattr=+relax %s -o %t.relax
13+
# RUN: llvm-readobj -r %t.relax | FileCheck %s --check-prefixes=GTOABS-RELOC,GTOABS-RELAX
814

915
# RELOC: Relocations [
1016
# RELOC-NEXT: Section ({{.*}}) .rela.text {
@@ -36,6 +42,10 @@ la.pcrel $a0, sym_pcrel
3642
# RELAX-NEXT: R_LARCH_RELAX - 0x0
3743
# RELOC-NEXT: R_LARCH_PCALA_LO12 sym_pcrel 0x0
3844
# RELAX-NEXT: R_LARCH_RELAX - 0x0
45+
# GTOABS-RELOC: R_LARCH_PCALA_HI20 sym_pcrel 0x0
46+
# GTOABS-RELAX-NEXT: R_LARCH_RELAX - 0x0
47+
# GTOABS-RELOC-NEXT: R_LARCH_PCALA_LO12 sym_pcrel 0x0
48+
# GTOABS-RELAX-NEXT: R_LARCH_RELAX - 0x0
3949

4050
la.got $a0, sym_got
4151
# CHECK-NEXT: pcalau12i $a0, %got_pc_hi20(sym_got)
@@ -73,7 +83,9 @@ la.tls.ie $a0, sym_ie
7383
# ABS-NEXT: ld.d $a0, $a0, 0
7484
# ABS-EMPTY:
7585
# RELOC-NEXT: R_LARCH_TLS_IE_PC_HI20 sym_ie 0x0
86+
# RELAX-NEXT: R_LARCH_RELAX - 0x0
7687
# RELOC-NEXT: R_LARCH_TLS_IE_PC_LO12 sym_ie 0x0
88+
# RELAX-NEXT: R_LARCH_RELAX - 0x0
7789

7890
la.tls.ld $a0, sym_ld
7991
# CHECK-NEXT: pcalau12i $a0, %ld_pc_hi20(sym_ld)
@@ -85,7 +97,9 @@ la.tls.ld $a0, sym_ld
8597
# ABS-NEXT: lu52i.d $a0, $a0, %got64_hi12(sym_ld)
8698
# ABS-EMPTY:
8799
# RELOC-NEXT: R_LARCH_TLS_LD_PC_HI20 sym_ld 0x0
100+
# RELAX-NEXT: R_LARCH_RELAX - 0x0
88101
# RELOC-NEXT: R_LARCH_GOT_PC_LO12 sym_ld 0x0
102+
# RELAX-NEXT: R_LARCH_RELAX - 0x0
89103

90104
la.tls.gd $a0, sym_gd
91105
# CHECK-NEXT: pcalau12i $a0, %gd_pc_hi20(sym_gd)
@@ -97,7 +111,9 @@ la.tls.gd $a0, sym_gd
97111
# ABS-NEXT: lu52i.d $a0, $a0, %got64_hi12(sym_gd)
98112
# ABS-EMPTY:
99113
# RELOC-NEXT: R_LARCH_TLS_GD_PC_HI20 sym_gd 0x0
114+
# RELAX-NEXT: R_LARCH_RELAX - 0x0
100115
# RELOC-NEXT: R_LARCH_GOT_PC_LO12 sym_gd 0x0
116+
# RELAX-NEXT: R_LARCH_RELAX - 0x0
101117

102118
la.tls.desc $a0, sym_desc
103119
# CHECK-NEXT: pcalau12i $a0, %desc_pc_hi20(sym_desc)
@@ -113,9 +129,13 @@ la.tls.desc $a0, sym_desc
113129
# ABS-NEXT: jirl $ra, $ra, %desc_call(sym_desc)
114130
# ABS-EMPTY:
115131
# RELOC-NEXT: R_LARCH_TLS_DESC_PC_HI20 sym_desc 0x0
132+
# RELAX-NEXT: R_LARCH_RELAX - 0x0
116133
# RELOC-NEXT: R_LARCH_TLS_DESC_PC_LO12 sym_desc 0x0
134+
# RELAX-NEXT: R_LARCH_RELAX - 0x0
117135
# RELOC-NEXT: R_LARCH_TLS_DESC_LD sym_desc 0x0
136+
# RELAX-NEXT: R_LARCH_RELAX - 0x0
118137
# RELOC-NEXT: R_LARCH_TLS_DESC_CALL sym_desc 0x0
138+
# RELAX-NEXT: R_LARCH_RELAX - 0x0
119139

120140
#############################################################
121141
## with a temporary register.

0 commit comments

Comments
 (0)