Skip to content

Commit d8fd9bc

Browse files
[SYCL] Support __builtin_printf for SYCL device (#7483)
1 parent 7b47ebb commit d8fd9bc

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,11 @@ bool Sema::isDeclAllowedInSYCLDeviceCode(const Decl *D) {
368368
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
369369
const IdentifierInfo *II = FD->getIdentifier();
370370

371-
// Allow __builtin_assume_aligned to be called from within device code.
371+
// Allow __builtin_assume_aligned and __builtin_printf to be called from
372+
// within device code.
372373
if (FD->getBuiltinID() &&
373-
FD->getBuiltinID() == Builtin::BI__builtin_assume_aligned)
374+
(FD->getBuiltinID() == Builtin::BI__builtin_assume_aligned ||
375+
FD->getBuiltinID() == Builtin::BI__builtin_printf))
374376
return true;
375377

376378
// Allow to use `::printf` only for CUDA.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %clang_cc1 -fsycl-is-device -internal-isystem %S/Inputs -sycl-std=2020 -emit-llvm %s -o - | FileCheck %s
2+
// This test checks if __builtin_printf is emitted in the IR.
3+
4+
#include "sycl.hpp"
5+
6+
using namespace sycl;
7+
queue q;
8+
9+
int main() {
10+
q.submit([&](handler &h) {
11+
// CHECK: define {{.*}}spir_kernel {{.*}}
12+
h.single_task<class kernelA>([=]() {
13+
// CHECK: printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef 24)
14+
__builtin_printf("hello, %d\n", 24);
15+
});
16+
});
17+
return 0;
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clang_cc1 -fsycl-is-device -internal-isystem %S/Inputs -sycl-std=2020 -verify -fsyntax-only %s
2+
// This test checks if __builtin_printf does not throw an error when
3+
// called from within device code.
4+
5+
#include "sycl.hpp"
6+
7+
using namespace sycl;
8+
queue q;
9+
10+
int main() {
11+
// expected-no-diagnostics
12+
q.submit([&](handler &h) {
13+
h.single_task<class kernelA>([=]() {
14+
__builtin_printf("hello, %d\n", 23);
15+
});
16+
});
17+
return 0;
18+
}
19+

0 commit comments

Comments
 (0)