Skip to content

Commit 68dff6e

Browse files
AlcpzAerialMantisjoeatoddPietroGhgRuyk
authored andcommitted
[SYCL][COMPAT] SYCLcompat dim and defs headers (intel#9976)
This is an implementation of SYCLcompat, the stand alone library proposed in intel#9646. Please, find the documentation there. SYCLcompat has two primary goals: - Improve the adoption of SYCL. This library is designed to provide a familiar programming interface that resembles other popular heterogeneous programming models. By reducing the learning curve, it enables developers to leverage SYCL's power and features more easily. - Source-to-Source Translation Support. SYCLcompat is also designed to facilitate automatic source-to-source translation from other heterogeneous programming models to SYCL and offer a more standardized and consistent programming interface. This feature can significantly streamline the migration and integration of existing codebases into the SYCL ecosystem The PR also includes tests. We were advised to include the tests as part of `sycl/unittests`, so we added a different mechanism to build them using `clang`. As we stated in the docs PR, we are open to any suggestions, concerns, or improvements you may have, so please, let us know if you have any. --------- Co-authored-by: Gordon Brown <[email protected]> Co-authored-by: Joe Todd <[email protected]> Co-authored-by: Pietro Ghiglio <[email protected]> Co-authored-by: Ruyman Reyes <[email protected]> Co-authored-by: tomflinda <[email protected]>
1 parent 45aaa1b commit 68dff6e

File tree

11 files changed

+456
-1
lines changed

11 files changed

+456
-1
lines changed

sycl/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,37 +193,48 @@ include(AddBoostMp11Headers)
193193
file(GLOB_RECURSE HEADERS_IN_SYCL_DIR CONFIGURE_DEPENDS "${sycl_inc_dir}/sycl/*")
194194
file(GLOB_RECURSE HEADERS_IN_CL_DIR CONFIGURE_DEPENDS "${sycl_inc_dir}/CL/*")
195195
file(GLOB_RECURSE HEADERS_IN_STD_DIR CONFIGURE_DEPENDS "${sycl_inc_dir}/std/*")
196+
file(GLOB_RECURSE HEADERS_IN_SYCLCOMPAT_DIR CONFIGURE_DEPENDS "${sycl_inc_dir}/syclcompat/*" "${sycl_inc_dir}/syclcompat.hpp")
197+
196198
string(REPLACE "${sycl_inc_dir}" "${SYCL_INCLUDE_BUILD_DIR}"
197199
OUT_HEADERS_IN_SYCL_DIR "${HEADERS_IN_SYCL_DIR}")
198200
string(REPLACE "${sycl_inc_dir}/CL" "${SYCL_INCLUDE_BUILD_DIR}/sycl/CL"
199201
OUT_HEADERS_IN_CL_DIR "${HEADERS_IN_CL_DIR}")
200202
string(REPLACE "${sycl_inc_dir}" "${SYCL_INCLUDE_BUILD_DIR}"
201203
OUT_HEADERS_IN_STD_DIR "${HEADERS_IN_STD_DIR}")
204+
string(REPLACE "${sycl_inc_dir}" "${SYCL_INCLUDE_BUILD_DIR}"
205+
OUT_HEADERS_IN_SYCLCOMPAT_DIR "${HEADERS_IN_SYCLCOMPAT_DIR}")
202206

203207
# Copy SYCL headers from sources to build directory
204208
add_custom_target(sycl-headers
205209
DEPENDS ${OUT_HEADERS_IN_SYCL_DIR}
206210
${OUT_HEADERS_IN_CL_DIR}
207211
${OUT_HEADERS_IN_STD_DIR}
212+
${OUT_HEADERS_IN_SYCLCOMPAT_DIR}
208213
boost_mp11-headers)
209214

210215
add_custom_command(
211216
OUTPUT ${OUT_HEADERS_IN_SYCL_DIR}
212217
${OUT_HEADERS_IN_CL_DIR}
213218
${OUT_HEADERS_IN_STD_DIR}
219+
${OUT_HEADERS_IN_SYCLCOMPAT_DIR}
214220
DEPENDS ${HEADERS_IN_SYCL_DIR}
215221
${HEADERS_IN_CL_DIR}
216222
${HEADERS_IN_STD_DIR}
223+
${HEADERS_IN_SYCLCOMPAT_DIR}
217224
COMMAND ${CMAKE_COMMAND} -E copy_directory ${sycl_inc_dir}/sycl ${SYCL_INCLUDE_BUILD_DIR}/sycl
218225
COMMAND ${CMAKE_COMMAND} -E copy_directory ${sycl_inc_dir}/CL ${SYCL_INCLUDE_BUILD_DIR}/sycl/CL
219226
COMMAND ${CMAKE_COMMAND} -E copy_directory ${sycl_inc_dir}/std ${SYCL_INCLUDE_BUILD_DIR}/std
227+
COMMAND ${CMAKE_COMMAND} -E copy_directory ${sycl_inc_dir}/syclcompat ${SYCL_INCLUDE_BUILD_DIR}/syclcompat
228+
COMMAND ${CMAKE_COMMAND} -E copy ${sycl_inc_dir}/syclcompat.hpp ${SYCL_INCLUDE_BUILD_DIR}/syclcompat.hpp
220229
COMMENT "Copying SYCL headers ...")
221230

222231
# Copy SYCL headers from source to install directory
223232
install(DIRECTORY "${sycl_inc_dir}/sycl" DESTINATION ${SYCL_INCLUDE_DIR} COMPONENT sycl-headers)
224233
install(DIRECTORY "${sycl_inc_dir}/CL" DESTINATION ${SYCL_INCLUDE_DIR}/sycl COMPONENT sycl-headers)
225234
install(DIRECTORY "${sycl_inc_dir}/std" DESTINATION ${SYCL_INCLUDE_DIR} COMPONENT sycl-headers)
226235
install(DIRECTORY ${BOOST_MP11_DESTINATION_DIR} DESTINATION ${SYCL_INCLUDE_DIR}/sycl/detail COMPONENT boost_mp11-headers)
236+
install(DIRECTORY "${sycl_inc_dir}/syclcompat" DESTINATION ${SYCL_INCLUDE_DIR}/syclcompat COMPONENT sycl-headers)
237+
install(FILES "${sycl_inc_dir}/syclcompat.hpp" DESTINATION ${SYCL_INCLUDE_DIR} COMPONENT sycl-headers)
227238

228239
if (WIN32)
229240
set(SYCL_RT_LIBS sycl${SYCL_MAJOR_VERSION})

sycl/cmake/modules/AddSYCLExecutable.cmake

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ macro(add_sycl_executable ARG_TARGET_NAME)
3636
COMMAND_EXPAND_LISTS)
3737
add_dependencies(${ARG_TARGET_NAME}_exec sycl-toolchain)
3838
foreach(_lib ${ARG_LIBRARIES})
39-
add_dependencies(${ARG_TARGET_NAME}_exec _lib)
39+
# Avoid errors when linking external targets such as dl
40+
if(TARGET ${_lib})
41+
add_dependencies(${ARG_TARGET_NAME}_exec ${_lib})
42+
endif()
4043
endforeach()
4144

4245
foreach(_dep ${ARG_DEPENDANTS})
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# add_sycl_library_unittest(test_suite_name sycl_extra_flags
2+
# file1.cpp file2.cpp ...)
3+
#
4+
# sycl_extra_flags: Clang extra compiler flags, e.g.
5+
# "-fsycl-unnamed-lambdas;-fsycl-device-code-split"
6+
#
7+
# Will compile the list of files together using clang.
8+
# Produces a single binary using all the .cpp files
9+
# named 'test_suite_name' at ${CMAKE_CURRENT_BINARY_DIR}.
10+
macro(add_sycl_library_unittest test_suite_name)
11+
cmake_parse_arguments(ARG
12+
""
13+
""
14+
"SYCL_EXTRA_FLAGS;SOURCES"
15+
${ARGN})
16+
17+
set(CXX_COMPILER clang++)
18+
if(MSVC)
19+
set(CXX_COMPILER clang-cl.exe)
20+
endif()
21+
22+
set(DEVICE_COMPILER_EXECUTABLE ${LLVM_RUNTIME_OUTPUT_INTDIR}/${CXX_COMPILER})
23+
set(_OUTPUT_BIN ${CMAKE_CURRENT_BINARY_DIR}/${test_suite_name}Tests)
24+
set(_TESTS_TARGET ${test_suite_name}Tests)
25+
set(_BIN_TARGET ${_TESTS_TARGET}_bin)
26+
set(_LLVM_TARGET_DEPENDENCIES
27+
"llvm_gtest_main;llvm_gtest;LLVMTestingSupport;LLVMSupport;LLVMDemangle")
28+
29+
foreach(_lib ${_LLVM_TARGET_DEPENDENCIES})
30+
list(APPEND _LIBRARIES $<TARGET_LINKER_FILE:${_lib}>)
31+
endforeach()
32+
33+
# Enable exception handling on Windows
34+
# Appends extra libraries not available in LIBPATH
35+
if(WIN32)
36+
set(_INTERNAL_LINKER_FLAGS /link /SUBSYSTEM:CONSOLE)
37+
list(APPEND _INTERNAL_EXTRA_FLAGS "/EHs")
38+
list(APPEND _LIBRARIES $<TARGET_LINKER_FILE:sycl>)
39+
list(APPEND _LIBRARIES ${LLVM_LIBRARY_OUTPUT_INTDIR}/sycl-devicelib-host.lib)
40+
endif()
41+
42+
if(UNIX)
43+
foreach(_lib "pthread" "dl" "ncurses")
44+
list(APPEND _LIBRARIES "-l${_lib}")
45+
endforeach()
46+
endif()
47+
48+
get_target_property(GTEST_INCLUDES llvm_gtest INCLUDE_DIRECTORIES)
49+
foreach(_dir ${GTEST_INCLUDES})
50+
# Avoid -I when _dir contains an empty generator expression.
51+
list(APPEND INCLUDE_COMPILER_STRING "$<$<BOOL:${_dir}>:-I${_dir}>")
52+
endforeach()
53+
54+
add_custom_target(${_BIN_TARGET}
55+
COMMAND ${DEVICE_COMPILER_EXECUTABLE} -fsycl ${ARG_SOURCES}
56+
-o ${_OUTPUT_BIN}
57+
${ARG_SYCL_EXTRA_FLAGS}
58+
${_INTERNAL_EXTRA_FLAGS}
59+
${INCLUDE_COMPILER_STRING}
60+
${_LIBRARIES}
61+
${_INTERNAL_LINKER_FLAGS}
62+
BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/${_TESTS_TARGET}
63+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
64+
COMMAND_EXPAND_LISTS)
65+
66+
add_dependencies(${_BIN_TARGET} sycl)
67+
foreach(_lib ${ARG_LIBRARIES})
68+
add_dependencies(${_BIN_TARGET} ${_TARGET_DEPENDENCIES})
69+
endforeach()
70+
71+
add_dependencies(SYCLUnitTests ${_BIN_TARGET})
72+
73+
add_executable(${_TESTS_TARGET} IMPORTED GLOBAL)
74+
set_target_properties(${_TESTS_TARGET} PROPERTIES
75+
IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR})
76+
77+
# Check target for Linux
78+
if (UNIX)
79+
add_custom_target(check-${test_suite_name}
80+
${CMAKE_COMMAND} -E
81+
env LD_LIBRARY_PATH="${CMAKE_BINARY_DIR}/lib"
82+
env SYCL_CONFIG_FILE_NAME=null.cfg
83+
env SYCL_DEVICELIB_NO_FALLBACK=1
84+
env SYCL_CACHE_DIR="${CMAKE_BINARY_DIR}/sycl_cache"
85+
${CMAKE_CURRENT_BINARY_DIR}/${_TESTS_TARGET}
86+
)
87+
add_dependencies(check-${test_suite_name} ${_BIN_TARGET})
88+
add_dependencies(check-sycl-unittests-libs check-${test_suite_name})
89+
endif()
90+
91+
endmacro()

sycl/include/syclcompat.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/***************************************************************************
2+
*
3+
* Copyright (C) Codeplay Software Ltd.
4+
*
5+
* Part of the LLVM Project, under the Apache License v2.0 with LLVM
6+
* Exceptions. See https://llvm.org/LICENSE.txt for license information.
7+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
* SYCLcompat
16+
*
17+
* syclcompat.hpp
18+
*
19+
* Description:
20+
* Main include header for SYCLcompat
21+
**************************************************************************/
22+
23+
#pragma once
24+
25+
#include <syclcompat/syclcompat.hpp>

sycl/include/syclcompat/defs.hpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/***************************************************************************
2+
*
3+
* Copyright (C) Codeplay Software Ltd.
4+
*
5+
* Part of the LLVM Project, under the Apache License v2.0 with LLVM
6+
* Exceptions. See https://llvm.org/LICENSE.txt for license information.
7+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
* SYCLcompat
16+
*
17+
* defs.hpp
18+
*
19+
* Description:
20+
* helper aliases and definitions for SYCLcompat
21+
*
22+
**************************************************************************/
23+
24+
// The original source was under the license below:
25+
//==---- dpct.hpp ---------------------------------*- C++ -*----------------==//
26+
//
27+
// Copyright (C) Intel Corporation
28+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
29+
// See https://llvm.org/LICENSE.txt for license information.
30+
//
31+
//===----------------------------------------------------------------------===//
32+
33+
#pragma once
34+
35+
template <class... Args> class sycl_compat_kernel_name;
36+
template <int Arg> class sycl_compat_kernel_scalar;
37+
38+
#define __sycl_compat_align__(n) alignas(n)
39+
#define __sycl_compat_inline__ __inline__ __attribute__((always_inline))
40+
41+
#define __sycl_compat_noinline__ __attribute__((noinline))
42+
43+
#define SYCL_COMPAT_COMPATIBILITY_TEMP (600)

sycl/include/syclcompat/dims.hpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/***************************************************************************
2+
*
3+
* Copyright (C) Codeplay Software Ltd.
4+
*
5+
* Part of the LLVM Project, under the Apache License v2.0 with LLVM
6+
* Exceptions. See https://llvm.org/LICENSE.txt for license information.
7+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
* SYCLcompat
16+
*
17+
* dims.hpp
18+
*
19+
* Description:
20+
* dim3 functionality for SYCLcompat
21+
**************************************************************************/
22+
23+
#pragma once
24+
25+
#include <tuple>
26+
27+
#include <sycl/range.hpp>
28+
29+
namespace syclcompat {
30+
31+
class dim3 {
32+
public:
33+
const size_t x, y, z;
34+
35+
dim3(const sycl::range<3> &r) : x(r[2]), y(r[1]), z(r[0]) {}
36+
37+
dim3(const sycl::range<2> &r) : x(r[1]), y(r[0]), z(1) {}
38+
39+
dim3(const sycl::range<1> &r) : x(r[0]), y(1), z(1) {}
40+
41+
constexpr dim3(size_t x, size_t y = 1, size_t z = 1) : x(x), y(y), z(z) {}
42+
43+
constexpr size_t size() const { return x * y * z; }
44+
45+
operator sycl::range<3>() const { return sycl::range<3>(z, y, x); }
46+
operator sycl::range<2>() const {
47+
if (z != 1)
48+
throw std::invalid_argument(
49+
"Attempting to convert a 3D dim3 into sycl::range<2>");
50+
return sycl::range<2>(y, x);
51+
}
52+
operator sycl::range<1>() const {
53+
if (z != 1 || y != 1)
54+
throw std::invalid_argument(
55+
"Attempting to convert a 2D or 3D dim3 into sycl::range<1>");
56+
return sycl::range<1>(x);
57+
}
58+
}; // namespace dim3
59+
60+
inline dim3 operator*(const dim3 &a, const dim3 &b) {
61+
return dim3{a.x * b.x, a.y * b.y, a.z * b.z};
62+
}
63+
64+
inline dim3 operator+(const dim3 &a, const dim3 &b) {
65+
return dim3{a.x + b.x, a.y + b.y, a.z + b.z};
66+
}
67+
68+
inline dim3 operator-(const dim3 &a, const dim3 &b) {
69+
return dim3{a.x - b.x, a.y - b.y, a.z - b.z};
70+
}
71+
72+
} // namespace syclcompat
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/***************************************************************************
2+
*
3+
* Copyright (C) Codeplay Software Ltd.
4+
*
5+
* Part of the LLVM Project, under the Apache License v2.0 with LLVM
6+
* Exceptions. See https://llvm.org/LICENSE.txt for license information.
7+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
* SYCLcompat
16+
*
17+
* syclcompat.hpp
18+
*
19+
* Description:
20+
* Main include internal header for SYCLcompat
21+
**************************************************************************/
22+
23+
#pragma once
24+
25+
#include <syclcompat/defs.hpp>
26+
#include <syclcompat/dims.hpp>

sycl/unittests/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,9 @@ if (NOT WIN32)
5656
add_subdirectory(xpti_trace)
5757
endif()
5858

59+
# Library unit testing
60+
include(AddSYCLLibraryUnitTest)
61+
add_custom_target(check-sycl-unittests-libs)
62+
add_dependencies(check-sycl-unittests check-sycl-unittests-libs)
63+
64+
add_subdirectory(syclcompat)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#
2+
# Copyright (C) Codeplay Software Ltd.
3+
#
4+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
# See https://llvm.org/LICENSE.txt for license information.
6+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations under
12+
# the License.
13+
#
14+
# SYCLcompat
15+
#
16+
17+
set(SYCL_EXTRA_FLAGS "-fsycl-unnamed-lambda;-fsycl-device-code-split=per_kernel")
18+
19+
if (UNIX)
20+
add_custom_target(check-syclcompat)
21+
add_dependencies(check-sycl-unittests check-syclcompat)
22+
endif()
23+
24+
add_sycl_library_unittest(syclcompat-base
25+
SYCL_EXTRA_FLAGS
26+
${SYCL_EXTRA_FLAGS}
27+
SOURCES
28+
Dim.cpp
29+
Defs.cpp)
30+
31+
if (UNIX)
32+
add_dependencies(check-syclcompat check-syclcompat-base)
33+
endif()

0 commit comments

Comments
 (0)