<div dir="ltr">It worked!  I have spent the last hour reading up on tablegen and I still don't understand any of that patch.  But it definitely solved the problem in my test case.  I also ran the clang tests with this change and they all passed so I'm not sure from where you are seeing the change in the behavior around the movdqa.  Maybe I ran them incorrectly?<div><br></div><div>I think that I understand the bit about the delay with pre-Ivybridge.  The delay is because those CPUs aren't able to run at full speed when the mov is followed by the usage.  It's a strange mov, though.  Why did it even do that?  Couldn't it just interleave the xmm register with itself?<br><div><br></div><div>As for collectTiedOperands, does undef mean that the register has yet to be allocated to a specific register in the machine?  What is the situation that you think that Valgrind will still have trouble with?</div></div><div><br></div><div>Thanks for your help!</div><div><br></div><div>Eyal</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Feb 27, 2021 at 2:31 PM Craig Topper <<a href="mailto:craig.topper@gmail.com">craig.topper@gmail.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 dir="ltr"><div>Hi Eyal,</div><div><br></div><div>LLVM knows that those bits are garbage and did this consciously. But it does create false execution dependency on xmm3. Potentially delaying the execution depending on what instruction is producing xmm3. This is definitely not the only case that this can happen.</div><div><br></div><div>This is probably the easiest fix which avoids ever creating the false dependency by recognizing that there is only one real input and assigning it to both sources.</div><div><br></div><div>





<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:10px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><b>--- a/llvm/lib/Target/X86/X86InstrSSE.td</b></span></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:10px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><b>+++ b/llvm/lib/Target/X86/X86InstrSSE.td</b></span></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:10px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(56,185,199)">@@ -3917,6 +3917,26 @@</span><span style="font-variant-ligatures:no-common-ligatures"> let Constraints = "$src1 = $dst" in {</span></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:10px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><span> </span>}</span></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:10px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><span> </span>} // ExeDomain = SSEPackedInt</span></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:10px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);min-height:11px"><span style="font-variant-ligatures:no-common-ligatures"><span> </span></span></p>
+let Predicates = [UseSSE2] in {<br>+def : Pat <(v16i8 (X86Unpckl undef, VR128:$src)),<br>+           (PUNPCKLBWrr VR128:$src, VR128:$src)>;<br>+def : Pat <(v8i16 (X86Unpckl undef, VR128:$src)),<br>+           (PUNPCKLWDrr VR128:$src, VR128:$src)>;<br>+def : Pat <(v4i32 (X86Unpckl undef, VR128:$src)),<br>+           (PUNPCKLDQrr VR128:$src, VR128:$src)>;<br>+def : Pat <(v2i64 (X86Unpckl undef, VR128:$src)),<br>+           (PUNPCKLQDQrr VR128:$src, VR128:$src)>;<br>+<br>+def : Pat <(v16i8 (X86Unpckh undef, VR128:$src)),<br>+           (PUNPCKHBWrr VR128:$src, VR128:$src)>;<br>+def : Pat <(v8i16 (X86Unpckh undef, VR128:$src)),<br>+           (PUNPCKHWDrr VR128:$src, VR128:$src)>;<br>+def : Pat <(v4i32 (X86Unpckh undef, VR128:$src)),<br>+           (PUNPCKHDQrr VR128:$src, VR128:$src)>;<br>+def : Pat <(v2i64 (X86Unpckh undef, VR128:$src)),<br>+           (PUNPCKHQDQrr VR128:$src, VR128:$src)>;<br>+}<br>+</div><div><br></div><div>Unfortunately, this can introduce extra mov instructions to copy the input register if it is used again by a later instruction. For example</div><div><br></div><div>





<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:10px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><span> </span>define <16 x i8> @test_remconstant_16i8(<16 x i8> %a) nounwind {</span></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:10px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><span> </span>; SSE2-LABEL: test_remconstant_16i8:</span></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:10px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><span> </span>; SSE2: <span>      </span># %bb.0:</span></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:10px;line-height:normal;font-family:Menlo;color:rgb(57,192,38)"><span style="font-variant-ligatures:no-common-ligatures">+; SSE2-NEXT:<span>    </span>movdqa %xmm0, %xmm1</span></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:10px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><span> </span>; SSE2-NEXT:<span>    </span>punpckhbw {{.*#+}} xmm1 = xmm1[8],xmm0[8],xmm1[9],xmm0[9],xmm1[10],xmm0[10],xmm1[11],xmm0[11],xmm1[12],xmm0[12],xmm1[13],xmm0[13],xmm1[14],xmm0[14],xmm1[15],xmm0[15]</span></p></div><div><br></div><div>Note, there's a weird quirk that only the tied source uses the result of the movdqa. The other source is still using xmm0.</div><div><br></div><div>On CPUs without move elimination, pre-Ivybridge for Intel) this potentially adds an extra delay to the execution of the punpck to wait for the movdqa to complete. But depending on the rest of the code in the function that might be better than the false dependency.</div><div><br></div><div>My other thought would be to do something different for undef sources in TwoAddressInstructionPass::collectTiedOperands. Rather than setting it to the destination virtual register, if there is another source with the same register class with a kill flag set, assign the undef vreg to that and keep the tied pair. This would do the same thing as the change in X86InstrSSE.td above, but applied much later and would be limited to cases where the source register isn't used later. But valgrind could still have an issue for code that doesn't get fixed due to an additional use of the register.</div><div><br></div><div><div dir="ltr">~Craig</div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Feb 27, 2021 at 9:53 AM Eyal Soha via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</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 dir="ltr">I spent a few days debugging a valgrind memcheck false positive that was introduced by a new optimization in clang version 10.<br><br>Valgrind is mistakenly reporting an error due to the way that clang 10 is emitting asm.  But it's a trivial matter to fix the code.  I just needed to change this:<br><div><br></div><div>  401697:       66 0f 6e d2             movd   %edx,%xmm2<br>  40169b:       66 0f 60 d2             punpcklbw %xmm2,%xmm2<br>  40169f:       66 0f 61 da             punpcklwd %xmm2,%xmm3<br></div><div><br></div><div>to this:</div><div><br></div><div>  4016a3:       66 0f 6e da             movd   %edx,%xmm3<br>  4016a7:       66 0f 60 db             punpcklbw %xmm3,%xmm3<br>  4016ab:       66 0f 61 db             punpcklwd %xmm3,%xmm3<br></div><div><br></div><div>So instead of using $xmm2 and then $xmm3 just use $xmm3.  This was a tiny change and I was able to modify the executable to do it.</div><div><br></div><div>Is it possible to modify clang/llvm to do this where possible?  Valgrind is a popular tool for finding bugs in code and if it's not too much trouble for llvm and there's no performance penalty, maybe LLVM could accommodate?</div><div><br></div><div>I'm willing to do the work but I'm not sure where to start.help but I'm not sure where to start.</div><div><br></div><div>There are more details about the issue in the Valgrind bug report: <a href="https://bugs.kde.org/show_bug.cgi?id=432801#c12" target="_blank">https://bugs.kde.org/show_bug.cgi?id=432801#c12</a></div><div><br></div><div>Thanks for your help!</div><div><br></div><div>Eyal</div></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>
</blockquote></div>