Skip to content

Commit 0c55d3a

Browse files
authored
[SYCL] Allow ::printf only for CUDA (#4978)
`printf` built-in as-is won't be properly converted into SPIR-V and will cause JIT compilation errors in OpenCL backends.
1 parent 0c97794 commit 0c55d3a

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,11 @@ static bool IsSyclMathFunc(unsigned BuiltinID) {
410410
bool Sema::isKnownGoodSYCLDecl(const Decl *D) {
411411
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
412412
const IdentifierInfo *II = FD->getIdentifier();
413-
if (FD->getBuiltinID() == Builtin::BIprintf)
414-
return true;
413+
// Allow to use `::printf` only for CUDA.
414+
if (Context.getTargetInfo().getTriple().isNVPTX()) {
415+
if (FD->getBuiltinID() == Builtin::BIprintf)
416+
return true;
417+
}
415418
const DeclContext *DC = FD->getDeclContext();
416419
if (II && II->isStr("__spirv_ocl_printf") &&
417420
!FD->isDefined() &&

clang/test/SemaSYCL/sycl-varargs-cconv.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ __attribute__((sycl_kernel)) void kernel_single_task(const Func &kernelFunc) {
4747
kernelFunc(); //expected-note 2+ {{called by 'kernel_single_task}}
4848
}
4949

50+
extern "C" int printf(const char *fmt, ...);
51+
5052
int main() {
5153
//expected-error@+1 {{SYCL kernel cannot call a variadic function}}
5254
kernel_single_task<class fake_kernel>([]() { foo(6); });
@@ -58,6 +60,10 @@ int main() {
5860
kernel_single_task<class fake_kernel>([]() { A::__spirv_ocl_printf("Hello world! %d%d\n", 4, 2); });
5961
//expected-error@+1 {{SYCL kernel cannot call a variadic function}}
6062
kernel_single_task<class fake_kernel>([]() { __spirv_ocl_printf("Hello world! %d%d\n", 4, 2); });
63+
64+
// Check that default printf is not allowed.
65+
//expected-error@+1 {{SYCL kernel cannot call a variadic function}}
66+
kernel_single_task<class fake_kernel>([]() { printf("Hello world! %d%d\n", 4, 2); });
6167
#elif defined(PRINTF_INVALID_DEF)
6268
//expected-error@+1 {{SYCL kernel cannot call a variadic function}}
6369
kernel_single_task<class fake_kernel>([]() { __spirv_ocl_printf("Hello world! %d%d\n", 4, 2); });

0 commit comments

Comments
 (0)