Skip to content

Commit 80c7803

Browse files
Merge pull request #2766 from varungandhi-apple/vg-apple/stable
Add optional verification to check that tail calls from swifttail->swiftail are marked musttail.
2 parents b8fc4ea + f652e1b commit 80c7803

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

llvm/lib/IR/Verifier.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3316,7 +3316,24 @@ static AttrBuilder getParameterABIAttributes(int I, AttributeList Attrs) {
33163316
return Copy;
33173317
}
33183318

3319+
static cl::opt<bool>
3320+
EnableSwiftTailCCMustTailCheck("enable-swifttailcc-musttail-check",
3321+
cl::init(false), cl::desc("Check that tail calls from swifttailcc functions to"
3322+
" swifttailcc functions are marked musttail."));
3323+
33193324
void Verifier::verifyMustTailCall(CallInst &CI) {
3325+
if (!CI.isMustTailCall()) {
3326+
if (EnableSwiftTailCCMustTailCheck &&
3327+
CI.getCallingConv() == CallingConv::SwiftTail &&
3328+
CI.getCaller()->getCallingConv() == CallingConv::SwiftTail &&
3329+
isa_and_nonnull<ReturnInst>(CI.getNextNode())) {
3330+
Assert(
3331+
false, "tail call from swifttail->swiftail should be marked musttail",
3332+
&CI);
3333+
}
3334+
return;
3335+
}
3336+
33203337
Assert(!CI.isInlineAsm(), "cannot use musttail call with inline asm", &CI);
33213338

33223339
Function *F = CI.getParent()->getParent();
@@ -3401,9 +3418,7 @@ void Verifier::verifyMustTailCall(CallInst &CI) {
34013418

34023419
void Verifier::visitCallInst(CallInst &CI) {
34033420
visitCallBase(CI);
3404-
3405-
if (CI.isMustTailCall())
3406-
verifyMustTailCall(CI);
3421+
verifyMustTailCall(CI);
34073422
}
34083423

34093424
void Verifier::visitInvokeInst(InvokeInst &II) {

llvm/lib/Transforms/IPO/MergeFunctions.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,11 @@ void MergeFunctions::writeThunk(Function *F, Function *G) {
713713

714714
CallInst *CI = Builder.CreateCall(F, Args);
715715
ReturnInst *RI = nullptr;
716-
CI->setTailCall();
716+
bool isSwiftTailCall =
717+
F->getCallingConv() == CallingConv::SwiftTail &&
718+
G->getCallingConv() == CallingConv::SwiftTail;
719+
CI->setTailCallKind(
720+
isSwiftTailCall ? llvm::CallInst::TCK_MustTail : llvm::CallInst::TCK_Tail);
717721
CI->setCallingConv(F->getCallingConv());
718722
CI->setAttributes(F->getAttributes());
719723
if (H->getReturnType()->isVoidTy()) {

0 commit comments

Comments
 (0)