<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 - Bad for loop copy optimization when using -fno-builtin-memcpy -fno-builtin-memmove"
href="https://bugs.llvm.org/show_bug.cgi?id=38103">38103</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Bad for loop copy optimization when using -fno-builtin-memcpy -fno-builtin-memmove
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>6.0
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</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>C++
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>gchatelet@google.com
</td>
</tr>
<tr>
<th>CC</th>
<td>dgregor@apple.com, llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>Straightforward copy using a for loop with known size at compile time leads to
very poor assembly.
<span class="quote">> #include <cstddef>
>
> template <size_t kBlockSize>
> void Copy(char* __restrict dst, const char* __restrict src) {
> for (size_t i = 0; i < kBlockSize; ++i) dst[i] = src[i];
> }
>
> template void Copy<15>(char* __restrict dst, const char* __restrict src);</span >
<a href="https://godbolt.org/g/YFq3o6">https://godbolt.org/g/YFq3o6</a>
This can be mitigated by the introduction of a temporary buffer like so:
<span class="quote">> template <size_t kBlockSize>
> void Copy(char* __restrict dst, const char* __restrict src) {
> char tmp[kBlockSize];
> for (size_t i = 0; i < kBlockSize; ++i) tmp[i] = src[i];
> for (size_t i = 0; i < kBlockSize; ++i) dst[i] = tmp[i];
> }</span >
<a href="https://godbolt.org/g/48Dghk">https://godbolt.org/g/48Dghk</a>
It works up to 25B and produce bad code from 26B onwards.
Check the resulting code for 32B for instance: <a href="https://godbolt.org/g/jZwcrv">https://godbolt.org/g/jZwcrv</a></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>