Skip to content

[SYCL] Support __builtin_printf for SYCL device #7483

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 5 commits into from
Nov 29, 2022
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
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;
}