Skip to content

[RISCV] Support PreserveMost calling convention #148214

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions llvm/docs/LangRef.rst
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ added in the future:
- On AArch64 the callee preserve all general purpose registers, except
X0-X8 and X16-X18. Not allowed with ``nest``.

- On RISC-V the callee preserve x5-x31 registers.
Copy link
Member

Choose a reason for hiding this comment

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

This isn't going to work as the PLT stub can/does clobber some temporary registers (and exactly which depend on a lot of things). This is why AArch64 does not preserve x16-x18.

Copy link
Member

Choose a reason for hiding this comment

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

Looking at lld, t1, t2, and t3 are clobbered by the PLT resolver, so we need to account for that.

I think this means:

  • The function should be saving these three regs.
  • the mask used for preserved registers in calls should not include these three.


The idea behind this convention is to support calls to runtime functions
that have a hot path and a cold path. The hot path is usually a small piece
of code that doesn't use many registers. The cold path might need to call out to
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Target/RISCV/RISCVCallingConv.td
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,7 @@ def CSR_XLEN_F32_V_Interrupt_RVE: CalleeSavedRegs<(sub CSR_XLEN_F32_V_Interrupt,
// Same as CSR_XLEN_F64_V_Interrupt, but excluding X16-X31.
def CSR_XLEN_F64_V_Interrupt_RVE: CalleeSavedRegs<(sub CSR_XLEN_F64_V_Interrupt,
(sequence "X%u", 16, 31))>;

def CSR_RT_MostRegs : CalleeSavedRegs<(add X1, (sequence "X%u", 5, 31))>;
def CSR_RT_MostRegs_RVE : CalleeSavedRegs<(sub CSR_RT_MostRegs,
(sequence "X%u", 16, 31))>;
1 change: 1 addition & 0 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22205,6 +22205,7 @@ SDValue RISCVTargetLowering::LowerFormalArguments(
case CallingConv::C:
case CallingConv::Fast:
case CallingConv::SPIR_KERNEL:
case CallingConv::PreserveMost:
case CallingConv::GRAAL:
case CallingConv::RISCV_VectorCall:
#define CC_VLS_CASE(ABI_VLEN) case CallingConv::RISCV_VLSCall_##ABI_VLEN:
Expand Down
12 changes: 11 additions & 1 deletion llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

#include "RISCVRegisterInfo.h"
#include "MCTargetDesc/RISCVBaseInfo.h"
#include "RISCV.h"
#include "RISCVSubtarget.h"
#include "llvm/ADT/SmallSet.h"
Expand Down Expand Up @@ -68,6 +69,9 @@ RISCVRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
auto &Subtarget = MF->getSubtarget<RISCVSubtarget>();
if (MF->getFunction().getCallingConv() == CallingConv::GHC)
return CSR_NoRegs_SaveList;
if (MF->getFunction().getCallingConv() == CallingConv::PreserveMost)
return Subtarget.hasStdExtE() ? CSR_RT_MostRegs_RVE_SaveList
: CSR_RT_MostRegs_SaveList;
if (MF->getFunction().hasFnAttribute("interrupt")) {
if (Subtarget.hasVInstructions()) {
if (Subtarget.hasStdExtD())
Expand Down Expand Up @@ -811,7 +815,13 @@ RISCVRegisterInfo::getCallPreservedMask(const MachineFunction & MF,

if (CC == CallingConv::GHC)
return CSR_NoRegs_RegMask;
switch (Subtarget.getTargetABI()) {
RISCVABI::ABI ABI = Subtarget.getTargetABI();
if (CC == CallingConv::PreserveMost) {
if (ABI == RISCVABI::ABI_ILP32E || ABI == RISCVABI::ABI_LP64E)
return CSR_RT_MostRegs_RVE_RegMask;
return CSR_RT_MostRegs_RegMask;
}
switch (ABI) {
default:
llvm_unreachable("Unrecognized ABI");
case RISCVABI::ABI_ILP32E:
Expand Down
187 changes: 187 additions & 0 deletions llvm/test/CodeGen/RISCV/calling-conv-preserve-most.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc -mtriple=riscv32 < %s | FileCheck %s -check-prefix=RV32I
; RUN: llc -mtriple=riscv64 < %s | FileCheck %s -check-prefix=RV64I
; RUN: llc -mtriple=riscv32 -mattr=+e -target-abi ilp32e < %s | FileCheck %s -check-prefix=RV32E
; RUN: llc -mtriple=riscv64 -mattr=+e -target-abi lp64e < %s | FileCheck %s -check-prefix=RV64E

; Check the PreserveMost calling convention works.

declare void @standard_cc_func()
declare preserve_mostcc void @preserve_mostcc_func()

define preserve_mostcc void @preserve_mostcc1() nounwind {
; RV32I-LABEL: preserve_mostcc1:
; RV32I: # %bb.0: # %entry
; RV32I-NEXT: addi sp, sp, -64
; RV32I-NEXT: sw ra, 60(sp) # 4-byte Folded Spill
; RV32I-NEXT: sw t0, 56(sp) # 4-byte Folded Spill
; RV32I-NEXT: sw t1, 52(sp) # 4-byte Folded Spill
; RV32I-NEXT: sw t2, 48(sp) # 4-byte Folded Spill
; RV32I-NEXT: sw a0, 44(sp) # 4-byte Folded Spill
; RV32I-NEXT: sw a1, 40(sp) # 4-byte Folded Spill
; RV32I-NEXT: sw a2, 36(sp) # 4-byte Folded Spill
; RV32I-NEXT: sw a3, 32(sp) # 4-byte Folded Spill
; RV32I-NEXT: sw a4, 28(sp) # 4-byte Folded Spill
; RV32I-NEXT: sw a5, 24(sp) # 4-byte Folded Spill
; RV32I-NEXT: sw a6, 20(sp) # 4-byte Folded Spill
; RV32I-NEXT: sw a7, 16(sp) # 4-byte Folded Spill
; RV32I-NEXT: sw t3, 12(sp) # 4-byte Folded Spill
; RV32I-NEXT: sw t4, 8(sp) # 4-byte Folded Spill
; RV32I-NEXT: sw t5, 4(sp) # 4-byte Folded Spill
; RV32I-NEXT: sw t6, 0(sp) # 4-byte Folded Spill
; RV32I-NEXT: call standard_cc_func
; RV32I-NEXT: lw ra, 60(sp) # 4-byte Folded Reload
; RV32I-NEXT: lw t0, 56(sp) # 4-byte Folded Reload
; RV32I-NEXT: lw t1, 52(sp) # 4-byte Folded Reload
; RV32I-NEXT: lw t2, 48(sp) # 4-byte Folded Reload
; RV32I-NEXT: lw a0, 44(sp) # 4-byte Folded Reload
; RV32I-NEXT: lw a1, 40(sp) # 4-byte Folded Reload
; RV32I-NEXT: lw a2, 36(sp) # 4-byte Folded Reload
; RV32I-NEXT: lw a3, 32(sp) # 4-byte Folded Reload
; RV32I-NEXT: lw a4, 28(sp) # 4-byte Folded Reload
; RV32I-NEXT: lw a5, 24(sp) # 4-byte Folded Reload
; RV32I-NEXT: lw a6, 20(sp) # 4-byte Folded Reload
; RV32I-NEXT: lw a7, 16(sp) # 4-byte Folded Reload
; RV32I-NEXT: lw t3, 12(sp) # 4-byte Folded Reload
; RV32I-NEXT: lw t4, 8(sp) # 4-byte Folded Reload
; RV32I-NEXT: lw t5, 4(sp) # 4-byte Folded Reload
; RV32I-NEXT: lw t6, 0(sp) # 4-byte Folded Reload
; RV32I-NEXT: addi sp, sp, 64
; RV32I-NEXT: ret
;
; RV64I-LABEL: preserve_mostcc1:
; RV64I: # %bb.0: # %entry
; RV64I-NEXT: addi sp, sp, -128
; RV64I-NEXT: sd ra, 120(sp) # 8-byte Folded Spill
; RV64I-NEXT: sd t0, 112(sp) # 8-byte Folded Spill
; RV64I-NEXT: sd t1, 104(sp) # 8-byte Folded Spill
; RV64I-NEXT: sd t2, 96(sp) # 8-byte Folded Spill
; RV64I-NEXT: sd a0, 88(sp) # 8-byte Folded Spill
; RV64I-NEXT: sd a1, 80(sp) # 8-byte Folded Spill
; RV64I-NEXT: sd a2, 72(sp) # 8-byte Folded Spill
; RV64I-NEXT: sd a3, 64(sp) # 8-byte Folded Spill
; RV64I-NEXT: sd a4, 56(sp) # 8-byte Folded Spill
; RV64I-NEXT: sd a5, 48(sp) # 8-byte Folded Spill
; RV64I-NEXT: sd a6, 40(sp) # 8-byte Folded Spill
; RV64I-NEXT: sd a7, 32(sp) # 8-byte Folded Spill
; RV64I-NEXT: sd t3, 24(sp) # 8-byte Folded Spill
; RV64I-NEXT: sd t4, 16(sp) # 8-byte Folded Spill
; RV64I-NEXT: sd t5, 8(sp) # 8-byte Folded Spill
; RV64I-NEXT: sd t6, 0(sp) # 8-byte Folded Spill
; RV64I-NEXT: call standard_cc_func
; RV64I-NEXT: ld ra, 120(sp) # 8-byte Folded Reload
; RV64I-NEXT: ld t0, 112(sp) # 8-byte Folded Reload
; RV64I-NEXT: ld t1, 104(sp) # 8-byte Folded Reload
; RV64I-NEXT: ld t2, 96(sp) # 8-byte Folded Reload
; RV64I-NEXT: ld a0, 88(sp) # 8-byte Folded Reload
; RV64I-NEXT: ld a1, 80(sp) # 8-byte Folded Reload
; RV64I-NEXT: ld a2, 72(sp) # 8-byte Folded Reload
; RV64I-NEXT: ld a3, 64(sp) # 8-byte Folded Reload
; RV64I-NEXT: ld a4, 56(sp) # 8-byte Folded Reload
; RV64I-NEXT: ld a5, 48(sp) # 8-byte Folded Reload
; RV64I-NEXT: ld a6, 40(sp) # 8-byte Folded Reload
; RV64I-NEXT: ld a7, 32(sp) # 8-byte Folded Reload
; RV64I-NEXT: ld t3, 24(sp) # 8-byte Folded Reload
; RV64I-NEXT: ld t4, 16(sp) # 8-byte Folded Reload
; RV64I-NEXT: ld t5, 8(sp) # 8-byte Folded Reload
; RV64I-NEXT: ld t6, 0(sp) # 8-byte Folded Reload
; RV64I-NEXT: addi sp, sp, 128
; RV64I-NEXT: ret
;
; RV32E-LABEL: preserve_mostcc1:
; RV32E: # %bb.0: # %entry
; RV32E-NEXT: addi sp, sp, -40
; RV32E-NEXT: sw ra, 36(sp) # 4-byte Folded Spill
; RV32E-NEXT: sw t0, 32(sp) # 4-byte Folded Spill
; RV32E-NEXT: sw t1, 28(sp) # 4-byte Folded Spill
; RV32E-NEXT: sw t2, 24(sp) # 4-byte Folded Spill
; RV32E-NEXT: sw a0, 20(sp) # 4-byte Folded Spill
; RV32E-NEXT: sw a1, 16(sp) # 4-byte Folded Spill
; RV32E-NEXT: sw a2, 12(sp) # 4-byte Folded Spill
; RV32E-NEXT: sw a3, 8(sp) # 4-byte Folded Spill
; RV32E-NEXT: sw a4, 4(sp) # 4-byte Folded Spill
; RV32E-NEXT: sw a5, 0(sp) # 4-byte Folded Spill
; RV32E-NEXT: call standard_cc_func
; RV32E-NEXT: lw ra, 36(sp) # 4-byte Folded Reload
; RV32E-NEXT: lw t0, 32(sp) # 4-byte Folded Reload
; RV32E-NEXT: lw t1, 28(sp) # 4-byte Folded Reload
; RV32E-NEXT: lw t2, 24(sp) # 4-byte Folded Reload
; RV32E-NEXT: lw a0, 20(sp) # 4-byte Folded Reload
; RV32E-NEXT: lw a1, 16(sp) # 4-byte Folded Reload
; RV32E-NEXT: lw a2, 12(sp) # 4-byte Folded Reload
; RV32E-NEXT: lw a3, 8(sp) # 4-byte Folded Reload
; RV32E-NEXT: lw a4, 4(sp) # 4-byte Folded Reload
; RV32E-NEXT: lw a5, 0(sp) # 4-byte Folded Reload
; RV32E-NEXT: addi sp, sp, 40
; RV32E-NEXT: ret
;
; RV64E-LABEL: preserve_mostcc1:
; RV64E: # %bb.0: # %entry
; RV64E-NEXT: addi sp, sp, -80
; RV64E-NEXT: sd ra, 72(sp) # 8-byte Folded Spill
; RV64E-NEXT: sd t0, 64(sp) # 8-byte Folded Spill
; RV64E-NEXT: sd t1, 56(sp) # 8-byte Folded Spill
; RV64E-NEXT: sd t2, 48(sp) # 8-byte Folded Spill
; RV64E-NEXT: sd a0, 40(sp) # 8-byte Folded Spill
; RV64E-NEXT: sd a1, 32(sp) # 8-byte Folded Spill
; RV64E-NEXT: sd a2, 24(sp) # 8-byte Folded Spill
; RV64E-NEXT: sd a3, 16(sp) # 8-byte Folded Spill
; RV64E-NEXT: sd a4, 8(sp) # 8-byte Folded Spill
; RV64E-NEXT: sd a5, 0(sp) # 8-byte Folded Spill
; RV64E-NEXT: call standard_cc_func
; RV64E-NEXT: ld ra, 72(sp) # 8-byte Folded Reload
; RV64E-NEXT: ld t0, 64(sp) # 8-byte Folded Reload
; RV64E-NEXT: ld t1, 56(sp) # 8-byte Folded Reload
; RV64E-NEXT: ld t2, 48(sp) # 8-byte Folded Reload
; RV64E-NEXT: ld a0, 40(sp) # 8-byte Folded Reload
; RV64E-NEXT: ld a1, 32(sp) # 8-byte Folded Reload
; RV64E-NEXT: ld a2, 24(sp) # 8-byte Folded Reload
; RV64E-NEXT: ld a3, 16(sp) # 8-byte Folded Reload
; RV64E-NEXT: ld a4, 8(sp) # 8-byte Folded Reload
; RV64E-NEXT: ld a5, 0(sp) # 8-byte Folded Reload
; RV64E-NEXT: addi sp, sp, 80
; RV64E-NEXT: ret
entry:
call void @standard_cc_func()
ret void
}

define preserve_mostcc void @preserve_mostcc2() nounwind {
; RV32I-LABEL: preserve_mostcc2:
; RV32I: # %bb.0:
; RV32I-NEXT: addi sp, sp, -16
; RV32I-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
; RV32I-NEXT: call preserve_mostcc_func
; RV32I-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
; RV32I-NEXT: addi sp, sp, 16
; RV32I-NEXT: ret
;
; RV64I-LABEL: preserve_mostcc2:
; RV64I: # %bb.0:
; RV64I-NEXT: addi sp, sp, -16
; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
; RV64I-NEXT: call preserve_mostcc_func
; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
; RV64I-NEXT: addi sp, sp, 16
; RV64I-NEXT: ret
;
; RV32E-LABEL: preserve_mostcc2:
; RV32E: # %bb.0:
; RV32E-NEXT: addi sp, sp, -4
; RV32E-NEXT: sw ra, 0(sp) # 4-byte Folded Spill
; RV32E-NEXT: call preserve_mostcc_func
; RV32E-NEXT: lw ra, 0(sp) # 4-byte Folded Reload
; RV32E-NEXT: addi sp, sp, 4
; RV32E-NEXT: ret
;
; RV64E-LABEL: preserve_mostcc2:
; RV64E: # %bb.0:
; RV64E-NEXT: addi sp, sp, -8
; RV64E-NEXT: sd ra, 0(sp) # 8-byte Folded Spill
; RV64E-NEXT: call preserve_mostcc_func
; RV64E-NEXT: ld ra, 0(sp) # 8-byte Folded Reload
; RV64E-NEXT: addi sp, sp, 8
; RV64E-NEXT: ret
call preserve_mostcc void @preserve_mostcc_func()
ret void
}
Loading