<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/63708>63708</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            LLVM/clang-cl 16.0.6 emitting "packed single" instructions reading and using potentially uninitialized memory
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          MarcKamradt
      </td>
    </tr>
</table>

<pre>
    We are using Visual Studio 2022 and clang-cl with an external installation of LLVM to generate an x64 executable.

After switching from 15.0.7 to any of the 16.0.x version we encounter multiple miscompilations in the release builds of our project.

I have narrowed a case down to a pretty small test project which is attached. It shows that the generated code uses "packed single" instructions instead of the expected "scalar single" instructions. These SIMD instructions read beyond the actual data involved in the calculation, which can be uninitialized or even unallocated memory. In the test project I forced that data to be NaNs so when stepping through the assembly code it can be observed that those NaNs are read into SIMD registers and then used in math operations, which in turn can raise floating point exceptions - which is exactly what is happening in our application.

Here is a snippet of the source:

```
[...]

// method of class SmoothedValue
float nextValue ()
{
    current += ( target - current ) * lerpValue;
    return current;
}

[...]

// using the class
const float dry = m_dry.nextValue ();
const float wet = m_wet.nextValue ();

[...]
```

And here is a snippet of the resulting assembly code:

```
movups      xmm0,xmmword ptr [rcx+4Ch]  // <-- READS DATA BEYOND THE LAST FLOAT OF THE CLASS
movups      xmm1,xmmword ptr [rcx+48h]  
movss       xmm2,dword ptr [rcx+30h]  
movss       xmm3,dword ptr [rcx+34h]  
movss       xmm4,dword ptr [rcx+38h]  
movlhps     xmm4,xmm0  
shufps      xmm4,xmm0,0E2h              // also: why override xmm4 which has just been written to?
movlhps     xmm3,xmm1  
shufps      xmm3,xmm1,0E2h  
subps       xmm4,xmm3  
movups      xmm0,xmmword ptr [rcx+44h]  
movlhps     xmm2,xmm0  
shufps      xmm2,xmm0,0E2h  
mulps       xmm2,xmm4                   // <-- POTENTIALLY USE NANS FOR MATH OPERATIONS
addps       xmm2,xmm3  

```

The Visual Studio 2022 project:

[TestClang16.zip](https://github.com/llvm/llvm-project/files/11965947/TestClang16.zip)

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVltvIjsS_jXmpZRW4-aWBx4aCJpoczka2Fmdp5WxC9pn3XbLruYyv37lvkwCk2T3oCgk2N9X9X1VVJcIQR8s4pyNF2y8GoiaCufnz8LLf4jSC0WDnVOX-b8QhEeog7YH-KFDLQxsqFbaAU85B2EVSCPs4U4aOGkqQFjAM6G3woC2gYQxgrSz4Pbw9PTjGcjBAS16QRgvnycjwDPKmsTOYMLSFUvz9ne-J_QQTppkEePvvSthOE7SZBpZhL1EUioQhpMkTc5wRB9iqBMCWulqG_FlbUhXBqHUQbqy0m0-AbRtsB4NioCwq7VRITK62kPl3V8o6SqfRyjEEcEK790JFQiQEajcyTb5QOWR6AKhFMYAYaCeBk6FlgXoAIJIyAJVAo8EoXCnAFQIajLpbVEgnYqmYwDGeSXkf1BBLIFBxnljq69lryIQCtU7gecKZaRgnAcpjPCfABPYFhgQNo_Pq2tGH-l2eHFWNZRCUiy7EiRA26MzR1S9eVIYWbeGMr7sZEphYYdQW201aWH0T1TgPOARLdRWGONkI7PE0vlLAo8t2ZVjj7B3XqJq7WmCk4u0L-IlQHBwKtBCIKyq2BtUeFcfijbhELDcmUtro6Y-IbcL6I89JRUudGyxxRvV2pJrHfF40IHQh6bFKcaqQ6u7FFSAq2Kpol9vuqMntbdNOC90QNgbJyimVzltCfAssWpNvntrCTwLSeYCp5iVDlCIqkIbUdo2vSiqymjZRLvqx2_osekpCFZXFVLfBMHVXiLL8ve32STtftp_x4skSdh4dXWHrxlfQ4lUuKalpBEhwKZ0jgpUP4Spsb3ZKAOLZ2o-BMZnjN93LNNF98eSszyF7k3W3qMlYHzBslVEAAl_QIK7d2f3wHgOBn3VRss-5PLYOt3C3i5Nr-V8obEdaU0PR43tmXQ2UFs1UP4C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