Skip to content

Commit fbdd948

Browse files
[LoongArch] Use R_LARCH_ALIGN without symbol as much as possible (#93775)
To support the third parameter of the alignment directive, R_LARCH_ALIGN relocations need a non-zero symbol index. In many cases we don't need the third parameter and can set the symbol index to 0. This patch will remove a lot of .Lla-relax-align* symbols and mitigate the size regression due to #72962. Co-authored-by: Jinyang He <[email protected]> Co-authored-by: Weining Lu <[email protected]>
1 parent 8ec1161 commit fbdd948

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

lld/test/ELF/loongarch-relax-emit-relocs.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# CHECK-NEXT: R_LARCH_PCALA_LO12 _start
2626
# CHECK-NEXT: R_LARCH_RELAX *ABS*
2727
# CHECK-NEXT: nop
28-
# CHECK-NEXT: R_LARCH_ALIGN .Lla-relax-align0+0x4
28+
# CHECK-NEXT: R_LARCH_ALIGN *ABS*+0xc
2929
# CHECK-NEXT: nop
3030
# CHECK-NEXT: ret
3131

@@ -37,7 +37,7 @@
3737
# CHECKR-NEXT: R_LARCH_PCALA_LO12 _start
3838
# CHECKR-NEXT: R_LARCH_RELAX *ABS*
3939
# CHECKR-NEXT: nop
40-
# CHECKR-NEXT: R_LARCH_ALIGN .Lla-relax-align0+0x4
40+
# CHECKR-NEXT: R_LARCH_ALIGN *ABS*+0xc
4141
# CHECKR-NEXT: nop
4242
# CHECKR-NEXT: nop
4343
# CHECKR-NEXT: ret

llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ bool LoongArchAsmBackend::shouldInsertFixupForCodeAlign(
214214

215215
// Calculate total Nops we need to insert. If there are none to insert
216216
// then simply return.
217-
unsigned Count;
218-
if (!shouldInsertExtraNopBytesForCodeAlign(AF, Count))
217+
unsigned InsertedNopBytes;
218+
if (!shouldInsertExtraNopBytesForCodeAlign(AF, InsertedNopBytes))
219219
return false;
220220

221221
MCSection *Sec = AF.getParent();
@@ -224,20 +224,26 @@ bool LoongArchAsmBackend::shouldInsertFixupForCodeAlign(
224224
// Create fixup_loongarch_align fixup.
225225
MCFixup Fixup =
226226
MCFixup::create(0, Dummy, MCFixupKind(LoongArch::fixup_loongarch_align));
227-
const MCSymbolRefExpr *MCSym = getSecToAlignSym()[Sec];
228-
if (MCSym == nullptr) {
229-
// Create a symbol and make the value of symbol is zero.
230-
MCSymbol *Sym = Ctx.createNamedTempSymbol("la-relax-align");
231-
Sym->setFragment(&*Sec->getBeginSymbol()->getFragment());
232-
Asm.registerSymbol(*Sym);
233-
MCSym = MCSymbolRefExpr::create(Sym, Ctx);
234-
getSecToAlignSym()[Sec] = MCSym;
235-
}
227+
unsigned MaxBytesToEmit = AF.getMaxBytesToEmit();
228+
229+
auto createExtendedValue = [&]() {
230+
const MCSymbolRefExpr *MCSym = getSecToAlignSym()[Sec];
231+
if (MCSym == nullptr) {
232+
// Define a marker symbol at the section with an offset of 0.
233+
MCSymbol *Sym = Ctx.createNamedTempSymbol("la-relax-align");
234+
Sym->setFragment(&*Sec->getBeginSymbol()->getFragment());
235+
Asm.registerSymbol(*Sym);
236+
MCSym = MCSymbolRefExpr::create(Sym, Ctx);
237+
getSecToAlignSym()[Sec] = MCSym;
238+
}
239+
return MCValue::get(MCSym, nullptr,
240+
MaxBytesToEmit << 8 | Log2(AF.getAlignment()));
241+
};
236242

237243
uint64_t FixedValue = 0;
238-
unsigned Lo = Log2_64(Count) + 1;
239-
unsigned Hi = AF.getMaxBytesToEmit() >= Count ? 0 : AF.getMaxBytesToEmit();
240-
MCValue Value = MCValue::get(MCSym, nullptr, Hi << 8 | Lo);
244+
MCValue Value = MaxBytesToEmit >= InsertedNopBytes
245+
? MCValue::get(InsertedNopBytes)
246+
: createExtendedValue();
241247
Asm.getWriter().recordRelocation(Asm, Layout, &AF, Fixup, Value, FixedValue);
242248

243249
return true;

llvm/test/MC/LoongArch/Relocations/relax-addsub.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
# RELAX: Relocations [
3030
# RELAX-NEXT: Section ({{.*}}) .rela.text {
31-
# RELAX-NEXT: 0x4 R_LARCH_ALIGN {{.*}} 0x4
31+
# RELAX-NEXT: 0x4 R_LARCH_ALIGN - 0xC
3232
# RELAX-NEXT: 0x10 R_LARCH_PCALA_HI20 .L1 0x0
3333
# RELAX-NEXT: 0x10 R_LARCH_RELAX - 0x0
3434
# RELAX-NEXT: 0x14 R_LARCH_PCALA_LO12 .L1 0x0

llvm/test/MC/LoongArch/Relocations/relax-align.s

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,19 @@ ret
6363
## Test the symbol index is different from .text.
6464
.section .text2, "ax"
6565
.p2align 4
66+
.p2align 4, , 4
6667
break 7
6768

6869
# RELOC: Relocations [
6970
# RELAX-RELOC-NEXT: Section ({{.*}}) .rela.text {
70-
# RELAX-RELOC-NEXT: 0x24 R_LARCH_ALIGN .Lla-relax-align0 0x4
71-
# RELAX-RELOC-NEXT: 0x34 R_LARCH_ALIGN .Lla-relax-align0 0x5
72-
# RELAX-RELOC-NEXT: 0x50 R_LARCH_ALIGN .Lla-relax-align0 0x4
71+
# RELAX-RELOC-NEXT: 0x24 R_LARCH_ALIGN - 0xC
72+
# RELAX-RELOC-NEXT: 0x34 R_LARCH_ALIGN - 0x1C
73+
# RELAX-RELOC-NEXT: 0x50 R_LARCH_ALIGN - 0xC
7374
# RELAX-RELOC-NEXT: 0x60 R_LARCH_ALIGN .Lla-relax-align0 0xB04
74-
# RELAX-RELOC-NEXT: 0x70 R_LARCH_ALIGN .Lla-relax-align0 0x4
75+
# RELAX-RELOC-NEXT: 0x70 R_LARCH_ALIGN - 0xC
7576
# RELAX-RELOC-NEXT: }
7677
# RELAX-RELOC-NEXT: Section ({{.*}}) .rela.text2 {
77-
# RELAX-RELOC-NEXT: 0x0 R_LARCH_ALIGN .Lla-relax-align1 0x4
78+
# RELAX-RELOC-NEXT: 0x0 R_LARCH_ALIGN - 0xC
79+
# RELAX-RELOC-NEXT: 0xC R_LARCH_ALIGN .Lla-relax-align1 0x404
7880
# RELAX-RELOC-NEXT: }
7981
# RELOC-NEXT: ]

0 commit comments

Comments
 (0)