[llvm-branch-commits] [llvm] [LoongArch] Reduce number of reserved relocations when relax enabled (PR #153769)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Aug 15 02:58:58 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mc
@llvm/pr-subscribers-backend-loongarch
Author: ZhaoQi (zhaoqi5)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/153769.diff
2 Files Affected:
- (modified) llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp (+9-18)
- (modified) llvm/test/MC/LoongArch/Relocations/relax-attr.s (+4-3)
``````````diff
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
``````````
</details>
https://github.com/llvm/llvm-project/pull/153769
More information about the llvm-branch-commits
mailing list