-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathrun_sycl.cmake
More file actions
190 lines (167 loc) · 6.71 KB
/
run_sycl.cmake
File metadata and controls
190 lines (167 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Copyright 2020-2025 Intel Corporation
#
# 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
##########################################################################
# This file runs the SYCL compiler commands to produce the desired output file
# along with the dependency file needed by CMake to compute dependencies.
# In addition the file checks the output of each command and if the command fails
# it deletes the output files.
# Input variables
#
# verbose:BOOL=<> OFF: Be as quiet as possible (default)
# ON : Describe each step
#
# generated_file:STRING=<> File to generate. This argument must be passed in.
cmake_policy(PUSH)
cmake_policy(SET CMP0007 NEW)
cmake_policy(SET CMP0010 NEW)
if(NOT generated_file)
message(FATAL_ERROR "You must specify generated_file on the command line")
endif()
set(CMAKE_COMMAND "@CMAKE_COMMAND@") # path
set(source_file "@source_file@") # path
set(SYCL_generated_dependency_file "@SYCL_generated_dependency_file@") # path
set(cmake_dependency_file "@cmake_dependency_file@") # path
set(SYCL_make2cmake "@SYCL_make2cmake@") # path
set(SYCL_host_compiler "@SYCL_HOST_COMPILER@") # path
set(generated_file_path "@generated_file_path@") # path
set(generated_file_internal "@generated_file@") # path
set(SYCL_executable "@SYCL_EXECUTABLE@") # path
set(SYCL_compile_flags @SYCL_COMPILE_FLAGS@) # list
set(SYCL_include_dirs [==[@SYCL_include_dirs@]==]) # list
set(SYCL_compile_definitions [==[@SYCL_compile_definitions@]==]) # list
list(REMOVE_DUPLICATES SYCL_INCLUDE_DIRS)
set(SYCL_include_args)
foreach(dir ${SYCL_include_dirs})
# Args with spaces need quotes around them to get them to be parsed as a single argument.
if(dir MATCHES " ")
list(APPEND SYCL_include_args "-I\"${dir}\"")
else()
list(APPEND SYCL_include_args -I${dir})
endif()
endforeach()
# Clean up list of compile definitions, add -D flags, and append to SYCL_compile_flags
list(REMOVE_DUPLICATES SYCL_compile_definitions)
foreach(def ${SYCL_compile_definitions})
list(APPEND SYCL_compile_flags "-D${def}")
endforeach()
# Choose host flags in FindSYCL.cmake
@SYCL_host_flags@
# Adding permissive flag for MSVC build to overcome ambiguous symbol error.
if(WIN32)
list(APPEND SYCL_compile_flags "/permissive-")
endif()
list(REMOVE_DUPLICATES CMAKE_HOST_FLAGS)
foreach(flag ${CMAKE_HOST_FLAGS})
# Extract -D defines from CMAKE_HOST_FLAGS and pass them directly to icpx,
# since host compiler is removed. This is needed for macros like
# HAVE_AVX512_CPU_DEFINITION that control signatures, to avoid
# symbol mismatch with libtorch_cpu.so.
if(flag MATCHES "^-D")
list(APPEND SYCL_compile_flags "${flag}")
endif()
endforeach()
# SYCL_execute_process - Executes a command with optional command echo and status message.
#
# status - Status message to print if verbose is true
# command - COMMAND argument from the usual execute_process argument structure
# ARGN - Remaining arguments are the command with arguments
#
# SYCL_result - return value from running the command
#
# Make this a macro instead of a function, so that things like RESULT_VARIABLE
# and other return variables are present after executing the process.
macro(SYCL_execute_process status command)
set(_command ${command})
if(NOT "x${_command}" STREQUAL "xCOMMAND")
message(FATAL_ERROR "Malformed call to SYCL_execute_process. Missing COMMAND as second argument. (command = ${command})")
endif()
if(verbose)
execute_process(COMMAND "${CMAKE_COMMAND}" -E echo -- ${status})
# Now we need to build up our command string. We are accounting for quotes
# and spaces, anything else is left up to the user to fix if they want to
# copy and paste a runnable command line.
set(SYCL_execute_process_string)
foreach(arg ${ARGN})
# If there are quotes, excape them, so they come through.
string(REPLACE "\"" "\\\"" arg ${arg})
# Args with spaces need quotes around them to get them to be parsed as a single argument.
if(arg MATCHES " ")
list(APPEND SYCL_execute_process_string "\"${arg}\"")
else()
list(APPEND SYCL_execute_process_string ${arg})
endif()
endforeach()
# Echo the command
execute_process(COMMAND ${CMAKE_COMMAND} -E echo ${SYCL_execute_process_string})
endif()
# Run the command
execute_process(COMMAND ${ARGN} RESULT_VARIABLE SYCL_result )
endmacro()
# Delete the target file
SYCL_execute_process(
"Removing ${generated_file}"
COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
)
# Generate the code
if(WIN32)
set(SYCL_dependency_file_args /clang:-MD /clang:-MF /clang:${SYCL_generated_dependency_file})
else()
set(SYCL_dependency_file_args -MD -MF "${SYCL_generated_dependency_file}")
endif()
SYCL_execute_process(
"Generating ${generated_file}"
COMMAND "${SYCL_executable}"
${SYCL_dependency_file_args}
-c
"${source_file}"
-o "${generated_file}"
${SYCL_include_args}
${SYCL_compile_flags}
)
if(SYCL_result)
SYCL_execute_process(
"Removing ${generated_file}"
COMMAND "${CMAKE_COMMAND}" -E remove "${generated_file}"
)
message(FATAL_ERROR "Error generating file ${generated_file}")
endif()
# Parse *.d file to retrieve included headers. These headers are dependencies
# of custom compilation command. Inform cmake to scan these files and
# retrigger compilation if anything change in these headers.
SYCL_execute_process(
"Generating temporary cmake readable file: ${cmake_dependency_file}.tmp"
COMMAND "${CMAKE_COMMAND}"
-D "input_file:FILEPATH=${SYCL_generated_dependency_file}"
-D "output_file:FILEPATH=${cmake_dependency_file}.tmp"
-D "verbose=${verbose}"
-P "${SYCL_make2cmake}"
)
if(SYCL_result)
message(FATAL_ERROR "Error generating ${generated_file}")
endif()
# Update dependencies list. When we remove some header in .cpp, then the
# header should be removed from dependencies list. Or unnecessary re-compilation
# will be triggered, when the header changes.
SYCL_execute_process(
"Copy if different ${cmake_dependency_file}.tmp to ${cmake_dependency_file}"
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${cmake_dependency_file}.tmp" "${cmake_dependency_file}"
)
if(SYCL_result)
message(FATAL_ERROR "Error generating ${generated_file}")
endif()
SYCL_execute_process(
"Removing ${cmake_dependency_file}.tmp and ${SYCL_generated_dependency_file}"
COMMAND "${CMAKE_COMMAND}" -E remove "${cmake_dependency_file}.tmp" "${SYCL_generated_dependency_file}"
)
if(SYCL_result)
message(FATAL_ERROR "Error generating ${generated_file}")
endif()
if(verbose)
message("Generated ${generated_file} successfully.")
endif()
cmake_policy(POP)