-
Notifications
You must be signed in to change notification settings - Fork 0
/
cl_offline_compiler.sh.in.cmake
286 lines (249 loc) · 9.01 KB
/
cl_offline_compiler.sh.in.cmake
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/bin/bash
#=============================================================================
# cl_offline_compiler.sh script
#
# Copyright (c) 2022-2024 Michal Babej / Intel Finland Oy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#=============================================================================
# Takes an OpenCL C source file and uses Clang + LLVM-SPIRV to convert it to
# a SPIR-V binary. Used by OpenCL-CTS in offline compilation mode. Unlike
# poclcc, this does not return a poclbinary, but a SPIR-V binary, and is
# somewhat independent of PoCL (there is some option / extension processing
# that is PoCL-specific)
# Likely requires recent Clang (14 tested, 13 and older untested).
# Might work with -cl-std < 3.0 with older Clangs, but 3.0 requires 14+
# mandatory arguments:
# --source FILE OpenCL C source file to compile
# --output FILE SPIR-V or binary file to create
# --cl-device-info FILE OpenCL device info file
#
# optional arguments:
# --debug Enable some debugging output
# --mode compilation mode (spir-v or binary)
MODE=spir-v
DEBUG=false
for i in "$@"; do
case $i in
--source=*)
SOURCE="${i#*=}"
shift
;;
--output=*)
OUTPUT="${i#*=}"
shift
;;
--cl-device-info=*)
CL_DEV_INFO="${i#*=}"
shift
;;
--mode=*)
MODE="${i#*=}"
shift
;;
--debug)
DEBUG=true
shift
;;
--)
shift
break
;;
*)
;;
esac
done
if [ -z "$SOURCE" ] || [ -z "$OUTPUT" ] || [ -z "$CL_DEV_INFO" ]; then
echo "USAGE: $0 --source=/path/source.cl --output=/path/output.spv --cl-device-info=/path/to/dev-info.txt [--debug] [--mode=MODE] -- [opencl compile options...]"
exit 1
fi
if [ "$MODE" != "spir-v" ]; then
echo "this script only compiles to SPIR-V"
exit 1
fi
if [ -f "$OUTPUT" ]; then
echo "output already exists, skipping compilation"
exit 0
fi
CL_IS_30=false
CL_FAST_MATH=false
CL_UNSAFE_MATH=false
BUILD_OPTIONS="-cl-opt-disable"
CL_STD="-cl-std=CL1.2"
for i in "$@"; do
case $i in
-cl-std=*)
CL_STD="$i"
shift
;;
-cl-fast-relaxed-math)
CL_FAST_MATH=true
shift
;;
-cl-unsafe-math)
CL_UNSAFE_MATH=true
shift
;;
*)
BUILD_OPTIONS="$BUILD_OPTIONS $i"
shift
;;
esac
done
if [ "$CL_STD" = "-cl-std=CL3.0" ]; then
CL_IS_30=true
fi
TARGET=none
DEV_VER=100
DEV_C_VER=100
CL_EXT_DEFS="-D__ENDIAN_LITTLE__=1"
# TODO there is not enough information to figure out the feature macros
# that might be supported by the device, because --cl-device-info FILE does
# not contain the list of the feature macros.
# However, at least __opencl_c_generic_address_space is required by the
# C11 atomic tests in SPIR-V mode. This is therefore a hack, but should
# be fine because PoCL's Level0 and CPU drivers support gen. AS; atomic_order
# features are optional, but again they are supported by both CPU and L0
CL_EXTS="-Xclang -cl-ext=-all,+__opencl_c_generic_address_space,+__opencl_c_atomic_order_acq_rel,+__opencl_c_atomic_order_seq_cst,+__opencl_c_atomic_scope_device"
if [ -e "${CL_DEV_INFO}" ]; then
echo "CL_DEV_INFO: ${CL_DEV_INFO}"
source ${CL_DEV_INFO}
if [ "$CL_DEVICE_IMAGE_SUPPORT" -eq 1 ]; then
if [ "$CL_IS_30" = "true" ]; then
CL_EXTS="${CL_EXTS},+__opencl_c_images"
fi
CL_EXT_DEFS="${CL_EXT_DEFS} -D__IMAGE_SUPPORT__=1"
else
if [ "$CL_IS_30" = "true" ]; then
CL_EXTS="${CL_EXTS},-__opencl_c_images"
fi
CL_EXT_DEFS="${CL_EXT_DEFS} -U__IMAGE_SUPPORT__ -D__undef___opencl_c_read_write_images"
fi
if [ "$CL_FAST_MATH" = "true" ]; then
CL_EXT_DEFS="${CL_EXT_DEFS} -cl-finite-math-only"
CL_UNSAFE_MATH=true
fi
if [ "$CL_UNSAFE_MATH" = "true" ]; then
CL_EXT_DEFS="${CL_EXT_DEFS} -cl-no-signed-zeros -cl-mad-enable -ffp-contract=fast"
fi
if [[ "$CL_DEVICE_VERSION" =~ "PoCL" ]] && [ "$CL_IS_30" = "true" ]; then
if [[ "$CL_DEVICE_VERSION" =~ "basic" ]] || [[ "$CL_DEVICE_VERSION" =~ "pthread" ]] || [[ "$CL_DEVICE_VERSION" =~ "cpu" ]]; then
CL_EXT_DEFS="${CL_EXT_DEFS} -D__opencl_c_named_address_space_builtins=1 -D__opencl_c_int64=1 -D__opencl_c_atomic_order_acq_rel=1 -D__opencl_c_atomic_order_seq_cst=1 -D__opencl_c_atomic_scope_device=1 -D__opencl_c_program_scope_global_variables=1 -D__opencl_c_generic_address_space=1"
CL_EXTS="${CL_EXTS},+__opencl_c_named_address_space_builtins,+__opencl_c_int64,+__opencl_c_atomic_order_acq_rel,+__opencl_c_atomic_order_seq_cst,+__opencl_c_atomic_scope_device,+__opencl_c_program_scope_global_variables,+__opencl_c_generic_address_space"
fi
fi
if [[ $CL_DEVICE_ADDRESS_BITS==64 ]]; then
TARGET=spir64-unknown-unknown
elif [[ $CL_DEVICE_ADDRESS_BITS==32 ]]; then
TARGET=spir-unknown-unknown
else
echo "unknown device address bits: ${CL_DEVICE_ADDRESS_BITS}"
exit 1
fi
if [[ "$CL_DEVICE_VERSION" =~ "OpenCL 3.0" ]]; then
DEV_VER=300
elif [[ "$CL_DEVICE_VERSION" =~ "OpenCL 2.2" ]]; then
DEV_VER=220
elif [[ "$CL_DEVICE_VERSION" =~ "OpenCL 2.1" ]]; then
DEV_VER=210
elif [[ "$CL_DEVICE_VERSION" =~ "OpenCL 2.0" ]]; then
DEV_VER=200
elif [[ "$CL_DEVICE_VERSION" =~ "OpenCL 1.2" ]]; then
DEV_VER=120
elif [[ "$CL_DEVICE_VERSION" =~ "OpenCL 1.1" ]]; then
DEV_VER=110
elif [[ "$CL_DEVICE_VERSION" =~ "OpenCL 1.0" ]]; then
DEV_VER=100
else
echo "unknown device version: ${CL_DEVICE_VERSION}"
exit 1
fi
BUILD_OPTIONS="$BUILD_OPTIONS -D__OPENCL_VERSION__=${DEV_VER}"
if [[ "$CL_STD" =~ "CL3.0" ]]; then
DEV_C_VER=300
elif [[ "$CL_STD" =~ "CL2.2" ]]; then
DEV_C_VER=220
elif [[ "$CL_STD" =~ "CL2.1" ]]; then
DEV_C_VER=210
elif [[ "$CL_STD" =~ "CL2.0" ]]; then
DEV_C_VER=200
elif [[ "$CL_STD" =~ "CL1.2" ]]; then
DEV_C_VER=120
elif [[ "$CL_STD" =~ "CL1.1" ]]; then
DEV_C_VER=110
elif [[ "$CL_STD" =~ "CL1.0" ]]; then
DEV_C_VER=100
else
echo "unknown device C version: ${CL_STD}"
exit 1
fi
BUILD_OPTIONS="$BUILD_OPTIONS -D__OPENCL_C_VERSION__=${DEV_C_VER}"
for EXT in $CL_DEVICE_EXTENSIONS ; do
CL_EXT_DEFS="${CL_EXT_DEFS} -D${EXT}"
CL_EXTS="${CL_EXTS},+${EXT}"
if [ "$CL_IS_30" = "true" ]; then
case $EXT in
cl_khr_3d_image_writes)
CL_EXT_DEFS="${CL_EXT_DEFS} -D__opencl_c_3d_image_writes=1"
CL_EXTS="${CL_EXTS},+__opencl_c_3d_image_writes"
;;
cl_khr_fp64)
CL_EXT_DEFS="${CL_EXT_DEFS} -D__opencl_c_fp64=1"
CL_EXTS="${CL_EXTS},+__opencl_c_fp64"
;;
cl_khr_fp16)
CL_EXT_DEFS="${CL_EXT_DEFS} -D__opencl_c_fp16=1"
CL_EXTS="${CL_EXTS},+__opencl_c_fp16"
;;
esac
fi
done
BUILD_OPTIONS="${BUILD_OPTIONS} ${CL_EXT_DEFS} ${CL_EXTS}"
# TODO check IL_VER
fi
SOURCE_BASE=$(basename ${SOURCE})
TEMP_BC_FILE=$(mktemp --tmpdir ${SOURCE_BASE}.XXXXXX.bc)
TEMP_SPV_ATOM_FILE=$(mktemp --tmpdir ${SOURCE_BASE}.atom.XXXXXX.spv)
TEMP_SPV_FINAL_FILE=$(mktemp --tmpdir ${SOURCE_BASE}.final.XXXXXX.spv)
function cleanup {
rm -f "${TEMP_BC_FILE}" "${TEMP_SPV_FINAL_FILE}" "${TEMP_SPV_ATOM_FILE}"
}
trap cleanup EXIT
if [ "$DEBUG" = "true" ]; then
echo "SOURCE: ${SOURCE}"
echo "TEMP_BC: ${TEMP_BC_FILE}"
echo "OUTPUT: ${OUTPUT}"
fi
CLANG_OPTIONS="--target=${TARGET} -x cl ${CL_STD} ${BUILD_OPTIONS} -o ${TEMP_BC_FILE} -emit-llvm -c ${SOURCE}"
LLVM_SPIRV_OPTIONS="--spirv-gen-kernel-arg-name-md --spirv-max-version=1.2 -o ${TEMP_SPV_ATOM_FILE} ${TEMP_BC_FILE}"
if [ "$DEBUG" = "true" ]; then
echo "Running @CLANG@ ${CLANG_OPTIONS}"
fi
@CLANG@ ${CLANG_OPTIONS} || exit 1
if [ "$DEBUG" = "true" ]; then
echo "Running @LLVM_SPIRV@ ${LLVM_SPIRV_OPTIONS}"
fi
@LLVM_SPIRV@ ${LLVM_SPIRV_OPTIONS} || exit 1
if [ "$DEBUG" = "true" ]; then
echo "Running $<TARGET_FILE:spirv_fix_atomic_compare_exchange> "${TEMP_SPV_ATOM_FILE}" "${TEMP_SPV_FINAL_FILE}""
fi
$<TARGET_FILE:spirv_fix_atomic_compare_exchange> "${TEMP_SPV_ATOM_FILE}" "${TEMP_SPV_FINAL_FILE}"
mv -n "${TEMP_SPV_FINAL_FILE}" "${OUTPUT}" || echo "output already exists, skipping write"
exit 0