[RISCV] add new relocation type for gp
Currently, the RISC-V architecture addresses constant subscript elements of global arrays using lui + ld/st, and non-constant subscript elements using lui + addi + add + ld/st. The low-address calculation process of addi is decomposed into ld/st, thereby eliminating low-address calculation and reducing addressing instructions.
C code test like
__attribute__((noinline)) char
ld_char (int i)
{
return arr1[i];
}
__attribute__((noinline)) char
ld_char_u (unsigned int i)
{
return arr1[i];
}
__attribute__((noinline)) short
ld_short (int i)
{
return arr2[i];
}
__attribute__((noinline)) short
ld_short_u (unsigned int i)
{
return arr2[i];
}
- Without GP relaxation lui + addi + add/shxadd + ld/st ----Transform ---- lui + add/shxadd + ld/st
- With GP relaxation addi + add + ld/st ------ Transform ----- add + ld/st
As shown in the diagram above, I need to update the add and ld/st instructions. Therefore, I need to add a new relocation type for this scenario to ensure that it is reprocessed during the linking phase. As like:
lui vr1, %hi(sym)
addi vr1, vr1, %lo(sym)
add vr2, vrx, vr1
lbu vr3, off(vr2)
-----Transform-----
lui vr1, %hi(sym+off)
add vr2, vrx, vr1, %gprel_add(sym+off)
lbu vr3, %gprel_lo(sym+off)(vr2)
My implement MR : https://github.com/llvm/llvm-project/pull/185353
@llvm/issue-subscribers-lld-elf
Author: LiqinWeng (LiqinWeng)
<img width="1708" height="292" alt="Image" src="https://github.com/user-attachments/assets/94c4b0ed-9c60-46e6-a66d-0a2ef22aeed4" />
- With GP relaxation addi + add + ld/st ------ Transform ----- add + ld/st
<img width="1780" height="260" alt="Image" src="https://github.com/user-attachments/assets/44f2939c-8477-4f67-867f-e3f14a176f5d" />
As shown in the diagram above, I need to update the add and ld/st instructions. Therefore, I need to add a new relocation type for this scenario to ensure that it is reprocessed during the linking phase. As like:
lui vr1, %hi(sym)
addi vr1, vr1, %lo(sym)
add vr2, vrx, vr1
lbu vr3, off(vr2)
-----Transform-----
lui vr1, %hi(sym+off)
add vr2, vrx, vr1, %gprel_add(sym+off)
lbu vr3, %gprel_lo(sym+off)(vr2)
My implement MR : https://github.com/llvm/llvm-project/pull/185353
@llvm/issue-subscribers-backend-risc-v
Author: LiqinWeng (LiqinWeng)
<img width="1708" height="292" alt="Image" src="https://github.com/user-attachments/assets/94c4b0ed-9c60-46e6-a66d-0a2ef22aeed4" />
- With GP relaxation addi + add + ld/st ------ Transform ----- add + ld/st
<img width="1780" height="260" alt="Image" src="https://github.com/user-attachments/assets/44f2939c-8477-4f67-867f-e3f14a176f5d" />
As shown in the diagram above, I need to update the add and ld/st instructions. Therefore, I need to add a new relocation type for this scenario to ensure that it is reprocessed during the linking phase. As like:
lui vr1, %hi(sym)
addi vr1, vr1, %lo(sym)
add vr2, vrx, vr1
lbu vr3, off(vr2)
-----Transform-----
lui vr1, %hi(sym+off)
add vr2, vrx, vr1, %gprel_add(sym+off)
lbu vr3, %gprel_lo(sym+off)(vr2)
My implement MR : https://github.com/llvm/llvm-project/pull/185353