Skip to content

Commit

Permalink
Check forbidden kernel constructs in constant evaluated context
Browse files Browse the repository at this point in the history
There are a lot of restrictions for kernel code, except when present in a
constant evaluated context.

This is a test corresponding to the specification clarification introduced by PR
KhronosGroup/SYCL-Docs#388
  • Loading branch information
keryell committed Oct 30, 2024
1 parent eb5eb0c commit 0266d6d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/language/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_cts_test(constant_evaluation.cpp)
60 changes: 60 additions & 0 deletions tests/language/constant_evaluation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*******************************************************************************
//
// SYCL 2020 Conformance Test Suite
//
// Copyright (c) 2024 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
*******************************************************************************/

// Check that constant evaluated code inside a kernel with some usually
// forbidden constructs works.

#include "../common/get_cts_object.h"
#include "catch2/catch_test_macros.hpp"

namespace language::constant_evaluation {

// A very inefficient way to compute Fibonacci's series using recursion which is
// forbidden inside non constant-evaluated kernel code/
auto constexpr f(int v) {
if (v < 2) return v;
return f(v - 1) + f(v - 2);
}

// This corresponds to clarification introduced with
// https://github.com/KhronosGroup/SYCL-Docs/pull/388
TEST_CASE("constant evaluated code works inside kernel", "[language]") {
sycl::buffer<int> b{1};
sycl_cts::util::get_cts_object::queue().submit([&](sycl::handler& cgh) {
sycl::accessor a{b, cgh, sycl::write_only};
cgh.single_task([=] {
// A constant-evaluated recursive function.
constexpr auto result = f(10);
a[0] = result;

if constexpr (false) {
// A non constant-evaluated recursive function going further more
// through forbidden function pointer which is skipped by the if
// constexpr.
auto *p = f;
auto other = p(5) + f(6);
}
});
});
std::cout << sycl::host_accessor{b}[0] << std::endl;
CHECK(sycl::host_accessor{b}[0] == 55);
}

} // namespace language::constant_evaluation

0 comments on commit 0266d6d

Please sign in to comment.