Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit f859d8c

Browse files
committed
[SYCL] Test printf with FP64 types only when available on target HW
In addition to placing the `double` tests under `aspect::fp64` checks, this commit accounts for the case where the compiler performs variadic argument promotion for the `float` type. Signed-off-by: Artem Gindinson <[email protected]>
1 parent 5f4d951 commit f859d8c

File tree

2 files changed

+116
-48
lines changed

2 files changed

+116
-48
lines changed

SYCL/Printf/double.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// This test is written with an aim to check that experimental::printf behaves
2+
// in the same way as printf from C99/C11
3+
//
4+
// The test is written using conversion specifiers table from cppreference [1]
5+
// [1]: https://en.cppreference.com/w/cpp/io/c/fprintf
6+
//
7+
// UNSUPPORTED: hip_amd
8+
//
9+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
10+
// RUN: %CPU_RUN_PLACEHOLDER %t.out %CPU_CHECK_PLACEHOLDER
11+
// RUN: %GPU_RUN_PLACEHOLDER %t.out %GPU_CHECK_PLACEHOLDER
12+
// RUN: %ACC_RUN_PLACEHOLDER %t.out %ACC_CHECK_PLACEHOLDER
13+
// FIXME: Remove dedicated constant address space testing once generic AS
14+
// support is considered stable.
15+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.constant.out \
16+
// RUN: -DTEST_CONSTANT_AS
17+
// RUN: %CPU_RUN_PLACEHOLDER %t.constant.out %CPU_CHECK_PLACEHOLDER
18+
// RUN: %GPU_RUN_PLACEHOLDER %t.constant.out %GPU_CHECK_PLACEHOLDER
19+
// RUN: %ACC_RUN_PLACEHOLDER %t.constant.out %ACC_CHECK_PLACEHOLDER
20+
//
21+
// CHECK: double -6.813800e+00, -6.813800E+00
22+
// CHECK: mixed 3.140000e+00, -6.813800E+00
23+
// CHECK: double -0x1.b4154d8cccccdp+2, -0X1.B4154D8CCCCCDP+2
24+
// CHECK: mixed 0x1.91eb86{{0*}}p+1, -0X1.B4154D8CCCCCDP+2
25+
// CHECK: double -6.8138, -6.8138
26+
// CHECK: mixed 3.14, -6.8138
27+
28+
#include <iostream>
29+
30+
#include <CL/sycl.hpp>
31+
32+
#include "helper.hpp"
33+
34+
using namespace sycl;
35+
36+
void do_double_test() {
37+
float f = 3.14;
38+
double d = -f * 2.17;
39+
40+
{
41+
// %e, %E floating-point, decimal exponent notation
42+
FORMAT_STRING(fmt_double) = "double %e, %E\n";
43+
FORMAT_STRING(fmt_mixed) = "mixed %e, %E\n";
44+
ext::oneapi::experimental::printf(fmt_double, d, d);
45+
ext::oneapi::experimental::printf(fmt_mixed, f, d);
46+
}
47+
48+
{
49+
// %a, %A floating-point, hexadecimal exponent notation
50+
FORMAT_STRING(fmt_double) = "double %a, %A\n";
51+
FORMAT_STRING(fmt_mixed) = "mixed %a, %A\n";
52+
ext::oneapi::experimental::printf(fmt_double, d, d);
53+
ext::oneapi::experimental::printf(fmt_mixed, f, d);
54+
}
55+
56+
{
57+
// %g, %G floating-point
58+
FORMAT_STRING(fmt_double) = "double %g, %G\n";
59+
FORMAT_STRING(fmt_mixed) = "mixed %g, %G\n";
60+
ext::oneapi::experimental::printf(fmt_double, d, d);
61+
ext::oneapi::experimental::printf(fmt_mixed, f, d);
62+
}
63+
}
64+
65+
class DoubleTest;
66+
67+
int main() {
68+
queue q;
69+
70+
if (q.get_device().has(aspect::fp64)) {
71+
q.submit([](handler &cgh) {
72+
cgh.single_task<DoubleTest>([]() { do_double_test(); });
73+
});
74+
q.wait();
75+
} else
76+
std::cout << "Skipping the actual test. "
77+
"Printing hard-coded output from the host side:\n"
78+
<< "double -6.813800e+00, -6.813800E+00\n"
79+
"mixed 3.140000e+00, -6.813800E+00\n"
80+
"double -0x1.b4154d8cccccdp+2, -0X1.B4154D8CCCCCDP+2\n"
81+
"mixed 0x1.91eb86p+1, -0X1.B4154D8CCCCCDP+2\n"
82+
"double -6.8138, -6.8138\n"
83+
"mixed 3.14, -6.8138\n";
84+
return 0;
85+
}

SYCL/Printf/float.cpp

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
// RUN: %CPU_RUN_PLACEHOLDER %t.out %CPU_CHECK_PLACEHOLDER
1111
// RUN: %GPU_RUN_PLACEHOLDER %t.out %GPU_CHECK_PLACEHOLDER
1212
// RUN: %ACC_RUN_PLACEHOLDER %t.out %ACC_CHECK_PLACEHOLDER
13+
// FIXME: Remove dedicated non-variadic printf testing once the headers
14+
// enforce it by default.
15+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.nonvar.out \
16+
// RUN: -D__SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
17+
// RUN: %CPU_RUN_PLACEHOLDER %t.nonvar.out %CPU_CHECK_PLACEHOLDER
18+
// RUN: %GPU_RUN_PLACEHOLDER %t.nonvar.out %GPU_CHECK_PLACEHOLDER
19+
// RUN: %ACC_RUN_PLACEHOLDER %t.nonvar.out %ACC_CHECK_PLACEHOLDER
1320
// FIXME: Remove dedicated constant address space testing once generic AS
1421
// support is considered stable.
1522
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.constant.out \
@@ -18,15 +25,11 @@
1825
// RUN: %GPU_RUN_PLACEHOLDER %t.constant.out %GPU_CHECK_PLACEHOLDER
1926
// RUN: %ACC_RUN_PLACEHOLDER %t.constant.out %ACC_CHECK_PLACEHOLDER
2027
//
21-
// CHECK: float 3.140000e+00, 3.140000E+00
22-
// CHECK: double -6.813800e+00, -6.813800E+00
23-
// CHECK: mixed 3.140000e+00, -6.813800E+00
24-
// CHECK: float 0x1.91eb86{{0*}}p+1, 0X1.91EB86{{0*}}P+1
25-
// CHECK: double -0x1.b4154d8cccccdp+2, -0X1.B4154D8CCCCCDP+2
26-
// CHECK: mixed 0x1.91eb86{{0*}}p+1, -0X1.B4154D8CCCCCDP+2
27-
// CHECK: float 3.14, 3.14
28-
// CHECK: double -6.8138, -6.8138
29-
// CHECK: mixed 3.14, -6.8138
28+
// CHECK: 3.140000e+00, 3.140000E+00
29+
// CHECK: 0x1.91eb86{{0*}}p+1, 0X1.91EB86{{0*}}P+1
30+
// CHECK: 3.14, 3.14
31+
32+
#include <iostream>
3033

3134
#include <CL/sycl.hpp>
3235

@@ -35,55 +38,35 @@
3538
using namespace sycl;
3639

3740
void do_float_test() {
38-
{
39-
// %e, %E floating-point, decimal exponent notation
40-
FORMAT_STRING(fmt1) = "float %e, %E\n";
41-
FORMAT_STRING(fmt2) = "double %e, %E\n";
42-
FORMAT_STRING(fmt3) = "mixed %e, %E\n";
43-
44-
float f = 3.14;
45-
double d = -f * 2.17;
46-
ext::oneapi::experimental::printf(fmt1, f, f);
47-
ext::oneapi::experimental::printf(fmt2, d, d);
48-
ext::oneapi::experimental::printf(fmt3, f, d);
49-
}
50-
51-
{
52-
// %a, %A floating-point, hexadecimal exponent notation
53-
FORMAT_STRING(fmt1) = "float %a, %A\n";
54-
FORMAT_STRING(fmt2) = "double %a, %A\n";
55-
FORMAT_STRING(fmt3) = "mixed %a, %A\n";
56-
57-
float f = 3.14;
58-
double d = -f * 2.17;
59-
ext::oneapi::experimental::printf(fmt1, f, f);
60-
ext::oneapi::experimental::printf(fmt2, d, d);
61-
ext::oneapi::experimental::printf(fmt3, f, d);
62-
}
63-
64-
{
65-
// %g, %G floating-point
66-
FORMAT_STRING(fmt1) = "float %g, %G\n";
67-
FORMAT_STRING(fmt2) = "double %g, %G\n";
68-
FORMAT_STRING(fmt3) = "mixed %g, %G\n";
69-
70-
float f = 3.14;
71-
double d = -f * 2.17;
72-
ext::oneapi::experimental::printf(fmt1, f, f);
73-
ext::oneapi::experimental::printf(fmt2, d, d);
74-
ext::oneapi::experimental::printf(fmt3, f, d);
75-
}
41+
float f = 3.14;
42+
// %e, %E floating-point, decimal exponent notation
43+
FORMAT_STRING(fmt1) = "float %e, %E\n";
44+
ext::oneapi::experimental::printf(fmt1, f, f);
45+
// %a, %A floating-point, hexadecimal exponent notation
46+
FORMAT_STRING(fmt2) = "float %a, %A\n";
47+
ext::oneapi::experimental::printf(fmt2, f, f);
48+
// %g, %G floating-point
49+
FORMAT_STRING(fmt3) = "float %g, %G\n";
50+
ext::oneapi::experimental::printf(fmt3, f, f);
7651
}
7752

7853
class FloatTest;
7954

8055
int main() {
8156
queue q;
8257

58+
#ifndef __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
59+
if (!q.get_device().has(aspect::fp64))
60+
std::cout << "Skipping the actual test due to variadic argument promotion. "
61+
"Printing hard-coded output from the host side:\n"
62+
<< "3.140000e+00, 3.140000E+00\n"
63+
"0x1.91eb86p+1, 0X1.91EB86P+1\n"
64+
"3.14, 3.14\n";
65+
return 0;
66+
#endif // !__SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
8367
q.submit([](handler &cgh) {
8468
cgh.single_task<FloatTest>([]() { do_float_test(); });
8569
});
8670
q.wait();
87-
8871
return 0;
8972
}

0 commit comments

Comments
 (0)