Skip to content

[SYCL] Allow __spirv_ocl_printf vararg function #909

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
merged 4 commits into from
Dec 9, 2019
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
19 changes: 18 additions & 1 deletion clang/lib/Sema/SemaSYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ static bool IsSyclMathFunc(unsigned BuiltinID) {
return true;
}

static bool isKnownGoodDecl(const Decl *D) {
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
const IdentifierInfo *II = FD->getIdentifier();
const DeclContext *DC = FD->getDeclContext();
if (II && II->isStr("__spirv_ocl_printf") &&
!FD->isDefined() &&
FD->getLanguageLinkage() == CXXLanguageLinkage &&
DC->getEnclosingNamespaceContext()->isTranslationUnit())
return true;
}
return false;
}

class MarkDeviceFunction : public RecursiveASTVisitor<MarkDeviceFunction> {
public:
MarkDeviceFunction(Sema &S)
Expand Down Expand Up @@ -312,8 +325,12 @@ class MarkDeviceFunction : public RecursiveASTVisitor<MarkDeviceFunction> {
}

bool VisitDeclRefExpr(DeclRefExpr *E) {
Decl* D = E->getDecl();
if (isKnownGoodDecl(D))
return true;

CheckSYCLType(E->getType(), E->getSourceRange());
if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
bool IsConst = VD->getType().getNonReferenceType().isConstQualified();
if (!IsConst && VD->isStaticDataMember())
SemaRef.Diag(E->getExprLoc(), diag::err_sycl_restrict)
Expand Down
41 changes: 41 additions & 0 deletions clang/test/SemaSYCL/sycl-varargs-cconv.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
// RUN: %clang_cc1 -fsycl-is-device -verify -fsyntax-only -x c++ %s
// RUN: %clang_cc1 -fsycl-is-device -verify -fsyntax-only -DPRINTF_INVALID_DEF -x c++ %s
// RUN: %clang_cc1 -fsycl-is-device -verify -fsyntax-only -DPRINTF_INVALID_DECL -x c++ %s
// RUN: %clang_cc1 -fsycl-is-device -verify -fsyntax-only -DPRINTF_VALID1 -x c++ %s
// RUN: %clang_cc1 -fsycl-is-device -verify -fsyntax-only -DPRINTF_VALID2 -x c++ %s

#if defined(PRINTF_INVALID_DECL)
extern "C" int __spirv_ocl_printf(const char *__format, ...);
namespace A {
int __spirv_ocl_printf(const char *__format, ...);
}
#elif defined(PRINTF_INVALID_DEF)
int __spirv_ocl_printf(const char *__format, ...) {
return 42;
}
#elif defined(PRINTF_VALID1)
class A {
friend int __spirv_ocl_printf(const char *__format, ...);
};
int __spirv_ocl_printf(const char *__format, ...);
#elif defined(PRINTF_VALID2)
extern "C" {
extern "C++" {
int __spirv_ocl_printf(const char *__format, ...);
}
}
#else
int __spirv_ocl_printf(const char *__format, ...);
#endif

int __cdecl foo(int, ...); // expected-no-error

Expand All @@ -19,6 +47,19 @@ int main() {
kernel_single_task<class fake_kernel>([]() { foo(6); });
//expected-error@+1 {{SYCL kernel cannot call a variadic function}}
kernel_single_task<class fake_kernel>([]() { bar(9.0); });

#if defined(PRINTF_INVALID_DECL)
//expected-error@+1 {{SYCL kernel cannot call a variadic function}}
kernel_single_task<class fake_kernel>([]() { A::__spirv_ocl_printf("Hello world! %d%d\n", 4, 2); });
//expected-error@+1 {{SYCL kernel cannot call a variadic function}}
kernel_single_task<class fake_kernel>([]() { __spirv_ocl_printf("Hello world! %d%d\n", 4, 2); });
#elif defined(PRINTF_INVALID_DEF)
//expected-error@+1 {{SYCL kernel cannot call a variadic function}}
kernel_single_task<class fake_kernel>([]() { __spirv_ocl_printf("Hello world! %d%d\n", 4, 2); });
#else
kernel_single_task<class fake_kernel>([]() { __spirv_ocl_printf("Hello world! %d%d\n", 4, 2); });
#endif

bar();
return 0;
}