[llvm] 76fb161 - [LoongArch] Reduce number of reserved relocations when relax enabled (#153769)

via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 18 02:42:46 PDT 2025


Author: ZhaoQi
Date: 2025-08-18T17:42:43+08:00
New Revision: 76fb1619f096d91a7ce231d9cc446b1a4454b3cf

URL: https://github.com/llvm/llvm-project/commit/76fb1619f096d91a7ce231d9cc446b1a4454b3cf
DIFF: https://github.com/llvm/llvm-project/commit/76fb1619f096d91a7ce231d9cc446b1a4454b3cf.diff

LOG: [LoongArch] Reduce number of reserved relocations when relax enabled (#153769)

Added: 
    

Modified: 
    llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
    llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h
    llvm/test/MC/LoongArch/Relocations/relax-attr.s

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
index cb0014450702f..994e8577e496e 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) {
@@ -491,9 +475,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/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;

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


        


More information about the llvm-commits mailing list