diff --git a/llvm/lib/Target/RISCV/RISCVCallingConv.cpp b/llvm/lib/Target/RISCV/RISCVCallingConv.cpp index e0d1fb2facc87..cb6117eb0917b 100644 --- a/llvm/lib/Target/RISCV/RISCVCallingConv.cpp +++ b/llvm/lib/Target/RISCV/RISCVCallingConv.cpp @@ -333,15 +333,23 @@ bool llvm::CC_RISCV(unsigned ValNo, MVT ValVT, MVT LocVT, unsigned XLen = Subtarget.getXLen(); MVT XLenVT = Subtarget.getXLenVT(); - // Static chain parameter must not be passed in normal argument registers, - // so we assign t2/t3 for it as done in GCC's __builtin_call_with_static_chain - bool HasCFBranch = - Subtarget.hasStdExtZicfilp() && - MF.getFunction().getParent()->getModuleFlag("cf-protection-branch"); - // Normal: t2, Branch control flow protection: t3 - const auto StaticChainReg = HasCFBranch ? RISCV::X28 : RISCV::X7; - if (ArgFlags.isNest()) { + // Static chain parameter must not be passed in normal argument registers, + // so we assign t2/t3 for it as done in GCC's + // __builtin_call_with_static_chain + bool HasCFBranch = + Subtarget.hasStdExtZicfilp() && + MF.getFunction().getParent()->getModuleFlag("cf-protection-branch"); + + // Normal: t2, Branch control flow protection: t3 + const auto StaticChainReg = HasCFBranch ? RISCV::X28 : RISCV::X7; + + RISCVABI::ABI ABI = Subtarget.getTargetABI(); + if (HasCFBranch && + (ABI == RISCVABI::ABI_ILP32E || ABI == RISCVABI::ABI_LP64E)) + reportFatalUsageError( + "Nested functions with control flow protection are not " + "usable with ILP32E or LP64E ABI."); if (MCRegister Reg = State.AllocateReg(StaticChainReg)) { State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo)); return false; diff --git a/llvm/test/CodeGen/RISCV/nest-register.ll b/llvm/test/CodeGen/RISCV/nest-register.ll index 9f8e4e1a2d8d3..6e892e05c4297 100644 --- a/llvm/test/CodeGen/RISCV/nest-register.ll +++ b/llvm/test/CodeGen/RISCV/nest-register.ll @@ -5,6 +5,8 @@ ; RUN: | FileCheck -check-prefix=RV64I %s ; RUN: llc -mtriple=riscv64 -mattr=+experimental-zicfilp -verify-machineinstrs < %s \ ; RUN: | FileCheck -check-prefix=RV64I-ZICFILP %s +; RUN: not llc -mtriple=riscv64 -target-abi=lp64e -mattr=+experimental-zicfilp \ +; RUN: -verify-machineinstrs < %s 2>&1 | FileCheck -check-prefix=LP64E-ZICFILP %s ; Tests that the 'nest' parameter attribute causes the relevant parameter to be ; passed in the right register. @@ -63,6 +65,7 @@ define ptr @nest_caller(ptr %arg) nounwind { ret ptr %result } +; LP64E-ZICFILP: LLVM ERROR: Nested functions with control flow protection are not usable with ILP32E or LP64E ABI. !llvm.module.flags = !{!0} !0 = !{i32 8, !"cf-protection-branch", i32 1}