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)
29 #include "nir/nir_builtin_builder.h"
31 #include "vtn_private.h"
32 #include "OpenCL.std.h"
34 typedef nir_ssa_def
*(*nir_handler
)(struct vtn_builder
*b
, enum OpenCLstd 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 opcode
, const uint32_t *w
,
40 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
, enum OpenCLstd opcode
)
66 case Fabs
: return nir_op_fabs
;
67 case SAbs
: return nir_op_iabs
;
68 case SAdd_sat
: return nir_op_iadd_sat
;
69 case UAdd_sat
: return nir_op_uadd_sat
;
70 case Ceil
: return nir_op_fceil
;
71 case Cos
: return nir_op_fcos
;
72 case Exp2
: return nir_op_fexp2
;
73 case Log2
: return nir_op_flog2
;
74 case Floor
: return nir_op_ffloor
;
75 case SHadd
: return nir_op_ihadd
;
76 case UHadd
: return nir_op_uhadd
;
77 case Fma
: return nir_op_ffma
;
78 case Fmax
: return nir_op_fmax
;
79 case SMax
: return nir_op_imax
;
80 case UMax
: return nir_op_umax
;
81 case Fmin
: return nir_op_fmin
;
82 case SMin
: return nir_op_imin
;
83 case UMin
: return nir_op_umin
;
84 case Fmod
: return nir_op_fmod
;
85 case Mix
: return nir_op_flrp
;
86 case SMul_hi
: return nir_op_imul_high
;
87 case UMul_hi
: return nir_op_umul_high
;
88 case Popcount
: return nir_op_bit_count
;
89 case Pow
: return nir_op_fpow
;
90 case Remainder
: return nir_op_frem
;
91 case SRhadd
: return nir_op_irhadd
;
92 case URhadd
: return nir_op_urhadd
;
93 case Rsqrt
: return nir_op_frsq
;
94 case Sign
: return nir_op_fsign
;
95 case Sin
: return nir_op_fsin
;
96 case Sqrt
: return nir_op_fsqrt
;
97 case SSub_sat
: return nir_op_isub_sat
;
98 case USub_sat
: return nir_op_usub_sat
;
99 case Trunc
: return nir_op_ftrunc
;
101 case UAbs
: return nir_op_imov
;
103 vtn_fail("No NIR equivalent");
108 handle_alu(struct vtn_builder
*b
, enum OpenCLstd opcode
, unsigned num_srcs
,
109 nir_ssa_def
**srcs
, const struct glsl_type
*dest_type
)
111 return nir_build_alu(&b
->nb
, nir_alu_op_for_opencl_opcode(b
, opcode
),
112 srcs
[0], srcs
[1], srcs
[2], NULL
);
116 handle_special(struct vtn_builder
*b
, enum OpenCLstd opcode
, unsigned num_srcs
,
117 nir_ssa_def
**srcs
, const struct glsl_type
*dest_type
)
119 nir_builder
*nb
= &b
->nb
;
123 return nir_iabs_diff(nb
, srcs
[0], srcs
[1]);
125 return nir_uabs_diff(nb
, srcs
[0], srcs
[1]);
127 return nir_bitselect(nb
, srcs
[0], srcs
[1], srcs
[2]);
129 return nir_fclamp(nb
, srcs
[0], srcs
[1], srcs
[2]);
131 return nir_iclamp(nb
, srcs
[0], srcs
[1], srcs
[2]);
133 return nir_uclamp(nb
, srcs
[0], srcs
[1], srcs
[2]);
135 return nir_copysign(nb
, srcs
[0], srcs
[1]);
137 if (glsl_get_components(dest_type
) == 4)
138 return nir_cross4(nb
, srcs
[0], srcs
[1]);
139 return nir_cross3(nb
, srcs
[0], srcs
[1]);
141 return nir_degrees(nb
, srcs
[0]);
143 return nir_fdim(nb
, srcs
[0], srcs
[1]);
145 return nir_distance(nb
, srcs
[0], srcs
[1]);
147 return nir_fast_distance(nb
, srcs
[0], srcs
[1]);
149 return nir_fast_length(nb
, srcs
[0]);
151 return nir_fast_normalize(nb
, srcs
[0]);
153 return nir_length(nb
, srcs
[0]);
155 return nir_fmad(nb
, srcs
[0], srcs
[1], srcs
[2]);
157 return nir_maxmag(nb
, srcs
[0], srcs
[1]);
159 return nir_minmag(nb
, srcs
[0], srcs
[1]);
161 return nir_nan(nb
, srcs
[0]);
163 return nir_nextafter(nb
, srcs
[0], srcs
[1]);
165 return nir_normalize(nb
, srcs
[0]);
167 return nir_radians(nb
, srcs
[0]);
169 return nir_rotate(nb
, srcs
[0], srcs
[1]);
171 return nir_smoothstep(nb
, srcs
[0], srcs
[1], srcs
[2]);
173 return nir_select(nb
, srcs
[0], srcs
[1], srcs
[2]);
175 return nir_sge(nb
, srcs
[1], srcs
[0]);
178 return nir_upsample(nb
, srcs
[0], srcs
[1]);
180 vtn_fail("No NIR equivalent");
186 handle_printf(struct vtn_builder
*b
, enum OpenCLstd opcode
, unsigned num_srcs
,
187 nir_ssa_def
**srcs
, const struct glsl_type
*dest_type
)
189 /* hahah, yeah, right.. */
190 return nir_imm_int(&b
->nb
, -1);
194 vtn_handle_opencl_instruction(struct vtn_builder
*b
, uint32_t ext_opcode
,
195 const uint32_t *w
, unsigned count
)
197 switch (ext_opcode
) {
233 handle_instr(b
, ext_opcode
, w
, count
, handle_alu
);
263 handle_instr(b
, ext_opcode
, w
, count
, handle_special
);
266 handle_instr(b
, ext_opcode
, w
, count
, handle_printf
);
269 /* TODO maybe add a nir instruction for this? */
272 vtn_fail("unhandled opencl opc: %u\n", ext_opcode
);