<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/54921>54921</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Floating point exception in clang14.0.0 with -mavx2
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
KierenP
</td>
</tr>
</table>
<pre>
Hi, I'm hoping someone can look at this and tell me if I've found a legitimate compiler bug.
I am compiling the following the small following test program:
```
#include <cfenv>
#include <iostream>
bool driver(unsigned int value, double lower, double upper)
{
return lower <= value && value <= upper;
}
int main()
{
feenableexcept(FE_INVALID);
volatile int x = 62;
volatile double lower = 31;
volatile double upper = 62;
volatile int y = driver(x, lower, upper);
}
```
I compile with the command:
```
clang++ test_file.cpp -mavx2 -O3 -g
```
and when I run, I get:
```
Floating point exception (core dumped)
```
----------------------
Compiler information:
```
clang version 14.0.0
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/kieren.pearson/.conan/data/clang/14.0.0/_/_/package/2201725ed6c2736f243c00af939498ce6c18440d/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.5
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.4.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.3.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8.0.1
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.3.0
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64
```
---------------------
I have explored this code in godbolt and compared the assembly dif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cdJ2nDhVFPeIwviSQDj2QxgAOFCDw0A8uAOCBZysSwMVwQgSCOVcKgODx4PoTNtkWddN0C0hd0kOkDzpU871W086WHY9hyB09JpfUh5OHH9Zt4ebP2/X9XsAkCICQODIIe8hKAxhCNuQyQNB/DCsOIHC8PogjmGIOjSPIxgCGo2i30YzaWMnNikpbLjJx45A%2BI2UihKHejRPEySMEF9c5ImpSmBUtSNK0xgZL04RRHEYy1bMtQ310AIbJQOzTrFpyoBc9h3M87yQF8/y3uCmpwsi6LYvi6quecCBXHq69AlSvrctQ5J8pqX3pFKmpA8GYPGpqFqmnSnI/bj2qemjuJY7qpPWmkSZ0%2By9qQFQwalxGioxqlwcR1fej5sW5bVvWmztskXbdsO477IOc6iEe66MTuzHy1XMwFhe/83oQL4sDiXVwd4KGYbfeHbERv8tDe3dh1Pb60LpLZPuh486WPLZJq2Wu5o/JHJ%2BrjgzCvuGb43rcvOwjI9yAA%3D%3D
When looking into the clang14.0.0 code:
```
vcvtsi2sd xmm0, xmm0, rax
vmovsd xmm1, qword ptr [rsp + 24] # xmm1 = mem[0],zero
vmovsd xmm2, qword ptr [rsp + 16] # xmm2 = mem[0],zero
vcmplepd xmm2, xmm0, xmm2
vcmplepd xmm0, xmm1, xmm0
vandpd xmm0, xmm0, xmm2
vmovd eax, xmm0
and eax, 1
```
we can see that the integer is first converted into a double in the lower 64 bits of the `xmm0` register. The lower and upper bounds are put into the `xmm1` and `xmm2` registers. Then `vcmplepd` compares each double in the respective registers. Then a logical and is done and we finally extract the very lowest bit.
I believe clang is going wrong here because it doesn't believe it needs to zero the `xmm0` register. `vcvtsi2sd` leaves the upper 64 bits unchanged and we later discard all but the lowest bit, so this would normally be fine except `vcmplepd` will raise a signal if either of the operands is a `NAN` which I believe is the case here. For whatever reason the register on my computer starts off with a `NAN` in the higher 64 bits.
Clang13.0.1 on the other hand, generates:
```
vcvtsi2sd xmm0, xmm0, rax
vucomisd xmm0, qword ptr [rsp + 24]
vmovsd xmm1, qword ptr [rsp + 16] # xmm1 = mem[0],zero
setae al
vucomisd xmm1, xmm0
setae cl
and cl, al
```
notably, it uses a different comparison function `vucomisd`, and this comparison function only compares the lowest 32 or 64 bits of the register depending on the operands. This is safe because the unchanged higher bits in xmm0 after `vcvtsi2sd` are not compared.
It seems the clang14 generated code is wrong.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy1WFlz4roS_jWZF9dQ3jDwkAd5A7ObJYS8THkRtsC2jBds8utvy4YkM5PJOXXrTIoYZLU-9a5uudS_Po7Ig6hx1oPYi7mQpiQJuJzGmCaY85yEiyg9cU7BFSHJOSfxuQJHERdjjhyaRRfMHWgJ7x0uwgEpSOwUsJLGKYlwxrll0Hng9QcetU-Lc-LbLNupCNnyKKLVfZTHDuB_eIfzgkszGmRO_CChj1gPCn_7tENRIokXlT7mHiTNO-Dk8iAZn80RmhcZZnjGRzyX0ojzM3LB2YPYL5OcBAn2OZIU3MWJSsz05NPSjTBopWJEb-MyTdl4wN3wemr7g4O_DBdllrRL2O4Pkt7icQ-iAp_7oJ1pkST1DqR_5JCxEjskAfZ-2ox73-2AceIAS7j2cFoAoWn8sOZPaGrpsOQNmJFeaOQUYKVGwppjuyvi5xQfxW4IJeFLwkaKLxHZnteG4k3jNVPom2bvKv1EF7_Y_eZYN5_jKlKEjSvBixg8FryG-3ShFzlJ8CCq8Gnc7McBlne8NOW-x86lFrnvC4n7HnyxKYuHKsQJZ3FZmTRxxAW4-OOOZkRBevDqlDL5WxsRmoAn9D2agfrKOMU-k_rPm37_9O8jhXaPPpIcaAYBCVt8rQUOLJAzRgS5w3duMxsnuwlT95Ufivy9TE4JrZLvELpl_T1IyhtdCMHkczH1ccSoU5qT-maVJC8gnrGvk6zhQDRDSC7wdSI4w0knxU6WA3ei2fFo4rAfvlM48HWzjnnjSDR_3P5Txzs5AcMQRV7oiV3sK57Yk5SDKEsezzuHgTSQB30PK57Ql2Ue9Gm6EDStCZpkBZnNJz7LVENNAzU1XL7rSTTLH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