Skip to content

Commit f5bfc83

Browse files
authored
[LoongArch] Add relax feature and keep relocations (#72191)
Add relax feature. To support linker relocation, we should make relocation with a symbol rather than section plus offset, and keep all relocations with non-abs symbol.
1 parent a53f731 commit f5bfc83

File tree

6 files changed

+55
-8
lines changed

6 files changed

+55
-8
lines changed

llvm/lib/Target/LoongArch/LoongArch.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ def FeatureUAL
102102
: SubtargetFeature<"ual", "HasUAL", "true",
103103
"Allow memory accesses to be unaligned">;
104104

105+
def FeatureRelax
106+
: SubtargetFeature<"relax", "HasLinkerRelax", "true",
107+
"Enable Linker relaxation">;
108+
105109
//===----------------------------------------------------------------------===//
106110
// Registers, instruction descriptions ...
107111
//===----------------------------------------------------------------------===//

llvm/lib/Target/LoongArch/LoongArchSubtarget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class LoongArchSubtarget : public LoongArchGenSubtargetInfo {
4343
bool HasLaGlobalWithAbs = false;
4444
bool HasLaLocalWithAbs = false;
4545
bool HasUAL = false;
46+
bool HasLinkerRelax = false;
4647
unsigned GRLen = 32;
4748
MVT GRLenVT = MVT::i32;
4849
LoongArchABI::ABI TargetABI = LoongArchABI::ABI_Unknown;
@@ -100,6 +101,7 @@ class LoongArchSubtarget : public LoongArchGenSubtargetInfo {
100101
bool hasLaGlobalWithAbs() const { return HasLaGlobalWithAbs; }
101102
bool hasLaLocalWithAbs() const { return HasLaLocalWithAbs; }
102103
bool hasUAL() const { return HasUAL; }
104+
bool hasLinkerRelax() const { return HasLinkerRelax; }
103105
MVT getGRLenVT() const { return GRLenVT; }
104106
unsigned getGRLen() const { return GRLen; }
105107
LoongArchABI::ABI getTargetABI() const { return TargetABI; }

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ bool LoongArchAsmBackend::shouldForceRelocation(const MCAssembler &Asm,
167167
return true;
168168
switch (Fixup.getTargetKind()) {
169169
default:
170-
return false;
170+
return STI.hasFeature(LoongArch::FeatureRelax);
171171
case FK_Data_1:
172172
case FK_Data_2:
173173
case FK_Data_4:
@@ -192,7 +192,8 @@ bool LoongArchAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
192192

193193
std::unique_ptr<MCObjectTargetWriter>
194194
LoongArchAsmBackend::createObjectTargetWriter() const {
195-
return createLoongArchELFObjectWriter(OSABI, Is64Bit);
195+
return createLoongArchELFObjectWriter(
196+
OSABI, Is64Bit, STI.hasFeature(LoongArch::FeatureRelax));
196197
}
197198

198199
MCAsmBackend *llvm::createLoongArchAsmBackend(const Target &T,

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,27 @@ using namespace llvm;
2020
namespace {
2121
class LoongArchELFObjectWriter : public MCELFObjectTargetWriter {
2222
public:
23-
LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit);
23+
LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit, bool EnableRelax);
2424

2525
~LoongArchELFObjectWriter() override;
2626

27+
bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
28+
unsigned Type) const override {
29+
return EnableRelax;
30+
}
31+
2732
protected:
2833
unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
2934
const MCFixup &Fixup, bool IsPCRel) const override;
35+
bool EnableRelax;
3036
};
3137
} // end namespace
3238

33-
LoongArchELFObjectWriter::LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit)
39+
LoongArchELFObjectWriter::LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit,
40+
bool EnableRelax)
3441
: MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_LOONGARCH,
35-
/*HasRelocationAddend*/ true) {}
42+
/*HasRelocationAddend=*/true),
43+
EnableRelax(EnableRelax) {}
3644

3745
LoongArchELFObjectWriter::~LoongArchELFObjectWriter() {}
3846

@@ -87,6 +95,6 @@ unsigned LoongArchELFObjectWriter::getRelocType(MCContext &Ctx,
8795
}
8896

8997
std::unique_ptr<MCObjectTargetWriter>
90-
llvm::createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit) {
91-
return std::make_unique<LoongArchELFObjectWriter>(OSABI, Is64Bit);
98+
llvm::createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit, bool Relax) {
99+
return std::make_unique<LoongArchELFObjectWriter>(OSABI, Is64Bit, Relax);
92100
}

llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCTargetDesc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ MCAsmBackend *createLoongArchAsmBackend(const Target &T,
3636
const MCTargetOptions &Options);
3737

3838
std::unique_ptr<MCObjectTargetWriter>
39-
createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit);
39+
createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit, bool Relax);
4040

4141
} // end namespace llvm
4242

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# RUN: llvm-mc --filetype=obj --triple=loongarch64 %s -o %t
2+
# RUN: llvm-readobj -r %t | FileCheck %s
3+
# RUN: llvm-mc --filetype=obj --triple=loongarch64 -mattr=+relax %s -o %t
4+
# RUN: llvm-readobj -r %t | FileCheck %s --check-prefix=CHECKR
5+
6+
# CHECK: Relocations [
7+
# CHECK-NEXT: Section ({{.*}}) .rela.data {
8+
# CHECK-NEXT: 0x0 R_LARCH_64 .text 0x4
9+
# CHECK-NEXT: }
10+
# CHECK-NEXT: ]
11+
12+
# CHECKR: Relocations [
13+
# CHECKR-NEXT: Section ({{.*}}) .rela.text {
14+
# CHECKR-NEXT: 0x8 R_LARCH_B21 .L1 0x0
15+
# CHECKR-NEXT: 0xC R_LARCH_B16 .L1 0x0
16+
# CHECKR-NEXT: 0x10 R_LARCH_B26 .L1 0x0
17+
# CHECKR-NEXT: }
18+
# CHECKR-NEXT: Section ({{.*}}) .rela.data {
19+
# CHECKR-NEXT: 0x0 R_LARCH_64 .L1 0x0
20+
# CHECKR-NEXT: }
21+
# CHECKR-NEXT: ]
22+
23+
.text
24+
nop
25+
.L1:
26+
nop
27+
beqz $a0, .L1
28+
blt $a0, $a1, .L1
29+
b .L1
30+
31+
.data
32+
.dword .L1

0 commit comments

Comments
 (0)