<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Mar 23, 2018 at 12:33 PM, Karol Herbst <span dir="ltr"><<a href="mailto:kherbst@redhat.com" target="_blank">kherbst@redhat.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Rob Clark <<a href="mailto:robdclark@gmail.com">robdclark@gmail.com</a>><br>
<br>
Not complete, mostly just adding things as I encounter them in CTS. But<br>
not getting far enough yet to hit most of the OpenCL.std instructions.<br>
<br>
v2: update hadd definition (Karol Herbst <<a href="mailto:kherbst@redhat.com">kherbst@redhat.com</a>>)<br>
<br>
Signed-off-by: Rob Clark <<a href="mailto:robdclark@gmail.com">robdclark@gmail.com</a>><br>
Signed-off-by: Karol Herbst <<a href="mailto:kherbst@redhat.com">kherbst@redhat.com</a>><br>
---<br>
src/compiler/nir/meson.build | 1 +<br>
src/compiler/nir/nir_opcodes.<wbr>py | 3 +-<br>
src/compiler/spirv/spirv_to_<wbr>nir.c | 2 +<br>
src/compiler/spirv/vtn_opencl.<wbr>c | 266 ++++++++++++++++++++++++++++++<wbr>++++++++<br>
src/compiler/spirv/vtn_<wbr>private.h | 3 +<br>
5 files changed, 274 insertions(+), 1 deletion(-)<br>
create mode 100644 src/compiler/spirv/vtn_opencl.<wbr>c<br>
<br>
diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build<br>
index a70c236b958..213a139a1b8 100644<br>
--- a/src/compiler/nir/meson.build<br>
+++ b/src/compiler/nir/meson.build<br>
@@ -192,6 +192,7 @@ files_libnir = files(<br>
'../spirv/vtn_amd.c',<br>
'../spirv/vtn_cfg.c',<br>
'../spirv/vtn_glsl450.c',<br>
+ '../spirv/vtn_opencl.c',<br>
'../spirv/vtn_private.h',<br>
'../spirv/vtn_subgroup.c',<br>
'../spirv/vtn_variables.c',<br>
diff --git a/src/compiler/nir/nir_<wbr>opcodes.py b/src/compiler/nir/nir_<wbr>opcodes.py<br>
index 65d13200624..86fd6b6d68e 100644<br>
--- a/src/compiler/nir/nir_<wbr>opcodes.py<br>
+++ b/src/compiler/nir/nir_<wbr>opcodes.py<br>
@@ -768,4 +768,5 @@ dst.z = src2.x;<br>
dst.w = src3.x;<br>
""")<br>
<br>
-<br>
+binop("ihadd", tint, commutative, "(src0 >> 1) + (src1 >> 1) + (src0 & src1 & 1)")<br>
+binop("uhadd", tuint, commutative, "(src0 >> 1) + (src1 >> 1) + (src0 & src1 & 1)")<br></blockquote><div><br></div><div>This bit should be it's own patch. It looks correct to me. Might as well add the other two when you make a patch for it though.<br><br></div><div>For whatever it's worth, Intel hardware calls this opcode AVG (for average). I'm not sure what "hadd" is supposed to stand for. Maybe half-add? Anyway, I don't care about naming.<br><br></div><div>--Jason<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
diff --git a/src/compiler/spirv/spirv_to_<wbr>nir.c b/src/compiler/spirv/spirv_to_<wbr>nir.c<br>
index 3acb3fc0b42..6a16d77a771 100644<br>
--- a/src/compiler/spirv/spirv_to_<wbr>nir.c<br>
+++ b/src/compiler/spirv/spirv_to_<wbr>nir.c<br>
@@ -379,6 +379,8 @@ vtn_handle_extension(struct vtn_builder *b, SpvOp opcode,<br>
} else if ((strcmp((const char *)&w[2], "SPV_AMD_gcn_shader") == 0)<br>
&& (b->options && b->options->caps.gcn_shader)) {<br>
val->ext_handler = vtn_handle_amd_gcn_shader_<wbr>instruction;<br>
+ } else if (strcmp(ext, "OpenCL.std") == 0) {<br>
+ val->ext_handler = vtn_handle_opencl_instruction;<br>
} else {<br>
vtn_fail("Unsupported extension: %s", ext);<br>
}<br>
diff --git a/src/compiler/spirv/vtn_<wbr>opencl.c b/src/compiler/spirv/vtn_<wbr>opencl.c<br>
new file mode 100644<br>
index 00000000000..3c5ecd22452<br>
--- /dev/null<br>
+++ b/src/compiler/spirv/vtn_<wbr>opencl.c<br>
@@ -0,0 +1,266 @@<br>
+/*<br>
+ * Copyright © 2018 Red Hat<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ *<br>
+ * Authors:<br>
+ * Rob Clark (<a href="mailto:robdclark@gmail.com">robdclark@gmail.com</a>)<br>
+ */<br>
+<br>
+#include "vtn_private.h"<br>
+#include "OpenCL.std.h"<br>
+<br>
+typedef nir_ssa_def *(*nir_handler)(struct vtn_builder *b, enum OpenCLstd opcode,<br>
+ unsigned num_srcs, nir_ssa_def **srcs);<br>
+<br>
+static void<br>
+handle_instr(struct vtn_builder *b, enum OpenCLstd opcode, const uint32_t *w,<br>
+ unsigned count, nir_handler handler)<br>
+{<br>
+ const struct glsl_type *dest_type =<br>
+ vtn_value(b, w[1], vtn_value_type_type)->type-><wbr>type;<br>
+<br>
+ unsigned num_srcs = count - 5;<br>
+ nir_ssa_def *srcs[3] = { NULL, };<br>
+ vtn_assert(num_srcs <= ARRAY_SIZE(srcs));<br>
+ for (unsigned i = 0; i < num_srcs; i++) {<br>
+ srcs[i] = vtn_ssa_value(b, w[i + 5])->def;<br>
+ }<br>
+<br>
+ nir_ssa_def *result = handler(b, opcode, num_srcs, srcs);<br>
+ if (result) {<br>
+ struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);<br>
+ val->ssa = vtn_create_ssa_value(b, dest_type);<br>
+ val->ssa->def = result;<br>
+ } else {<br>
+ vtn_assert(dest_type == glsl_void_type());<br>
+ }<br>
+}<br>
+<br>
+static nir_op<br>
+nir_alu_op_for_opencl_opcode(<wbr>struct vtn_builder *b, enum OpenCLstd opcode)<br>
+{<br>
+ switch (opcode) {<br>
+ case SHadd: return nir_op_ihadd;<br>
+ case UHadd: return nir_op_uhadd;<br>
+ default:<br>
+ vtn_fail("No NIR equivalent");<br>
+ }<br>
+}<br>
+<br>
+static nir_ssa_def *<br>
+handle_alu(struct vtn_builder *b, enum OpenCLstd opcode, unsigned num_srcs,<br>
+ nir_ssa_def **srcs)<br>
+{<br>
+ return nir_build_alu(&b->nb, nir_alu_op_for_opencl_opcode(<wbr>b, opcode),<br>
+ srcs[0], srcs[1], srcs[2], NULL);<br>
+}<br>
+<br>
+static nir_ssa_def *<br>
+handle_printf(struct vtn_builder *b, enum OpenCLstd opcode, unsigned num_srcs,<br>
+ nir_ssa_def **srcs)<br>
+{<br>
+ /* hahah, yeah, right.. */<br>
+ return nir_imm_int(&b->nb, -1);<br>
+}<br>
+<br>
+static void<br>
+vtn_handle_opencl_vload(<wbr>struct vtn_builder *b, enum OpenCLstd opcode,<br>
+ const uint32_t *w, unsigned count)<br>
+{<br>
+ const struct glsl_type *dest_type =<br>
+ vtn_value(b, w[1], vtn_value_type_type)->type-><wbr>type;<br>
+ unsigned num_components, bit_size;<br>
+<br>
+ struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);<br>
+ val->ssa = vtn_create_ssa_value(b, dest_type);<br>
+<br>
+ switch (opcode) {<br>
+ case Vload_half:<br>
+ case Vload_halfn:<br>
+ case Vloada_halfn:<br>
+ bit_size = 16;<br>
+ break;<br>
+ default:<br>
+ bit_size = glsl_get_bit_size(dest_type);<br>
+ break;<br>
+ }<br>
+<br>
+ nir_ssa_def *offset = vtn_ssa_value(b, w[5])->def;<br>
+ nir_ssa_def *p = vtn_ssa_value(b, w[6])->def;<br>
+<br>
+ if (count >= 7) {<br>
+ num_components = w[7];<br>
+<br>
+ unsigned off;<br>
+ if ((opcode == Vloada_halfn) && (num_components == 3)) {<br>
+ off = 4 * bit_size / 8;<br>
+ } else {<br>
+ off = num_components * bit_size / 8;<br>
+ }<br>
+<br>
+ nir_ssa_def *n = (b->ptr_size == 64) ?<br>
+ nir_imm_int64(&b->nb, off) :<br>
+ nir_imm_int(&b->nb, off);<br>
+<br>
+ offset = nir_imul(&b->nb, offset, n);<br>
+ } else {<br>
+ num_components = 1;<br>
+ }<br>
+<br>
+ /* add offset to pointer: */<br>
+ p = nir_iadd(&b->nb, p, offset);<br>
+<br>
+ nir_intrinsic_op op = nir_intrinsic_load_global;<br>
+<br>
+ nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b-><wbr>shader, op);<br>
+ intrin->num_components = num_components;<br>
+ intrin->src[0] = nir_src_for_ssa(p);<br>
+<br>
+ nir_ssa_dest_init(&intrin-><wbr>instr, &intrin->dest,<br>
+ intrin->num_components,<br>
+ bit_size,<br>
+ NULL);<br>
+ val->ssa->def = &intrin->dest.ssa;<br>
+<br>
+ nir_builder_instr_insert(&b-><wbr>nb, &intrin->instr);<br>
+<br>
+ switch (opcode) {<br>
+ case Vload_half:<br>
+ case Vload_halfn:<br>
+ case Vloada_halfn:<br>
+ /* convert f16->f32: */<br>
+ val->ssa->def = nir_f2f32(&b->nb, val->ssa->def);<br>
+ break;<br>
+ default:<br>
+ break;<br>
+ }<br>
+}<br>
+<br>
+static void<br>
+vtn_handle_opencl_vstore(<wbr>struct vtn_builder *b, enum OpenCLstd opcode,<br>
+ const uint32_t *w, unsigned count)<br>
+{<br>
+ unsigned num_components, bit_size;<br>
+ const struct glsl_type *src_type = vtn_ssa_value(b, w[5])->type;<br>
+<br>
+ nir_ssa_def *data = vtn_ssa_value(b, w[5])->def;<br>
+ nir_ssa_def *offset = vtn_ssa_value(b, w[6])->def;<br>
+ nir_ssa_def *p = vtn_ssa_value(b, w[7])->def;<br>
+<br>
+ // ??? MAX2(1, glsl_get_vector_elements(src_<wbr>type));<br>
+ num_components = data->num_components;<br>
+<br>
+ switch (opcode) {<br>
+ case Vstore_half_r:<br>
+ case Vstore_halfn_r:<br>
+ case Vstorea_halfn_r:<br>
+ bit_size = 16;<br>
+ switch (w[8]) {<br>
+ case SpvFPRoundingModeRTE:<br>
+ data = nir_f2f16_rtne(&b->nb, data);<br>
+ break;<br>
+ case SpvFPRoundingModeRTZ:<br>
+ data = nir_f2f16_rtz(&b->nb, data);<br>
+ break;<br>
+ case SpvFPRoundingModeRTP:<br>
+ case SpvFPRoundingModeRTN:<br>
+ default:<br>
+ vtn_fail("unsupported rounding mode: %u\n", w[8]);<br>
+ break;<br>
+ }<br>
+ break;<br>
+ case Vstore_half:<br>
+ case Vstore_halfn:<br>
+ case Vstorea_halfn:<br>
+ bit_size = 16;<br>
+ data = nir_f2f16_undef(&b->nb, data);<br>
+ break;<br>
+ default:<br>
+ bit_size = glsl_get_bit_size(src_type);<br>
+ break;<br>
+ }<br>
+<br>
+ if (num_components > 1) {<br>
+ unsigned off;<br>
+<br>
+ if (((opcode == Vstorea_halfn_r) || (opcode == Vstorea_halfn)) &&<br>
+ (num_components == 3)) {<br>
+ off = 4 * bit_size / 8;<br>
+ } else {<br>
+ off = num_components * bit_size / 8;<br>
+ }<br>
+<br>
+ nir_ssa_def *n = (b->ptr_size == 64) ?<br>
+ nir_imm_int64(&b->nb, off) :<br>
+ nir_imm_int(&b->nb, off);<br>
+<br>
+ offset = nir_imul(&b->nb, offset, n);<br>
+ }<br>
+<br>
+ /* add offset to pointer: */<br>
+ p = nir_iadd(&b->nb, p, offset);<br>
+<br>
+ nir_intrinsic_op op = nir_intrinsic_store_global;<br>
+<br>
+ nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b-><wbr>shader, op);<br>
+ intrin->num_components = num_components;<br>
+ intrin->src[0] = nir_src_for_ssa(data);<br>
+ intrin->src[1] = nir_src_for_ssa(p);<br>
+ nir_intrinsic_set_write_mask(<wbr>intrin, (1 << num_components) - 1);<br>
+<br>
+ nir_builder_instr_insert(&b-><wbr>nb, &intrin->instr);<br>
+}<br>
+<br>
+bool<br>
+vtn_handle_opencl_<wbr>instruction(struct vtn_builder *b, uint32_t ext_opcode,<br>
+ const uint32_t *w, unsigned count)<br>
+{<br>
+ switch (ext_opcode) {<br>
+ case SHadd:<br>
+ case UHadd:<br>
+ handle_instr(b, ext_opcode, w, count, handle_alu);<br>
+ return true;<br>
+ case Vloadn:<br>
+ case Vload_half:<br>
+ case Vload_halfn:<br>
+ case Vloada_halfn:<br>
+ vtn_handle_opencl_vload(b, ext_opcode, w, count);<br>
+ return true;<br>
+ case Vstoren:<br>
+ case Vstore_half:<br>
+ case Vstore_half_r:<br>
+ case Vstore_halfn:<br>
+ case Vstore_halfn_r:<br>
+ case Vstorea_halfn:<br>
+ case Vstorea_halfn_r:<br>
+ vtn_handle_opencl_vstore(b, ext_opcode, w, count);<br>
+ return true;<br>
+ case Printf:<br>
+ handle_instr(b, ext_opcode, w, count, handle_printf);<br>
+ return true;<br>
+ case Prefetch:<br>
+ /* TODO maybe add a nir instruction for this? */<br>
+ return true;<br>
+ default:<br>
+ vtn_fail("unhandled opencl opc: %u\n", ext_opcode);<br>
+ return false;<br>
+ }<br>
+}<br>
diff --git a/src/compiler/spirv/vtn_<wbr>private.h b/src/compiler/spirv/vtn_<wbr>private.h<br>
index dbfe9eab58a..510c12faa87 100644<br>
--- a/src/compiler/spirv/vtn_<wbr>private.h<br>
+++ b/src/compiler/spirv/vtn_<wbr>private.h<br>
@@ -744,6 +744,9 @@ void vtn_handle_subgroup(struct vtn_builder *b, SpvOp opcode,<br>
bool vtn_handle_glsl450_<wbr>instruction(struct vtn_builder *b, uint32_t ext_opcode,<br>
const uint32_t *words, unsigned count);<br>
<br>
+bool vtn_handle_opencl_instruction(<wbr>struct vtn_builder *b, uint32_t ext_opcode,<br>
+ const uint32_t *words, unsigned count);<br>
+<br>
static inline uint32_t<br>
vtn_align_u32(uint32_t v, uint32_t a)<br>
{<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.14.3<br>
<br>
______________________________<wbr>_________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/<wbr>mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div></div>