|
| 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