<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - [X86] Compiler wrongly emits temporal stores when vectorizing a scalar nontemporal memcpy loop."
href="https://bugs.llvm.org/show_bug.cgi?id=40759">40759</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>[X86] Compiler wrongly emits temporal stores when vectorizing a scalar nontemporal memcpy loop.
</td>
</tr>
<tr>
<th>Product</th>
<td>libraries
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Windows NT
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>Backend: X86
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>andrea.dibiagio@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>craig.topper@gmail.com, llvm-bugs@lists.llvm.org, llvm-dev@redking.me.uk, spatel+llvm@rotateright.com
</td>
</tr></table>
<p>
<div>
<pre>Example:
```
void foo(unsigned *A, unsigned *B, unsigned Elts) {
for (unsigned I = 0; I < Elts; ++I) {
unsigned X = A[I];
__builtin_nontemporal_store(X, &B[I]);
}
}
```
<span class="quote">> clang -O2 -march=btver2 -S -o -</span >
```
.LBB0_8: # %for.body
movl (%rdi,%rcx,4), %edx
movntil %edx, (%rsi,%rcx,4) # <<== OK. Nontemporal store.
incq %rcx
cmpq %rcx, %rax
jne .LBB0_8
retq
.LBB0_5: # %vector.ph
movl %eax, %ecx
xorl %edx, %edx
andl $-32, %ecx
.p2align 4, 0x90
.LBB0_6: # %vector.body
vmovups (%rdi,%rdx,4), %ymm0
vmovups 32(%rdi,%rdx,4), %ymm1
vmovups 64(%rdi,%rdx,4), %ymm2
vmovups 96(%rdi,%rdx,4), %ymm3
vmovups %ymm0, (%rsi,%rdx,4) # <<== WRONG. Temporal vector store.
vmovups %ymm1, 32(%rsi,%rdx,4) # Same...
vmovups %ymm2, 64(%rsi,%rdx,4) # Same...
vmovups %ymm3, 96(%rsi,%rdx,4) # Same...
addq $32, %rdx
cmpq %rdx, %rcx
jne .LBB0_6
# %bb.7: # %middle.block
cmpq %rax, %rcx
jne .LBB0_8
```
On X86, (V)MOVNTPS can be used to do non-temporal vector stores.
However, VMOVNTPS requires that the memory operand for the destination is
aligned by 16-bytes (for the 128-bit stores), or 32-bytes (for the 256-bit
stores).
In this example, store instructions are marked as 4-bytes aligned.
When the loop vectorizer kicks in, it generates a vector loop body, and all
vector stores are correctly annotated with metadata flag "!nontemporal" and
aligment 4.
However, on x86 there is no support for unaligned nontemporal stores.
So, ISel falls back to selecting normal (i.e. "temporal") unaligned stores (see
the VMOVUPS from the assembly above).
When vectorizing a memcpy-like loop, we should probably check if the target has
support for unaligned nontemporal vector stores before transforming the loop.
Otherwise, we risk to accidentally introduce temporal stores that pollute the
caches.</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>