[lld] [llvm] [LoongArch] Use R_LARCH_ALIGN without symbol as much as possible (PR #93775)
Lu Weining via llvm-commits
llvm-commits at lists.llvm.org
Thu May 30 20:04:15 PDT 2024
https://github.com/SixWeining updated https://github.com/llvm/llvm-project/pull/93775
>From 5dec9e87eac0e626bacf961848af6be868f30edf Mon Sep 17 00:00:00 2001
From: Jinyang He <hejinyang at loongson.cn>
Date: Mon, 26 Feb 2024 19:01:38 +0800
Subject: [PATCH 1/2] [LoongArch] Use R_LARCH_ALIGN without symbol as much as
possible
In LoongArch psABI v2.30, the R_LARCH_ALIGN requires symbol index to
to support the third parameter of alignment directive. Following this
extended R_LARCH_ALIGN make the symbol index be redundant in some case.
E.g. `.align 4` or `.align 4,,16`. Thus, use the extended R_LARCH_ALIGN
only when the third parameter is effective. Otherwise use the orginal
R_LARCH_ALIGN for simple.
---
lld/test/ELF/loongarch-relax-emit-relocs.s | 4 +--
.../MCTargetDesc/LoongArchAsmBackend.cpp | 29 +++++++++++--------
.../MC/LoongArch/Relocations/relax-addsub.s | 2 +-
.../MC/LoongArch/Relocations/relax-align.s | 12 ++++----
4 files changed, 27 insertions(+), 20 deletions(-)
diff --git a/lld/test/ELF/loongarch-relax-emit-relocs.s b/lld/test/ELF/loongarch-relax-emit-relocs.s
index 581fce8c95caa..ba414e8c93f0f 100644
--- a/lld/test/ELF/loongarch-relax-emit-relocs.s
+++ b/lld/test/ELF/loongarch-relax-emit-relocs.s
@@ -25,7 +25,7 @@
# CHECK-NEXT: R_LARCH_PCALA_LO12 _start
# CHECK-NEXT: R_LARCH_RELAX *ABS*
# CHECK-NEXT: nop
-# CHECK-NEXT: R_LARCH_ALIGN .Lla-relax-align0+0x4
+# CHECK-NEXT: R_LARCH_ALIGN *ABS*+0xc
# CHECK-NEXT: nop
# CHECK-NEXT: ret
@@ -37,7 +37,7 @@
# CHECKR-NEXT: R_LARCH_PCALA_LO12 _start
# CHECKR-NEXT: R_LARCH_RELAX *ABS*
# CHECKR-NEXT: nop
-# CHECKR-NEXT: R_LARCH_ALIGN .Lla-relax-align0+0x4
+# CHECKR-NEXT: R_LARCH_ALIGN *ABS*+0xc
# CHECKR-NEXT: nop
# CHECKR-NEXT: nop
# CHECKR-NEXT: ret
diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
index de492f2b1f0a4..1006cc42ad2ef 100644
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
@@ -224,20 +224,25 @@ bool LoongArchAsmBackend::shouldInsertFixupForCodeAlign(
// Create fixup_loongarch_align fixup.
MCFixup Fixup =
MCFixup::create(0, Dummy, MCFixupKind(LoongArch::fixup_loongarch_align));
- const MCSymbolRefExpr *MCSym = getSecToAlignSym()[Sec];
- if (MCSym == nullptr) {
- // Create a symbol and make the value of symbol is zero.
- MCSymbol *Sym = Ctx.createNamedTempSymbol("la-relax-align");
- Sym->setFragment(&*Sec->getBeginSymbol()->getFragment());
- Asm.registerSymbol(*Sym);
- MCSym = MCSymbolRefExpr::create(Sym, Ctx);
- getSecToAlignSym()[Sec] = MCSym;
- }
+
+ auto createExtendedValue = [&]() {
+ const MCSymbolRefExpr *MCSym = getSecToAlignSym()[Sec];
+ if (MCSym == nullptr) {
+ // Create a symbol and make the value of symbol is zero.
+ MCSymbol *Sym = Ctx.createNamedTempSymbol("la-relax-align");
+ Sym->setFragment(&*Sec->getBeginSymbol()->getFragment());
+ Asm.registerSymbol(*Sym);
+ MCSym = MCSymbolRefExpr::create(Sym, Ctx);
+ getSecToAlignSym()[Sec] = MCSym;
+ }
+ unsigned Lo = Log2_64(Count) + 1;
+ unsigned Hi = AF.getMaxBytesToEmit();
+ return MCValue::get(MCSym, nullptr, Hi << 8 | Lo);
+ };
uint64_t FixedValue = 0;
- unsigned Lo = Log2_64(Count) + 1;
- unsigned Hi = AF.getMaxBytesToEmit() >= Count ? 0 : AF.getMaxBytesToEmit();
- MCValue Value = MCValue::get(MCSym, nullptr, Hi << 8 | Lo);
+ MCValue Value = AF.getMaxBytesToEmit() >= Count ? MCValue::get(Count)
+ : createExtendedValue();
Asm.getWriter().recordRelocation(Asm, Layout, &AF, Fixup, Value, FixedValue);
return true;
diff --git a/llvm/test/MC/LoongArch/Relocations/relax-addsub.s b/llvm/test/MC/LoongArch/Relocations/relax-addsub.s
index 18e0ede5e2937..f2524b29d230b 100644
--- a/llvm/test/MC/LoongArch/Relocations/relax-addsub.s
+++ b/llvm/test/MC/LoongArch/Relocations/relax-addsub.s
@@ -28,7 +28,7 @@
# RELAX: Relocations [
# RELAX-NEXT: Section ({{.*}}) .rela.text {
-# RELAX-NEXT: 0x4 R_LARCH_ALIGN {{.*}} 0x4
+# RELAX-NEXT: 0x4 R_LARCH_ALIGN - 0xC
# RELAX-NEXT: 0x10 R_LARCH_PCALA_HI20 .L1 0x0
# RELAX-NEXT: 0x10 R_LARCH_RELAX - 0x0
# RELAX-NEXT: 0x14 R_LARCH_PCALA_LO12 .L1 0x0
diff --git a/llvm/test/MC/LoongArch/Relocations/relax-align.s b/llvm/test/MC/LoongArch/Relocations/relax-align.s
index 294fd9fb916c7..477d5ca24ec7d 100644
--- a/llvm/test/MC/LoongArch/Relocations/relax-align.s
+++ b/llvm/test/MC/LoongArch/Relocations/relax-align.s
@@ -63,17 +63,19 @@ ret
## Test the symbol index is different from .text.
.section .text2, "ax"
.p2align 4
+.p2align 4, , 4
break 7
# RELOC: Relocations [
# RELAX-RELOC-NEXT: Section ({{.*}}) .rela.text {
-# RELAX-RELOC-NEXT: 0x24 R_LARCH_ALIGN .Lla-relax-align0 0x4
-# RELAX-RELOC-NEXT: 0x34 R_LARCH_ALIGN .Lla-relax-align0 0x5
-# RELAX-RELOC-NEXT: 0x50 R_LARCH_ALIGN .Lla-relax-align0 0x4
+# RELAX-RELOC-NEXT: 0x24 R_LARCH_ALIGN - 0xC
+# RELAX-RELOC-NEXT: 0x34 R_LARCH_ALIGN - 0x1C
+# RELAX-RELOC-NEXT: 0x50 R_LARCH_ALIGN - 0xC
# RELAX-RELOC-NEXT: 0x60 R_LARCH_ALIGN .Lla-relax-align0 0xB04
-# RELAX-RELOC-NEXT: 0x70 R_LARCH_ALIGN .Lla-relax-align0 0x4
+# RELAX-RELOC-NEXT: 0x70 R_LARCH_ALIGN - 0xC
# RELAX-RELOC-NEXT: }
# RELAX-RELOC-NEXT: Section ({{.*}}) .rela.text2 {
-# RELAX-RELOC-NEXT: 0x0 R_LARCH_ALIGN .Lla-relax-align1 0x4
+# RELAX-RELOC-NEXT: 0x0 R_LARCH_ALIGN - 0xC
+# RELAX-RELOC-NEXT: 0xC R_LARCH_ALIGN .Lla-relax-align1 0x404
# RELAX-RELOC-NEXT: }
# RELOC-NEXT: ]
>From 3c3c763c7b3b03c3670d961771ec7b69d9d2a6c2 Mon Sep 17 00:00:00 2001
From: Weining Lu <luweining at loongson.cn>
Date: Fri, 31 May 2024 11:03:46 +0800
Subject: [PATCH 2/2] Address @MaskRay's comments
---
.../MCTargetDesc/LoongArchAsmBackend.cpp | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
index 1006cc42ad2ef..a36632d3fe2b0 100644
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
@@ -214,8 +214,8 @@ bool LoongArchAsmBackend::shouldInsertFixupForCodeAlign(
// Calculate total Nops we need to insert. If there are none to insert
// then simply return.
- unsigned Count;
- if (!shouldInsertExtraNopBytesForCodeAlign(AF, Count))
+ unsigned InsertedNopBytes;
+ if (!shouldInsertExtraNopBytesForCodeAlign(AF, InsertedNopBytes))
return false;
MCSection *Sec = AF.getParent();
@@ -224,25 +224,26 @@ bool LoongArchAsmBackend::shouldInsertFixupForCodeAlign(
// Create fixup_loongarch_align fixup.
MCFixup Fixup =
MCFixup::create(0, Dummy, MCFixupKind(LoongArch::fixup_loongarch_align));
+ unsigned MaxBytesToEmit = AF.getMaxBytesToEmit();
auto createExtendedValue = [&]() {
const MCSymbolRefExpr *MCSym = getSecToAlignSym()[Sec];
if (MCSym == nullptr) {
- // Create a symbol and make the value of symbol is zero.
+ // Define a marker symbol at the section with an offset of 0.
MCSymbol *Sym = Ctx.createNamedTempSymbol("la-relax-align");
Sym->setFragment(&*Sec->getBeginSymbol()->getFragment());
Asm.registerSymbol(*Sym);
MCSym = MCSymbolRefExpr::create(Sym, Ctx);
getSecToAlignSym()[Sec] = MCSym;
}
- unsigned Lo = Log2_64(Count) + 1;
- unsigned Hi = AF.getMaxBytesToEmit();
- return MCValue::get(MCSym, nullptr, Hi << 8 | Lo);
+ return MCValue::get(MCSym, nullptr,
+ MaxBytesToEmit << 8 | Log2(AF.getAlignment()));
};
uint64_t FixedValue = 0;
- MCValue Value = AF.getMaxBytesToEmit() >= Count ? MCValue::get(Count)
- : createExtendedValue();
+ MCValue Value = MaxBytesToEmit >= InsertedNopBytes
+ ? MCValue::get(InsertedNopBytes)
+ : createExtendedValue();
Asm.getWriter().recordRelocation(Asm, Layout, &AF, Fixup, Value, FixedValue);
return true;
More information about the llvm-commits
mailing list