<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Mon, Jan 21, 2019 at 2:09 AM Iago Toral <<a href="mailto:itoral@igalia.com">itoral@igalia.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="text-align:left;direction:ltr"><div>On Fri, 2019-01-18 at 06:46 -0600, Jason Ekstrand wrote:</div><blockquote type="cite" style="margin:0px 0px 0px 0.8ex;border-left:2px solid rgb(114,159,207);padding-left:1ex">
<div dir="auto">
<div dir="auto"><span style="font-family:sans-serif;font-size:10pt">On January 18, 2019 01:48:25 Iago Toral <<a href="mailto:itoral@igalia.com" target="_blank">itoral@igalia.com</a>> wrote:</span></div><div id="gmail-m_2616852666509088647aqm-original" style="color:black"><div style="text-align:left;direction:ltr" class="gmail-m_2616852666509088647aqm-original-body"><div style="color:black">
<blockquote type="cite" style="margin:0px 0px 0px 0.8ex;border-left:2px solid rgb(114,159,207);padding-left:1ex">
<div>On Thu, 2019-01-17 at 13:42 -0600, Jason Ekstrand wrote:</div><blockquote type="cite" style="margin:0px 0px 0px 0.8ex;border-left:2px solid rgb(114,159,207);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Tue, Jan 15, 2019 at 7:54 AM Iago Toral Quiroga <<a href="mailto:itoral@igalia.com" target="_blank">itoral@igalia.com</a>> wrote<br></div></div></div></blockquote></blockquote></div></div></div></div></blockquote><div><br></div><div>(...)</div><div><br></div><blockquote type="cite" style="margin:0px 0px 0px 0.8ex;border-left:2px solid rgb(114,159,207);padding-left:1ex"><div dir="auto"><div id="gmail-m_2616852666509088647aqm-original" style="color:black" dir="auto"><div style="text-align:left;direction:ltr" class="gmail-m_2616852666509088647aqm-original-body"><div style="color:black"><blockquote type="cite" style="margin:0px 0px 0px 0.8ex;border-left:2px solid rgb(114,159,207);padding-left:1ex"><blockquote type="cite" style="margin:0px 0px 0px 0.8ex;border-left:2px solid rgb(114,159,207);padding-left:1ex"><div><br></div><div dir="ltr"><div class="gmail_quote"><blockquote type="cite" style="margin:0px 0px 0px 0.8ex;border-left:2px solid rgb(114,159,207);padding-left:1ex">
+   nir_ssa_def *tmp = nir_build_alu(b, op1, src, NULL, NULL, NULL);<br>
+   nir_ssa_def *res = nir_build_alu(b, op2, tmp, NULL, NULL, NULL);<br>
+   nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, nir_src_for_ssa(res));<br>
+   nir_instr_remove(&alu->instr);<br>
+}<br>
+<br>
+static bool<br>
+lower_instr(nir_builder *b, nir_alu_instr *alu)<br>
+{<br>
+   unsigned src_bit_size = nir_src_bit_size(alu->src[0].src);<br>
+   nir_alu_type src_type = nir_op_infos[alu->op].input_types[0];<br>
+   nir_alu_type src_full_type = (nir_alu_type) (src_type | src_bit_size);<br>
+<br>
+   unsigned dst_bit_size = nir_dest_bit_size(alu->dest.dest);<br>
+   nir_alu_type dst_type = nir_op_infos[alu->op].output_type;<br>
+   nir_alu_type dst_full_type = (nir_alu_type) (dst_type | dst_bit_size);<br></blockquote><div><br></div><div>The nir_op_infos[*].output_type will already be a full type.  Maybe just delete the dst_type temporary so that no one gets confused and thinks it's a base type.<br></div></div></div></blockquote><div><br></div><div>Right, will do that.</div></blockquote></div></div></div></div></blockquote><div><br></div><div>Actually, we want the base type too, since that is what we pass to the get_conversion_op() helper, I'll just pick it from the dst_full_type with the nir_alu_type_get_base_type() helper.</div></div></blockquote><div><br></div><div>Sounds like the right thing to do.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="text-align:left;direction:ltr"><blockquote type="cite" style="margin:0px 0px 0px 0.8ex;border-left:2px solid rgb(114,159,207);padding-left:1ex"><div dir="auto"><div id="gmail-m_2616852666509088647aqm-original" style="color:black" dir="auto"><div style="text-align:left;direction:ltr" class="gmail-m_2616852666509088647aqm-original-body"><div style="color:black"><blockquote type="cite" style="margin:0px 0px 0px 0.8ex;border-left:2px solid rgb(114,159,207);padding-left:1ex"><blockquote type="cite" style="margin:0px 0px 0px 0.8ex;border-left:2px solid rgb(114,159,207);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><blockquote type="cite" style="margin:0px 0px 0px 0.8ex;border-left:2px solid rgb(114,159,207);padding-left:1ex">
+<br>
+   /* BDW PRM, vol02, Command Reference Instructions, mov - MOVE:<br>
+    *<br>
+    *   "There is no direct conversion from HF to DF or DF to HF.<br>
+    *    Use two instructions and F (Float) as an intermediate type.<br>
+    *<br>
+    *    There is no direct conversion from HF to Q/UQ or Q/UQ to HF.<br>
+    *    Use two instructions and F (Float) or a word integer type<br>
+    *    or a DWord integer type as an intermediate type."<br>
+    */<br>
+   if ((src_full_type == nir_type_float16 && dst_bit_size == 64) ||<br>
+       (src_bit_size == 64 && dst_full_type == nir_type_float16)) {<br>
+      nir_op op1 = get_conversion_op(src_type, src_bit_size, src_type, 32,<br>
+                                     nir_rounding_mode_undef);<br>
+      nir_op op2 = get_conversion_op(src_type, 32, dst_type, dst_bit_size,<br>
+                                     get_opcode_rounding_mode(alu->op));<br>
+      split_conversion(b, alu, op1, op2);<br>
+      return true;<br>
+   }<br>
+<br>
+   /* SKL PRM, vol 02a, Command Reference: Instructions, Move:<br>
+    *<br>
+    *   "There is no direct conversion from B/UB to DF or DF to B/UB. Use<br>
+    *    two instructions and a word or DWord intermediate type."<br>
+    *<br>
+    *   "There is no direct conversion from B/UB to Q/UQ or Q/UQ to B/UB.<br>
+    *    Use two instructions and a word or DWord intermediate integer<br>
+    *    type."<br>
+    */<br>
+   if ((src_bit_size == 8 && dst_bit_size == 64) ||<br>
+       (src_bit_size == 64 && dst_bit_size == 8)) {<br>
+      nir_op op1 = get_conversion_op(src_type, src_bit_size, src_type, 32,<br>
+                                     nir_rounding_mode_undef);<br>
+      nir_op op2 = get_conversion_op(src_type, 32, dst_type, dst_bit_size,<br>
+                                     nir_rounding_mode_undef);<br>
+      split_conversion(b, alu, op1, op2);<br>
+      return true;<br>
+   }<br>
+<br>
+   return false;<br>
+}<br>
+<br>
+static bool<br>
+lower_impl(nir_function_impl *impl)<br>
+{<br>
+   nir_builder b;<br>
+   nir_builder_init(&b, impl);<br>
+   bool progress = false;<br>
+<br>
+   nir_foreach_block(block, impl) {<br>
+      nir_foreach_instr_safe(instr, block) {<br>
+         if (instr->type != nir_instr_type_alu)<br>
+            continue;<br>
+<br>
+         nir_alu_instr *alu = nir_instr_as_alu(instr);<br>
+         assert(alu->dest.dest.is_ssa);<br>
+<br>
+         if (nir_op_infos[alu->op].num_inputs > 1)<br></blockquote><div><br></div><div>I don't think this is a reliable way to detect conversion opcodes.  There are many unary operations that aren't conversions.  You're only getting lucky because there are non which do float16 <-> 64=bit or 8-bit <-> 64-bit.  I've thought many times about throwing a boolean in nir_op_infos for is_conversion.  Maybe now is a good time to do it?<br></div></div></div></blockquote><div><br></div><div>Yes, I knew that this was not going to only let conversions through and was relying on what you say above. Adding an is_conversion field souns good to me, alternatively I guess we could add a is_conversion() helper, but I guess having the field in nir_op_infos maye be a bit easier to maintain if we want to add more opcodes in the future.</div></blockquote></div></div></div></div></blockquote><div><br></div><div>Thinking about this, I guess we only want to set this to True for the opcodes that are explicit numeric conversions, that is, the ones we generate right after "# Generate all of the numeric conversion opcodes". I am saying this because even if we have unop_convert(), that is also used for things like unpack opcodes, but_count, etc that are not real conversions. I was thinking about creating a new unop_numeric_convert() helper and have all other helpers set this new field to False. Sounds good?</div></div></blockquote><div><br></div><div>Seems reasonable.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="text-align:left;direction:ltr"><blockquote type="cite" style="margin:0px 0px 0px 0.8ex;border-left:2px solid rgb(114,159,207);padding-left:1ex"><div dir="auto"><div id="gmail-m_2616852666509088647aqm-original" style="color:black" dir="auto"><div style="text-align:left;direction:ltr" class="gmail-m_2616852666509088647aqm-original-body"><div style="color:black"><blockquote type="cite" style="margin:0px 0px 0px 0.8ex;border-left:2px solid rgb(114,159,207);padding-left:1ex"><blockquote type="cite" style="margin:0px 0px 0px 0.8ex;border-left:2px solid rgb(114,159,207);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><blockquote type="cite" style="margin:0px 0px 0px 0.8ex;border-left:2px solid rgb(114,159,207);padding-left:1ex">
+            continue;<br>
+<br>
+         progress = lower_instr(&b, alu) || progress;<br>
+      }<br>
+   }<br>
+<br>
+   if (progress) {<br>
+      nir_metadata_preserve(impl, nir_metadata_block_index |<br>
+                                  nir_metadata_dominance);<br>
+   }<br>
+<br>
+   return progress;<br>
+}<br>
+<br>
+bool<br>
+brw_nir_lower_conversions(nir_shader *shader)<br>
+{<br>
+   bool progress = false;<br>
+<br>
+   nir_foreach_function(function, shader) {<br>
+      if (function->impl)<br>
+         progress |= lower_impl(function->impl);<br>
+   }<br>
+<br>
+   return progress;<br>
+}<br>
diff --git a/src/intel/compiler/meson.build b/src/intel/compiler/meson.build<br>
index f8e5e2518fe..9daf2a94260 100644<br>
--- a/src/intel/compiler/meson.build<br>
+++ b/src/intel/compiler/meson.build<br>
@@ -76,6 +76,7 @@ libintel_compiler_files = files(<br>
   'brw_nir_analyze_boolean_resolves.c',<br>
   'brw_nir_analyze_ubo_ranges.c',<br>
   'brw_nir_attribute_workarounds.c',<br>
+  'brw_nir_lower_conversions.c',<br>
   'brw_nir_lower_cs_intrinsics.c',<br>
   'brw_nir_lower_image_load_store.c',<br>
   'brw_nir_lower_mem_access_bit_sizes.c',<br>
</blockquote></div></div></blockquote><br></blockquote>
</div>
</div>


</div><div dir="auto"><br></div>
</div>

</blockquote></div>
</blockquote></div></div>