Bug 80439 - __attribute__((target("xxx"))) not applied to lambdas
Summary: __attribute__((target("xxx"))) not applied to lambdas
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: c++-lambda, opt-attribute, wrong-code
Depends on:
Blocks: lambdas
  Show dependency treegraph
 
Reported: 2017-04-15 21:26 UTC by Thiago Macieira
Modified: 2022-03-11 00:32 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-10-05 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thiago Macieira 2017-04-15 21:26:17 UTC
Testcase (see also https://godbolt.org/g/H2xjNc for GCC and Clang build):

========
#include <immintrin.h>
#include <stdint.h>

__attribute__((target("sse4.2")))
unsigned aeshash(const uint8_t *p, size_t len, unsigned seed)
{
    const auto l = [](unsigned data) {
        __m128i m = _mm_insert_epi32(_mm_setzero_si128(), data, 1);
        return _mm_extract_epi32(m, 1);
    };
    return l(seed);
}========

In the testcase above, if the source is compiled with base options for x86 (either 32- or 64-bit mode), GCC fails to compile with error:

/usr/lib/gcc/x86_64-linux-gnu/6.3.0/include/smmintrin.h:447:1: error: inlining failed in call to always_inline 'int _mm_extract_epi32(__m128i, int)': target specific option mismatch
 _mm_extract_epi32 (__m128i __X, const int __N)
 ^~~~~~~~~~~~~~~~~
<source>:9:38: note: called from here
         return _mm_extract_epi32(m, 1);
                                      ^

Clang compiles the above just fine.

The compilation works if I add __attribute__((target("sse4.2"))) to the lambda.
Comment 1 Richard Biener 2017-04-20 10:24:25 UTC
Hmm, I think it would make sense if lambdas (and nested functions) "inherit"
both target and optimize attributes from their parents.
Comment 2 Paolo Carlini 2017-10-05 09:37:01 UTC
Richard, can you actually reproduce this? I can't with the released 7.1.0 (given the error message I doubt submitter was actually using a 7 pre). Neither I can reproduce it with 6.3.0, anyway. I'm passing, as requested, -m32 -msse4.1 (or -m32 -msse4.2)
Comment 3 Paolo Carlini 2017-10-05 09:40:32 UTC
Ah, ok, now I get it, the issue is that it should be accepted without, say, -m32 -msse4.1, on the command line.
Comment 4 Richard Biener 2017-10-05 09:45:38 UTC
Confirmed.

> g++-7 t.C -S
In file included from /usr/lib64/gcc/x86_64-suse-linux/7/include/immintrin.h:37:0,
                 from t.C:1:
t.C: In lambda function:
t.C:9:19: error: ‘__builtin_ia32_vec_set_v4si’ needs isa option -m32 -msse4.1
       __m128i m = _mm_insert_epi32(_mm_setzero_si128(), data, 1);
                   ^

> g++-7 t.C -S -O
In file included from /usr/lib64/gcc/x86_64-suse-linux/7/include/immintrin.h:37:0,
                 from t.C:1:
/usr/lib64/gcc/x86_64-suse-linux/7/include/smmintrin.h: In lambda function:
/usr/lib64/gcc/x86_64-suse-linux/7/include/smmintrin.h:447:1: error: inlining failed in call to always_inline ‘int _mm_extract_epi32(__m128i, int)’: target specific option mismatch
 _mm_extract_epi32 (__m128i __X, const int __N)
 ^~~~~~~~~~~~~~~~~
t.C:10:36: note: called from here
       return _mm_extract_epi32(m, 1);
                                    ^
In file included from /usr/lib64/gcc/x86_64-suse-linux/7/include/immintrin.h:37:0,
                 from t.C:1:
/usr/lib64/gcc/x86_64-suse-linux/7/include/smmintrin.h:406:1: error: inlining failed in call to always_inline ‘__m128i _mm_insert_epi32(__m128i, int, int)’: target specific option mismatch
...


This is with 7.2.1 (rev. 253227) but 7.1.0 and 7.2.0 are affected the same,
so is 6.4.0.  GCC 5 rejects the testcase on unknown C++ grounds.

The key is to _not_ pass -msse4.2 -- it shouldn't be necessary given the
target attribute and as the following testcase dropping the lambda shows:

#include <immintrin.h>
#include <stdint.h>

__attribute__((target("sse4.2")))
unsigned aeshash(const uint8_t *p, size_t len, unsigned seed)
{
    __m128i m = _mm_insert_epi32(_mm_setzero_si128(), seed, 1);
    return _mm_extract_epi32(m, 1);
}