Skip to content

Commit 8063330

Browse files
authored
[RISCV] Guard the alternative static chain register use on ILP32E/LP64E (#142715)
Asserts the use of t3(x28) as the static chain register when branch control flow protection is enabled with ILP32E/LP64E, because such register is not present within the ABI.
1 parent 42c82fc commit 8063330

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

llvm/lib/Target/RISCV/RISCVCallingConv.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,23 @@ bool llvm::CC_RISCV(unsigned ValNo, MVT ValVT, MVT LocVT,
333333
unsigned XLen = Subtarget.getXLen();
334334
MVT XLenVT = Subtarget.getXLenVT();
335335

336-
// Static chain parameter must not be passed in normal argument registers,
337-
// so we assign t2/t3 for it as done in GCC's __builtin_call_with_static_chain
338-
bool HasCFBranch =
339-
Subtarget.hasStdExtZicfilp() &&
340-
MF.getFunction().getParent()->getModuleFlag("cf-protection-branch");
341-
// Normal: t2, Branch control flow protection: t3
342-
const auto StaticChainReg = HasCFBranch ? RISCV::X28 : RISCV::X7;
343-
344336
if (ArgFlags.isNest()) {
337+
// Static chain parameter must not be passed in normal argument registers,
338+
// so we assign t2/t3 for it as done in GCC's
339+
// __builtin_call_with_static_chain
340+
bool HasCFBranch =
341+
Subtarget.hasStdExtZicfilp() &&
342+
MF.getFunction().getParent()->getModuleFlag("cf-protection-branch");
343+
344+
// Normal: t2, Branch control flow protection: t3
345+
const auto StaticChainReg = HasCFBranch ? RISCV::X28 : RISCV::X7;
346+
347+
RISCVABI::ABI ABI = Subtarget.getTargetABI();
348+
if (HasCFBranch &&
349+
(ABI == RISCVABI::ABI_ILP32E || ABI == RISCVABI::ABI_LP64E))
350+
reportFatalUsageError(
351+
"Nested functions with control flow protection are not "
352+
"usable with ILP32E or LP64E ABI.");
345353
if (MCRegister Reg = State.AllocateReg(StaticChainReg)) {
346354
State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
347355
return false;

llvm/test/CodeGen/RISCV/nest-register.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
; RUN: | FileCheck -check-prefix=RV64I %s
66
; RUN: llc -mtriple=riscv64 -mattr=+experimental-zicfilp -verify-machineinstrs < %s \
77
; RUN: | FileCheck -check-prefix=RV64I-ZICFILP %s
8+
; RUN: not llc -mtriple=riscv64 -target-abi=lp64e -mattr=+experimental-zicfilp \
9+
; RUN: -verify-machineinstrs < %s 2>&1 | FileCheck -check-prefix=LP64E-ZICFILP %s
810

911
; Tests that the 'nest' parameter attribute causes the relevant parameter to be
1012
; passed in the right register.
@@ -63,6 +65,7 @@ define ptr @nest_caller(ptr %arg) nounwind {
6365
ret ptr %result
6466
}
6567

68+
; LP64E-ZICFILP: LLVM ERROR: Nested functions with control flow protection are not usable with ILP32E or LP64E ABI.
6669
!llvm.module.flags = !{!0}
6770

6871
!0 = !{i32 8, !"cf-protection-branch", i32 1}

0 commit comments

Comments
 (0)