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

Commit 6e8d25a

Browse files
committed
[SYCL] Added kernel/program compile/build tests
1 parent bbaa966 commit 6e8d25a

File tree

10 files changed

+596
-2
lines changed

10 files changed

+596
-2
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// XFAIL: cuda
2+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
3+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
6+
7+
//==--- basic-program.cpp - Basic test of program and kernel APIs ----------==//
8+
//
9+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
10+
// See https://llvm.org/LICENSE.txt for license information.
11+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#include <CL/sycl.hpp>
16+
17+
#include <iostream>
18+
19+
int main() {
20+
21+
// Test program and kernel APIs when building a kernel.
22+
{
23+
cl::sycl::queue q;
24+
int data = 0;
25+
{
26+
cl::sycl::buffer<int, 1> buf(&data, cl::sycl::range<1>(1));
27+
cl::sycl::program prg(q.get_context());
28+
assert(prg.get_state() == cl::sycl::program_state::none);
29+
prg.build_with_kernel_type<class BuiltKernel>();
30+
assert(prg.get_state() == cl::sycl::program_state::linked);
31+
cl::sycl::vector_class<cl::sycl::vector_class<char>> binaries =
32+
prg.get_binaries();
33+
assert(prg.has_kernel<class BuiltKernel>());
34+
cl::sycl::kernel krn = prg.get_kernel<class BuiltKernel>();
35+
cl::sycl::string_class name =
36+
krn.get_info<cl::sycl::info::kernel::function_name>();
37+
assert(prg.has_kernel(name));
38+
39+
q.submit([&](cl::sycl::handler &cgh) {
40+
auto acc = buf.get_access<cl::sycl::access::mode::read_write>(cgh);
41+
cgh.single_task<class BuiltKernel>(krn, [=]() { acc[0] = acc[0] + 1; });
42+
});
43+
}
44+
assert(data == 1);
45+
}
46+
47+
// Test program and kernel APIs when compiling / linking a kernel.
48+
{
49+
cl::sycl::queue q;
50+
int data = 0;
51+
{
52+
cl::sycl::buffer<int, 1> buf(&data, cl::sycl::range<1>(1));
53+
cl::sycl::program prg(q.get_context());
54+
assert(prg.get_state() == cl::sycl::program_state::none);
55+
prg.compile_with_kernel_type<class CompiledKernel>();
56+
assert(prg.get_state() == cl::sycl::program_state::compiled);
57+
prg.link();
58+
assert(prg.get_state() == cl::sycl::program_state::linked);
59+
cl::sycl::vector_class<cl::sycl::vector_class<char>> binaries =
60+
prg.get_binaries();
61+
assert(prg.has_kernel<class CompiledKernel>());
62+
cl::sycl::kernel krn = prg.get_kernel<class CompiledKernel>();
63+
cl::sycl::string_class name =
64+
krn.get_info<cl::sycl::info::kernel::function_name>();
65+
assert(prg.has_kernel(name));
66+
67+
q.submit([&](cl::sycl::handler &cgh) {
68+
auto acc = buf.get_access<cl::sycl::access::mode::read_write>(cgh);
69+
cgh.single_task<class CompiledKernel>(krn,
70+
[=]() { acc[0] = acc[0] + 1; });
71+
});
72+
}
73+
assert(data == 1);
74+
}
75+
76+
{
77+
sycl::context context;
78+
std::vector<sycl::device> devices = context.get_devices();
79+
80+
sycl::program prg1(context, sycl::property_list{});
81+
sycl::program prg2(
82+
context, devices,
83+
sycl::property_list{sycl::property::buffer::use_host_ptr{}});
84+
if (!prg2.has_property<sycl::property::buffer::use_host_ptr>()) {
85+
std::cerr << "Line " << __LINE__ << ": Property was not found"
86+
<< std::endl;
87+
return 1;
88+
}
89+
90+
sycl::property::buffer::use_host_ptr Prop =
91+
prg2.get_property<sycl::property::buffer::use_host_ptr>();
92+
}
93+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// XFAIL: cuda
2+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
3+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
6+
7+
//==--- build-log.cpp - Test log message from faild build ----------==//
8+
//
9+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
10+
// See https://llvm.org/LICENSE.txt for license information.
11+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
12+
//
13+
//===--------------------------------------------------------------===//
14+
15+
#include <CL/sycl.hpp>
16+
17+
SYCL_EXTERNAL
18+
void symbol_that_does_not_exist();
19+
20+
void test() {
21+
cl::sycl::queue Queue;
22+
23+
// Submitting this kernel should result in a compile_program_error exception
24+
// with a message indicating that "symbol_that_does_not_exist" is undefined.
25+
auto Kernel = []() {
26+
#ifdef __SYCL_DEVICE_ONLY__
27+
symbol_that_does_not_exist();
28+
#endif
29+
};
30+
31+
std::string Msg;
32+
int Result;
33+
34+
try {
35+
Queue.submit([&](cl::sycl::handler &CGH) {
36+
CGH.single_task<class SingleTask>(Kernel);
37+
});
38+
assert(false && "There must be compilation error");
39+
} catch (const cl::sycl::compile_program_error &e) {
40+
std::string Msg(e.what());
41+
assert(Msg.find("symbol_that_does_not_exist") != std::string::npos);
42+
} catch (...) {
43+
assert(false && "There must be cl::sycl::compile_program_error");
44+
}
45+
}
46+
47+
int main() {
48+
test();
49+
50+
return 0;
51+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// RUN: %clangxx -fsycl %s -o %t.out
2+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
3+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
5+
6+
#include <CL/sycl.hpp>
7+
8+
SYCL_EXTERNAL
9+
void undefined();
10+
11+
void test() {
12+
cl::sycl::queue Queue;
13+
14+
auto Kernel = []() {
15+
#ifdef __SYCL_DEVICE_ONLY__
16+
undefined();
17+
#endif
18+
};
19+
20+
std::string Msg;
21+
int Result;
22+
23+
for (int Idx = 0; Idx < 2; ++Idx) {
24+
try {
25+
Queue.submit([&](cl::sycl::handler &CGH) {
26+
CGH.single_task<class SingleTask>(Kernel);
27+
});
28+
assert(false && "There must be compilation error");
29+
} catch (const cl::sycl::compile_program_error &e) {
30+
fprintf(stderr, "Exception: %s, %d\n", e.what(), e.get_cl_code());
31+
if (Idx == 0) {
32+
Msg = e.what();
33+
Result = e.get_cl_code();
34+
} else {
35+
// Exception constantly adds info on its error code in the message
36+
assert(Msg.find_first_of(e.what()) == 0 && "Exception text differs");
37+
assert(Result == e.get_cl_code() && "Exception code differs");
38+
}
39+
} catch (...) {
40+
assert(false && "There must be cl::sycl::compile_program_error");
41+
}
42+
}
43+
}
44+
45+
int main() {
46+
test();
47+
48+
return 0;
49+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// RUN: %clangxx -fsycl %s -o %t.out
2+
// RUN: %HOST_RUN_PLACEHOLDER %t.out
3+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
6+
7+
#include <CL/sycl.hpp>
8+
9+
#include <cassert>
10+
11+
// Check program::get_compile/link/build_options functions
12+
13+
class KernelName;
14+
void submitKernel() {
15+
cl::sycl::queue q;
16+
q.submit(
17+
[&](cl::sycl::handler &cgh) { cgh.single_task<KernelName>([]() {}); });
18+
}
19+
20+
int main() {
21+
const cl::sycl::string_class CompileOpts{"-cl-opt-disable"};
22+
const cl::sycl::string_class LinkOpts{"-cl-fast-relaxed-math"};
23+
const cl::sycl::string_class BuildOpts{
24+
"-cl-opt-disable -cl-fast-relaxed-math"};
25+
26+
cl::sycl::context Ctx;
27+
cl::sycl::program PrgA{Ctx};
28+
assert(PrgA.get_compile_options().empty());
29+
assert(PrgA.get_link_options().empty());
30+
assert(PrgA.get_build_options().empty());
31+
32+
PrgA.build_with_kernel_type<KernelName>(BuildOpts);
33+
assert(PrgA.get_compile_options().empty());
34+
assert(PrgA.get_link_options().empty());
35+
assert(PrgA.get_build_options() == (PrgA.is_host() ? "" : BuildOpts));
36+
37+
cl::sycl::program PrgB{Ctx};
38+
PrgB.compile_with_kernel_type<KernelName>(CompileOpts);
39+
assert(PrgB.get_compile_options() == (PrgB.is_host() ? "" : CompileOpts));
40+
assert(PrgB.get_link_options().empty());
41+
assert(PrgB.get_build_options() == (PrgB.is_host() ? "" : CompileOpts));
42+
43+
PrgB.link(LinkOpts);
44+
assert(PrgB.get_compile_options() == (PrgB.is_host() ? "" : CompileOpts));
45+
assert(PrgB.get_link_options() == (PrgB.is_host() ? "" : LinkOpts));
46+
assert(PrgB.get_build_options() == (PrgB.is_host() ? "" : LinkOpts));
47+
}

0 commit comments

Comments
 (0)