Skip to content

Commit 1722a4b

Browse files
committed
[Backport][LoongArch] Add the support for relax feature on LoongArch
1 parent a75eeab commit 1722a4b

16 files changed

+3737
-0
lines changed

debian/changelog

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
llvm-toolchain-17 (1:17.0.6-6) unstable; urgency=medium
2+
3+
[ Jinyang He ]
4+
* Support the R_LARCH_{ADD,SUB}6 relocation type (#72190)
5+
* Add relax feature and keep relocations (#72191)
6+
* Allow delayed decision for ADD/SUB relocations (#72960)
7+
* Emit R_LARCH_RELAX when expanding some LoadAddress (#72961)
8+
* Add AlignFragment size if layout is available and not need
9+
insert nops (#76552)
10+
* Add relaxDwarfLineAddr and relaxDwarfCFA to handle the mutable
11+
label diff in dwarfinfo (#77728)
12+
* Insert nops and emit align reloc when handle alignment directive
13+
(#72962)
14+
* Support relax R_LARCH_ALIGN (#78692)
15+
* Support the R_LARCH_{ADD,SUB}_ULEB128 relocation types (#81133)
16+
17+
[ Job Noorman ]
18+
* Implement --emit-relocs with relaxation
19+
20+
[ Weining Lu ]
21+
* Update dwarf-loongarch-relocs.ll
22+
23+
[ Fangrui Song ]
24+
* Change ELF/uleb-ehtable.s Mach-O to use private symbols in .uleb128
25+
for label differences
26+
* AttemptToFoldSymbolOffsetDifference: revert isMicroMips special case
27+
28+
* Support R_LARCH_{ADD,SUB}_ULEB128/R_RISCV_{SET,SUB}_ULEB128 for
29+
.uleb128 directives
30+
31+
-- Ami-zhang <[email protected]> Thu, 22 Aug 2024 14:32:09 +0800
32+
133
llvm-toolchain-17 (1:17.0.6-5deepin1) unstable; urgency=medium
234

335
[ chenli ]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
From 7f14e7c1b116fc865ddebb78e67816bfc5216178 Mon Sep 17 00:00:00 2001
2+
From: Jinyang He <[email protected]>
3+
Date: Wed, 15 Nov 2023 09:57:45 +0800
4+
Subject: [PATCH 01/14] [lld][LoongArch] Support the R_LARCH_{ADD,SUB}6
5+
relocation type (#72190)
6+
7+
The R_LARCH_{ADD,SUB}6 relocation type are usually used by DwarfCFA to
8+
calculate a tiny offset. They appear after binutils 2.41, with GAS
9+
enabling relaxation by default.
10+
11+
(cherry picked from commit 72accbfd0a1023b3182202276904120524ff9200)
12+
Change-Id: Iad676e522f11c52e5dc381243f1df60edcef58f5
13+
---
14+
lld/ELF/Arch/LoongArch.cpp | 8 ++++++++
15+
lld/test/ELF/loongarch-add-sub.s | 6 +++++-
16+
2 files changed, 13 insertions(+), 1 deletion(-)
17+
18+
diff --git a/lld/ELF/Arch/LoongArch.cpp b/lld/ELF/Arch/LoongArch.cpp
19+
index 04ddb4682917..d3a538577a59 100644
20+
--- a/lld/ELF/Arch/LoongArch.cpp
21+
+++ b/lld/ELF/Arch/LoongArch.cpp
22+
@@ -444,10 +444,12 @@ RelExpr LoongArch::getRelExpr(const RelType type, const Symbol &s,
23+
case R_LARCH_TLS_LE64_LO20:
24+
case R_LARCH_TLS_LE64_HI12:
25+
return R_TPREL;
26+
+ case R_LARCH_ADD6:
27+
case R_LARCH_ADD8:
28+
case R_LARCH_ADD16:
29+
case R_LARCH_ADD32:
30+
case R_LARCH_ADD64:
31+
+ case R_LARCH_SUB6:
32+
case R_LARCH_SUB8:
33+
case R_LARCH_SUB16:
34+
case R_LARCH_SUB32:
35+
@@ -650,6 +652,9 @@ void LoongArch::relocate(uint8_t *loc, const Relocation &rel,
36+
write32le(loc, setK12(read32le(loc), extractBits(val, 63, 52)));
37+
return;
38+
39+
+ case R_LARCH_ADD6:
40+
+ *loc = (*loc & 0xc0) | ((*loc + val) & 0x3f);
41+
+ return;
42+
case R_LARCH_ADD8:
43+
*loc += val;
44+
return;
45+
@@ -662,6 +667,9 @@ void LoongArch::relocate(uint8_t *loc, const Relocation &rel,
46+
case R_LARCH_ADD64:
47+
write64le(loc, read64le(loc) + val);
48+
return;
49+
+ case R_LARCH_SUB6:
50+
+ *loc = (*loc & 0xc0) | ((*loc - val) & 0x3f);
51+
+ return;
52+
case R_LARCH_SUB8:
53+
*loc -= val;
54+
return;
55+
diff --git a/lld/test/ELF/loongarch-add-sub.s b/lld/test/ELF/loongarch-add-sub.s
56+
index 63a3f7de179e..35f8a053d69c 100644
57+
--- a/lld/test/ELF/loongarch-add-sub.s
58+
+++ b/lld/test/ELF/loongarch-add-sub.s
59+
@@ -6,7 +6,7 @@
60+
# RUN: llvm-readelf -x .rodata %t.la64 | FileCheck --check-prefix=CHECK %s
61+
# CHECK: section '.rodata':
62+
# CHECK-NEXT: 0x9876543210 10325476 98badcfe 804602be 79ffffff
63+
-# CHECK-NEXT: 0x9876543220 804602be 804680
64+
+# CHECK-NEXT: 0x9876543220 804602be 80468097
65+
66+
.text
67+
.global _start
68+
@@ -34,3 +34,7 @@ quux:
69+
.byte 0
70+
.reloc quux, R_LARCH_ADD8, 1b
71+
.reloc quux, R_LARCH_SUB8, 2b
72+
+qux:
73+
+ .byte 0b10000000
74+
+ .reloc qux, R_LARCH_ADD6, qux
75+
+ .reloc qux, R_LARCH_SUB6, 2b
76+
--
77+
2.20.1
78+
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
From 6f135b13769c64a6942b4b232a350b6a6207f2b2 Mon Sep 17 00:00:00 2001
2+
From: Jinyang He <[email protected]>
3+
Date: Thu, 16 Nov 2023 11:01:26 +0800
4+
Subject: [PATCH 02/14] [LoongArch] Add relax feature and keep relocations
5+
(#72191)
6+
7+
Add relax feature. To support linker relocation, we should make
8+
relocation with a symbol rather than section plus offset, and keep all
9+
relocations with non-abs symbol.
10+
11+
(cherry picked from commit f5bfc833fcbf17a5876911783d1adaca7028d20c)
12+
Change-Id: Ief38b480016175f2cc9939b74a84d9444559ffd6
13+
---
14+
llvm/lib/Target/LoongArch/LoongArch.td | 4 +++
15+
.../lib/Target/LoongArch/LoongArchSubtarget.h | 2 ++
16+
.../MCTargetDesc/LoongArchAsmBackend.cpp | 5 +--
17+
.../MCTargetDesc/LoongArchELFObjectWriter.cpp | 18 ++++++++---
18+
.../MCTargetDesc/LoongArchMCTargetDesc.h | 2 +-
19+
.../MC/LoongArch/Relocations/relax-attr.s | 32 +++++++++++++++++++
20+
6 files changed, 55 insertions(+), 8 deletions(-)
21+
create mode 100644 llvm/test/MC/LoongArch/Relocations/relax-attr.s
22+
23+
diff --git a/llvm/lib/Target/LoongArch/LoongArch.td b/llvm/lib/Target/LoongArch/LoongArch.td
24+
index 0675caa3b601..75b65fe69f26 100644
25+
--- a/llvm/lib/Target/LoongArch/LoongArch.td
26+
+++ b/llvm/lib/Target/LoongArch/LoongArch.td
27+
@@ -102,6 +102,10 @@ def FeatureUAL
28+
: SubtargetFeature<"ual", "HasUAL", "true",
29+
"Allow memory accesses to be unaligned">;
30+
31+
+def FeatureRelax
32+
+ : SubtargetFeature<"relax", "HasLinkerRelax", "true",
33+
+ "Enable Linker relaxation">;
34+
+
35+
//===----------------------------------------------------------------------===//
36+
// Registers, instruction descriptions ...
37+
//===----------------------------------------------------------------------===//
38+
diff --git a/llvm/lib/Target/LoongArch/LoongArchSubtarget.h b/llvm/lib/Target/LoongArch/LoongArchSubtarget.h
39+
index 0fbe23f2f62d..5c173675cca4 100644
40+
--- a/llvm/lib/Target/LoongArch/LoongArchSubtarget.h
41+
+++ b/llvm/lib/Target/LoongArch/LoongArchSubtarget.h
42+
@@ -43,6 +43,7 @@ class LoongArchSubtarget : public LoongArchGenSubtargetInfo {
43+
bool HasLaGlobalWithAbs = false;
44+
bool HasLaLocalWithAbs = false;
45+
bool HasUAL = false;
46+
+ bool HasLinkerRelax = false;
47+
unsigned GRLen = 32;
48+
MVT GRLenVT = MVT::i32;
49+
LoongArchABI::ABI TargetABI = LoongArchABI::ABI_Unknown;
50+
@@ -100,6 +101,7 @@ public:
51+
bool hasLaGlobalWithAbs() const { return HasLaGlobalWithAbs; }
52+
bool hasLaLocalWithAbs() const { return HasLaLocalWithAbs; }
53+
bool hasUAL() const { return HasUAL; }
54+
+ bool hasLinkerRelax() const { return HasLinkerRelax; }
55+
MVT getGRLenVT() const { return GRLenVT; }
56+
unsigned getGRLen() const { return GRLen; }
57+
LoongArchABI::ABI getTargetABI() const { return TargetABI; }
58+
diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
59+
index ecb68ff401e9..aae3e544d326 100644
60+
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
61+
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
62+
@@ -168,7 +168,7 @@ bool LoongArchAsmBackend::shouldForceRelocation(const MCAssembler &Asm,
63+
return true;
64+
switch (Fixup.getTargetKind()) {
65+
default:
66+
- return false;
67+
+ return STI.hasFeature(LoongArch::FeatureRelax);
68+
case FK_Data_1:
69+
case FK_Data_2:
70+
case FK_Data_4:
71+
@@ -193,7 +193,8 @@ bool LoongArchAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
72+
73+
std::unique_ptr<MCObjectTargetWriter>
74+
LoongArchAsmBackend::createObjectTargetWriter() const {
75+
- return createLoongArchELFObjectWriter(OSABI, Is64Bit);
76+
+ return createLoongArchELFObjectWriter(
77+
+ OSABI, Is64Bit, STI.hasFeature(LoongArch::FeatureRelax));
78+
}
79+
80+
MCAsmBackend *llvm::createLoongArchAsmBackend(const Target &T,
81+
diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchELFObjectWriter.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchELFObjectWriter.cpp
82+
index a6b9c0652639..e60b9c2cfd97 100644
83+
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchELFObjectWriter.cpp
84+
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchELFObjectWriter.cpp
85+
@@ -20,19 +20,27 @@ using namespace llvm;
86+
namespace {
87+
class LoongArchELFObjectWriter : public MCELFObjectTargetWriter {
88+
public:
89+
- LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit);
90+
+ LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit, bool EnableRelax);
91+
92+
~LoongArchELFObjectWriter() override;
93+
94+
+ bool needsRelocateWithSymbol(const MCSymbol &Sym,
95+
+ unsigned Type) const override {
96+
+ return EnableRelax;
97+
+ }
98+
+
99+
protected:
100+
unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
101+
const MCFixup &Fixup, bool IsPCRel) const override;
102+
+ bool EnableRelax;
103+
};
104+
} // end namespace
105+
106+
-LoongArchELFObjectWriter::LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit)
107+
+LoongArchELFObjectWriter::LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit,
108+
+ bool EnableRelax)
109+
: MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_LOONGARCH,
110+
- /*HasRelocationAddend*/ true) {}
111+
+ /*HasRelocationAddend=*/true),
112+
+ EnableRelax(EnableRelax) {}
113+
114+
LoongArchELFObjectWriter::~LoongArchELFObjectWriter() {}
115+
116+
@@ -87,6 +95,6 @@ unsigned LoongArchELFObjectWriter::getRelocType(MCContext &Ctx,
117+
}
118+
119+
std::unique_ptr<MCObjectTargetWriter>
120+
-llvm::createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit) {
121+
- return std::make_unique<LoongArchELFObjectWriter>(OSABI, Is64Bit);
122+
+llvm::createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit, bool Relax) {
123+
+ return std::make_unique<LoongArchELFObjectWriter>(OSABI, Is64Bit, Relax);
124+
}
125+
diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCTargetDesc.h b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCTargetDesc.h
126+
index ab35a0096c8a..bb05baa9b717 100644
127+
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCTargetDesc.h
128+
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCTargetDesc.h
129+
@@ -36,7 +36,7 @@ MCAsmBackend *createLoongArchAsmBackend(const Target &T,
130+
const MCTargetOptions &Options);
131+
132+
std::unique_ptr<MCObjectTargetWriter>
133+
-createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit);
134+
+createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit, bool Relax);
135+
136+
} // end namespace llvm
137+
138+
diff --git a/llvm/test/MC/LoongArch/Relocations/relax-attr.s b/llvm/test/MC/LoongArch/Relocations/relax-attr.s
139+
new file mode 100644
140+
index 000000000000..b1e648d850bb
141+
--- /dev/null
142+
+++ b/llvm/test/MC/LoongArch/Relocations/relax-attr.s
143+
@@ -0,0 +1,32 @@
144+
+# RUN: llvm-mc --filetype=obj --triple=loongarch64 %s -o %t
145+
+# RUN: llvm-readobj -r %t | FileCheck %s
146+
+# RUN: llvm-mc --filetype=obj --triple=loongarch64 -mattr=+relax %s -o %t
147+
+# RUN: llvm-readobj -r %t | FileCheck %s --check-prefix=CHECKR
148+
+
149+
+# CHECK: Relocations [
150+
+# CHECK-NEXT: Section ({{.*}}) .rela.data {
151+
+# CHECK-NEXT: 0x0 R_LARCH_64 .text 0x4
152+
+# CHECK-NEXT: }
153+
+# CHECK-NEXT: ]
154+
+
155+
+# CHECKR: Relocations [
156+
+# CHECKR-NEXT: Section ({{.*}}) .rela.text {
157+
+# CHECKR-NEXT: 0x8 R_LARCH_B21 .L1 0x0
158+
+# CHECKR-NEXT: 0xC R_LARCH_B16 .L1 0x0
159+
+# CHECKR-NEXT: 0x10 R_LARCH_B26 .L1 0x0
160+
+# CHECKR-NEXT: }
161+
+# CHECKR-NEXT: Section ({{.*}}) .rela.data {
162+
+# CHECKR-NEXT: 0x0 R_LARCH_64 .L1 0x0
163+
+# CHECKR-NEXT: }
164+
+# CHECKR-NEXT: ]
165+
+
166+
+.text
167+
+ nop
168+
+.L1:
169+
+ nop
170+
+ beqz $a0, .L1
171+
+ blt $a0, $a1, .L1
172+
+ b .L1
173+
+
174+
+.data
175+
+.dword .L1
176+
--
177+
2.20.1
178+

0 commit comments

Comments
 (0)