Skip to content

Commit e94d82f

Browse files
authored
Merge pull request #1911 from kbobrovs/sc-throw
[SYCL] Fix program::set_spec_constant - add state check.
2 parents f0a4fe2 + e2e3d3d commit e94d82f

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

sycl/source/detail/program_impl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,9 @@ vector_class<device> program_impl::get_info<info::program::devices>() const {
476476

477477
void program_impl::set_spec_constant_impl(const char *Name, const void *ValAddr,
478478
size_t ValSize) {
479+
if (MState != program_state::none)
480+
throw cl::sycl::experimental::spec_const_error("Invalid program state",
481+
PI_INVALID_PROGRAM);
479482
// Reuse cached programs lock as opposed to introducing a new lock.
480483
auto LockGuard = MContext->getKernelProgramCache().acquireCachedPrograms();
481484
spec_constant_impl &SC = SpecConstRegistry[Name];
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// RUN: %clangxx -fsycl %s -o %t.out
2+
// RUN: env SYCL_DEVICE_TYPE=HOST %t.out
3+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
// Specialization constants are not supported on FPGA h/w and emulator.
6+
// UNSUPPORTED: cuda || level0
7+
//
8+
//==----------- spec_const_hw.cpp ------------------------------------------==//
9+
//
10+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
11+
// See https://llvm.org/LICENSE.txt for license information.
12+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
13+
//
14+
//===----------------------------------------------------------------------===//
15+
// The test that specialization constant implementation throws exceptions when
16+
// expected.
17+
18+
#include <CL/sycl.hpp>
19+
20+
#include <iostream>
21+
#include <vector>
22+
23+
class MyInt32Const;
24+
25+
using namespace sycl;
26+
27+
class KernelAAAi;
28+
29+
int main(int argc, char **argv) {
30+
cl::sycl::queue q(default_selector{}, [](exception_list l) {
31+
for (auto ep : l) {
32+
try {
33+
std::rethrow_exception(ep);
34+
} catch (cl::sycl::exception &e0) {
35+
std::cout << e0.what();
36+
} catch (std::exception &e1) {
37+
std::cout << e1.what();
38+
} catch (...) {
39+
std::cout << "*** catch (...)\n";
40+
}
41+
}
42+
});
43+
44+
std::cout << "Running on " << q.get_device().get_info<info::device::name>()
45+
<< "\n";
46+
cl::sycl::program program1(q.get_context());
47+
48+
cl::sycl::experimental::spec_constant<int32_t, MyInt32Const> i32 =
49+
program1.set_spec_constant<MyInt32Const>(10);
50+
51+
std::vector<int> veci(1);
52+
bool passed = false;
53+
54+
program1.build_with_kernel_type<KernelAAAi>();
55+
56+
try {
57+
// This is an attempt to set a spec constant after the program has been
58+
// built - spec_const_error should be thrown
59+
cl::sycl::experimental::spec_constant<int32_t, MyInt32Const> i32 =
60+
program1.set_spec_constant<MyInt32Const>(10);
61+
62+
cl::sycl::buffer<int, 1> bufi(veci.data(), veci.size());
63+
64+
q.submit([&](cl::sycl::handler &cgh) {
65+
auto acci = bufi.get_access<cl::sycl::access::mode::write>(cgh);
66+
cgh.single_task<KernelAAAi>(
67+
program1.get_kernel<KernelAAAi>(),
68+
[=]() {
69+
acci[0] = i32.get();
70+
});
71+
});
72+
} catch (cl::sycl::experimental::spec_const_error &sc_err) {
73+
passed = true;
74+
} catch (cl::sycl::exception &e) {
75+
std::cout << "*** Exception caught: " << e.what() << "\n";
76+
return 1;
77+
}
78+
std::cout << (passed ? "passed\n" : "FAILED\n");
79+
return passed ? 0 : 1;
80+
}

0 commit comments

Comments
 (0)