Skip to content

Commit af6c43c

Browse files
xen0nSixWeining
authored andcommitted
[LoongArch] Allow %pc_lo12 relocs in JIRL's immediate operand position
Currently, gcc-13 will generate such assembly when `-mcmodel=medium`, which is ostensibly a dirty hack to allow bigger offsets for extern function calls without having to add more reloc types. This is not the best way to accomplish the original goal, but such usages will appear soon and we have to support it anyway. Example: ```c extern int foo(int); int bar(int x) { return foo(x + 123); } ``` will produce the following (simplified) assembly when compiled with `-O2 -mcmodel=medium`: ``` .globl bar .type bar, @function bar: .cfi_startproc addi.w $r4,$r4,123 pcalau12i $r12,%pc_hi20(foo) jirl $r0,$r12,%pc_lo12(foo) .cfi_endproc ``` Reviewed By: SixWeining, wangleiat, MaskRay, xry111 Differential Revision: https://reviews.llvm.org/D142278
1 parent 53a1314 commit af6c43c

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

llvm/lib/Target/LoongArch/AsmParser/LoongArchAsmParser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ class LoongArchOperand : public MCParsedAsmOperand {
318318
LoongArchMCExpr::VariantKind VK = LoongArchMCExpr::VK_LoongArch_None;
319319
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
320320
bool IsValidKind = VK == LoongArchMCExpr::VK_LoongArch_None ||
321-
VK == LoongArchMCExpr::VK_LoongArch_B16;
321+
VK == LoongArchMCExpr::VK_LoongArch_B16 ||
322+
VK == LoongArchMCExpr::VK_LoongArch_PCALA_LO12;
322323
return IsConstantImm
323324
? isShiftedInt<16, 2>(Imm) && IsValidKind
324325
: LoongArchAsmParser::classifySymbolRef(getImm(), VK) &&

llvm/test/MC/LoongArch/Relocations/relocations.s

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ addi.d $t1, $t1, %pc_lo12(foo+4)
7474
# INSTR: addi.d $t1, $t1, %pc_lo12(foo+4)
7575
# FIXUP: fixup A - offset: 0, value: %pc_lo12(foo+4), kind: FK_NONE
7676

77+
jirl $zero, $t1, %pc_lo12(foo)
78+
# RELOC: R_LARCH_PCALA_LO12 foo 0x0
79+
# INSTR: jirl $zero, $t1, %pc_lo12(foo)
80+
# FIXUP: fixup A - offset: 0, value: %pc_lo12(foo), kind: FK_NONE
81+
7782
st.b $t1, $a2, %pc_lo12(foo)
7883
# RELOC: R_LARCH_PCALA_LO12 foo 0x0
7984
# INSTR: st.b $t1, $a2, %pc_lo12(foo)

0 commit comments

Comments
 (0)