Skip to content

[RISCV] Exclude X1 and X5 from register scavenging for long branch. #80215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2024

Conversation

topperc
Copy link
Collaborator

@topperc topperc commented Jan 31, 2024

When a branch target is too far away we need to emit an indirect branch. We scavenge a register for this since we don't know we need this until after register allocation.

Jumps using X1 and X5 as the source are hints to the hardware to pop the return-address stack. We should avoiding using them for jumps that aren't a return or tail call.

Need to look into how to create enough register pressure to test this.

When a branch target is too far away we need to emit an indirect
branch. We scavenge a register for this since we don't know we
need this until after register allocation.

Jumps using X1 and X5 as the source are hints to the hardware to
pop the return-address stack. We should avoiding using them for
jumps that aren't a return or tail call.
@llvmbot
Copy link
Member

llvmbot commented Jan 31, 2024

@llvm/pr-subscribers-backend-risc-v

Author: Craig Topper (topperc)

Changes

When a branch target is too far away we need to emit an indirect branch. We scavenge a register for this since we don't know we need this until after register allocation.

Jumps using X1 and X5 as the source are hints to the hardware to pop the return-address stack. We should avoiding using them for jumps that aren't a return or tail call.

Need to look into how to create enough register pressure to test this.


Full diff: https://github.com/llvm/llvm-project/pull/80215.diff

1 Files Affected:

  • (modified) llvm/lib/Target/RISCV/RISCVInstrInfo.cpp (+1-1)
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp b/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
index 592962cebe897..48e83e345c9b1 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
@@ -1115,7 +1115,7 @@ void RISCVInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB,
   // FIXME: A virtual register must be used initially, as the register
   // scavenger won't work with empty blocks (SIInstrInfo::insertIndirectBranch
   // uses the same workaround).
-  Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
+  Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRJALRRegClass);
   auto II = MBB.end();
   // We may also update the jump target to RestoreBB later.
   MachineInstr &MI = *BuildMI(MBB, II, DL, get(RISCV::PseudoJump))

@preames
Copy link
Collaborator

preames commented Feb 1, 2024

Jumps using X1 and X5 as the source are hints to the hardware to pop the return-address stack. We should avoiding using them for jumps that aren't a return or tail call.

Could we instead prefer non-X1 and non-X5 registers, then fallback to scavenging them if that's all that's available? Confusing the hardware RAS predictor seems arguably better than crashing the compiler with a scavenging failure.

@topperc
Copy link
Collaborator Author

topperc commented Feb 1, 2024

Jumps using X1 and X5 as the source are hints to the hardware to pop the return-address stack. We should avoiding using them for jumps that aren't a return or tail call.

Could we instead prefer non-X1 and non-X5 registers, then fallback to scavenging them if that's all that's available? Confusing the hardware RAS predictor seems arguably better than crashing the compiler with a scavenging failure.

Don't we fall back to emergency spill?

@preames
Copy link
Collaborator

preames commented Feb 1, 2024

Don't we fall back to emergency spill?

You're correct. I didn't read far enough down in the existing code and got distracted by the /AllowSpill=/false bit.

@topperc topperc requested a review from wangpc-pp February 11, 2024 06:20
Copy link
Collaborator

@preames preames left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

(Thought I'd approved this after our previous exchange, but looks like I didn't. Sorry for the delay.)

@topperc topperc merged commit 0b6e040 into llvm:main Feb 12, 2024
@topperc topperc deleted the pr/regscav branch February 12, 2024 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants