diff --git a/clang/lib/Sema/SemaSYCL.cpp b/clang/lib/Sema/SemaSYCL.cpp index 6327e7015a12b..1a5ab2e5bfb20 100644 --- a/clang/lib/Sema/SemaSYCL.cpp +++ b/clang/lib/Sema/SemaSYCL.cpp @@ -368,9 +368,11 @@ bool Sema::isDeclAllowedInSYCLDeviceCode(const Decl *D) { if (const FunctionDecl *FD = dyn_cast(D)) { const IdentifierInfo *II = FD->getIdentifier(); - // Allow __builtin_assume_aligned to be called from within device code. + // Allow __builtin_assume_aligned and __builtin_printf to be called from + // within device code. if (FD->getBuiltinID() && - FD->getBuiltinID() == Builtin::BI__builtin_assume_aligned) + (FD->getBuiltinID() == Builtin::BI__builtin_assume_aligned || + FD->getBuiltinID() == Builtin::BI__builtin_printf)) return true; // Allow to use `::printf` only for CUDA. diff --git a/clang/test/CodeGenSYCL/remove-restriction-builtin-printf.cpp b/clang/test/CodeGenSYCL/remove-restriction-builtin-printf.cpp new file mode 100644 index 0000000000000..45afece4f7df1 --- /dev/null +++ b/clang/test/CodeGenSYCL/remove-restriction-builtin-printf.cpp @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 -fsycl-is-device -internal-isystem %S/Inputs -sycl-std=2020 -emit-llvm %s -o - | FileCheck %s +// This test checks if __builtin_printf is emitted in the IR. + +#include "sycl.hpp" + +using namespace sycl; +queue q; + +int main() { + q.submit([&](handler &h) { + // CHECK: define {{.*}}spir_kernel {{.*}} + h.single_task([=]() { + // CHECK: printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef 24) + __builtin_printf("hello, %d\n", 24); + }); + }); + return 0; +} diff --git a/clang/test/SemaSYCL/remove-restriction-builtin-printf.cpp b/clang/test/SemaSYCL/remove-restriction-builtin-printf.cpp new file mode 100644 index 0000000000000..24e0a4fd402fa --- /dev/null +++ b/clang/test/SemaSYCL/remove-restriction-builtin-printf.cpp @@ -0,0 +1,19 @@ +// RUN: %clang_cc1 -fsycl-is-device -internal-isystem %S/Inputs -sycl-std=2020 -verify -fsyntax-only %s +// This test checks if __builtin_printf does not throw an error when +// called from within device code. + +#include "sycl.hpp" + +using namespace sycl; +queue q; + +int main() { + // expected-no-diagnostics + q.submit([&](handler &h) { + h.single_task([=]() { + __builtin_printf("hello, %d\n", 23); + }); + }); + return 0; +} +