[lld] [llvm] [RISCV] Teach RISCVMergeBaseOffset to merge %lo into load/store folding arithmetic (PR #185353)
Sam Elliott via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 10 12:04:46 PDT 2026
lenary wrote:
> > I think I did get confused when you started calling these "gprel", as we used to have some "gprel" relocations which were GP-relative. In this case, they're just relative to any other GPR, rather than `__global_pointer$` specifically, right? In this case, let's not use "gprel", maybe call them "regrel" or something, for clarity?
>
> if GP Relation, Still related to GP. with this patch, C code is:
>
> ```
> char gp[10];
> char foo(int i) {
> return gp[i]
> }
> ```
>
> <img alt="image" width="2000" height="306" src="https://private-user-images.githubusercontent.com/19768075/560758002-986066b6-f4da-4053-8ed4-f97ad2b8e540.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NzMxNjkxMzgsIm5iZiI6MTc3MzE2ODgzOCwicGF0aCI6Ii8xOTc2ODA3NS81NjA3NTgwMDItOTg2MDY2YjYtZjRkYS00MDUzLThlZDQtZjk3YWQyYjhlNTQwLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNjAzMTAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjYwMzEwVDE4NTM1OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTJkNzZiZDA2YjllMWY1YWYwZTJkOGIwZDYxN2VhMGI3YTc5NDBjZjI0NmQ3MDc3Nzc4Njg2OGYzZGNmZmZhZmEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.4oOEI0-e7TjiMubD03r-8XzDy5haxOR0KuIdEDg9T7o"> As for how to set the relocation type name, we can discuss that later. What do you think?
The problem with your example here is that this is *also* doing Global Pointer based relaxation, which is a specific thing, and when the ABI refers to `gp-rel`, they mean "Global Pointer relative". If your example `gp` variable (also confusingly named!) was in the lowest page, then you would end up with a sequence starting `add xN, xN, zero` because the LUI had been deleted, and the register it sets would have been replaced with `zero`.
What I want to see is, for example 1:
```
lui vr1, %hi(sym)
addi vr1, vr1, %lo(sym)
add vr2, vrx, vr1
lbu vr3, off(vr2)
# => After Compiler Transformation
lui vr1, %hi(sym+off)
add vr2, vrx, vr1, %NAME_add(sym+off)
lbu vr3, %NAME_lo(sym+off)(vr2)
# => After Linker Relaxation:
# if sym+off is within 12 signed bits of `__global_pointer$`
add vr2, vrx, gp
lbu vr3, <gp-relative-offset>(vr2)
# OR:
# if sym+off is within 12 signed bits of `0`
add vr2, vrx, zero
lbu vr3, <absolute offset>(vr2)
# OR
# <condition>
<instructions>
```
There are likely multiple different possible results after linker relaxation. All of these will need to be documented in the psABI proposal you prepare. I have done two for you, but maybe there are more, and maybe you want the linker to produce a different result from what I have shown.
You will also need to do the same for case (2).
The psABI doesn't care about your documented case (3), that's a compiler-only transformation (folding the `addi` immediate into the addend).
https://github.com/llvm/llvm-project/pull/185353
More information about the llvm-commits
mailing list