Skip to content

[BOLT][RISCV] Fix AUIPC/JALR call rewriting - #216882

Draft
rdtscp wants to merge 1 commit into
llvm:mainfrom
rdtscp:risc-v-bolt
Draft

[BOLT][RISCV] Fix AUIPC/JALR call rewriting#216882
rdtscp wants to merge 1 commit into
llvm:mainfrom
rdtscp:risc-v-bolt

Conversation

@rdtscp

@rdtscp rdtscp commented Aug 17, 2026

Copy link
Copy Markdown

R_RISCV_CALL and R_RISCV_CALL_PLT cover an AUIPC/JALR pair, but BOLT treated them as four-byte relocations and decoded only the AUIPC immediate. Read both instructions and combine their signed high and low immediates so relocated call targets retain the low 12 bits.

LTO can also leave linker-resolved intra-section AUIPC/JALR calls without relocations. Recognize valid standard call and tail-call pairs during disassembly, reconstruct the target (including JALR target-bit clearing), and attach a symbol reference before function reordering.

This follows the RISC-V Unprivileged ISA RV32I sections "Integer Computational Instructions" (AUIPC) and "Control Transfer Instructions" (JALR): https://docs.riscv.org/reference/isa/v20260120/unpriv/rv32.html

It also follows the RISC-V ELF psABI "Relocations" chapter, specifically "Procedure Calls", where R_RISCV_CALL and R_RISCV_CALL_PLT apply to the AUIPC/JALR pair: https://riscv-non-isa.github.io/riscv-elf-psabi-doc/#_relocations

Tested with the focused RISC-V CoreTests and the full BOLT RISCV lit directory (38 passed).

Scope: Relocation-less AUIPC/JALR recovery is intentionally RV64-only. RV32 XLEN-wrapped target arithmetic is deferred to a follow-up.

Assisted-by: Codex

@github-actions

Copy link
Copy Markdown

Hello @rdtscp 👋

Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.

  • All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
  • The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
  • Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.

Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.


Frequently asked questions

How do I add reviewers?

This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.

You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.

What if there are no comments?

If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.

Are any special GitHub settings required to contribute to LLVM?

We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.


If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.

Thank you,
The LLVM Community

R_RISCV_CALL and R_RISCV_CALL_PLT cover an AUIPC/JALR pair, but BOLT treated them as four-byte relocations and decoded only the AUIPC immediate. Read both instructions and combine their signed high and low immediates so relocated call targets retain the low 12 bits.

LTO can also leave linker-resolved intra-section AUIPC/JALR calls without relocations. On RV64, recognize valid call and tail-call pairs during disassembly, reconstruct the target (including JALR target-bit clearing), and attach an exact entry-point symbol before function reordering. Preserve alternate link registers such as x5 when canonicalizing calls.

Relocation-less recovery is deliberately limited to RV64. RV32 target calculation requires XLEN-wrapped address arithmetic and remains out of scope for this change.

This follows the RISC-V Unprivileged ISA RV32I sections "Integer Computational Instructions" (AUIPC) and "Control Transfer Instructions" (JALR):
https://docs.riscv.org/reference/isa/v20260120/unpriv/rv32.html

It also follows the RISC-V ELF psABI "Relocations" chapter, specifically "Procedure Calls", where R_RISCV_CALL and R_RISCV_CALL_PLT apply to the AUIPC/JALR pair:
https://riscv-non-isa.github.io/riscv-elf-psabi-doc/#_relocations

Tested with four focused RISC-V CoreTests and the full BOLT RISCV lit directory (39 passed).

Assisted-by: Codex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant