Skip to content

Commit f7bbcc6

Browse files
committed
[SYCL] Add NVPTX printf test and printf support in mock SYCL header
1 parent d4dffe4 commit f7bbcc6

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

clang/test/CodeGenSYCL/Inputs/sycl.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#define ATTR_SYCL_KERNEL __attribute__((sycl_kernel))
44

5+
extern "C" int printf(const char* fmt, ...);
6+
57
// Dummy runtime classes to model SYCL API.
68
inline namespace cl {
79
namespace sycl {
@@ -310,6 +312,21 @@ class spec_constant {
310312
return get();
311313
}
312314
};
315+
316+
#ifdef __SYCL_DEVICE_ONLY__
317+
#define __SYCL_CONSTANT_AS __attribute__((opencl_constant))
318+
#else
319+
#define __SYCL_CONSTANT_AS
320+
#endif
321+
template <typename... Args>
322+
int printf(const __SYCL_CONSTANT_AS char *__format, Args... args) {
323+
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
324+
return __spirv_ocl_printf(__format, args...);
325+
#else
326+
return ::printf(__format, args...);
327+
#endif // defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
328+
}
329+
313330
} // namespace experimental
314331
} // namespace oneapi
315332
} // namespace ext
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 -triple nvptx64-nvidia-cuda-sycldevice -std=c++11 -S -emit-llvm -x c++ %s -o - | FileCheck %s
2+
3+
#include "Inputs/sycl.hpp"
4+
5+
#ifdef __SYCL_DEVICE_ONLY__
6+
#define CONSTANT __attribute__((opencl_constant))
7+
#else
8+
#define CONSTANT
9+
#endif
10+
11+
static const CONSTANT char format_2[] = "Hello! %d %f\n";
12+
13+
int main() {
14+
// Make sure that device printf is dispatched to CUDA's vprintf syscall.
15+
// CHECK: alloca %printf_args
16+
// CHECK: call i32 @vprintf
17+
cl::sycl::kernel_single_task<class first_kernel>([]() { cl::sycl::ext::oneapi::experimental::printf(format_2, 123, 1.23); });
18+
}

0 commit comments

Comments
 (0)