This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/80561] New: Missed optimization: std::array data is aligned if array is aligned
- From: "jzwinck at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sat, 29 Apr 2017 08:23:27 +0000
- Subject: [Bug c++/80561] New: Missed optimization: std::array data is aligned if array is aligned
- Auto-submitted: auto-generated
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80561
Bug ID: 80561
Summary: Missed optimization: std::array data is aligned if
array is aligned
Product: gcc
Version: 6.3.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: jzwinck at gmail dot com
Target Milestone: ---
In the following code, GCC fails to recognize that the data inside a std::array
has the same alignment guarantees as the array itself. The result is that
using std::array instead of a C-style array carries a significant runtime
penalty, as the alignment is checked unnecessarily and code is generated for
the unaligned case which should never be used. I tested this using:
g++ -std=c++14 -O3 -march=haswell
GCC 6.1, 6.3 and 7 all fail to optimize this. Clang 3.7 through 4.0 optimizes
it as expected.
In the code below, you can swap the comment on the two typedefs to confirm that
GCC properly optimizes the C-style array.
The optimal code is 4 vmovupd, 2 vaddpd, and 1 vzeroupper. The suboptimal code
is 73 instructions including 7 branches.
This was discussed on Stack Overflow:
http://stackoverflow.com/questions/43651923
---
#include <array>
static constexpr size_t my_elements = 8;
typedef std::array<double, my_elements> Vec __attribute__((aligned(32)));
// typedef double Vec[my_elements] __attribute__((aligned(32)));
void func(Vec& __restrict__ v1, const Vec& v2)
{
for (unsigned i = 0; i < my_elements; ++i)
{
v1[i] += v2[i];
}
}