Skip to content

Commit d704d3c

Browse files
[SYCL] Flip default printf implementation (#13055)
Currently, the implementation of printf uses a variadic implementation by default. This proved to be problematic as the implementation would promote float arguments to doubles, implicitly requring fp64. As a result of this, an alternative implementation was introduced, but was made enableable using a __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__ directive to avoid problems with targets that did not support it. We expect most relevant backends to support it now, so we flip the default. --------- Signed-off-by: Larsen, Steffen <[email protected]>
1 parent 43f0963 commit d704d3c

File tree

6 files changed

+37
-39
lines changed

6 files changed

+37
-39
lines changed

sycl/include/CL/__spirv/spirv_ops.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,18 +1183,18 @@ __clc_BarrierTestWait(int64_t *state, int64_t arrival) noexcept;
11831183
__SYCL_CONVERGENT__ extern __DPCPP_SYCL_EXTERNAL __SYCL_EXPORT void
11841184
__clc_BarrierArriveAndWait(int64_t *state) noexcept;
11851185

1186-
#ifdef __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
1186+
#ifdef __SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__
1187+
extern __DPCPP_SYCL_EXTERNAL int
1188+
__spirv_ocl_printf(const __attribute__((opencl_constant)) char *Format, ...);
1189+
extern __DPCPP_SYCL_EXTERNAL int __spirv_ocl_printf(const char *Format, ...);
1190+
#else
11871191
template <typename... Args>
11881192
extern __DPCPP_SYCL_EXTERNAL int
11891193
__spirv_ocl_printf(const __attribute__((opencl_constant)) char *Format,
11901194
Args... args);
11911195
template <typename... Args>
11921196
extern __DPCPP_SYCL_EXTERNAL int __spirv_ocl_printf(const char *Format,
11931197
Args... args);
1194-
#else
1195-
extern __DPCPP_SYCL_EXTERNAL int
1196-
__spirv_ocl_printf(const __attribute__((opencl_constant)) char *Format, ...);
1197-
extern __DPCPP_SYCL_EXTERNAL int __spirv_ocl_printf(const char *Format, ...);
11981198
#endif
11991199

12001200
// Native builtin extension

sycl/test-e2e/Basic/built-ins.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %{build} -o %t.out
22
// RUN: %{run} %t.out | FileCheck %s
33

4-
// RUN: %{build} -D__SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__ -o %t_nonvar.out
5-
// RUN: %{run} %t_nonvar.out | FileCheck %s
4+
// RUN: %{build} -D__SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__ -o %t_var.out
5+
// RUN: %{run} %t_var.out | FileCheck %s
66

77
// Hits an assertion with AMD:
88
// XFAIL: hip_amd
@@ -26,12 +26,11 @@ static const CONSTANT char format[] = "Hello, World! %d %f\n";
2626
int main() {
2727
s::queue q{};
2828

29-
#ifndef __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
29+
#ifdef __SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__
3030
if (!q.get_device().has(sycl::aspect::fp64)) {
31-
std::cout
32-
<< "Test without __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__ defined is "
33-
"skipped because the device did not have fp64."
34-
<< std::endl;
31+
std::cout << "Test with __SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__ defined is "
32+
"skipped because the device did not have fp64."
33+
<< std::endl;
3534
return 0;
3635
}
3736
#endif

sycl/test-e2e/DeviceLib/built-ins/printf.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// HIP doesn't support printf.
33
// CUDA doesn't support vector format specifiers ("%v").
44
//
5-
// RUN: %{build} -fsycl-device-code-split=per_kernel -o %t.out
5+
// RUN: %{build} -o %t.out
66
// RUN: %{run} %t.out | FileCheck %s
77
//
8-
// RUN: %{build} -D__SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__ -o %t_nonvar.out
9-
// RUN: %{run} %t_nonvar.out | FileCheck %s
8+
// RUN: %{build} -fsycl-device-code-split=per_kernel -D__SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__ -o %t_var.out
9+
// RUN: %{run} %t_var.out | FileCheck %s
1010

1111
#include <sycl/sycl.hpp>
1212

@@ -96,13 +96,13 @@ int main() {
9696
Queue.wait();
9797
}
9898

99-
#ifndef __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
99+
#ifdef __SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__
100100
// Currently printf will promote floating point values to doubles.
101-
// __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__ changes the behavior to not use
102-
// a variadic function, so if it is defined it will not promote the floating
101+
// __SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__ changes the behavior to use
102+
// a variadic function, so if it is defined it will promote the floating
103103
// point arguments.
104104
if (Queue.get_device().has(sycl::aspect::fp64))
105-
#endif // __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
105+
#endif // __SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__
106106
{
107107
Queue.submit([&](handler &CGH) {
108108
CGH.single_task<class floating_points>([=]() {
@@ -118,12 +118,12 @@ int main() {
118118
});
119119
Queue.wait();
120120
}
121-
#ifndef __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
121+
#ifdef __SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__
122122
else {
123123
std::cout << "Skipped floating point test." << std::endl;
124124
std::cout << "Skipped floating point test." << std::endl;
125125
}
126-
#endif // __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
126+
#endif // __SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__
127127
// CHECK-NEXT: {{(33.4|Skipped floating point test.)}}
128128
// CHECK-NEXT: {{(-33.4|Skipped floating point test.)}}
129129

sycl/test-e2e/ESIMD/printf.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
//===----------------------------------------------------------------------===//
88
//
99
//
10-
// RUN: %{build} -fsycl-device-code-split=per_kernel -o %t.out
10+
// RUN: %{build} -o %t.out
1111
// RUN: %{run} %t.out | FileCheck %s
1212
//
13-
// RUN: %{build} -D__SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__ -o %t_nonvar.out
14-
// RUN: %{run} %t_nonvar.out | FileCheck %s
13+
// RUN: %{build} -fsycl-device-code-split=per_kernel -D__SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__ -o %t_var.out
14+
// RUN: %{run} %t_var.out | FileCheck %s
1515
//
1616
//===----------------------------------------------------------------------===//
1717
//
@@ -70,13 +70,13 @@ int main() {
7070
Queue.wait();
7171
}
7272

73-
#ifndef __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
73+
#ifdef __SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__
7474
// Currently printf will promote floating point values to doubles.
75-
// __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__ changes the behavior to not use
76-
// a variadic function, so if it is defined it will not promote the floating
75+
// __SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__ changes the behavior to use
76+
// a variadic function, so if it is defined it will promote the floating
7777
// point arguments.
7878
if (Queue.get_device().has(sycl::aspect::fp64))
79-
#endif // __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
79+
#endif // __SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__
8080
{
8181
Queue.submit([&](handler &CGH) {
8282
CGH.single_task<class floating_points>([=]() {
@@ -92,12 +92,12 @@ int main() {
9292
});
9393
Queue.wait();
9494
}
95-
#ifndef __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
95+
#ifdef __SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__
9696
else {
9797
std::cout << "Skipped floating point test." << std::endl;
9898
std::cout << "Skipped floating point test." << std::endl;
9999
}
100-
#endif // __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
100+
#endif // __SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__
101101
// CHECK-NEXT: {{(33.4|Skipped floating point test.)}}
102102
// CHECK-NEXT: {{(-33.4|Skipped floating point test.)}}
103103

sycl/test-e2e/Printf/float.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
//
1010
// RUN: %{build} -o %t.out
1111
// RUN: %{run} %t.out | FileCheck %s
12-
// FIXME: Remove dedicated non-variadic printf testing once the headers
13-
// enforce it by default.
14-
// RUN: %{build} -o %t.nonvar.out -D__SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
12+
// FIXME: Remove dedicated variadic printf testing once the option is removed.
13+
// RUN: %{build} -o %t.nonvar.out -D__SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__
1514
// RUN: %{run} %t.nonvar.out | FileCheck %s
1615
// FIXME: Remove dedicated constant address space testing once generic AS
1716
// support is considered stable.
@@ -48,7 +47,7 @@ class FloatTest;
4847
int main() {
4948
queue q;
5049

51-
#ifndef __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
50+
#ifdef __SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__
5251
if (!q.get_device().has(aspect::fp64)) {
5352
std::cout << "Skipping the actual test due to variadic argument promotion. "
5453
"Printing hard-coded output from the host side:\n"
@@ -58,7 +57,7 @@ int main() {
5857
<< std::endl;
5958
return 0;
6059
}
61-
#endif // !__SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
60+
#endif // __SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__
6261
q.submit([](handler &cgh) {
6362
cgh.single_task<FloatTest>([]() { do_float_test(); });
6463
});

sycl/test/extensions/experimental-printf.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// This test is intended to check that internal
2-
// __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__ works as expected, i.e. we can
2+
// __SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__ works as expected, i.e. we can
33
// see printf ExtInst regardless of the macro presence and that argument
44
// promotion is disabled if the macro is present.
55
//
66
// RUN: %clangxx -fsycl -fsycl-device-only -fno-sycl-use-bitcode %s -o %t.spv
77
// RUN: llvm-spirv -to-text %t.spv -o %t.spt
8-
// RUN: FileCheck %s --check-prefixes CHECK,CHECK-DOUBLE < %t.spt
8+
// RUN: FileCheck %s --check-prefixes CHECK,CHECK-FLOAT < %t.spt
99
//
10-
// RUN: %clangxx -fsycl -fsycl-device-only -fno-sycl-use-bitcode -D__SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__ %s -o %t.spv
10+
// RUN: %clangxx -fsycl -fsycl-device-only -fno-sycl-use-bitcode -D__SYCL_USE_VARIADIC_SPIRV_OCL_PRINTF__ %s -o %t.spv
1111
// RUN: llvm-spirv -to-text %t.spv -o %t.spt
12-
// RUN: FileCheck %s --check-prefixes CHECK,CHECK-FLOAT < %t.spt
12+
// RUN: FileCheck %s --check-prefixes CHECK,CHECK-DOUBLE < %t.spt
1313

1414
// CHECK-FLOAT: TypeFloat [[#TYPE:]] 32
1515
// CHECK-DOUBLE: TypeFloat [[#TYPE:]] 64

0 commit comments

Comments
 (0)