Skip to content

Correctly handle method calls and function pointers for swiftasync. #2747

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

Merged
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
1 change: 1 addition & 0 deletions clang/lib/AST/ExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
return cast<CXXMethodDecl>(MemExpr->getMemberDecl());

// FIXME: Will eventually need to cope with member pointers.
// NOTE: Update makeTailCallIfSwiftAsync on fixing this.
return nullptr;
}

Expand Down
21 changes: 18 additions & 3 deletions clang/lib/CodeGen/CGStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1149,10 +1149,25 @@ struct SaveRetExprRAII {
/// codegen it as 'tail call ...; ret void;'.
static void makeTailCallIfSwiftAsync(const CallExpr *CE, CGBuilderTy &Builder,
const CGFunctionInfo *CurFnInfo) {
auto callee = CE->getDirectCallee();
if (!callee)
auto calleeQualType = CE->getCallee()->getType();
const FunctionType *calleeType = nullptr;
if (calleeQualType->isFunctionPointerType() ||
calleeQualType->isFunctionReferenceType() ||
calleeQualType->isBlockPointerType() ||
calleeQualType->isMemberFunctionPointerType()) {
calleeType = calleeQualType->getPointeeType()->castAs<FunctionType>();
} else if (auto *ty = dyn_cast<FunctionType>(calleeQualType)) {
calleeType = ty;
} else if (auto CMCE = dyn_cast<CXXMemberCallExpr>(CE)) {
if (auto methodDecl = CMCE->getMethodDecl()) {
// getMethodDecl() doesn't handle member pointers at the moment.
calleeType = methodDecl->getType()->castAs<FunctionType>();
} else {
return;
}
} else {
return;
auto calleeType = callee->getFunctionType();
}
if (calleeType->getCallConv() == CallingConv::CC_SwiftAsync
&& (CurFnInfo->getASTCallingConvention() ==
CallingConv::CC_SwiftAsync)) {
Expand Down
56 changes: 51 additions & 5 deletions clang/test/CodeGen/swift-async-call-conv.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
// RUN: %clang_cc1 -triple armv7s-apple-ios9 -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -triple armv7k-apple-ios9 -emit-llvm -o - %s | FileCheck %s

// RUN: %clang_cc1 -x c++ -triple x86_64-apple-darwin10 -target-cpu core2 -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -x c++ -triple arm64-apple-ios9 -target-cpu cyclone -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -x c++ -triple armv7-apple-darwin9 -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -x c++ -triple armv7s-apple-ios9 -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -x c++ -triple armv7k-apple-ios9 -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -x c++ -triple x86_64-apple-darwin10 -target-cpu core2 -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CPPONLY
// RUN: %clang_cc1 -x c++ -triple arm64-apple-ios9 -target-cpu cyclone -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CPPONLY
// RUN: %clang_cc1 -x c++ -triple armv7-apple-darwin9 -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CPPONLY
// RUN: %clang_cc1 -x c++ -triple armv7s-apple-ios9 -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CPPONLY
// RUN: %clang_cc1 -x c++ -triple armv7k-apple-ios9 -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CPPONLY

// Test tail call behavior when a swiftasynccall function is called
// from another swiftasynccall function.
Expand Down Expand Up @@ -136,3 +136,49 @@ char c_calling_async(MYBOOL b, unsigned u) {
return x;
}

#if __cplusplus
struct S {
SWIFTASYNCCALL void (*fptr)(char * ASYNC_CONTEXT);

SWIFTASYNCCALL void async_leaf_method(char * ASYNC_CONTEXT ctx) {
*ctx += 1;
}
SWIFTASYNCCALL void async_nonleaf_method1(char * ASYNC_CONTEXT ctx) {
return async_leaf_method(ctx);
}
SWIFTASYNCCALL void async_nonleaf_method2(char * ASYNC_CONTEXT ctx) {
return this->async_leaf_method(ctx);
}
};

SWIFTASYNCCALL void (S::*async_leaf_method_ptr)(char * ASYNC_CONTEXT) = &S::async_leaf_method;

// CPPONLY-LABEL: swifttailcc void {{.*}}async_struct_field_and_methods
// CPPONLY: musttail call swifttailcc void %{{[0-9]+}}
// CPPONLY: musttail call swifttailcc void @{{.*}}async_nonleaf_method1
// CPPONLY: musttail call swifttailcc void %{{[0-9]+}}
// CPPONLY: musttail call swifttailcc void @{{.*}}async_nonleaf_method2
// CPPONLY-NOT: musttail call swifttailcc void @{{.*}}async_leaf_method
// ^ TODO: Member pointers should also work.
SWIFTASYNCCALL void async_struct_field_and_methods(int i, S &sref, S *sptr) {
char x = 'a';
if (i == 0) {
return (*sref.fptr)(&x);
} else if (i == 1) {
return sref.async_nonleaf_method1(&x);
} else if (i == 2) {
return (*(sptr->fptr))(&x);
} else if (i == 3) {
return sptr->async_nonleaf_method2(&x);
} else if (i == 4) {
return (sref.*async_leaf_method_ptr)(&x);
}
return (sptr->*async_leaf_method_ptr)(&x);
}

// CPPONLY-LABEL: define{{.*}} swifttailcc void @{{.*}}async_nonleaf_method1
// CPPONLY: musttail call swifttailcc void @{{.*}}async_leaf_method

// CPPONLY-LABEL: define{{.*}} swifttailcc void @{{.*}}async_nonleaf_method2
// CPPONLY: musttail call swifttailcc void @{{.*}}async_leaf_method
#endif