<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/114540>114540</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang] Incorrect assembly code generated when optimizing storage of doubles in an int buffer
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
john-bell-unity
</td>
</tr>
</table>
<pre>
The code below...
```
#include <stdint.h>
// Write a 64-bit double an int buffer as 2 x 32 bit ints (a and b)
void WriteDoubleToIntBuffer(int32_t* destIntBuffer, double sourceDouble)
{
int32_t* aSource = (int32_t*)&sourceDouble;
int32_t* bSource = (int32_t*)((intptr_t)aSource + 4);
destIntBuffer[0] = *aSource;
destIntBuffer[1] = *bSource;
}
```
...compiles to the following assembly when using O2 or higher optimization, on Clang 12.0.0 or later (11.0.1 does not have this bug, and lower optimizations settings do not have this bug):
```
WriteDoubleToBufferAsInts(void*, double):
sub sp, sp, #16
fmov x8, d0
str w8, [x0]
ldr w8, [sp, #12]
str w8, [x0, #4]
add sp, sp, #16
ret
```
In the `ldr w8, [sp, #12]` statement, we expect to be able to low the high bytes of the double from the stack, but we never stored the double to the stack in preparation for this (although we do put it in a corresponding location if using lower optimization levels, or Clang version 11.0.1 or earlier). I can reproduce this either locally (via the macOS commands below) on or Compiler Explorer ([link](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,selection:(endColumn:33,endLineNumber:9,positionColumn:33,positionLineNumber:9,selectionStartColumn:33,selectionStartLineNumber:9,startColumn:33,startLineNumber:9),source:'%23include+%3Cstdint.h%3E%0A%0A//+Write+a+double+an+int+buffer+as+2+x+32+bit+ints+(a+and+b)%0Avoid+WriteDoubleToIntBuffer(int32_t*+destIntBuffer,+double+sourceDouble)%0A%7B%0A++++int32_t*+aSource+%3D+(int32_t*)%26sourceDouble%3B%0A++++int32_t*+bSource+%3D+(int32_t*)((intptr_t)aSource+%2B+4)%3B%0A++++destIntBuffer%5B0%5D+%3D+*aSource%3B%0A++++destIntBuffer%5B1%5D+%3D+*bSource%3B%0A%7D'),l:'5',n:'1',o:'C%2B%2B+source+%231',t:'0')),k:61.67344685839352,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:armv8-clang-trunk,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!(),options:'-O2',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+armv8-a+clang+(trunk)+(Editor+%231)',t:'0')),k:38.32655314160647,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)).
`/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -std=c++11 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.4.sdk -O2 -o issue.o -stdlib=libc++ -arch arm64 -mios-version-min=10.12 -c -x c++ issue.cpp`
```
Apple clang version 15.0.0 (clang-1500.3.9.4)
Target: arm64-apple-darwin23.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysWEtv4zrS_TX0pmBBoh62F15YdgcIvg-TBpJ57AaUVLbYTZECSTnO_PoBSfkhx52-c3GDwDbJU6f4qDolihnDDxJxTfKS5LsZG2yr9PqHauW8QiHmg-T2Y1ap5mP91iLUqkGoUKj3KIpIvCPx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