2 * Copyright © 2018 Red Hat
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:
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
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
24 * Rob Clark (robdclark@gmail.com)
28 #include "nir/nir_builtin_builder.h"
30 #include "vtn_private.h"
31 #include "OpenCL.std.h"
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
);
39 handle_instr(struct vtn_builder
*b
, enum OpenCLstd_Entrypoints opcode
,
40 const uint32_t *w
, unsigned count
, nir_handler handler
)
42 const struct glsl_type
*dest_type
=
43 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
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
;
52 nir_ssa_def
*result
= handler(b
, opcode
, num_srcs
, srcs
, dest_type
);
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
;
58 vtn_assert(dest_type
== glsl_void_type());
63 nir_alu_op_for_opencl_opcode(struct vtn_builder
*b
,
64 enum OpenCLstd_Entrypoints 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_exp2
: return nir_op_fexp2
;
88 case OpenCLstd_Native_log2
: return nir_op_flog2
;
89 case OpenCLstd_SMul_hi
: return nir_op_imul_high
;
90 case OpenCLstd_UMul_hi
: return nir_op_umul_high
;
91 case OpenCLstd_Popcount
: return nir_op_bit_count
;
92 case OpenCLstd_Pow
: return nir_op_fpow
;
93 case OpenCLstd_Remainder
: return nir_op_frem
;
94 case OpenCLstd_SRhadd
: return nir_op_irhadd
;
95 case OpenCLstd_URhadd
: return nir_op_urhadd
;
96 case OpenCLstd_Rsqrt
: return nir_op_frsq
;
97 case OpenCLstd_Sign
: return nir_op_fsign
;
98 case OpenCLstd_Sin
: return nir_op_fsin
;
99 case OpenCLstd_Sqrt
: return nir_op_fsqrt
;
100 case OpenCLstd_SSub_sat
: return nir_op_isub_sat
;
101 case OpenCLstd_USub_sat
: return nir_op_usub_sat
;
102 case OpenCLstd_Trunc
: return nir_op_ftrunc
;
103 case OpenCLstd_Rint
: return nir_op_fround_even
;
105 case OpenCLstd_UAbs
: return nir_op_mov
;
107 vtn_fail("No NIR equivalent");
112 handle_alu(struct vtn_builder
*b
, enum OpenCLstd_Entrypoints opcode
,
113 unsigned num_srcs
, nir_ssa_def
**srcs
,
114 const struct glsl_type
*dest_type
)
116 return nir_build_alu(&b
->nb
, nir_alu_op_for_opencl_opcode(b
, opcode
),
117 srcs
[0], srcs
[1], srcs
[2], NULL
);
121 handle_special(struct vtn_builder
*b
, enum OpenCLstd_Entrypoints opcode
,
122 unsigned num_srcs
, nir_ssa_def
**srcs
,
123 const struct glsl_type
*dest_type
)
125 nir_builder
*nb
= &b
->nb
;
128 case OpenCLstd_SAbs_diff
:
129 return nir_iabs_diff(nb
, srcs
[0], srcs
[1]);
130 case OpenCLstd_UAbs_diff
:
131 return nir_uabs_diff(nb
, srcs
[0], srcs
[1]);
132 case OpenCLstd_Bitselect
:
133 return nir_bitselect(nb
, srcs
[0], srcs
[1], srcs
[2]);
134 case OpenCLstd_SMad_hi
:
135 return nir_imad_hi(nb
, srcs
[0], srcs
[1], srcs
[2]);
136 case OpenCLstd_UMad_hi
:
137 return nir_umad_hi(nb
, srcs
[0], srcs
[1], srcs
[2]);
138 case OpenCLstd_SMul24
:
139 return nir_imul24(nb
, srcs
[0], srcs
[1]);
140 case OpenCLstd_UMul24
:
141 return nir_umul24(nb
, srcs
[0], srcs
[1]);
142 case OpenCLstd_SMad24
:
143 return nir_imad24(nb
, srcs
[0], srcs
[1], srcs
[2]);
144 case OpenCLstd_UMad24
:
145 return nir_umad24(nb
, srcs
[0], srcs
[1], srcs
[2]);
146 case OpenCLstd_FClamp
:
147 return nir_fclamp(nb
, srcs
[0], srcs
[1], srcs
[2]);
148 case OpenCLstd_SClamp
:
149 return nir_iclamp(nb
, srcs
[0], srcs
[1], srcs
[2]);
150 case OpenCLstd_UClamp
:
151 return nir_uclamp(nb
, srcs
[0], srcs
[1], srcs
[2]);
152 case OpenCLstd_Copysign
:
153 return nir_copysign(nb
, srcs
[0], srcs
[1]);
154 case OpenCLstd_Cross
:
155 if (glsl_get_components(dest_type
) == 4)
156 return nir_cross4(nb
, srcs
[0], srcs
[1]);
157 return nir_cross3(nb
, srcs
[0], srcs
[1]);
158 case OpenCLstd_Degrees
:
159 return nir_degrees(nb
, srcs
[0]);
161 return nir_fdim(nb
, srcs
[0], srcs
[1]);
162 case OpenCLstd_Distance
:
163 return nir_distance(nb
, srcs
[0], srcs
[1]);
164 case OpenCLstd_Fast_distance
:
165 return nir_fast_distance(nb
, srcs
[0], srcs
[1]);
166 case OpenCLstd_Fast_length
:
167 return nir_fast_length(nb
, srcs
[0]);
168 case OpenCLstd_Fast_normalize
:
169 return nir_fast_normalize(nb
, srcs
[0]);
170 case OpenCLstd_Length
:
171 return nir_length(nb
, srcs
[0]);
173 return nir_fmad(nb
, srcs
[0], srcs
[1], srcs
[2]);
174 case OpenCLstd_Maxmag
:
175 return nir_maxmag(nb
, srcs
[0], srcs
[1]);
176 case OpenCLstd_Minmag
:
177 return nir_minmag(nb
, srcs
[0], srcs
[1]);
179 return nir_nan(nb
, srcs
[0]);
180 case OpenCLstd_Nextafter
:
181 return nir_nextafter(nb
, srcs
[0], srcs
[1]);
182 case OpenCLstd_Normalize
:
183 return nir_normalize(nb
, srcs
[0]);
184 case OpenCLstd_Radians
:
185 return nir_radians(nb
, srcs
[0]);
186 case OpenCLstd_Rotate
:
187 return nir_rotate(nb
, srcs
[0], srcs
[1]);
188 case OpenCLstd_Smoothstep
:
189 return nir_smoothstep(nb
, srcs
[0], srcs
[1], srcs
[2]);
191 return nir_clz_u(nb
, srcs
[0]);
192 case OpenCLstd_Select
:
193 return nir_select(nb
, srcs
[0], srcs
[1], srcs
[2]);
195 return nir_sge(nb
, srcs
[1], srcs
[0]);
196 case OpenCLstd_S_Upsample
:
197 case OpenCLstd_U_Upsample
:
198 return nir_upsample(nb
, srcs
[0], srcs
[1]);
199 case OpenCLstd_Native_exp
:
200 return nir_fexp(nb
, srcs
[0]);
201 case OpenCLstd_Native_exp10
:
202 return nir_fexp2(nb
, nir_fmul_imm(nb
, srcs
[0], log(10) / log(2)));
203 case OpenCLstd_Native_log
:
204 return nir_flog(nb
, srcs
[0]);
205 case OpenCLstd_Native_log10
:
206 return nir_fmul_imm(nb
, nir_flog2(nb
, srcs
[0]), log(2) / log(10));
207 case OpenCLstd_Native_tan
:
208 return nir_ftan(nb
, srcs
[0]);
210 vtn_fail("No NIR equivalent");
216 _handle_v_load_store(struct vtn_builder
*b
, enum OpenCLstd_Entrypoints opcode
,
217 const uint32_t *w
, unsigned count
, bool load
)
219 struct vtn_type
*type
;
221 type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
223 type
= vtn_untyped_value(b
, w
[5])->type
;
224 unsigned a
= load
? 0 : 1;
226 const struct glsl_type
*dest_type
= type
->type
;
227 unsigned components
= glsl_get_vector_elements(dest_type
);
229 nir_ssa_def
*offset
= vtn_ssa_value(b
, w
[5 + a
])->def
;
230 struct vtn_value
*p
= vtn_value(b
, w
[6 + a
], vtn_value_type_pointer
);
232 struct vtn_ssa_value
*comps
[NIR_MAX_VEC_COMPONENTS
];
233 nir_ssa_def
*ncomps
[NIR_MAX_VEC_COMPONENTS
];
235 nir_ssa_def
*moffset
= nir_imul_imm(&b
->nb
, offset
, components
);
236 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, p
->pointer
);
238 for (int i
= 0; i
< components
; i
++) {
239 nir_ssa_def
*coffset
= nir_iadd_imm(&b
->nb
, moffset
, i
);
240 nir_deref_instr
*arr_deref
= nir_build_deref_ptr_as_array(&b
->nb
, deref
, coffset
);
243 comps
[i
] = vtn_local_load(b
, arr_deref
, p
->type
->access
);
244 ncomps
[i
] = comps
[i
]->def
;
246 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, glsl_scalar_type(glsl_get_base_type(dest_type
)));
247 struct vtn_ssa_value
*val
= vtn_ssa_value(b
, w
[5]);
248 ssa
->def
= nir_channel(&b
->nb
, val
->def
, i
);
249 vtn_local_store(b
, ssa
, arr_deref
, p
->type
->access
);
253 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, dest_type
);
254 ssa
->def
= nir_vec(&b
->nb
, ncomps
, components
);
255 vtn_push_ssa(b
, w
[2], type
, ssa
);
260 vtn_handle_opencl_vload(struct vtn_builder
*b
, enum OpenCLstd_Entrypoints opcode
,
261 const uint32_t *w
, unsigned count
)
263 _handle_v_load_store(b
, opcode
, w
, count
, true);
267 vtn_handle_opencl_vstore(struct vtn_builder
*b
, enum OpenCLstd_Entrypoints opcode
,
268 const uint32_t *w
, unsigned count
)
270 _handle_v_load_store(b
, opcode
, w
, count
, false);
274 handle_printf(struct vtn_builder
*b
, enum OpenCLstd_Entrypoints opcode
,
275 unsigned num_srcs
, nir_ssa_def
**srcs
,
276 const struct glsl_type
*dest_type
)
278 /* hahah, yeah, right.. */
279 return nir_imm_int(&b
->nb
, -1);
283 handle_shuffle(struct vtn_builder
*b
, enum OpenCLstd_Entrypoints opcode
, unsigned num_srcs
,
284 nir_ssa_def
**srcs
, const struct glsl_type
*dest_type
)
286 struct nir_ssa_def
*input
= srcs
[0];
287 struct nir_ssa_def
*mask
= srcs
[1];
289 unsigned out_elems
= glsl_get_vector_elements(dest_type
);
290 nir_ssa_def
*outres
[NIR_MAX_VEC_COMPONENTS
];
291 unsigned in_elems
= input
->num_components
;
292 if (mask
->bit_size
!= 32)
293 mask
= nir_u2u32(&b
->nb
, mask
);
294 mask
= nir_iand(&b
->nb
, mask
, nir_imm_intN_t(&b
->nb
, in_elems
- 1, mask
->bit_size
));
295 for (unsigned i
= 0; i
< out_elems
; i
++)
296 outres
[i
] = nir_vector_extract(&b
->nb
, input
, nir_channel(&b
->nb
, mask
, i
));
298 return nir_vec(&b
->nb
, outres
, out_elems
);
302 handle_shuffle2(struct vtn_builder
*b
, enum OpenCLstd_Entrypoints opcode
, unsigned num_srcs
,
303 nir_ssa_def
**srcs
, const struct glsl_type
*dest_type
)
305 struct nir_ssa_def
*input0
= srcs
[0];
306 struct nir_ssa_def
*input1
= srcs
[1];
307 struct nir_ssa_def
*mask
= srcs
[2];
309 unsigned out_elems
= glsl_get_vector_elements(dest_type
);
310 nir_ssa_def
*outres
[NIR_MAX_VEC_COMPONENTS
];
311 unsigned in_elems
= input0
->num_components
;
312 unsigned total_mask
= 2 * in_elems
- 1;
313 unsigned half_mask
= in_elems
- 1;
314 if (mask
->bit_size
!= 32)
315 mask
= nir_u2u32(&b
->nb
, mask
);
316 mask
= nir_iand(&b
->nb
, mask
, nir_imm_intN_t(&b
->nb
, total_mask
, mask
->bit_size
));
317 for (unsigned i
= 0; i
< out_elems
; i
++) {
318 nir_ssa_def
*this_mask
= nir_channel(&b
->nb
, mask
, i
);
319 nir_ssa_def
*vmask
= nir_iand(&b
->nb
, this_mask
, nir_imm_intN_t(&b
->nb
, half_mask
, mask
->bit_size
));
320 nir_ssa_def
*val0
= nir_vector_extract(&b
->nb
, input0
, vmask
);
321 nir_ssa_def
*val1
= nir_vector_extract(&b
->nb
, input1
, vmask
);
322 nir_ssa_def
*sel
= nir_ilt(&b
->nb
, this_mask
, nir_imm_intN_t(&b
->nb
, in_elems
, mask
->bit_size
));
323 outres
[i
] = nir_bcsel(&b
->nb
, sel
, val0
, val1
);
325 return nir_vec(&b
->nb
, outres
, out_elems
);
329 vtn_handle_opencl_instruction(struct vtn_builder
*b
, SpvOp ext_opcode
,
330 const uint32_t *w
, unsigned count
)
332 enum OpenCLstd_Entrypoints cl_opcode
= (enum OpenCLstd_Entrypoints
) ext_opcode
;
338 case OpenCLstd_SAdd_sat
:
339 case OpenCLstd_UAdd_sat
:
344 case OpenCLstd_Floor
:
347 case OpenCLstd_SHadd
:
348 case OpenCLstd_UHadd
:
355 case OpenCLstd_Native_exp2
:
356 case OpenCLstd_Native_log2
:
358 case OpenCLstd_SMul_hi
:
359 case OpenCLstd_UMul_hi
:
360 case OpenCLstd_Popcount
:
362 case OpenCLstd_Remainder
:
363 case OpenCLstd_SRhadd
:
364 case OpenCLstd_URhadd
:
365 case OpenCLstd_Rsqrt
:
369 case OpenCLstd_SSub_sat
:
370 case OpenCLstd_USub_sat
:
371 case OpenCLstd_Trunc
:
373 handle_instr(b
, cl_opcode
, w
, count
, handle_alu
);
375 case OpenCLstd_SAbs_diff
:
376 case OpenCLstd_UAbs_diff
:
377 case OpenCLstd_SMad_hi
:
378 case OpenCLstd_UMad_hi
:
379 case OpenCLstd_SMad24
:
380 case OpenCLstd_UMad24
:
381 case OpenCLstd_SMul24
:
382 case OpenCLstd_UMul24
:
383 case OpenCLstd_Bitselect
:
384 case OpenCLstd_FClamp
:
385 case OpenCLstd_SClamp
:
386 case OpenCLstd_UClamp
:
387 case OpenCLstd_Copysign
:
388 case OpenCLstd_Cross
:
389 case OpenCLstd_Degrees
:
391 case OpenCLstd_Distance
:
392 case OpenCLstd_Fast_distance
:
393 case OpenCLstd_Fast_length
:
394 case OpenCLstd_Fast_normalize
:
395 case OpenCLstd_Length
:
397 case OpenCLstd_Maxmag
:
398 case OpenCLstd_Minmag
:
400 case OpenCLstd_Nextafter
:
401 case OpenCLstd_Normalize
:
402 case OpenCLstd_Radians
:
403 case OpenCLstd_Rotate
:
404 case OpenCLstd_Select
:
406 case OpenCLstd_Smoothstep
:
407 case OpenCLstd_S_Upsample
:
408 case OpenCLstd_U_Upsample
:
410 case OpenCLstd_Native_exp
:
411 case OpenCLstd_Native_exp10
:
412 case OpenCLstd_Native_log
:
413 case OpenCLstd_Native_log10
:
414 case OpenCLstd_Native_tan
:
415 handle_instr(b
, cl_opcode
, w
, count
, handle_special
);
417 case OpenCLstd_Vloadn
:
418 vtn_handle_opencl_vload(b
, cl_opcode
, w
, count
);
420 case OpenCLstd_Vstoren
:
421 vtn_handle_opencl_vstore(b
, cl_opcode
, w
, count
);
423 case OpenCLstd_Shuffle
:
424 handle_instr(b
, cl_opcode
, w
, count
, handle_shuffle
);
426 case OpenCLstd_Shuffle2
:
427 handle_instr(b
, cl_opcode
, w
, count
, handle_shuffle2
);
429 case OpenCLstd_Printf
:
430 handle_instr(b
, cl_opcode
, w
, count
, handle_printf
);
432 case OpenCLstd_Prefetch
:
433 /* TODO maybe add a nir instruction for this? */
436 vtn_fail("unhandled opencl opc: %u\n", ext_opcode
);