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

Commit 9d3a5cb

Browse files
[SYCL] Add more tests for experimental::printf (#569)
Signed-off-by: Artem Gindinson <[email protected]>
1 parent 81b7908 commit 9d3a5cb

File tree

8 files changed

+670
-0
lines changed

8 files changed

+670
-0
lines changed

.github/CODEOWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ SYCL/OnlineCompiler @v-klochkov
5555
# Plugin interface
5656
SYCL/Plugin @smaslov-intel
5757

58+
# Printf
59+
SYCL/Printf @AlexeySachkov @AGindinson @mlychkov
60+
5861
# Reduction algorithms
5962
SYCL/Reduction @v-klochkov
6063

SYCL/Printf/char.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
8+
// RUN: %CPU_RUN_PLACEHOLDER %t.out %CPU_CHECK_PLACEHOLDER
9+
// RUN: %GPU_RUN_PLACEHOLDER %t.out %GPU_CHECK_PLACEHOLDER
10+
// RUN: %ACC_RUN_PLACEHOLDER %t.out %ACC_CHECK_PLACEHOLDER
11+
// FIXME: Remove dedicated constant address space testing once generic AS
12+
// support is considered stable.
13+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.constant.out \
14+
// RUN: -DTEST_CONSTANT_AS
15+
// RUN: %CPU_RUN_PLACEHOLDER %t.constant.out %CPU_CHECK_PLACEHOLDER
16+
// RUN: %GPU_RUN_PLACEHOLDER %t.constant.out %GPU_CHECK_PLACEHOLDER
17+
// RUN: %ACC_RUN_PLACEHOLDER %t.constant.out %ACC_CHECK_PLACEHOLDER
18+
//
19+
// FIXME: wchar_t* is not supported on GPU
20+
// FIXME: String literal prefixes (L, u8, u, U) are not functioning on Windows
21+
//
22+
// CHECK: c=a
23+
// CHECK: literal strings: s=Hello World!
24+
// CHECK_DISABLED: non-literal strings: s=Hello, World! ls=
25+
26+
#include <CL/sycl.hpp>
27+
28+
#include <cstring>
29+
30+
#include "helper.hpp"
31+
32+
using namespace sycl;
33+
34+
void do_char_string_test() {
35+
{
36+
// %c format specifier, single character
37+
FORMAT_STRING(fmt) = "c=%c\n"; // FIXME: lc is not tested
38+
39+
char c = 'a';
40+
41+
ext::oneapi::experimental::printf(fmt, c);
42+
}
43+
44+
{
45+
// %s format specifier, character string
46+
FORMAT_STRING(fmt1) = "literal strings: s=%s %s\n";
47+
ext::oneapi::experimental::printf(fmt1, "Hello",
48+
// World!
49+
"\x57\x6F\x72\x6C\x64\x21");
50+
51+
// FIXME: lack of support for non-literal strings in %s is an OpenCL
52+
// limitation
53+
/*
54+
FORMAT_STRING(fmt2) = "non-literal strings: s=%s ls=%ls\n";
55+
char str[20] = { '\0' };
56+
const char *s = "Hello, World!";
57+
for (int i = 0; i < 13; ++i) {
58+
str[i] = s[i];
59+
}
60+
61+
// FIXME: ls is untested here
62+
ext::oneapi::experimental::printf(fmt2, str, "");
63+
*/
64+
}
65+
}
66+
67+
class CharTest;
68+
69+
int main() {
70+
queue q;
71+
72+
q.submit([](handler &cgh) {
73+
cgh.single_task<CharTest>([]() { do_char_string_test(); });
74+
});
75+
q.wait();
76+
77+
return 0;
78+
}

SYCL/Printf/float.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
8+
// RUN: %CPU_RUN_PLACEHOLDER %t.out %CPU_CHECK_PLACEHOLDER
9+
// RUN: %GPU_RUN_PLACEHOLDER %t.out %GPU_CHECK_PLACEHOLDER
10+
// RUN: %ACC_RUN_PLACEHOLDER %t.out %ACC_CHECK_PLACEHOLDER
11+
// FIXME: Remove dedicated constant address space testing once generic AS
12+
// support is considered stable.
13+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.constant.out \
14+
// RUN: -DTEST_CONSTANT_AS
15+
// RUN: %CPU_RUN_PLACEHOLDER %t.constant.out %CPU_CHECK_PLACEHOLDER
16+
// RUN: %GPU_RUN_PLACEHOLDER %t.constant.out %GPU_CHECK_PLACEHOLDER
17+
// RUN: %ACC_RUN_PLACEHOLDER %t.constant.out %ACC_CHECK_PLACEHOLDER
18+
//
19+
// CHECK: float 3.140000e+00, 3.140000E+00
20+
// CHECK: double -6.813800e+00, -6.813800E+00
21+
// CHECK: mixed 3.140000e+00, -6.813800E+00
22+
// CHECK: float 0x1.91eb86{{0*}}p+1, 0X1.91EB86{{0*}}P+1
23+
// CHECK: double -0x1.b4154d8cccccdp+2, -0X1.B4154D8CCCCCDP+2
24+
// CHECK: mixed 0x1.91eb86{{0*}}p+1, -0X1.B4154D8CCCCCDP+2
25+
// CHECK: float 3.14, 3.14
26+
// CHECK: double -6.8138, -6.8138
27+
// CHECK: mixed 3.14, -6.8138
28+
29+
#include <CL/sycl.hpp>
30+
31+
#include "helper.hpp"
32+
33+
using namespace sycl;
34+
35+
void do_float_test() {
36+
{
37+
// %e, %E floating-point, decimal exponent notation
38+
FORMAT_STRING(fmt1) = "float %e, %E\n";
39+
FORMAT_STRING(fmt2) = "double %e, %E\n";
40+
FORMAT_STRING(fmt3) = "mixed %e, %E\n";
41+
42+
float f = 3.14;
43+
double d = -f * 2.17;
44+
ext::oneapi::experimental::printf(fmt1, f, f);
45+
ext::oneapi::experimental::printf(fmt2, d, d);
46+
ext::oneapi::experimental::printf(fmt3, f, d);
47+
}
48+
49+
{
50+
// %a, %A floating-point, hexadecimal exponent notation
51+
FORMAT_STRING(fmt1) = "float %a, %A\n";
52+
FORMAT_STRING(fmt2) = "double %a, %A\n";
53+
FORMAT_STRING(fmt3) = "mixed %a, %A\n";
54+
55+
float f = 3.14;
56+
double d = -f * 2.17;
57+
ext::oneapi::experimental::printf(fmt1, f, f);
58+
ext::oneapi::experimental::printf(fmt2, d, d);
59+
ext::oneapi::experimental::printf(fmt3, f, d);
60+
}
61+
62+
{
63+
// %g, %G floating-point
64+
FORMAT_STRING(fmt1) = "float %g, %G\n";
65+
FORMAT_STRING(fmt2) = "double %g, %G\n";
66+
FORMAT_STRING(fmt3) = "mixed %g, %G\n";
67+
68+
float f = 3.14;
69+
double d = -f * 2.17;
70+
ext::oneapi::experimental::printf(fmt1, f, f);
71+
ext::oneapi::experimental::printf(fmt2, d, d);
72+
ext::oneapi::experimental::printf(fmt3, f, d);
73+
}
74+
}
75+
76+
class FloatTest;
77+
78+
int main() {
79+
queue q;
80+
81+
q.submit([](handler &cgh) {
82+
cgh.single_task<FloatTest>([]() { do_float_test(); });
83+
});
84+
q.wait();
85+
86+
return 0;
87+
}

SYCL/Printf/helper.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#if defined(__SYCL_DEVICE_ONLY__) && defined(TEST_CONSTANT_AS)
2+
// On device side, we have to put format string into a constant address space
3+
// FIXME: remove this header completely once the toolchain's support for
4+
// generic address-spaced format strings is stable.
5+
#define FORMAT_STRING(X) static const __attribute__((opencl_constant)) char X[]
6+
#else
7+
#define FORMAT_STRING(X) static const char X[]
8+
#endif

0 commit comments

Comments
 (0)