Skip to content

Commit 634579c

Browse files
Add CMake build files for dpctl_ext
1 parent 4f63340 commit 634579c

2 files changed

Lines changed: 380 additions & 0 deletions

File tree

dpctl_ext/CMakeLists.txt

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
# -*- coding: utf-8 -*-
2+
# *****************************************************************************
3+
# Copyright (c) 2026, Intel Corporation
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions are met:
8+
# - Redistributions of source code must retain the above copyright notice,
9+
# this list of conditions and the following disclaimer.
10+
# - Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions and the following disclaimer in the documentation
12+
# and/or other materials provided with the distribution.
13+
# - Neither the name of the copyright holder nor the names of its contributors
14+
# may be used to endorse or promote products derived from this software
15+
# without specific prior written permission.
16+
#
17+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27+
# THE POSSIBILITY OF SUCH DAMAGE.
28+
# *****************************************************************************
29+
30+
find_package(Python REQUIRED COMPONENTS NumPy)
31+
32+
# -t is to only Cythonize sources with timestamps newer than existing CXX files (if present)
33+
# -w is to set working directory (and correctly set __pyx_f[] array of filenames)
34+
set(CYTHON_FLAGS "-t -w \"${CMAKE_SOURCE_DIR}\"")
35+
find_package(Cython REQUIRED)
36+
37+
if(WIN32)
38+
string(
39+
CONCAT WARNING_FLAGS
40+
"-Wall "
41+
"-Wextra "
42+
"-Winit-self "
43+
"-Wunused-function "
44+
"-Wuninitialized "
45+
"-Wmissing-declarations "
46+
"-Wstrict-prototypes "
47+
"-Wno-unused-parameter "
48+
)
49+
string(CONCAT SDL_FLAGS "/GS " "/DynamicBase ")
50+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Ox ${WARNING_FLAGS} ${SDL_FLAGS}")
51+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Ox ${WARNING_FLAGS} ${SDL_FLAGS}")
52+
set(CMAKE_C_FLAGS_DEBUG
53+
"${CMAKE_C_FLAGS_DEBUG} ${WARNING_FLAGS} ${SDL_FLAGS} -O0 -g1 -DDEBUG -Xsycl-target-frontend=spir64 \"-g0\""
54+
)
55+
set(CMAKE_CXX_FLAGS_DEBUG
56+
"${CMAKE_CXX_FLAGS_DEBUG} ${WARNING_FLAGS} ${SDL_FLAGS} -O0 -g1 -DDEBUG -Xsycl-target-frontend=spir64 \"-g0\""
57+
)
58+
set(CMAKE_C_FLAGS_COVERAGE
59+
"${CMAKE_C_FLAGS_DEBUG} ${WARNING_FLAGS} ${SDL_FLAGS} -O1 -g1 -DDEBUG"
60+
)
61+
set(CMAKE_CXX_FLAGS_COVERAGE
62+
"${CMAKE_CXX_FLAGS_DEBUG} ${WARNING_FLAGS} ${SDL_FLAGS} -O1 -g1 -DDEBUG"
63+
)
64+
set(CMAKE_MODULE_LINKER_FLAGS_COVERAGE "${CMAKE_MODULE_LINKER_FLAGS_DEBUG}")
65+
set(DPCTL_LDFLAGS "/NXCompat;/DynamicBase")
66+
mark_as_advanced(
67+
CMAKE_CXX_FLAGS_COVERAGE
68+
CMAKE_C_FLAGS_COVERAGE
69+
CMAKE_MODULE_LINKER_FLAGS_COVERAGE
70+
)
71+
elseif(UNIX)
72+
string(
73+
CONCAT WARNING_FLAGS
74+
"-Wall "
75+
"-Wextra "
76+
"-Winit-self "
77+
"-Wunused-function "
78+
"-Wuninitialized "
79+
"-Wmissing-declarations "
80+
"-Wstrict-prototypes "
81+
"-Wno-unused-parameter "
82+
"-fdiagnostics-color=auto "
83+
)
84+
string(
85+
CONCAT SDL_FLAGS
86+
"-fstack-protector "
87+
"-fstack-protector-all "
88+
"-fpic "
89+
"-fPIC "
90+
"-D_FORTIFY_SOURCE=2 "
91+
"-Wformat "
92+
"-Wformat-security "
93+
# "-fno-strict-overflow " # no-strict-overflow is implied by -fwrapv
94+
"-fno-delete-null-pointer-checks "
95+
"-fwrapv "
96+
)
97+
string(CONCAT CFLAGS "${WARNING_FLAGS}" "${SDL_FLAGS}")
98+
string(CONCAT CXXFLAGS "${WARNING_FLAGS}" "${SDL_FLAGS}")
99+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 ${CFLAGS}")
100+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 ${CXXFLAGS}")
101+
set(CMAKE_C_FLAGS_DEBUG
102+
"${CMAKE_C_FLAGS_DEBUG} ${CFLAGS} -O0 -g -DDEBUG -Xsycl-target-frontend=spir64 \"-g0\""
103+
)
104+
set(CMAKE_CXX_FLAGS_DEBUG
105+
"${CMAKE_CXX_FLAGS_DEBUG} ${CXXFLAGS} -O0 -g -DDEBUG -Xsycl-target-frontend=spir64 \"-g0\""
106+
)
107+
set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_DEBUG} ${CFLAGS} -O1 -g1 -DDEBUG")
108+
set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG} ${CXXFLAGS} -O1 -g1 -DDEBUG")
109+
set(CMAKE_MODULE_LINKER_FLAGS_COVERAGE "${CMAKE_MODULE_LINKER_FLAGS_DEBUG}")
110+
set(DPCTL_LDFLAGS "-z,noexecstack,-z,relro,-z,now")
111+
mark_as_advanced(
112+
CMAKE_CXX_FLAGS_COVERAGE
113+
CMAKE_C_FLAGS_COVERAGE
114+
CMAKE_MODULE_LINKER_FLAGS_COVERAGE
115+
)
116+
else()
117+
message(FATAL_ERROR "Unsupported system.")
118+
endif()
119+
120+
# at build time create include/ directory and copy header files over
121+
set(DPCTL_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
122+
123+
set(CMAKE_INSTALL_RPATH "$ORIGIN")
124+
125+
function(build_dpctl_ext _trgt _src _dest)
126+
set(options SYCL)
127+
cmake_parse_arguments(BUILD_DPCTL_EXT "${options}" "RELATIVE_PATH" "" ${ARGN})
128+
add_cython_target(${_trgt} ${_src} CXX OUTPUT_VAR _generated_src)
129+
set(_cythonize_trgt "${_trgt}_cythonize_pyx")
130+
python_add_library(${_trgt} MODULE WITH_SOABI ${_generated_src})
131+
if(BUILD_DPCTL_EXT_SYCL)
132+
add_sycl_to_target(TARGET ${_trgt} SOURCES ${_generated_src})
133+
target_compile_options(${_trgt} PRIVATE -fno-sycl-id-queries-fit-in-int)
134+
target_link_options(${_trgt} PRIVATE -fsycl-device-code-split=per_kernel)
135+
if(DPCTL_OFFLOAD_COMPRESS)
136+
target_link_options(${_trgt} PRIVATE --offload-compress)
137+
endif()
138+
if(_dpctl_sycl_targets)
139+
# make fat binary
140+
target_compile_options(
141+
${_trgt}
142+
PRIVATE ${_dpctl_sycl_target_compile_options}
143+
)
144+
target_link_options(${_trgt} PRIVATE ${_dpctl_sycl_target_link_options})
145+
endif()
146+
endif()
147+
target_link_libraries(${_trgt} PRIVATE Python::NumPy)
148+
if(DPCTL_GENERATE_COVERAGE)
149+
target_compile_definitions(${_trgt} PRIVATE CYTHON_TRACE=1 CYTHON_TRACE_NOGIL=1)
150+
if(BUILD_DPCTL_EXT_SYCL)
151+
target_compile_options(${_trgt} PRIVATE -fno-sycl-use-footer)
152+
endif()
153+
endif()
154+
target_link_libraries(${_trgt} PRIVATE DPCTLSyclInterface)
155+
set(_linker_options "LINKER:${DPCTL_LDFLAGS}")
156+
target_link_options(${_trgt} PRIVATE ${_linker_options})
157+
get_filename_component(_name_wle ${_generated_src} NAME_WLE)
158+
get_filename_component(_generated_src_dir ${_generated_src} DIRECTORY)
159+
set(_generated_public_h "${_generated_src_dir}/${_name_wle}.h")
160+
set(_generated_api_h "${_generated_src_dir}/${_name_wle}_api.h")
161+
162+
# TODO: create separate folder inside build folder that contains only
163+
# headers related to this target and appropriate folder structure to
164+
# eliminate shadow dependencies
165+
get_filename_component(_generated_src_dir_dir ${_generated_src_dir} DIRECTORY)
166+
# TODO: do not set directory if we did not generate header
167+
target_include_directories(${_trgt} INTERFACE ${_generated_src_dir_dir})
168+
set(_rpath_value "$ORIGIN")
169+
if(BUILD_DPCTL_EXT_RELATIVE_PATH)
170+
set(_rpath_value "${_rpath_value}/${BUILD_DPCTL_EXT_RELATIVE_PATH}")
171+
endif()
172+
if(DPCTL_WITH_REDIST)
173+
set(_rpath_value "${_rpath_value}:${_rpath_value}/../../..")
174+
endif()
175+
set_target_properties(${_trgt} PROPERTIES INSTALL_RPATH ${_rpath_value})
176+
177+
install(TARGETS ${_trgt} LIBRARY DESTINATION ${_dest})
178+
install(
179+
FILES ${_generated_api_h}
180+
# TODO: revert to `${CMAKE_INSTALL_PREFIX}/dpctl/include/${_dest}`
181+
DESTINATION ${CMAKE_INSTALL_PREFIX}/dpctl_ext/include/${_dest}
182+
OPTIONAL
183+
)
184+
install(
185+
FILES ${_generated_public_h}
186+
# TODO: revert to `${CMAKE_INSTALL_PREFIX}/dpctl/include/${_dest}`
187+
DESTINATION ${CMAKE_INSTALL_PREFIX}/dpctl_ext/include/${_dest}
188+
OPTIONAL
189+
)
190+
if(DPCTL_GENERATE_COVERAGE)
191+
get_filename_component(_original_src_dir ${_src} DIRECTORY)
192+
file(RELATIVE_PATH _rel_dir ${CMAKE_SOURCE_DIR} ${_original_src_dir})
193+
install(FILES ${_generated_src} DESTINATION ${CMAKE_INSTALL_PREFIX}/${_rel_dir})
194+
endif()
195+
196+
# Create target with headers only, because python is managing all the
197+
# library imports at runtime
198+
set(_trgt_headers ${_trgt}_headers)
199+
add_library(${_trgt_headers} INTERFACE)
200+
add_dependencies(${_trgt_headers} ${_trgt})
201+
get_target_property(_trgt_headers_dir ${_trgt} INTERFACE_INCLUDE_DIRECTORIES)
202+
target_include_directories(${_trgt_headers} INTERFACE ${_trgt_headers_dir})
203+
endfunction()
204+
205+
add_subdirectory(tensor)

dpctl_ext/tensor/CMakeLists.txt

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# -*- coding: utf-8 -*-
2+
# *****************************************************************************
3+
# Copyright (c) 2026, Intel Corporation
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions are met:
8+
# - Redistributions of source code must retain the above copyright notice,
9+
# this list of conditions and the following disclaimer.
10+
# - Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions and the following disclaimer in the documentation
12+
# and/or other materials provided with the distribution.
13+
# - Neither the name of the copyright holder nor the names of its contributors
14+
# may be used to endorse or promote products derived from this software
15+
# without specific prior written permission.
16+
#
17+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27+
# THE POSSIBILITY OF SUCH DAMAGE.
28+
# *****************************************************************************
29+
30+
if(WIN32)
31+
if(${CMAKE_VERSION} VERSION_LESS "3.23")
32+
# this is a work-around for target_link_options inserting option after -link option, cause
33+
# linker to ignore it.
34+
set(CMAKE_CXX_LINK_FLAGS
35+
"${CMAKE_CXX_LINK_FLAGS} -fsycl-device-code-split=per_kernel"
36+
)
37+
endif()
38+
endif()
39+
40+
set(_static_lib_sources
41+
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/simplify_iteration_space.cpp
42+
)
43+
set(_tensor_impl_sources
44+
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/tensor_ctors.cpp
45+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/accumulators.cpp
46+
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/copy_and_cast_usm_to_usm.cpp
47+
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/copy_as_contig.cpp
48+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/copy_numpy_ndarray_into_usm_ndarray.cpp
49+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/copy_for_reshape.cpp
50+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/copy_for_roll.cpp
51+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/linear_sequences.cpp
52+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/integer_advanced_indexing.cpp
53+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/boolean_advanced_indexing.cpp
54+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/eye_ctor.cpp
55+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/full_ctor.cpp
56+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/zeros_ctor.cpp
57+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/triul_ctor.cpp
58+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/where.cpp
59+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/device_support_queries.cpp
60+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/repeat.cpp
61+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/clip.cpp
62+
)
63+
64+
set(_static_lib_trgt simplify_iteration_space)
65+
66+
add_library(${_static_lib_trgt} STATIC ${_static_lib_sources})
67+
target_include_directories(
68+
${_static_lib_trgt}
69+
PRIVATE
70+
${Python_INCLUDE_DIRS}
71+
${DPCTL_INCLUDE_DIR}
72+
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/include
73+
)
74+
target_link_libraries(${_static_lib_trgt} PRIVATE pybind11::headers ${Python_LIBRARIES})
75+
set_target_properties(${_static_lib_trgt} PROPERTIES POSITION_INDEPENDENT_CODE ON)
76+
77+
set(_py_trgts)
78+
79+
set(python_module_name _tensor_impl)
80+
pybind11_add_module(${python_module_name} MODULE ${_tensor_impl_sources})
81+
add_sycl_to_target(TARGET ${python_module_name} SOURCES ${_tensor_impl_sources})
82+
target_link_libraries(${python_module_name} PRIVATE ${_static_lib_trgt})
83+
list(APPEND _py_trgts ${python_module_name})
84+
85+
set(_clang_prefix "")
86+
if(WIN32)
87+
set(_clang_prefix "/clang:")
88+
endif()
89+
90+
set(_no_fast_math_sources
91+
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/copy_and_cast_usm_to_usm.cpp
92+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/full_ctor.cpp
93+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/linear_sequences.cpp
94+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/clip.cpp
95+
# ${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/where.cpp
96+
)
97+
list(
98+
APPEND _no_fast_math_sources
99+
# ${_elementwise_sources}
100+
# ${_reduction_sources}
101+
# ${_sorting_sources}
102+
# ${_linalg_sources}
103+
# ${_accumulator_sources}
104+
)
105+
106+
foreach(_src_fn ${_no_fast_math_sources})
107+
get_source_file_property(_cmpl_options_prop ${_src_fn} COMPILE_OPTIONS)
108+
set(_combined_options_prop ${_cmpl_options_prop} "${_clang_prefix}-fno-fast-math")
109+
set_source_files_properties(
110+
${_src_fn}
111+
PROPERTIES COMPILE_OPTIONS "${_combined_options_prop}"
112+
)
113+
endforeach()
114+
115+
set(_compiler_definitions "")
116+
117+
set(_linker_options "LINKER:${DPCTL_LDFLAGS}")
118+
foreach(python_module_name ${_py_trgts})
119+
target_compile_options(
120+
${python_module_name}
121+
PRIVATE -fno-sycl-id-queries-fit-in-int
122+
)
123+
target_link_options(
124+
${python_module_name}
125+
PRIVATE -fsycl-device-code-split=per_kernel
126+
)
127+
if(DPCTL_OFFLOAD_COMPRESS)
128+
target_link_options(${python_module_name} PRIVATE --offload-compress)
129+
endif()
130+
131+
target_include_directories(
132+
${python_module_name}
133+
PRIVATE
134+
${CMAKE_SOURCE_DIR}/dpnp/backend/include
135+
${Dpctl_INCLUDE_DIR}
136+
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/include
137+
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/
138+
)
139+
target_link_options(${python_module_name} PRIVATE ${_linker_options})
140+
if(DPCTL_GENERATE_COVERAGE)
141+
if(DPCTL_GENERATE_COVERAGE_FOR_PYBIND11_EXTENSIONS)
142+
target_compile_options(
143+
${python_module_name}
144+
PRIVATE -fprofile-instr-generate -fcoverage-mapping
145+
)
146+
endif()
147+
target_link_options(
148+
${python_module_name}
149+
PRIVATE -fprofile-instr-generate -fcoverage-mapping
150+
)
151+
endif()
152+
if(_dpctl_sycl_targets)
153+
# make fat binary
154+
target_compile_options(
155+
${python_module_name}
156+
PRIVATE ${_dpctl_sycl_target_compile_options}
157+
)
158+
target_link_options(
159+
${python_module_name}
160+
PRIVATE ${_dpctl_sycl_target_link_options}
161+
)
162+
endif()
163+
# TODO: update source so they reference individual libraries instead of
164+
# dpctl4pybind11.hpp. It will allow to simplify dependency tree
165+
# NOTE: dpctl C-API is resolved at runtime via Python
166+
# target_link_libraries(${python_module_name} PRIVATE DpctlCAPI)
167+
if(DPCTL_WITH_REDIST)
168+
set_target_properties(
169+
${python_module_name}
170+
PROPERTIES INSTALL_RPATH "$ORIGIN/../../../.."
171+
)
172+
endif()
173+
# TODO: revert to `DESTINATION "dpctl/tensor"`
174+
install(TARGETS ${python_module_name} DESTINATION "dpctl_ext/tensor")
175+
endforeach()

0 commit comments

Comments
 (0)