最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - How to fix a warning (ignoring attributes) with a `vector` of `__m256` - Stack Overflow

programmeradmin15浏览0评论

I am using GCC 14.1 with C++17, and want to allocate 32 bytes-aligned memory always.

#include <immintrin.h>
#include <vector>

int main() {
    std::vector<__m256> d;
}

g++ -std=c++17 -mavx2 test.cpp test.cpp: In function ‘int main()’: test.cpp:5:23: warning: ignoring attributes on template argument ‘__m256’ [-Wignored-attributes] 5 | std::vector<__m256> d;

Why do we see this warning? Thanks.

My understanding is that it should be legal from C++17.

I am using GCC 14.1 with C++17, and want to allocate 32 bytes-aligned memory always.

#include <immintrin.h>
#include <vector>

int main() {
    std::vector<__m256> d;
}

g++ -std=c++17 -mavx2 test.cpp test.cpp: In function ‘int main()’: test.cpp:5:23: warning: ignoring attributes on template argument ‘__m256’ [-Wignored-attributes] 5 | std::vector<__m256> d;

Why do we see this warning? Thanks.

My understanding is that it should be legal from C++17.

Share Improve this question edited Mar 27 at 13:29 Peter Cordes 367k49 gold badges717 silver badges980 bronze badges asked Mar 27 at 13:24 JS0JS0 674 bronze badges 7
  • 1 Relevant (possible dupe): Implication of GCC warning: ignoring attributes on template argument (-Wignored-attributes). – wohlstad Commented Mar 27 at 13:32
  • Just for the record, GCC's headers declares __m256d as typdef double __m256d __attribute__((vector_size(32),may_alias)). This implies alignof(__m256d) == 32, and yes C++17 supports over-aligned types in std::vector. This should work; I'm not sure why there's a warning. godbolt./z/7dhEfbfqa reproduces it with GCC14.2, but not clang20 with libstdc++ (the same C++ library Godbolt's Linux GCC install uses) or with libc++ (the LLVM project's rewrite of the C++ standard library). – Peter Cordes Commented Mar 27 at 13:32
  • No warning with GCC5.5, it seems to be new with GCC6. godbolt./z/881PbW1zE – Peter Cordes Commented Mar 27 at 13:36
  • 1 @PeterCordes sure, initially I wasn't sure it's a good duplicate. – wohlstad Commented Mar 27 at 16:11
  • 1 @wohlstad: yeah, it was an exact duplicate question, but the answers there weren't good so closing it without at least posting a new answer there wouldn't have been a very good way to handle it. Thanks for finding the old question so it could get linked to this. – Peter Cordes Commented Mar 27 at 17:27
 |  Show 2 more comments

1 Answer 1

Reset to default 5

The warning is actually not about std::vector not being able to work with over-aligned types (prior to C++17), but that the __attribute__ of the type is ignored, thus making, e.g. __m256 indistinguishable from __m256_u.

One way to show the meaningfulness of the warning, is if you also have std::vector<__m256_u> (__m256_u is defined by gcc/clang as typedef float __m256_u __attribute__ ((__vector_size__ (32), __may_alias__, __aligned__ (1)));, i.e., the same as __m256 except possibly being unaligned). From gcc's (and also clang's) point of view the attributes are ignored when passed as template arguments, thus std::vector<__m256_u> and std::vector<__m256> have the same type.

Example where this can cause unintended behavior:

void foo(std::vector<__m256>&);
void bar(std::vector<__m256_u>&);

int main() {
    std::vector<__m256> d;
    std::vector<__m256_u> e;
    foo(d);
    foo(e); // should not work, but does because attributes are ignored
    bar(d); // should not work, but does because attributes are ignored
    bar(e);
}

One possible workaround is to wrap __m256 into a struct, e.g. like so:

struct vec8f {
    __m256 data;
    vec8f(__m256 x) : data(x) {}
    operator __m256() const {return data;}
};

Godbolt-Demo: https://godbolt./z/zxsE5bseq

Of course, if you assume that the warning won't affect your code, you could just mask it by compiling with -Wno-ignored-attributes.

发布评论

评论列表(0)

  1. 暂无评论