简体   繁体   中英

Why does this template argument deduction fail on GCC but not Clang?

I'm using Clang and GCC trunk with -std=c++20 and the following code compiles fine on Clang but fails on GCC.

#include <cstdint>
#include <climits>
#include <type_traits>
#include <concepts>
#include <immintrin.h>

#define VECTOR_SIZE 32

template <typename T> requires std::is_arithmetic_v<T>
using vec __attribute__((__vector_size__(VECTOR_SIZE))) = T;

template <std::unsigned_integral T>
constexpr vec<T> rotl(vec<T> x, int k)
{
    constexpr auto N = CHAR_BIT * sizeof(T);
    return (x << k) | (x >> (N - k));
}

template <std::unsigned_integral T>
constexpr vec<T> rotr(vec<T> x, int k)
{
    constexpr auto N = CHAR_BIT * sizeof(T);
    return (x >> k) | (x << (N - k));
}

vec<uint32_t> test(vec<uint32_t> x)
{
    return rotr(x, 7);
}

GCC error:

x86-64 gcc (trunk) (Editor #1, Compiler #1) C++#1 with x86-64 gcc (trunk)
<source>: In function 'vec<unsigned int> test(vec<unsigned int>)':
<source>:27:20: error: no matching function for call to 'rotr(vec<unsigned int>&, int)'
27 |         return rotr(x, 7);
    |                ~~~~^~~~~~
<source>:19:22: note: candidate: 'template<class T>  requires  unsigned_integral<T> constexpr vec<T> rotr(vec<T>, int)'
19 |     constexpr vec<T> rotr(vec<T> x, int k)
    |                      ^~~~
<source>:19:22: note:   template argument deduction/substitution failed:
<source>:19:22: note: constraints not satisfied
In file included from <source>:3:
/opt/compiler-explorer/gcc-trunk-20210524/include/c++/12.0.0/concepts: In substitution of 'template<class T>  requires  unsigned_integral<T> constexpr vec<T> rotr(vec<T>, int) [with T = __vector(8) unsigned int]':
<source>:27:20:   required from here
/opt/compiler-explorer/gcc-trunk-20210524/include/c++/12.0.0/concepts:102:13:   required for the satisfaction of 'integral<_Tp>' [with _Tp = __vector(8) unsigned int]
/opt/compiler-explorer/gcc-trunk-20210524/include/c++/12.0.0/concepts:108:13:   required for the satisfaction of 'unsigned_integral<T>' [with T = __vector(8) unsigned int]
/opt/compiler-explorer/gcc-trunk-20210524/include/c++/12.0.0/concepts:102:24: note: the expression 'is_integral_v<_Tp> [with _Tp = __vector(8) unsigned int]' evaluated to 'false'
102 |     concept integral = is_integral_v<_Tp>;
    |                        ^~~~~~~~~~~~~~~~~~
ASM generation compiler returned: 1
<source>: In function 'vec<unsigned int> test(vec<unsigned int>)':
<source>:27:20: error: no matching function for call to 'rotr(vec<unsigned int>&, int)'
27 |         return rotr(x, 7);
    |                ~~~~^~~~~~
<source>:19:22: note: candidate: 'template<class T>  requires  unsigned_integral<T> constexpr vec<T> rotr(vec<T>, int)'
19 |     constexpr vec<T> rotr(vec<T> x, int k)
    |                      ^~~~
<source>:19:22: note:   template argument deduction/substitution failed:
<source>:19:22: note: constraints not satisfied
In file included from <source>:3:
/opt/compiler-explorer/gcc-trunk-20210524/include/c++/12.0.0/concepts: In substitution of 'template<class T>  requires  unsigned_integral<T> constexpr vec<T> rotr(vec<T>, int) [with T = __vector(8) unsigned int]':
<source>:27:20:   required from here
/opt/compiler-explorer/gcc-trunk-20210524/include/c++/12.0.0/concepts:102:13:   required for the satisfaction of 'integral<_Tp>' [with _Tp = __vector(8) unsigned int]
/opt/compiler-explorer/gcc-trunk-20210524/include/c++/12.0.0/concepts:108:13:   required for the satisfaction of 'unsigned_integral<T>' [with T = __vector(8) unsigned int]
/opt/compiler-explorer/gcc-trunk-20210524/include/c++/12.0.0/concepts:102:24: note: the expression 'is_integral_v<_Tp> [with _Tp = __vector(8) unsigned int]' evaluated to 'false'
102 |     concept integral = is_integral_v<_Tp>;
    |                        ^~~~~~~~~~~~~~~~~~
Execution build compiler returned: 1

I suspect it to be a GCC bug in the template-argument-deducation resulting from your alias in combination with the vector intrinsics attribute as:

So the code itself without template-argument-deduction seems to be perfectly valid just somehow GCC ends up deducting T = __vector(8) unsigned int which then fails the std::unsigned_integral<T> concept as is_integral_v<T> evaluates to false .

I guess for a better answer that explains you why this might happen you will need a language-lawyer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM