<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/61396>61396</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang AVX2 codegen regression with array of self referencing values
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
reductor
</td>
</tr>
</table>
<pre>
The code generates incorrect code in clang 15.0.0 to the latest trunk on compiler explorer ( 57b78faa9eb08017d95bb4e43242648953b08c13 )
```cpp
struct struct_with_sentinal
{
struct_with_sentinal()
{
m_sentinel = &m_sentinel;
}
void * m_sentinel;
};
template <typename T, int T_SIZE>
struct fixed_array
{
T m_data[T_SIZE];
};
fixed_array<struct_with_sentinal, 10240> my_test_array;
```
Current output (with `-std=c++20 -O3 -mavx2`)
```
_GLOBAL__sub_I_example.cpp: # @_GLOBAL__sub_I_example.cpp
xor eax, eax
lea rcx, [rip + my_test_array]
.LBB1_1: # =>This Inner Loop Header: Depth=1
lea rdx, [rcx + rax]
vmovq xmm0, rdx
vpbroadcastq ymm0, xmm0
lea rdx, [rax + rcx]
add rdx, 32
vmovq xmm1, rdx
vpbroadcastq ymm1, xmm1
lea rdx, [rax + rcx + 64]
vmovq xmm2, rdx
vpbroadcastq ymm2, xmm2
lea rdx, [rax + rcx + 96]
vmovq xmm3, rdx
vpbroadcastq ymm3, xmm3
vmovdqu ymmword ptr [rax + rcx], ymm0
vmovdqu ymmword ptr [rax + rcx + 32], ymm1
vmovdqu ymmword ptr [rax + rcx + 64], ymm2
vmovdqu ymmword ptr [rax + rcx + 96], ymm3
sub rax, -128
cmp rax, 81920
jne .LBB1_1
vzeroupper
ret
```
The `vpbroadcastq` and then `movdqu` back into the array ends up generating an array with every 4 elements pointing to the first element not to themselves.
The last known good code gen appears to be in clang 14.0.0 (Based on looking back in compiler explorer)
Compiler explorer link for example code gen:
https://godbolt.org/z/eqx5avca9
</pre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