public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/107123] New: Size deduction for vector size in template fails
@ 2022-10-02 10:59 milasudril at gmail dot com
  2022-10-03 18:42 ` [Bug c++/107123] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: milasudril at gmail dot com @ 2022-10-02 10:59 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107123

            Bug ID: 107123
           Summary: Size deduction for vector size in template fails
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: milasudril at gmail dot com
  Target Milestone: ---
              Host: x86-64_linux_gnu
            Target: x86-64_linux_gnu

I tried to write a generic inner product implementation, accepting vectorized
arguments:

```c++
#include <type_traits>
#include <cstddef>

template<class T>
concept arithmetic = std::is_arithmetic_v<T>;

template<arithmetic T, size_t N>
using native_vector [[gnu::vector_size(sizeof(T)*N)]] = T;

template<class T, size_t N>
auto inner_product(native_vector<T, N> a, native_vector<T, N> b)
{
    auto const prod = a * b;
    T ret{};
    for(size_t k = 0; k != N; ++k)
    { ret += prod[k]; }

    return ret;
}

auto test(native_vector<int, 4> a, native_vector<int, 4> b)
{
    return inner_product(a, b);
}
```

Apparently, it is not possible to deduce N here:

```
<source>: In function 'auto test(native_vector<int, 4>, native_vector<int,
4>)':
<source>:23:25: error: no matching function for call to
'inner_product(native_vector<int, 4>&, native_vector<int, 4>&)'
   23 |     return inner_product(a, b);
      |            ~~~~~~~~~~~~~^~~~~~
<source>:11:6: note: candidate: 'template<class T, long unsigned int N> auto
inner_product(native_vector<T, N>, native_vector<T, N>)'
   11 | auto inner_product(native_vector<T, N> a, native_vector<T, N> b)
      |      ^~~~~~~~~~~~~
<source>:11:6: note:   template argument deduction/substitution failed:
<source>:23:25: note:   couldn't deduce template parameter 'N'
   23 |     return inner_product(a, b);
```

I would appreciate if size deduction worked like for std::array:

```c++
#include <array>

template<class T, size_t N>
auto inner_product(std::array<T, N> a, std::array<T, N> b)
{
    T ret{};
    for(size_t k = 0; k != N; ++k)
    { ret += a[k]*b[k]; }

    return ret;
}

auto test(std::array<int, 4> a, std::array<int, 4> b)
{
    return inner_product(a, b);  // N deduced to 4
}
```

The problem is present on gcc 10-trunk.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/107123] Size deduction for vector size in template fails
  2022-10-02 10:59 [Bug c++/107123] New: Size deduction for vector size in template fails milasudril at gmail dot com
@ 2022-10-03 18:42 ` pinskia at gcc dot gnu.org
  2022-10-03 18:42 ` pinskia at gcc dot gnu.org
  2022-10-03 19:10 ` milasudril at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-10-03 18:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107123

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Yes GCC does not do deduction on the vector size attribute currently.
This part of the extension is not documented one way or the other either
because nobody thought of this right now.

One easy way around this is to wrap the vector type in class and use that class
always.

It would be nice to standardized the vector attribute.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/107123] Size deduction for vector size in template fails
  2022-10-02 10:59 [Bug c++/107123] New: Size deduction for vector size in template fails milasudril at gmail dot com
  2022-10-03 18:42 ` [Bug c++/107123] " pinskia at gcc dot gnu.org
@ 2022-10-03 18:42 ` pinskia at gcc dot gnu.org
  2022-10-03 19:10 ` milasudril at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-10-03 18:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107123

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |documentation
           Severity|normal                      |enhancement

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/107123] Size deduction for vector size in template fails
  2022-10-02 10:59 [Bug c++/107123] New: Size deduction for vector size in template fails milasudril at gmail dot com
  2022-10-03 18:42 ` [Bug c++/107123] " pinskia at gcc dot gnu.org
  2022-10-03 18:42 ` pinskia at gcc dot gnu.org
@ 2022-10-03 19:10 ` milasudril at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: milasudril at gmail dot com @ 2022-10-03 19:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107123

--- Comment #2 from milasudril at gmail dot com ---
> It would be nice to standardized the vector attribute.

Good features to have:

constexpr std::size (and also std::ssize)

template<class T>
inline constexpr std::is_simd_type_v = ...;

namespace std {
template<class T>
struct simd_element_type{
  using type = ...;
};

template<class T>
simd_element_type_t = typename simd_element_type<T>::type;
}

I belive the working paper is p2638r0

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2638r0.pdf

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-10-03 19:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-02 10:59 [Bug c++/107123] New: Size deduction for vector size in template fails milasudril at gmail dot com
2022-10-03 18:42 ` [Bug c++/107123] " pinskia at gcc dot gnu.org
2022-10-03 18:42 ` pinskia at gcc dot gnu.org
2022-10-03 19:10 ` milasudril at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).