<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 19, 2017 at 9:17 AM, Rackover, Zvi <span dir="ltr"><<a href="mailto:zvi.rackover@intel.com" target="_blank">zvi.rackover@intel.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">





<div lang="EN-US">
<div class="gmail-m_2621688253723161924WordSection1">
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)">Hi Sanjay,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)">I agree we should also discuss *<b>if</b>* this canonicalization is beneficial.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)">For starters, do we have a concrete case where we would benefit from canonicalizing shuffles <-> truncates in LLVM IR?<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)">IMO, we should not count benefits for codegen because that alone does not justify transforming the IR ; we could always do this on the SelectionDAG.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)"><u></u></span></p></div></div></blockquote><div><br></div><div>Agreed. If we're just talking about IR benefits, then it's easy to demonstrate a win for trunc/zext based on value tracking:<br><br>target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" ; little-endian<br><br>define <4 x i32> @shuffle(<4 x i64> %x) {<br>  %y = shl <4 x i64> %x, <i64 32, i64 32, i64 32, i64 32> ; low half of each elt is zero<br>  %bc = bitcast <4 x i64> %y to <8 x i32> ; even index elements are all zero<br>  %trunc = shufflevector <8 x i32> %bc, <8 x i32> undef, <4 x i32> <i32 0, i32 2, i32 4, i32 6><br>  ret <4 x i32> %trunc<br>}<br><br>define <4 x i32> @trunc(<4 x i64> %x) {<br>  %y = shl <4 x i64> %x <i64 32, i64 32, i64 32, i64 32>  ; low half of each elt is zero<br>  %trunc = trunc <4 x i64> %y to <4 x i32> ; so this must be zero...<br>  ret <4 x i32> %trunc<br>}<br><br></div><div><br>$ ./opt -instsimplify 31551.ll -S<br>...<br>define <4 x i32> @shuffle(<4 x i64> %x) {<br>  %y = shl <4 x i64> %x, <i64 32, i64 32, i64 32, i64 32><br>  %bc = bitcast <4 x i64> %y to <8 x i32><br>  %trunc = shufflevector <8 x i32> %bc, <8 x i32> undef, <4 x i32> <i32 0, i32 2, i32 4, i32 6><br>  ret <4 x i32> %trunc<br>}<br><br>define <4 x i32> @trunc(<4 x i64> %x) {<br>  ret <4 x i32> zeroinitializer<br>}<br><br></div><div>Of course, this is something I invented as an example, but AFAIK we have better value tracking for trunc/zext than shuffle, so we'll have an easier time folding the IR if that is possible.<br></div><div><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 lang="EN-US"><div class="gmail-m_2621688253723161924WordSection1"><p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)"> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)">--Zvi<u></u><u></u></span></p>
<p class="MsoNormal"><a name="m_2621688253723161924__MailEndCompose"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)"><u></u> <u></u></span></a></p>
<p class="MsoNormal"><a name="m_2621688253723161924______replyseparator"></a><b><span style="font-size:11pt;font-family:"calibri",sans-serif">From:</span></b><span style="font-size:11pt;font-family:"calibri",sans-serif"> Sanjay Patel [mailto:<a href="mailto:spatel@rotateright.com" target="_blank">spatel@rotateright.com</a><wbr>]
<br>
<b>Sent:</b> Tuesday, January 17, 2017 18:38</span></p><div><div class="gmail-h5"><br>
<b>To:</b> Rackover, Zvi <<a href="mailto:zvi.rackover@intel.com" target="_blank">zvi.rackover@intel.com</a>><br>
<b>Cc:</b> Friedman, Eli <<a href="mailto:efriedma@codeaurora.org" target="_blank">efriedma@codeaurora.org</a>>; llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>><br>
<b>Subject:</b> Re: [llvm-dev] IR canonicalization: shufflevector or vector trunc?<u></u><u></u></div></div><p></p><div><div class="gmail-h5">
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12pt">We use InstCombiner::<wbr>ShouldChangeType() to prevent transforms to illegal integer types, but I'm not sure how that would apply to vector types.
<br>
<br>
Ie, let's say v256 is a legal type in your example. DataLayout doesn't appear to specify what configurations of a 256-bit vector are legal, so I don't think we can currently use that to say v2i128 should be treated differently than v16i16.<u></u><u></u></p>
</div>
<p class="MsoNormal">Is this a valid argument to not canonicalize the IR?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">On Mon, Jan 16, 2017 at 10:16 AM, Rackover, Zvi <<a href="mailto:zvi.rackover@intel.com" target="_blank">zvi.rackover@intel.com</a>> wrote:<u></u><u></u></p>
<blockquote style="border-width:medium medium medium 1pt;border-style:none none none solid;border-color:-moz-use-text-color -moz-use-text-color -moz-use-text-color rgb(204,204,204);padding:0cm 0cm 0cm 6pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<p class="MsoNormal" style="background:white none repeat scroll 0% 0%">
<span style="font-size:10pt;font-family:"courier new";color:black">Suppose we prefer the ‘trunc’ form, then what about cases such as:</span><u></u><u></u></p>
<p class="MsoNormal" style="background:white none repeat scroll 0% 0%">
<span style="font-size:10pt;font-family:"courier new";color:black">define <2 x i16> @shuffle(<16 x i16> %x) {</span><u></u><u></u></p>
<p class="MsoNormal" style="background:white none repeat scroll 0% 0%">
<span style="font-size:10pt;font-family:"courier new";color:black">  %shuf = shufflevector <16 x i16> %x, <16 x i16> undef, <2 x i32> <i32 0, i32 8></span><u></u><u></u></p>
<p class="MsoNormal" style="background:white none repeat scroll 0% 0%">
<span style="font-size:10pt;font-family:"courier new";color:black">  ret <2 x i16> %shuf</span><u></u><u></u></p>
<p class="MsoNormal" style="background:white none repeat scroll 0% 0%">
<span style="font-size:10pt;font-family:"courier new";color:black">}</span><u></u><u></u></p>
<p class="MsoNormal" style="background:white none repeat scroll 0% 0%">
<span style="font-size:10pt;font-family:"courier new";color:black"> </span><u></u><u></u></p>
<p class="MsoNormal" style="background:white none repeat scroll 0% 0%">
<span style="font-size:10pt;font-family:"courier new";color:black">Will the ‘shufflevector’ be canonicalized to a ‘trunc’ of a vector of i128?</span><u></u><u></u></p>
<p class="MsoNormal" style="background:white none repeat scroll 0% 0%">
<span style="font-size:10pt;font-family:"courier new";color:black">define <2 x i16> @trunc(<16 x i16> %x) {</span><u></u><u></u></p>
<p class="MsoNormal" style="background:white none repeat scroll 0% 0%">
<span style="font-size:10pt;font-family:"courier new";color:black">  %bc = bitcast <16 x i16> %x to <2 x i128></span><u></u><u></u></p>
<p class="MsoNormal" style="background:white none repeat scroll 0% 0%">
<span style="font-size:10pt;font-family:"courier new";color:black">  %tr = trunc <2 x i128> %bc to <2 x i16></span><u></u><u></u></p>
<p class="MsoNormal" style="background:white none repeat scroll 0% 0%">
<span style="font-size:10pt;font-family:"courier new";color:black">  ret <2 x i16> %tr</span><u></u><u></u></p>
<p class="MsoNormal" style="background:white none repeat scroll 0% 0%">
<span style="font-size:10pt;font-family:"courier new";color:black">}</span><u></u><u></u></p>
<p class="MsoNormal" style="background:white none repeat scroll 0% 0%">
<span style="font-size:10pt;font-family:"courier new";color:black">This may challenge the Legalizer downstream.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)">--Zvi</span><u></u><u></u></p>
<p class="MsoNormal"><a name="m_2621688253723161924_m_4307999342758701085__MailEndCompose"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)"> </span></a><u></u><u></u></p>
<p class="MsoNormal"><a name="m_2621688253723161924_m_4307999342758701085______replyseparato"></a><b><span style="font-size:11pt;font-family:"calibri",sans-serif">From:</span></b><span style="font-size:11pt;font-family:"calibri",sans-serif">
 Sanjay Patel [mailto:<a href="mailto:spatel@rotateright.com" target="_blank">spatel@rotateright.com</a><wbr>]
<br>
<b>Sent:</b> Friday, January 13, 2017 18:19<br>
<b>To:</b> Rackover, Zvi <<a href="mailto:zvi.rackover@intel.com" target="_blank">zvi.rackover@intel.com</a>><br>
<b>Cc:</b> Friedman, Eli <<a href="mailto:efriedma@codeaurora.org" target="_blank">efriedma@codeaurora.org</a>>; llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span><u></u><u></u></p>
<div>
<div>
<p class="MsoNormal"><br>
<b>Subject:</b> Re: [llvm-dev] IR canonicalization: shufflevector or vector trunc?<u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12pt">Right - I think that case looks like this for little endian:<br>
<br>
define <2 x i32> @zextshuffle(<2 x i16> %x) {<br>
  %zext_shuffle = shufflevector <2 x i16> %x, <2 x i16> zeroinitializer, <4 x i32> <i32 0, i32 2, i32 1, i32 2><br>
  %bc = bitcast <4 x i16> %zext_shuffle to <2 x i32><br>
  ret <2 x i32> %bc<br>
}<br>
<br>
define <2 x i32> @zextvec(<2 x i16> %x) {<br>
  %zext = zext <2 x i16> %x to <2 x i32><br>
  ret <2 x i32> %zext<br>
}<u></u><u></u></p>
</div>
<p class="MsoNormal">IMO, the fact that we have to take endianness into account with the shuffles makes the trunc/zext forms the better choice. That way, we limit the endian dependency to one place
 in InstCombine, and other transforms don't have to worry about it. We also have lots of existing folds for trunc/zext and hardly any for shuffles.<u></u><u></u></p>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
<div>
<p class="MsoNormal">On Thu, Jan 12, 2017 at 1:14 PM, Rackover, Zvi <<a href="mailto:zvi.rackover@intel.com" target="_blank">zvi.rackover@intel.com</a>> wrote:<u></u><u></u></p>
<blockquote style="border-width:medium medium medium 1pt;border-style:none none none solid;border-color:-moz-use-text-color -moz-use-text-color -moz-use-text-color rgb(204,204,204);padding:0cm 0cm 0cm 6pt;margin:5pt 0cm 5pt 4.8pt">
<div>
<div>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)">Just to add, there is also the ‘zext’ – ‘shuffle with zero’ duality which can broaden the discussion.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)">--Zvi</span><u></u><u></u></p>
<p class="MsoNormal"><a name="m_2621688253723161924_m_4307999342758701085_m_6851396901653097"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)"> </span><u></u><u></u></a></p>
<p class="MsoNormal"><b><span style="font-size:11pt;font-family:"calibri",sans-serif">From:</span></b><span style="font-size:11pt;font-family:"calibri",sans-serif"> Sanjay Patel [mailto:<a href="mailto:spatel@rotateright.com" target="_blank">spatel@rotateright.com</a><wbr>]
<br>
<b>Sent:</b> Thursday, January 12, 2017 20:19<br>
<b>To:</b> Friedman, Eli <<a href="mailto:efriedma@codeaurora.org" target="_blank">efriedma@codeaurora.org</a>><br>
<b>Cc:</b> llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>>; Rackover, Zvi <<a href="mailto:zvi.rackover@intel.com" target="_blank">zvi.rackover@intel.com</a>><br>
<b>Subject:</b> Re: [llvm-dev] IR canonicalization: shufflevector or vector trunc?</span><u></u><u></u></p>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
<div>
<p class="MsoNormal">On Thu, Jan 12, 2017 at 11:06 AM, Friedman, Eli <<a href="mailto:efriedma@codeaurora.org" target="_blank">efriedma@codeaurora.org</a>> wrote:<u></u><u></u></p>
<blockquote style="border-width:medium medium medium 1pt;border-style:none none none solid;border-color:-moz-use-text-color -moz-use-text-color -moz-use-text-color rgb(204,204,204);padding:0cm 0cm 0cm 6pt;margin:5pt 0cm 5pt 4.8pt">
<div>
<div>
<div>
<div>
<p class="MsoNormal">On 1/12/2017 9:04 AM, Sanjay Patel via llvm-dev wrote:<u></u><u></u></p>
</div>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<p class="MsoNormal">It's time for another round of "What is the canonical IR?"<br>
<br>
Credit for this episode to Zvi and PR31551. :)<br>
<a href="https://llvm.org/bugs/show_bug.cgi?id=31551" target="_blank">https://llvm.org/bugs/show_<wbr>bug.cgi?id=31551</a>
<u></u><u></u></p>
<pre>define <4 x i16> @shuffle(<16 x i16> %x) {<u></u><u></u></pre>
<pre>  %shuf = shufflevector <16 x i16> %x, <16 x i16> undef, <4 x i32> <i32 0, i32 4, i32 8, i32 12><u></u><u></u></pre>
<pre>  ret <4 x i16> %shuf<u></u><u></u></pre>
<pre>}<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>define <4 x i16> @trunc(<16 x i16> %x) {<u></u><u></u></pre>
<pre>  %bc = bitcast <16 x i16> %x to <4 x i64><u></u><u></u></pre>
<pre>  %tr = trunc <4 x i64> %bc to <4 x i16><u></u><u></u></pre>
<pre>  ret <4 x i16> %tr<u></u><u></u></pre>
<pre>}<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<p class="MsoNormal">Potential reasons to prefer one or the other:<br>
1. Shuffle is the most compact.<br>
2. Trunc is easier to read.<br>
3. One of these is easier for value tracking.<br>
4. Compatibility with existing IR transforms (eg, InterleavedAccess recognizes the shuffle form).<u></u><u></u></p>
</div>
<p class="MsoNormal">5. We don't create arbitrary shuffle masks in IR because that's bad for a lot of targets (but maybe this mask pattern should always be recognized as special?).<u></u><u></u></p>
</div>
</blockquote>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<p class="MsoNormal">Hmm... not sure what the right answer is, but a couple more observations:<br>
1. If we're going to canonicalize, we should probably canonicalize the same way independent of the original argument type (so we would introduce bitcasts either way).<u></u><u></u></p>
</div>
</blockquote>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Ah, right - kill #1 in my list.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<blockquote style="border-width:medium medium medium 1pt;border-style:none none none solid;border-color:-moz-use-text-color -moz-use-text-color -moz-use-text-color rgb(204,204,204);padding:0cm 0cm 0cm 6pt;margin:5pt 0cm 5pt 4.8pt">
<div>
<p class="MsoNormal">2. Those two functions are only equivalent on little-endian platforms.<u></u><u></u></p>
</div>
</blockquote>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">I was wondering about that. So yes, if we do want to canonicalize (until the recent compile-time complaints, I always thought this was the objective of InstCombine...maybe it still
 is), then the masks we're matching or generating will differ based on endianness.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<p>------------------------------<wbr>------------------------------<wbr>---------<br>
Intel Israel (74) Limited<u></u><u></u></p>
<p>This e-mail and any attachments may contain confidential material for<br>
the sole use of the intended recipient(s). Any review or distribution<br>
by others is strictly prohibited. If you are not the intended<br>
recipient, please contact the sender and delete all copies.<u></u><u></u></p>
</div>
</blockquote>
</div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<p>------------------------------<wbr>------------------------------<wbr>---------<br>
Intel Israel (74) Limited<u></u><u></u></p>
<p>This e-mail and any attachments may contain confidential material for<br>
the sole use of the intended recipient(s). Any review or distribution<br>
by others is strictly prohibited. If you are not the intended<br>
recipient, please contact the sender and delete all copies.<u></u><u></u></p>
</div>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div></div></div><div><div class="gmail-h5">
<p>------------------------------<wbr>------------------------------<wbr>---------<br>
Intel Israel (74) Limited</p>

<p>This e-mail and any attachments may contain confidential material for<br>
the sole use of the intended recipient(s). Any review or distribution<br>
by others is strictly prohibited. If you are not the intended<br>
recipient, please contact the sender and delete all copies.</p></div></div></div>

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