Skip to content

[LoongArch] Add relax feature and keep relocations #72191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions llvm/lib/Target/LoongArch/LoongArch.td
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ def FeatureUAL
: SubtargetFeature<"ual", "HasUAL", "true",
"Allow memory accesses to be unaligned">;

def FeatureRelax
: SubtargetFeature<"relax", "HasLinkerRelax", "true",
"Enable Linker relaxation">;

//===----------------------------------------------------------------------===//
// Registers, instruction descriptions ...
//===----------------------------------------------------------------------===//
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/LoongArch/LoongArchSubtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class LoongArchSubtarget : public LoongArchGenSubtargetInfo {
bool HasLaGlobalWithAbs = false;
bool HasLaLocalWithAbs = false;
bool HasUAL = false;
bool HasLinkerRelax = false;
unsigned GRLen = 32;
MVT GRLenVT = MVT::i32;
LoongArchABI::ABI TargetABI = LoongArchABI::ABI_Unknown;
Expand Down Expand Up @@ -100,6 +101,7 @@ class LoongArchSubtarget : public LoongArchGenSubtargetInfo {
bool hasLaGlobalWithAbs() const { return HasLaGlobalWithAbs; }
bool hasLaLocalWithAbs() const { return HasLaLocalWithAbs; }
bool hasUAL() const { return HasUAL; }
bool hasLinkerRelax() const { return HasLinkerRelax; }
MVT getGRLenVT() const { return GRLenVT; }
unsigned getGRLen() const { return GRLen; }
LoongArchABI::ABI getTargetABI() const { return TargetABI; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ bool LoongArchAsmBackend::shouldForceRelocation(const MCAssembler &Asm,
return true;
switch (Fixup.getTargetKind()) {
default:
return false;
return STI.hasFeature(LoongArch::FeatureRelax);
case FK_Data_1:
case FK_Data_2:
case FK_Data_4:
Expand All @@ -192,7 +192,8 @@ bool LoongArchAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,

std::unique_ptr<MCObjectTargetWriter>
LoongArchAsmBackend::createObjectTargetWriter() const {
return createLoongArchELFObjectWriter(OSABI, Is64Bit);
return createLoongArchELFObjectWriter(
OSABI, Is64Bit, STI.hasFeature(LoongArch::FeatureRelax));
}

MCAsmBackend *llvm::createLoongArchAsmBackend(const Target &T,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,27 @@ using namespace llvm;
namespace {
class LoongArchELFObjectWriter : public MCELFObjectTargetWriter {
public:
LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit);
LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit, bool EnableRelax);

~LoongArchELFObjectWriter() override;

bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
unsigned Type) const override {
return EnableRelax;
}

protected:
unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
const MCFixup &Fixup, bool IsPCRel) const override;
bool EnableRelax;
};
} // end namespace

LoongArchELFObjectWriter::LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit)
LoongArchELFObjectWriter::LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit,
bool EnableRelax)
: MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_LOONGARCH,
/*HasRelocationAddend*/ true) {}
/*HasRelocationAddend=*/true),
EnableRelax(EnableRelax) {}

LoongArchELFObjectWriter::~LoongArchELFObjectWriter() {}

Expand Down Expand Up @@ -87,6 +95,6 @@ unsigned LoongArchELFObjectWriter::getRelocType(MCContext &Ctx,
}

std::unique_ptr<MCObjectTargetWriter>
llvm::createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit) {
return std::make_unique<LoongArchELFObjectWriter>(OSABI, Is64Bit);
llvm::createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit, bool Relax) {
return std::make_unique<LoongArchELFObjectWriter>(OSABI, Is64Bit, Relax);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ MCAsmBackend *createLoongArchAsmBackend(const Target &T,
const MCTargetOptions &Options);

std::unique_ptr<MCObjectTargetWriter>
createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit);
createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit, bool Relax);

} // end namespace llvm

Expand Down
32 changes: 32 additions & 0 deletions llvm/test/MC/LoongArch/Relocations/relax-attr.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# RUN: llvm-mc --filetype=obj --triple=loongarch64 %s -o %t
# RUN: llvm-readobj -r %t | FileCheck %s
# RUN: llvm-mc --filetype=obj --triple=loongarch64 -mattr=+relax %s -o %t
# RUN: llvm-readobj -r %t | FileCheck %s --check-prefix=CHECKR

# CHECK: Relocations [
# CHECK-NEXT: Section ({{.*}}) .rela.data {
# CHECK-NEXT: 0x0 R_LARCH_64 .text 0x4
# CHECK-NEXT: }
# CHECK-NEXT: ]

# CHECKR: Relocations [
# CHECKR-NEXT: Section ({{.*}}) .rela.text {
# CHECKR-NEXT: 0x8 R_LARCH_B21 .L1 0x0
# CHECKR-NEXT: 0xC R_LARCH_B16 .L1 0x0
# CHECKR-NEXT: 0x10 R_LARCH_B26 .L1 0x0
# CHECKR-NEXT: }
# CHECKR-NEXT: Section ({{.*}}) .rela.data {
# CHECKR-NEXT: 0x0 R_LARCH_64 .L1 0x0
# CHECKR-NEXT: }
# CHECKR-NEXT: ]

.text
nop
.L1:
nop
beqz $a0, .L1
blt $a0, $a1, .L1
b .L1

.data
.dword .L1