Skip to content

Commit 5e854ab

Browse files
committed
[lld][LoongArch] GOT indirection to PC relative optimization.
In LoongArch, this optimization is only supported when relaxation is enabled. From: * pcalau12i $a0, %got_pc_hi20(sym_got) * ld.w/d $a0, $a0, %got_pc_lo12(sym_got) To: * pcalau12i $a0, %pc_hi20(sym) * addi.w/d $a0, $a0, %pc_lo12(sym) If the original code sequence can be relaxed into a single instruction `pcaddi`, this patch will not be taken (see https://). The implementation related to `got` is split into two locations because the `relax()` function is part of an iteration fixed-point algorithm. We should minimize it to achieve better linker performance. FIXME: Althouth the optimization has been performed, the GOT entries still exists, similarly to AArch64. Eliminating the entries may be require additional marking in the common code.
1 parent 1b901e9 commit 5e854ab

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

lld/ELF/Arch/LoongArch.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class LoongArch final : public TargetInfo {
4646
private:
4747
void tlsdescToIe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
4848
void tlsdescToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
49+
bool tryGotToPCRel(uint8_t *loc, const Relocation &rHi20,
50+
const Relocation &rLo12, uint64_t secAddr) const;
4951
};
5052
} // end anonymous namespace
5153

@@ -1152,6 +1154,54 @@ void LoongArch::tlsdescToLe(uint8_t *loc, const Relocation &rel,
11521154
}
11531155
}
11541156

1157+
// Try GOT indirection to PC relative optimization when relaxation is enabled.
1158+
// From:
1159+
// * pcalau12i $a0, %got_pc_hi20(sym_got)
1160+
// * ld.w/d $a0, $a0, %got_pc_lo12(sym_got)
1161+
// To:
1162+
// * pcalau12i $a0, %pc_hi20(sym)
1163+
// * addi.w/d $a0, $a0, %pc_lo12(sym)
1164+
//
1165+
// FIXME: Althouth the optimization has been performed, the GOT entries still
1166+
// exists, similarly to AArch64. Eliminating the entries may be require
1167+
// additional marking in the common code.
1168+
bool LoongArch::tryGotToPCRel(uint8_t *loc, const Relocation &rHi20,
1169+
const Relocation &rLo12, uint64_t secAddr) const {
1170+
if (!rHi20.sym->isDefined() || rHi20.sym->isPreemptible ||
1171+
rHi20.sym->isGnuIFunc() ||
1172+
(ctx.arg.isPic && !cast<Defined>(*rHi20.sym).section))
1173+
return false;
1174+
1175+
Symbol &sym = *rHi20.sym;
1176+
uint64_t symLocal = sym.getVA(ctx) + rHi20.addend;
1177+
// Check if the address difference is within +/-2GB range.
1178+
// For simplicity, the range mentioned here is an approximate estimate and is
1179+
// not fully equivalent to the entire region that PC-relative addressing can
1180+
// cover.
1181+
int64_t pageOffset =
1182+
getLoongArchPage(symLocal) - getLoongArchPage(secAddr + rHi20.offset);
1183+
if (!isInt<20>(pageOffset >> 12))
1184+
return false;
1185+
1186+
Relocation newRHi20 = {RE_LOONGARCH_PAGE_PC, R_LARCH_PCALA_HI20, rHi20.offset,
1187+
rHi20.addend, &sym};
1188+
Relocation newRLo12 = {R_ABS, R_LARCH_PCALA_LO12, rLo12.offset, rLo12.addend,
1189+
&sym};
1190+
1191+
const uint32_t currInsn = read32le(loc);
1192+
const uint32_t nextInsn = read32le(loc + 4);
1193+
uint64_t pageDelta =
1194+
getLoongArchPageDelta(symLocal, secAddr + rHi20.offset, rHi20.type);
1195+
// pcalau12i $a0, %pc_hi20
1196+
write32le(loc, insn(PCALAU12I, getD5(currInsn), 0, 0));
1197+
relocate(loc, newRHi20, pageDelta);
1198+
// addi.w/d $a0, $a0, %pc_lo12
1199+
write32le(loc + 4, insn(ctx.arg.is64 ? ADDI_D : ADDI_W, getD5(nextInsn),
1200+
getJ5(nextInsn), 0));
1201+
relocate(loc + 4, newRLo12, SignExtend64(symLocal, 64));
1202+
return true;
1203+
}
1204+
11551205
// During TLSDESC GD_TO_IE, the converted code sequence always includes an
11561206
// instruction related to the Lo12 relocation (ld.[wd]). To obtain correct val
11571207
// in `getRelocTargetVA`, expr of this instruction should be adjusted to
@@ -1261,6 +1311,22 @@ void LoongArch::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
12611311
tlsdescToLe(loc, rel, val);
12621312
}
12631313
continue;
1314+
case RE_LOONGARCH_GOT_PAGE_PC:
1315+
// In LoongArch, we try GOT indirection to PC relative optimization only
1316+
// when relaxation is enabled. This approach avoids determining whether
1317+
// relocation types are paired and whether the destination register of
1318+
// pcalau12i is only used by the immediately following instruction.
1319+
// Moreover, if the original code sequence can be relaxed to a single
1320+
// instruction `pcaddi`, the first instruction will be removed and it will
1321+
// not reach here.
1322+
if (isPairRelaxable(relocs, i) && rel.type == R_LARCH_GOT_PC_HI20 &&
1323+
relocs[i + 2].type == R_LARCH_GOT_PC_LO12 &&
1324+
tryGotToPCRel(loc, rel, relocs[i + 2], secAddr)) {
1325+
i = i + 3; // skip relocations R_LARCH_RELAX, R_LARCH_GOT_PC_LO12,
1326+
// R_LARCH_RELAX
1327+
continue;
1328+
}
1329+
break;
12641330
default:
12651331
break;
12661332
}

lld/test/ELF/loongarch-relax-pc-hi20-lo12.s

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,26 @@
3030
## offset = 0x410000 - 0x10000: 0x400 pages, page offset 0
3131
# NORELAX32-NEXT: 10000: pcalau12i $a0, 1024
3232
# NORELAX32-NEXT: addi.w $a0, $a0, 0
33+
## Not relaxation, convertion to PCRel.
3334
# NORELAX32-NEXT: pcalau12i $a0, 1024
34-
# NORELAX32-NEXT: ld.w $a0, $a0, 4
35+
# NORELAX32-NEXT: addi.w $a0, $a0, 0
3536
# NORELAX32-NEXT: pcalau12i $a0, 1024
3637
# NORELAX32-NEXT: addi.w $a0, $a0, 0
3738
# NORELAX32-NEXT: pcalau12i $a0, 1024
38-
# NORELAX32-NEXT: ld.w $a0, $a0, 4
39+
# NORELAX32-NEXT: addi.w $a0, $a0, 0
3940

4041
# NORELAX64-LABEL: <_start>:
4142
## offset exceed range of pcaddi
4243
## offset = 0x410000 - 0x10000: 0x400 pages, page offset 0
4344
# NORELAX64-NEXT: 10000: pcalau12i $a0, 1024
4445
# NORELAX64-NEXT: addi.d $a0, $a0, 0
46+
## Not relaxation, convertion to PCRel.
4547
# NORELAX64-NEXT: pcalau12i $a0, 1024
46-
# NORELAX64-NEXT: ld.d $a0, $a0, 8
48+
# NORELAX64-NEXT: addi.d $a0, $a0, 0
4749
# NORELAX64-NEXT: pcalau12i $a0, 1024
4850
# NORELAX64-NEXT: addi.d $a0, $a0, 0
4951
# NORELAX64-NEXT: pcalau12i $a0, 1024
50-
# NORELAX64-NEXT: ld.d $a0, $a0, 8
52+
# NORELAX64-NEXT: addi.d $a0, $a0, 0
5153

5254
.section .text
5355
.global _start

0 commit comments

Comments
 (0)