Skip to content
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
3 changes: 2 additions & 1 deletion llvm/docs/LangRef.md
Original file line number Diff line number Diff line change
Expand Up @@ -2822,7 +2822,8 @@ fn -> other_fn -> other_fn ; fn is norecurse
or ZA/ZT0 protocols, Windows,
and other calling conventions are diagnosed. XRay entry/exit instrumentation
is unsupported because it can run after clearing; an explicit `xray-never`
opt-out remains supported. Ordinary tail calls are disabled for this request.
opt-out remains supported. Ordinary tail calls are disabled for this request;
mandatory tail calls are diagnosed.
Exceptional exits and shadow call stacks are diagnosed except for the
fixed-frame cleanup and Android shadow-stack configurations below.

Expand Down
17 changes: 14 additions & 3 deletions llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10366,9 +10366,20 @@ AArch64TargetLowering::LowerCall(CallLoweringInfo &CLI,
++NumTailCalls;
}

if (!IsTailCall && CLI.CB && CLI.CB->isMustTailCall())
report_fatal_error("failed to perform tail call elimination on a call "
"site marked musttail");
if (!IsTailCall && CLI.CB && CLI.CB->isMustTailCall()) {
// A function that clears its stack or flags returns through its own
// epilogue, so it has no tail calls. Report the conflict and lower an
// ordinary call.
// TODO: Profile what giving up tail calls costs these functions: each
// such call keeps a frame and adds a return.
const Function &CallerF = MF.getFunction();
if (!CallerF.hasZeroizeStack() && !CallerF.hasFnAttribute("zeroize-flags"))
report_fatal_error("failed to perform tail call elimination on a call "
"site marked musttail");
DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
CallerF, "AArch64 stack or flags clearing does not support musttail",
DL.getDebugLoc()));
}

// Get a count of how many bytes are to be pushed on the stack.
unsigned NumBytes = CCInfo.getStackSize();
Expand Down
13 changes: 12 additions & 1 deletion llvm/lib/Target/AArch64/GISel/AArch64CallLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "llvm/CodeGenTypes/MachineValueType.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
Expand Down Expand Up @@ -1370,7 +1371,17 @@ bool AArch64CallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
isEligibleForTailCallOptimization(MIRBuilder, Info, InArgs, OutArgs);

// We must emit a tail call if we have musttail.
if (Info.IsMustTailCall && !CanTailCallOpt) {
if (Info.IsMustTailCall && !CanTailCallOpt &&
(F.hasZeroizeStack() || F.hasFnAttribute("zeroize-flags"))) {
// A function that clears its stack or flags returns through its own
// epilogue, so it has no tail calls. Report the conflict and lower an
// ordinary call.
// TODO: Profile what giving up tail calls costs these functions: each
// such call keeps a frame and adds a return.
F.getContext().diagnose(DiagnosticInfoUnsupported(
F, "AArch64 stack or flags clearing does not support musttail",
MIRBuilder.getDL()));
} else if (Info.IsMustTailCall && !CanTailCallOpt) {
// There are types of incoming/outgoing arguments we can't handle yet, so
// it doesn't make sense to actually die here like in ISelLowering. Instead,
// fall back to SelectionDAG and let it try to handle this.
Expand Down
17 changes: 14 additions & 3 deletions llvm/lib/Target/ARM/ARMISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2094,9 +2094,20 @@ ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
++NumTailCalls;
}

if (!isTailCall && CLI.CB && CLI.CB->isMustTailCall())
report_fatal_error("failed to perform tail call elimination on a call "
"site marked musttail");
if (!isTailCall && CLI.CB && CLI.CB->isMustTailCall()) {
// A function that clears its stack or flags returns through its own
// epilogue, so it has no tail calls. Report the conflict and lower an
// ordinary call.
// TODO: Profile what giving up tail calls costs these functions: each
// such call keeps a frame and adds a return.
const Function &CallerF = MF.getFunction();
if (!CallerF.hasZeroizeStack() && !CallerF.hasFnAttribute("zeroize-flags"))
report_fatal_error("failed to perform tail call elimination on a call "
"site marked musttail");
DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
CallerF, "ARM stack or flags clearing does not support musttail",
dl.getDebugLoc()));
}

// Get a count of how many bytes are to be pushed on the stack.
unsigned NumBytes = CCInfo.getStackSize();
Expand Down
18 changes: 18 additions & 0 deletions llvm/test/CodeGen/AArch64/zeroize-flags-musttail.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; RUN: not llc -mtriple=aarch64-unknown-linux-gnu %s -o /dev/null 2>&1 | FileCheck %s
; RUN: not llc -mtriple=aarch64-unknown-linux-gnu -O0 %s -o /dev/null 2>&1 | FileCheck %s
; RUN: not llc -mtriple=aarch64-unknown-linux-gnu -fast-isel -O0 %s -o /dev/null 2>&1 | FileCheck %s
; RUN: not llc -mtriple=aarch64-unknown-linux-gnu -global-isel -global-isel-abort=1 %s -o /dev/null 2>&1 | FileCheck %s

; A function that clears its flags returns through its own epilogue, so it
; cannot make the tail call that musttail requires. Every instruction selector
; reports that once and does not reach the generic fatal error.

declare i64 @callee(i64)

; CHECK-NOT: LLVM ERROR
; CHECK: error: {{.*}}in function flags_musttail {{.*}}AArch64 stack or flags clearing does not support musttail
; CHECK-NOT: error:
define i64 @flags_musttail(i64 %x) "zeroize-flags" {
%r = musttail call i64 @callee(i64 %x)
ret i64 %r
}
8 changes: 6 additions & 2 deletions llvm/test/CodeGen/ARM/zeroize-flags-protocols.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
; RUN: not llc -mtriple=armv7-none-eabi %t/interrupt.ll -o /dev/null 2>&1 | FileCheck %s
; RUN: not llc -mtriple=armv7-none-eabi %t/cleanup.ll -o /dev/null 2>&1 | FileCheck %s
; RUN: not llc -mtriple=thumbv7-none-eabi -mattr=-dsp %t/ordinary.ll -o /dev/null 2>&1 | FileCheck %s
; RUN: not --crash llc -mtriple=armv7-none-eabi %t/musttail.ll -o /dev/null 2>&1 | FileCheck %s --check-prefix=TAIL
; RUN: not llc -mtriple=armv7-none-eabi %t/musttail.ll -o /dev/null 2>&1 | FileCheck %s --check-prefix=TAIL
; RUN: not llc -mtriple=armv7-none-eabi -O0 %t/musttail.ll -o /dev/null 2>&1 | FileCheck %s --check-prefix=TAIL
; RUN: not llc -mtriple=thumbv7-none-eabi %t/musttail.ll -o /dev/null 2>&1 | FileCheck %s --check-prefix=TAIL
; RUN: not llc -mtriple=thumbv6m-none-eabi %t/musttail.ll -o /dev/null 2>&1 | FileCheck %s --check-prefix=TAIL
; CHECK: error: {{.*}}"zeroize-flags" is not supported by this target or exit protocol
; TAIL: failed to perform tail call elimination on a call site marked musttail
; TAIL-NOT: LLVM ERROR
; TAIL: error: {{.*}}in function mandatory {{.*}}ARM stack or flags clearing does not support musttail

;--- interrupt.ll
define arm_aapcscc void @irq() "interrupt"="IRQ" "zeroize-flags" { ret void }
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/X86/zeroize-flags-musttail.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare i64 @callee(i64)

; CHECK: error: {{.*}}X86 stack or flags clearing does not support musttail
; CHECK: error: {{.*}}in function flags_musttail {{.*}}X86 stack or flags clearing does not support musttail
define i64 @flags_musttail(i64 %x) "zeroize-flags" {
%r = musttail call i64 @callee(i64 %x)
ret i64 %r
Expand Down