https://github.com/zhaoqi5 updated https://github.com/llvm/llvm-project/pull/153769
>From f67324528d93ca3e908f39e8e89caef5ecc3e11f Mon Sep 17 00:00:00 2001 From: Qi Zhao <zhaoq...@loongson.cn> Date: Fri, 15 Aug 2025 17:12:33 +0800 Subject: [PATCH 1/2] [LoongArch] Reduce number of reserved relocations when relax enabled --- .../MCTargetDesc/LoongArchAsmBackend.cpp | 27 +++++++------------ .../MC/LoongArch/Relocations/relax-attr.s | 7 ++--- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp index ca5d27d54bb81..c6eda4fd63a6b 100644 --- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp +++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp @@ -143,8 +143,6 @@ static void fixupLeb128(MCContext &Ctx, const MCFixup &Fixup, uint8_t *Data, void LoongArchAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup, const MCValue &Target, uint8_t *Data, uint64_t Value, bool IsResolved) { - if (IsResolved && shouldForceRelocation(Fixup, Target)) - IsResolved = false; IsResolved = addReloc(F, Fixup, Target, Value, IsResolved); if (!Value) return; // Doesn't change encoding. @@ -176,20 +174,6 @@ void LoongArchAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup, } } -bool LoongArchAsmBackend::shouldForceRelocation(const MCFixup &Fixup, - const MCValue &Target) { - switch (Fixup.getKind()) { - default: - return STI.hasFeature(LoongArch::FeatureRelax); - case FK_Data_1: - case FK_Data_2: - case FK_Data_4: - case FK_Data_8: - case FK_Data_leb128: - return !Target.isAbsolute(); - } -} - static inline std::pair<MCFixupKind, MCFixupKind> getRelocPairForSize(unsigned Size) { switch (Size) { @@ -484,9 +468,16 @@ bool LoongArchAsmBackend::addReloc(const MCFragment &F, const MCFixup &Fixup, return false; } - IsResolved = Fallback(); // If linker relaxation is enabled and supported by the current relocation, - // append a RELAX relocation. + // generate a relocation and then append a RELAX. + if (Fixup.isLinkerRelaxable()) + IsResolved = false; + if (IsResolved && Fixup.isPCRel()) + IsResolved = isPCRelFixupResolved(Target.getAddSym(), F); + + if (!IsResolved) + Asm->getWriter().recordRelocation(F, Fixup, Target, FixedValue); + if (Fixup.isLinkerRelaxable()) { auto FA = MCFixup::create(Fixup.getOffset(), nullptr, ELF::R_LARCH_RELAX); Asm->getWriter().recordRelocation(F, FA, MCValue::get(nullptr), diff --git a/llvm/test/MC/LoongArch/Relocations/relax-attr.s b/llvm/test/MC/LoongArch/Relocations/relax-attr.s index e3e9038b755d3..d94d32ebd7ab0 100644 --- a/llvm/test/MC/LoongArch/Relocations/relax-attr.s +++ b/llvm/test/MC/LoongArch/Relocations/relax-attr.s @@ -17,11 +17,8 @@ # CHECKR-NEXT: 0x4 R_LARCH_CALL36 foo 0x0 # CHECKR-NEXT: 0x4 R_LARCH_RELAX - 0x0 # CHECKR-NEXT: 0x10 R_LARCH_B21 .L0 0x0 -# CHECKR-NEXT: 0x14 R_LARCH_B21 .L1 0x0 # CHECKR-NEXT: 0x18 R_LARCH_B16 .L0 0x0 -# CHECKR-NEXT: 0x1C R_LARCH_B16 .L1 0x0 # CHECKR-NEXT: 0x20 R_LARCH_B26 .L0 0x0 -# CHECKR-NEXT: 0x24 R_LARCH_B26 .L1 0x0 # CHECKR-NEXT: } # CHECKR-NEXT: Section ({{.*}}) .rela.data { # CHECKR-NEXT: 0x0 R_LARCH_64 .L1 0x0 @@ -36,6 +33,10 @@ .L1: nop +## Relocations for branches to .L0 must be reserved and be fixed up by linker +## when linker relaxation enabled, because of the relaxable call36 instruction. +## Branches to .L1 can be resolved correctly at compile time, so their +## relocations can simply be removed. bnez $a0, .L0 beqz $a0, .L1 beq $a0, $a1, .L0 >From f491f2cf66ea530ef3a5f465b87a0dad0b4c6d5e Mon Sep 17 00:00:00 2001 From: Qi Zhao <zhaoq...@loongson.cn> Date: Fri, 15 Aug 2025 20:13:59 +0800 Subject: [PATCH 2/2] remove shouldForceRelocation declaration --- llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h index 1f1360119edba..f79d3aa48c54c 100644 --- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h +++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h @@ -44,8 +44,6 @@ class LoongArchAsmBackend : public MCAsmBackend { void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target, uint8_t *Data, uint64_t Value, bool IsResolved) override; - bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target); - std::optional<MCFixupKind> getFixupKind(StringRef Name) const override; MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override; _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits