vtn/opencl: native sqrt support
[mesa.git] / src / compiler / spirv / vtn_opencl.c
1 /*
2 * Copyright © 2018 Red Hat
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark (robdclark@gmail.com)
25 */
26
27 #include "math.h"
28 #include "nir/nir_builtin_builder.h"
29
30 #include "vtn_private.h"
31 #include "OpenCL.std.h"
32
33 typedef nir_ssa_def *(*nir_handler)(struct vtn_builder *b,
34 enum OpenCLstd_Entrypoints opcode,
35 unsigned num_srcs, nir_ssa_def **srcs,
36 const struct glsl_type *dest_type);
37
38 static void
39 handle_instr(struct vtn_builder *b, enum OpenCLstd_Entrypoints opcode,
40 const uint32_t *w, unsigned count, nir_handler handler)
41 {
42 const struct glsl_type *dest_type =
43 vtn_value(b, w[1], vtn_value_type_type)->type->type;
44
45 unsigned num_srcs = count - 5;
46 nir_ssa_def *srcs[3] = { NULL };
47 vtn_assert(num_srcs <= ARRAY_SIZE(srcs));
48 for (unsigned i = 0; i < num_srcs; i++) {
49 srcs[i] = vtn_ssa_value(b, w[i + 5])->def;
50 }
51
52 nir_ssa_def *result = handler(b, opcode, num_srcs, srcs, dest_type);
53 if (result) {
54 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
55 val->ssa = vtn_create_ssa_value(b, dest_type);
56 val->ssa->def = result;
57 } else {
58 vtn_assert(dest_type == glsl_void_type());
59 }
60 }
61
62 static nir_op
63 nir_alu_op_for_opencl_opcode(struct vtn_builder *b,
64 enum OpenCLstd_Entrypoints opcode)
65 {
66 switch (opcode) {
67 case OpenCLstd_Fabs: return nir_op_fabs;
68 case OpenCLstd_SAbs: return nir_op_iabs;
69 case OpenCLstd_SAdd_sat: return nir_op_iadd_sat;
70 case OpenCLstd_UAdd_sat: return nir_op_uadd_sat;
71 case OpenCLstd_Ceil: return nir_op_fceil;
72 case OpenCLstd_Cos: return nir_op_fcos;
73 case OpenCLstd_Exp2: return nir_op_fexp2;
74 case OpenCLstd_Log2: return nir_op_flog2;
75 case OpenCLstd_Floor: return nir_op_ffloor;
76 case OpenCLstd_SHadd: return nir_op_ihadd;
77 case OpenCLstd_UHadd: return nir_op_uhadd;
78 case OpenCLstd_Fma: return nir_op_ffma;
79 case OpenCLstd_Fmax: return nir_op_fmax;
80 case OpenCLstd_SMax: return nir_op_imax;
81 case OpenCLstd_UMax: return nir_op_umax;
82 case OpenCLstd_Fmin: return nir_op_fmin;
83 case OpenCLstd_SMin: return nir_op_imin;
84 case OpenCLstd_UMin: return nir_op_umin;
85 case OpenCLstd_Fmod: return nir_op_fmod;
86 case OpenCLstd_Mix: return nir_op_flrp;
87 case OpenCLstd_Native_cos: return nir_op_fcos;
88 case OpenCLstd_Native_divide: return nir_op_fdiv;
89 case OpenCLstd_Native_exp2: return nir_op_fexp2;
90 case OpenCLstd_Native_log2: return nir_op_flog2;
91 case OpenCLstd_Native_powr: return nir_op_fpow;
92 case OpenCLstd_Native_recip: return nir_op_frcp;
93 case OpenCLstd_Native_rsqrt: return nir_op_frsq;
94 case OpenCLstd_Native_sin: return nir_op_fsin;
95 case OpenCLstd_Native_sqrt: return nir_op_fsqrt;
96 case OpenCLstd_SMul_hi: return nir_op_imul_high;
97 case OpenCLstd_UMul_hi: return nir_op_umul_high;
98 case OpenCLstd_Popcount: return nir_op_bit_count;
99 case OpenCLstd_Pow: return nir_op_fpow;
100 case OpenCLstd_Remainder: return nir_op_frem;
101 case OpenCLstd_SRhadd: return nir_op_irhadd;
102 case OpenCLstd_URhadd: return nir_op_urhadd;
103 case OpenCLstd_Rsqrt: return nir_op_frsq;
104 case OpenCLstd_Sign: return nir_op_fsign;
105 case OpenCLstd_Sin: return nir_op_fsin;
106 case OpenCLstd_Sqrt: return nir_op_fsqrt;
107 case OpenCLstd_SSub_sat: return nir_op_isub_sat;
108 case OpenCLstd_USub_sat: return nir_op_usub_sat;
109 case OpenCLstd_Trunc: return nir_op_ftrunc;
110 case OpenCLstd_Rint: return nir_op_fround_even;
111 /* uhm... */
112 case OpenCLstd_UAbs: return nir_op_mov;
113 default:
114 vtn_fail("No NIR equivalent");
115 }
116 }
117
118 static nir_ssa_def *
119 handle_alu(struct vtn_builder *b, enum OpenCLstd_Entrypoints opcode,
120 unsigned num_srcs, nir_ssa_def **srcs,
121 const struct glsl_type *dest_type)
122 {
123 return nir_build_alu(&b->nb, nir_alu_op_for_opencl_opcode(b, opcode),
124 srcs[0], srcs[1], srcs[2], NULL);
125 }
126
127 static nir_ssa_def *
128 handle_special(struct vtn_builder *b, enum OpenCLstd_Entrypoints opcode,
129 unsigned num_srcs, nir_ssa_def **srcs,
130 const struct glsl_type *dest_type)
131 {
132 nir_builder *nb = &b->nb;
133
134 switch (opcode) {
135 case OpenCLstd_SAbs_diff:
136 return nir_iabs_diff(nb, srcs[0], srcs[1]);
137 case OpenCLstd_UAbs_diff:
138 return nir_uabs_diff(nb, srcs[0], srcs[1]);
139 case OpenCLstd_Bitselect:
140 return nir_bitselect(nb, srcs[0], srcs[1], srcs[2]);
141 case OpenCLstd_SMad_hi:
142 return nir_imad_hi(nb, srcs[0], srcs[1], srcs[2]);
143 case OpenCLstd_UMad_hi:
144 return nir_umad_hi(nb, srcs[0], srcs[1], srcs[2]);
145 case OpenCLstd_SMul24:
146 return nir_imul24(nb, srcs[0], srcs[1]);
147 case OpenCLstd_UMul24:
148 return nir_umul24(nb, srcs[0], srcs[1]);
149 case OpenCLstd_SMad24:
150 return nir_imad24(nb, srcs[0], srcs[1], srcs[2]);
151 case OpenCLstd_UMad24:
152 return nir_umad24(nb, srcs[0], srcs[1], srcs[2]);
153 case OpenCLstd_FClamp:
154 return nir_fclamp(nb, srcs[0], srcs[1], srcs[2]);
155 case OpenCLstd_SClamp:
156 return nir_iclamp(nb, srcs[0], srcs[1], srcs[2]);
157 case OpenCLstd_UClamp:
158 return nir_uclamp(nb, srcs[0], srcs[1], srcs[2]);
159 case OpenCLstd_Copysign:
160 return nir_copysign(nb, srcs[0], srcs[1]);
161 case OpenCLstd_Cross:
162 if (glsl_get_components(dest_type) == 4)
163 return nir_cross4(nb, srcs[0], srcs[1]);
164 return nir_cross3(nb, srcs[0], srcs[1]);
165 case OpenCLstd_Degrees:
166 return nir_degrees(nb, srcs[0]);
167 case OpenCLstd_Fdim:
168 return nir_fdim(nb, srcs[0], srcs[1]);
169 case OpenCLstd_Distance:
170 return nir_distance(nb, srcs[0], srcs[1]);
171 case OpenCLstd_Fast_distance:
172 return nir_fast_distance(nb, srcs[0], srcs[1]);
173 case OpenCLstd_Fast_length:
174 return nir_fast_length(nb, srcs[0]);
175 case OpenCLstd_Fast_normalize:
176 return nir_fast_normalize(nb, srcs[0]);
177 case OpenCLstd_Length:
178 return nir_length(nb, srcs[0]);
179 case OpenCLstd_Mad:
180 return nir_fmad(nb, srcs[0], srcs[1], srcs[2]);
181 case OpenCLstd_Maxmag:
182 return nir_maxmag(nb, srcs[0], srcs[1]);
183 case OpenCLstd_Minmag:
184 return nir_minmag(nb, srcs[0], srcs[1]);
185 case OpenCLstd_Nan:
186 return nir_nan(nb, srcs[0]);
187 case OpenCLstd_Nextafter:
188 return nir_nextafter(nb, srcs[0], srcs[1]);
189 case OpenCLstd_Normalize:
190 return nir_normalize(nb, srcs[0]);
191 case OpenCLstd_Radians:
192 return nir_radians(nb, srcs[0]);
193 case OpenCLstd_Rotate:
194 return nir_rotate(nb, srcs[0], srcs[1]);
195 case OpenCLstd_Smoothstep:
196 return nir_smoothstep(nb, srcs[0], srcs[1], srcs[2]);
197 case OpenCLstd_Clz:
198 return nir_clz_u(nb, srcs[0]);
199 case OpenCLstd_Select:
200 return nir_select(nb, srcs[0], srcs[1], srcs[2]);
201 case OpenCLstd_Step:
202 return nir_sge(nb, srcs[1], srcs[0]);
203 case OpenCLstd_S_Upsample:
204 case OpenCLstd_U_Upsample:
205 return nir_upsample(nb, srcs[0], srcs[1]);
206 case OpenCLstd_Native_exp:
207 return nir_fexp(nb, srcs[0]);
208 case OpenCLstd_Native_exp10:
209 return nir_fexp2(nb, nir_fmul_imm(nb, srcs[0], log(10) / log(2)));
210 case OpenCLstd_Native_log:
211 return nir_flog(nb, srcs[0]);
212 case OpenCLstd_Native_log10:
213 return nir_fmul_imm(nb, nir_flog2(nb, srcs[0]), log(2) / log(10));
214 case OpenCLstd_Native_tan:
215 return nir_ftan(nb, srcs[0]);
216 default:
217 vtn_fail("No NIR equivalent");
218 return NULL;
219 }
220 }
221
222 static void
223 _handle_v_load_store(struct vtn_builder *b, enum OpenCLstd_Entrypoints opcode,
224 const uint32_t *w, unsigned count, bool load)
225 {
226 struct vtn_type *type;
227 if (load)
228 type = vtn_value(b, w[1], vtn_value_type_type)->type;
229 else
230 type = vtn_untyped_value(b, w[5])->type;
231 unsigned a = load ? 0 : 1;
232
233 const struct glsl_type *dest_type = type->type;
234 unsigned components = glsl_get_vector_elements(dest_type);
235
236 nir_ssa_def *offset = vtn_ssa_value(b, w[5 + a])->def;
237 struct vtn_value *p = vtn_value(b, w[6 + a], vtn_value_type_pointer);
238
239 struct vtn_ssa_value *comps[NIR_MAX_VEC_COMPONENTS];
240 nir_ssa_def *ncomps[NIR_MAX_VEC_COMPONENTS];
241
242 nir_ssa_def *moffset = nir_imul_imm(&b->nb, offset, components);
243 nir_deref_instr *deref = vtn_pointer_to_deref(b, p->pointer);
244
245 for (int i = 0; i < components; i++) {
246 nir_ssa_def *coffset = nir_iadd_imm(&b->nb, moffset, i);
247 nir_deref_instr *arr_deref = nir_build_deref_ptr_as_array(&b->nb, deref, coffset);
248
249 if (load) {
250 comps[i] = vtn_local_load(b, arr_deref, p->type->access);
251 ncomps[i] = comps[i]->def;
252 } else {
253 struct vtn_ssa_value *ssa = vtn_create_ssa_value(b, glsl_scalar_type(glsl_get_base_type(dest_type)));
254 struct vtn_ssa_value *val = vtn_ssa_value(b, w[5]);
255 ssa->def = nir_channel(&b->nb, val->def, i);
256 vtn_local_store(b, ssa, arr_deref, p->type->access);
257 }
258 }
259 if (load) {
260 struct vtn_ssa_value *ssa = vtn_create_ssa_value(b, dest_type);
261 ssa->def = nir_vec(&b->nb, ncomps, components);
262 vtn_push_ssa(b, w[2], type, ssa);
263 }
264 }
265
266 static void
267 vtn_handle_opencl_vload(struct vtn_builder *b, enum OpenCLstd_Entrypoints opcode,
268 const uint32_t *w, unsigned count)
269 {
270 _handle_v_load_store(b, opcode, w, count, true);
271 }
272
273 static void
274 vtn_handle_opencl_vstore(struct vtn_builder *b, enum OpenCLstd_Entrypoints opcode,
275 const uint32_t *w, unsigned count)
276 {
277 _handle_v_load_store(b, opcode, w, count, false);
278 }
279
280 static nir_ssa_def *
281 handle_printf(struct vtn_builder *b, enum OpenCLstd_Entrypoints opcode,
282 unsigned num_srcs, nir_ssa_def **srcs,
283 const struct glsl_type *dest_type)
284 {
285 /* hahah, yeah, right.. */
286 return nir_imm_int(&b->nb, -1);
287 }
288
289 static nir_ssa_def *
290 handle_shuffle(struct vtn_builder *b, enum OpenCLstd_Entrypoints opcode, unsigned num_srcs,
291 nir_ssa_def **srcs, const struct glsl_type *dest_type)
292 {
293 struct nir_ssa_def *input = srcs[0];
294 struct nir_ssa_def *mask = srcs[1];
295
296 unsigned out_elems = glsl_get_vector_elements(dest_type);
297 nir_ssa_def *outres[NIR_MAX_VEC_COMPONENTS];
298 unsigned in_elems = input->num_components;
299 if (mask->bit_size != 32)
300 mask = nir_u2u32(&b->nb, mask);
301 mask = nir_iand(&b->nb, mask, nir_imm_intN_t(&b->nb, in_elems - 1, mask->bit_size));
302 for (unsigned i = 0; i < out_elems; i++)
303 outres[i] = nir_vector_extract(&b->nb, input, nir_channel(&b->nb, mask, i));
304
305 return nir_vec(&b->nb, outres, out_elems);
306 }
307
308 static nir_ssa_def *
309 handle_shuffle2(struct vtn_builder *b, enum OpenCLstd_Entrypoints opcode, unsigned num_srcs,
310 nir_ssa_def **srcs, const struct glsl_type *dest_type)
311 {
312 struct nir_ssa_def *input0 = srcs[0];
313 struct nir_ssa_def *input1 = srcs[1];
314 struct nir_ssa_def *mask = srcs[2];
315
316 unsigned out_elems = glsl_get_vector_elements(dest_type);
317 nir_ssa_def *outres[NIR_MAX_VEC_COMPONENTS];
318 unsigned in_elems = input0->num_components;
319 unsigned total_mask = 2 * in_elems - 1;
320 unsigned half_mask = in_elems - 1;
321 if (mask->bit_size != 32)
322 mask = nir_u2u32(&b->nb, mask);
323 mask = nir_iand(&b->nb, mask, nir_imm_intN_t(&b->nb, total_mask, mask->bit_size));
324 for (unsigned i = 0; i < out_elems; i++) {
325 nir_ssa_def *this_mask = nir_channel(&b->nb, mask, i);
326 nir_ssa_def *vmask = nir_iand(&b->nb, this_mask, nir_imm_intN_t(&b->nb, half_mask, mask->bit_size));
327 nir_ssa_def *val0 = nir_vector_extract(&b->nb, input0, vmask);
328 nir_ssa_def *val1 = nir_vector_extract(&b->nb, input1, vmask);
329 nir_ssa_def *sel = nir_ilt(&b->nb, this_mask, nir_imm_intN_t(&b->nb, in_elems, mask->bit_size));
330 outres[i] = nir_bcsel(&b->nb, sel, val0, val1);
331 }
332 return nir_vec(&b->nb, outres, out_elems);
333 }
334
335 bool
336 vtn_handle_opencl_instruction(struct vtn_builder *b, SpvOp ext_opcode,
337 const uint32_t *w, unsigned count)
338 {
339 enum OpenCLstd_Entrypoints cl_opcode = (enum OpenCLstd_Entrypoints) ext_opcode;
340
341 switch (cl_opcode) {
342 case OpenCLstd_Fabs:
343 case OpenCLstd_SAbs:
344 case OpenCLstd_UAbs:
345 case OpenCLstd_SAdd_sat:
346 case OpenCLstd_UAdd_sat:
347 case OpenCLstd_Ceil:
348 case OpenCLstd_Cos:
349 case OpenCLstd_Exp2:
350 case OpenCLstd_Log2:
351 case OpenCLstd_Floor:
352 case OpenCLstd_Fma:
353 case OpenCLstd_Fmax:
354 case OpenCLstd_SHadd:
355 case OpenCLstd_UHadd:
356 case OpenCLstd_SMax:
357 case OpenCLstd_UMax:
358 case OpenCLstd_Fmin:
359 case OpenCLstd_SMin:
360 case OpenCLstd_UMin:
361 case OpenCLstd_Mix:
362 case OpenCLstd_Native_cos:
363 case OpenCLstd_Native_divide:
364 case OpenCLstd_Native_exp2:
365 case OpenCLstd_Native_log2:
366 case OpenCLstd_Native_powr:
367 case OpenCLstd_Native_recip:
368 case OpenCLstd_Native_rsqrt:
369 case OpenCLstd_Native_sin:
370 case OpenCLstd_Native_sqrt:
371 case OpenCLstd_Fmod:
372 case OpenCLstd_SMul_hi:
373 case OpenCLstd_UMul_hi:
374 case OpenCLstd_Popcount:
375 case OpenCLstd_Pow:
376 case OpenCLstd_Remainder:
377 case OpenCLstd_SRhadd:
378 case OpenCLstd_URhadd:
379 case OpenCLstd_Rsqrt:
380 case OpenCLstd_Sign:
381 case OpenCLstd_Sin:
382 case OpenCLstd_Sqrt:
383 case OpenCLstd_SSub_sat:
384 case OpenCLstd_USub_sat:
385 case OpenCLstd_Trunc:
386 case OpenCLstd_Rint:
387 handle_instr(b, cl_opcode, w, count, handle_alu);
388 return true;
389 case OpenCLstd_SAbs_diff:
390 case OpenCLstd_UAbs_diff:
391 case OpenCLstd_SMad_hi:
392 case OpenCLstd_UMad_hi:
393 case OpenCLstd_SMad24:
394 case OpenCLstd_UMad24:
395 case OpenCLstd_SMul24:
396 case OpenCLstd_UMul24:
397 case OpenCLstd_Bitselect:
398 case OpenCLstd_FClamp:
399 case OpenCLstd_SClamp:
400 case OpenCLstd_UClamp:
401 case OpenCLstd_Copysign:
402 case OpenCLstd_Cross:
403 case OpenCLstd_Degrees:
404 case OpenCLstd_Fdim:
405 case OpenCLstd_Distance:
406 case OpenCLstd_Fast_distance:
407 case OpenCLstd_Fast_length:
408 case OpenCLstd_Fast_normalize:
409 case OpenCLstd_Length:
410 case OpenCLstd_Mad:
411 case OpenCLstd_Maxmag:
412 case OpenCLstd_Minmag:
413 case OpenCLstd_Nan:
414 case OpenCLstd_Nextafter:
415 case OpenCLstd_Normalize:
416 case OpenCLstd_Radians:
417 case OpenCLstd_Rotate:
418 case OpenCLstd_Select:
419 case OpenCLstd_Step:
420 case OpenCLstd_Smoothstep:
421 case OpenCLstd_S_Upsample:
422 case OpenCLstd_U_Upsample:
423 case OpenCLstd_Clz:
424 case OpenCLstd_Native_exp:
425 case OpenCLstd_Native_exp10:
426 case OpenCLstd_Native_log:
427 case OpenCLstd_Native_log10:
428 case OpenCLstd_Native_tan:
429 handle_instr(b, cl_opcode, w, count, handle_special);
430 return true;
431 case OpenCLstd_Vloadn:
432 vtn_handle_opencl_vload(b, cl_opcode, w, count);
433 return true;
434 case OpenCLstd_Vstoren:
435 vtn_handle_opencl_vstore(b, cl_opcode, w, count);
436 return true;
437 case OpenCLstd_Shuffle:
438 handle_instr(b, cl_opcode, w, count, handle_shuffle);
439 return true;
440 case OpenCLstd_Shuffle2:
441 handle_instr(b, cl_opcode, w, count, handle_shuffle2);
442 return true;
443 case OpenCLstd_Printf:
444 handle_instr(b, cl_opcode, w, count, handle_printf);
445 return true;
446 case OpenCLstd_Prefetch:
447 /* TODO maybe add a nir instruction for this? */
448 return true;
449 default:
450 vtn_fail("unhandled opencl opc: %u\n", ext_opcode);
451 return false;
452 }
453 }