spirv/nir/opencl: handle some multiply instructions.
[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
29 #include "nir/nir_builtin_builder.h"
30
31 #include "vtn_private.h"
32 #include "OpenCL.std.h"
33
34 typedef nir_ssa_def *(*nir_handler)(struct vtn_builder *b,
35 enum OpenCLstd_Entrypoints opcode,
36 unsigned num_srcs, nir_ssa_def **srcs,
37 const struct glsl_type *dest_type);
38
39 static void
40 handle_instr(struct vtn_builder *b, enum OpenCLstd_Entrypoints opcode,
41 const uint32_t *w, unsigned count, nir_handler handler)
42 {
43 const struct glsl_type *dest_type =
44 vtn_value(b, w[1], vtn_value_type_type)->type->type;
45
46 unsigned num_srcs = count - 5;
47 nir_ssa_def *srcs[3] = { NULL };
48 vtn_assert(num_srcs <= ARRAY_SIZE(srcs));
49 for (unsigned i = 0; i < num_srcs; i++) {
50 srcs[i] = vtn_ssa_value(b, w[i + 5])->def;
51 }
52
53 nir_ssa_def *result = handler(b, opcode, num_srcs, srcs, dest_type);
54 if (result) {
55 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
56 val->ssa = vtn_create_ssa_value(b, dest_type);
57 val->ssa->def = result;
58 } else {
59 vtn_assert(dest_type == glsl_void_type());
60 }
61 }
62
63 static nir_op
64 nir_alu_op_for_opencl_opcode(struct vtn_builder *b,
65 enum OpenCLstd_Entrypoints opcode)
66 {
67 switch (opcode) {
68 case OpenCLstd_Fabs: return nir_op_fabs;
69 case OpenCLstd_SAbs: return nir_op_iabs;
70 case OpenCLstd_SAdd_sat: return nir_op_iadd_sat;
71 case OpenCLstd_UAdd_sat: return nir_op_uadd_sat;
72 case OpenCLstd_Ceil: return nir_op_fceil;
73 case OpenCLstd_Cos: return nir_op_fcos;
74 case OpenCLstd_Exp2: return nir_op_fexp2;
75 case OpenCLstd_Log2: return nir_op_flog2;
76 case OpenCLstd_Floor: return nir_op_ffloor;
77 case OpenCLstd_SHadd: return nir_op_ihadd;
78 case OpenCLstd_UHadd: return nir_op_uhadd;
79 case OpenCLstd_Fma: return nir_op_ffma;
80 case OpenCLstd_Fmax: return nir_op_fmax;
81 case OpenCLstd_SMax: return nir_op_imax;
82 case OpenCLstd_UMax: return nir_op_umax;
83 case OpenCLstd_Fmin: return nir_op_fmin;
84 case OpenCLstd_SMin: return nir_op_imin;
85 case OpenCLstd_UMin: return nir_op_umin;
86 case OpenCLstd_Fmod: return nir_op_fmod;
87 case OpenCLstd_Mix: return nir_op_flrp;
88 case OpenCLstd_SMul_hi: return nir_op_imul_high;
89 case OpenCLstd_UMul_hi: return nir_op_umul_high;
90 case OpenCLstd_Popcount: return nir_op_bit_count;
91 case OpenCLstd_Pow: return nir_op_fpow;
92 case OpenCLstd_Remainder: return nir_op_frem;
93 case OpenCLstd_SRhadd: return nir_op_irhadd;
94 case OpenCLstd_URhadd: return nir_op_urhadd;
95 case OpenCLstd_Rsqrt: return nir_op_frsq;
96 case OpenCLstd_Sign: return nir_op_fsign;
97 case OpenCLstd_Sin: return nir_op_fsin;
98 case OpenCLstd_Sqrt: return nir_op_fsqrt;
99 case OpenCLstd_SSub_sat: return nir_op_isub_sat;
100 case OpenCLstd_USub_sat: return nir_op_usub_sat;
101 case OpenCLstd_Trunc: return nir_op_ftrunc;
102 /* uhm... */
103 case OpenCLstd_UAbs: return nir_op_mov;
104 default:
105 vtn_fail("No NIR equivalent");
106 }
107 }
108
109 static nir_ssa_def *
110 handle_alu(struct vtn_builder *b, enum OpenCLstd_Entrypoints opcode,
111 unsigned num_srcs, nir_ssa_def **srcs,
112 const struct glsl_type *dest_type)
113 {
114 return nir_build_alu(&b->nb, nir_alu_op_for_opencl_opcode(b, opcode),
115 srcs[0], srcs[1], srcs[2], NULL);
116 }
117
118 static nir_ssa_def *
119 handle_special(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 nir_builder *nb = &b->nb;
124
125 switch (opcode) {
126 case OpenCLstd_SAbs_diff:
127 return nir_iabs_diff(nb, srcs[0], srcs[1]);
128 case OpenCLstd_UAbs_diff:
129 return nir_uabs_diff(nb, srcs[0], srcs[1]);
130 case OpenCLstd_Bitselect:
131 return nir_bitselect(nb, srcs[0], srcs[1], srcs[2]);
132 case OpenCLstd_SMad_hi:
133 return nir_imad_hi(nb, srcs[0], srcs[1], srcs[2]);
134 case OpenCLstd_UMad_hi:
135 return nir_umad_hi(nb, srcs[0], srcs[1], srcs[2]);
136 case OpenCLstd_SMul24:
137 return nir_imul24(nb, srcs[0], srcs[1]);
138 case OpenCLstd_UMul24:
139 return nir_umul24(nb, srcs[0], srcs[1]);
140 case OpenCLstd_SMad24:
141 return nir_imad24(nb, srcs[0], srcs[1], srcs[2]);
142 case OpenCLstd_UMad24:
143 return nir_umad24(nb, srcs[0], srcs[1], srcs[2]);
144 case OpenCLstd_FClamp:
145 return nir_fclamp(nb, srcs[0], srcs[1], srcs[2]);
146 case OpenCLstd_SClamp:
147 return nir_iclamp(nb, srcs[0], srcs[1], srcs[2]);
148 case OpenCLstd_UClamp:
149 return nir_uclamp(nb, srcs[0], srcs[1], srcs[2]);
150 case OpenCLstd_Copysign:
151 return nir_copysign(nb, srcs[0], srcs[1]);
152 case OpenCLstd_Cross:
153 if (glsl_get_components(dest_type) == 4)
154 return nir_cross4(nb, srcs[0], srcs[1]);
155 return nir_cross3(nb, srcs[0], srcs[1]);
156 case OpenCLstd_Degrees:
157 return nir_degrees(nb, srcs[0]);
158 case OpenCLstd_Fdim:
159 return nir_fdim(nb, srcs[0], srcs[1]);
160 case OpenCLstd_Distance:
161 return nir_distance(nb, srcs[0], srcs[1]);
162 case OpenCLstd_Fast_distance:
163 return nir_fast_distance(nb, srcs[0], srcs[1]);
164 case OpenCLstd_Fast_length:
165 return nir_fast_length(nb, srcs[0]);
166 case OpenCLstd_Fast_normalize:
167 return nir_fast_normalize(nb, srcs[0]);
168 case OpenCLstd_Length:
169 return nir_length(nb, srcs[0]);
170 case OpenCLstd_Mad:
171 return nir_fmad(nb, srcs[0], srcs[1], srcs[2]);
172 case OpenCLstd_Maxmag:
173 return nir_maxmag(nb, srcs[0], srcs[1]);
174 case OpenCLstd_Minmag:
175 return nir_minmag(nb, srcs[0], srcs[1]);
176 case OpenCLstd_Nan:
177 return nir_nan(nb, srcs[0]);
178 case OpenCLstd_Nextafter:
179 return nir_nextafter(nb, srcs[0], srcs[1]);
180 case OpenCLstd_Normalize:
181 return nir_normalize(nb, srcs[0]);
182 case OpenCLstd_Radians:
183 return nir_radians(nb, srcs[0]);
184 case OpenCLstd_Rotate:
185 return nir_rotate(nb, srcs[0], srcs[1]);
186 case OpenCLstd_Smoothstep:
187 return nir_smoothstep(nb, srcs[0], srcs[1], srcs[2]);
188 case OpenCLstd_Select:
189 return nir_select(nb, srcs[0], srcs[1], srcs[2]);
190 case OpenCLstd_Step:
191 return nir_sge(nb, srcs[1], srcs[0]);
192 case OpenCLstd_S_Upsample:
193 case OpenCLstd_U_Upsample:
194 return nir_upsample(nb, srcs[0], srcs[1]);
195 default:
196 vtn_fail("No NIR equivalent");
197 return NULL;
198 }
199 }
200
201 static void
202 _handle_v_load_store(struct vtn_builder *b, enum OpenCLstd_Entrypoints opcode,
203 const uint32_t *w, unsigned count, bool load)
204 {
205 struct vtn_type *type;
206 if (load)
207 type = vtn_value(b, w[1], vtn_value_type_type)->type;
208 else
209 type = vtn_untyped_value(b, w[5])->type;
210 unsigned a = load ? 0 : 1;
211
212 const struct glsl_type *dest_type = type->type;
213 unsigned components = glsl_get_vector_elements(dest_type);
214 unsigned stride = components * glsl_get_bit_size(dest_type) / 8;
215
216 nir_ssa_def *offset = vtn_ssa_value(b, w[5 + a])->def;
217 struct vtn_value *p = vtn_value(b, w[6 + a], vtn_value_type_pointer);
218
219 nir_deref_instr *deref = vtn_pointer_to_deref(b, p->pointer);
220
221 /* 1. cast to vec type with adjusted stride */
222 deref = nir_build_deref_cast(&b->nb, &deref->dest.ssa, deref->mode,
223 dest_type, stride);
224 /* 2. deref ptr_as_array */
225 deref = nir_build_deref_ptr_as_array(&b->nb, deref, offset);
226
227 if (load) {
228 struct vtn_ssa_value *val = vtn_local_load(b, deref, p->type->access);
229 vtn_push_ssa(b, w[2], type, val);
230 } else {
231 struct vtn_ssa_value *val = vtn_ssa_value(b, w[5]);
232 vtn_local_store(b, val, deref, p->type->access);
233 }
234 }
235
236 static void
237 vtn_handle_opencl_vload(struct vtn_builder *b, enum OpenCLstd_Entrypoints opcode,
238 const uint32_t *w, unsigned count)
239 {
240 _handle_v_load_store(b, opcode, w, count, true);
241 }
242
243 static void
244 vtn_handle_opencl_vstore(struct vtn_builder *b, enum OpenCLstd_Entrypoints opcode,
245 const uint32_t *w, unsigned count)
246 {
247 _handle_v_load_store(b, opcode, w, count, false);
248 }
249
250 static nir_ssa_def *
251 handle_printf(struct vtn_builder *b, enum OpenCLstd_Entrypoints opcode,
252 unsigned num_srcs, nir_ssa_def **srcs,
253 const struct glsl_type *dest_type)
254 {
255 /* hahah, yeah, right.. */
256 return nir_imm_int(&b->nb, -1);
257 }
258
259 bool
260 vtn_handle_opencl_instruction(struct vtn_builder *b, SpvOp ext_opcode,
261 const uint32_t *w, unsigned count)
262 {
263 switch ((enum OpenCLstd_Entrypoints)ext_opcode) {
264 case OpenCLstd_Fabs:
265 case OpenCLstd_SAbs:
266 case OpenCLstd_UAbs:
267 case OpenCLstd_SAdd_sat:
268 case OpenCLstd_UAdd_sat:
269 case OpenCLstd_Ceil:
270 case OpenCLstd_Cos:
271 case OpenCLstd_Exp2:
272 case OpenCLstd_Log2:
273 case OpenCLstd_Floor:
274 case OpenCLstd_Fma:
275 case OpenCLstd_Fmax:
276 case OpenCLstd_SHadd:
277 case OpenCLstd_UHadd:
278 case OpenCLstd_SMax:
279 case OpenCLstd_UMax:
280 case OpenCLstd_Fmin:
281 case OpenCLstd_SMin:
282 case OpenCLstd_UMin:
283 case OpenCLstd_Mix:
284 case OpenCLstd_Fmod:
285 case OpenCLstd_SMul_hi:
286 case OpenCLstd_UMul_hi:
287 case OpenCLstd_Popcount:
288 case OpenCLstd_Pow:
289 case OpenCLstd_Remainder:
290 case OpenCLstd_SRhadd:
291 case OpenCLstd_URhadd:
292 case OpenCLstd_Rsqrt:
293 case OpenCLstd_Sign:
294 case OpenCLstd_Sin:
295 case OpenCLstd_Sqrt:
296 case OpenCLstd_SSub_sat:
297 case OpenCLstd_USub_sat:
298 case OpenCLstd_Trunc:
299 handle_instr(b, ext_opcode, w, count, handle_alu);
300 return true;
301 case OpenCLstd_SAbs_diff:
302 case OpenCLstd_UAbs_diff:
303 case OpenCLstd_SMad_hi:
304 case OpenCLstd_UMad_hi:
305 case OpenCLstd_SMad24:
306 case OpenCLstd_UMad24:
307 case OpenCLstd_SMul24:
308 case OpenCLstd_UMul24:
309 case OpenCLstd_Bitselect:
310 case OpenCLstd_FClamp:
311 case OpenCLstd_SClamp:
312 case OpenCLstd_UClamp:
313 case OpenCLstd_Copysign:
314 case OpenCLstd_Cross:
315 case OpenCLstd_Degrees:
316 case OpenCLstd_Fdim:
317 case OpenCLstd_Distance:
318 case OpenCLstd_Fast_distance:
319 case OpenCLstd_Fast_length:
320 case OpenCLstd_Fast_normalize:
321 case OpenCLstd_Length:
322 case OpenCLstd_Mad:
323 case OpenCLstd_Maxmag:
324 case OpenCLstd_Minmag:
325 case OpenCLstd_Nan:
326 case OpenCLstd_Nextafter:
327 case OpenCLstd_Normalize:
328 case OpenCLstd_Radians:
329 case OpenCLstd_Rotate:
330 case OpenCLstd_Select:
331 case OpenCLstd_Step:
332 case OpenCLstd_Smoothstep:
333 case OpenCLstd_S_Upsample:
334 case OpenCLstd_U_Upsample:
335 handle_instr(b, ext_opcode, w, count, handle_special);
336 return true;
337 case OpenCLstd_Vloadn:
338 vtn_handle_opencl_vload(b, ext_opcode, w, count);
339 return true;
340 case OpenCLstd_Vstoren:
341 vtn_handle_opencl_vstore(b, ext_opcode, w, count);
342 return true;
343 case OpenCLstd_Printf:
344 handle_instr(b, ext_opcode, w, count, handle_printf);
345 return true;
346 case OpenCLstd_Prefetch:
347 /* TODO maybe add a nir instruction for this? */
348 return true;
349 default:
350 vtn_fail("unhandled opencl opc: %u\n", ext_opcode);
351 return false;
352 }
353 }