Skip to content

Commit

Permalink
[SYCL] Support __builtin_printf for SYCL device (#7483)
Browse files Browse the repository at this point in the history
  • Loading branch information
srividya-sundaram authored Nov 29, 2022
1 parent 7b47ebb commit d8fd9bc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
6 changes: 4 additions & 2 deletions clang/lib/Sema/SemaSYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,11 @@ bool Sema::isDeclAllowedInSYCLDeviceCode(const Decl *D) {
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(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.
Expand Down
18 changes: 18 additions & 0 deletions clang/test/CodeGenSYCL/remove-restriction-builtin-printf.cpp
Original file line number Diff line number Diff line change
@@ -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<class kernelA>([=]() {
// CHECK: printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef 24)
__builtin_printf("hello, %d\n", 24);
});
});
return 0;
}
19 changes: 19 additions & 0 deletions clang/test/SemaSYCL/remove-restriction-builtin-printf.cpp
Original file line number Diff line number Diff line change
@@ -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<class kernelA>([=]() {
__builtin_printf("hello, %d\n", 23);
});
});
return 0;
}

0 comments on commit d8fd9bc

Please sign in to comment.