public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/105353] New: __builtin_shufflevector with template parameter fails to compile on GCC 12 but compiles on clang
@ 2022-04-22 21:07 john_platts at hotmail dot com
  2022-04-22 21:21 ` [Bug c++/105353] __builtin_shufflevector with value dependent constant pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: john_platts at hotmail dot com @ 2022-04-22 21:07 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105353
           Summary: __builtin_shufflevector with template parameter fails
                    to compile on GCC 12 but compiles on clang
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: john_platts at hotmail dot com
  Target Milestone: ---

The following code fails to compile with GCC 12 but compiles successfully on
clang (with the -std=c++17 flag):
#include <cstdint>

typedef std::uint8_t Simd128U8VectT __attribute__((__vector_size__(16)));

template<int ShuffleIndex>
static inline Simd128U8VectT ShufFunc(Simd128U8VectT vect) noexcept {
    if constexpr(unsigned(ShuffleIndex) >= 16)
        return Simd128U8VectT { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
    else if constexpr(ShuffleIndex == 0)
        return vect;
    else
        return __builtin_shufflevector(vect, vect, ShuffleIndex, ShuffleIndex +
1,
            ShuffleIndex + 2, ShuffleIndex + 3, ShuffleIndex + 4, ShuffleIndex
+ 5,
            ShuffleIndex + 6, ShuffleIndex + 7, ShuffleIndex + 8, ShuffleIndex
+ 9,
            ShuffleIndex + 10, ShuffleIndex + 11, ShuffleIndex + 12,
ShuffleIndex + 13,
            ShuffleIndex + 14, ShuffleIndex + 15);
}

auto func1(Simd128U8VectT vect) noexcept {
    return ShufFunc<5>(vect);
}

Here is the assembly output when the above code is compiled on clang 14.0.0
with the -O2 -std=c++17 flags:
func1(unsigned char __vector(16)):                         # @func1(unsigned
char __vector(16))
        movdqa  xmm1, xmm0
        psrldq  xmm1, 5                         # xmm1 =
xmm1[5,6,7,8,9,10,11,12,13,14,15],zero,zero,zero,zero,zero
        pslldq  xmm0, 11                        # xmm0 =
zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,zero,xmm0[0,1,2,3,4]
        por     xmm0, xmm1
        ret

The below code compiles successfully with GCC 11 and GCC 12 (which uses
__builtin_shuffle instead of __builtin_shufflevector):
#include <cstdint>

typedef std::uint8_t Simd128U8VectT __attribute__((__vector_size__(16)));

template<int ShuffleIndex>
static inline Simd128U8VectT ShufFunc(Simd128U8VectT vect) noexcept {
    if constexpr(unsigned(ShuffleIndex) >= 16)
        return Simd128U8VectT { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
    else if constexpr(ShuffleIndex == 0)
        return vect;
    else
        return __builtin_shuffle(vect, vect, (Simd128U8VectT){
            ShuffleIndex, ShuffleIndex + 1,
            ShuffleIndex + 2, ShuffleIndex + 3, ShuffleIndex + 4, ShuffleIndex
+ 5,
            ShuffleIndex + 6, ShuffleIndex + 7, ShuffleIndex + 8, ShuffleIndex
+ 9,
            ShuffleIndex + 10, ShuffleIndex + 11, ShuffleIndex + 12,
ShuffleIndex + 13,
            ShuffleIndex + 14, ShuffleIndex + 15 });
}

auto func1(Simd128U8VectT vect) noexcept {
    return ShufFunc<5>(vect);
}

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

* [Bug c++/105353] __builtin_shufflevector with value dependent constant
  2022-04-22 21:07 [Bug c++/105353] New: __builtin_shufflevector with template parameter fails to compile on GCC 12 but compiles on clang john_platts at hotmail dot com
@ 2022-04-22 21:21 ` pinskia at gcc dot gnu.org
  2022-04-22 21:40 ` mpolacek at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-04-22 21:21 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|__builtin_shufflevector     |__builtin_shufflevector
                   |with template parameter     |with value dependent
                   |fails to compile on GCC 12  |constant
                   |but compiles on clang       |
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2022-04-22
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed, it is all value dependent constants really. e.g. even sizeof fails:



#include <cstdint>

typedef std::uint8_t Simd128U8VectT __attribute__((__vector_size__(16)));

#define ShuffleIndex sizeof(T)

template<class T>
static inline Simd128U8VectT ShufFunc(Simd128U8VectT vect) noexcept {
    if constexpr(unsigned(ShuffleIndex) >= 16)
        return Simd128U8VectT { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
    else if constexpr(ShuffleIndex == 0)
        return vect;
    else
        return __builtin_shufflevector(vect, vect, ShuffleIndex, ShuffleIndex +
1,
            ShuffleIndex + 2, ShuffleIndex + 3, ShuffleIndex + 4, ShuffleIndex
+ 5,
            ShuffleIndex + 6, ShuffleIndex + 7, ShuffleIndex + 8, ShuffleIndex
+ 9,
            ShuffleIndex + 10, ShuffleIndex + 11, ShuffleIndex + 12,
ShuffleIndex + 13,
            ShuffleIndex + 14, ShuffleIndex + 15);
}

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

* [Bug c++/105353] __builtin_shufflevector with value dependent constant
  2022-04-22 21:07 [Bug c++/105353] New: __builtin_shufflevector with template parameter fails to compile on GCC 12 but compiles on clang john_platts at hotmail dot com
  2022-04-22 21:21 ` [Bug c++/105353] __builtin_shufflevector with value dependent constant pinskia at gcc dot gnu.org
@ 2022-04-22 21:40 ` mpolacek at gcc dot gnu.org
  2022-04-22 21:50 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-04-22 21:40 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |mpolacek at gcc dot gnu.org
             Status|NEW                         |ASSIGNED
                 CC|                            |mpolacek at gcc dot gnu.org

--- Comment #2 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Right.  I have a fix.

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

* [Bug c++/105353] __builtin_shufflevector with value dependent constant
  2022-04-22 21:07 [Bug c++/105353] New: __builtin_shufflevector with template parameter fails to compile on GCC 12 but compiles on clang john_platts at hotmail dot com
  2022-04-22 21:21 ` [Bug c++/105353] __builtin_shufflevector with value dependent constant pinskia at gcc dot gnu.org
  2022-04-22 21:40 ` mpolacek at gcc dot gnu.org
@ 2022-04-22 21:50 ` jakub at gcc dot gnu.org
  2022-04-22 21:55 ` mpolacek at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-04-22 21:50 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
As c_build_shufflevector requires that the 3rd+ args are all INTEGER_CSTs that
fit into shwi, I think we need to avoid calling that function if any of those
isn't such and processing_template_decl.  Perhaps we can
fold_non_dependent_expr them first.

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

* [Bug c++/105353] __builtin_shufflevector with value dependent constant
  2022-04-22 21:07 [Bug c++/105353] New: __builtin_shufflevector with template parameter fails to compile on GCC 12 but compiles on clang john_platts at hotmail dot com
                   ` (2 preceding siblings ...)
  2022-04-22 21:50 ` jakub at gcc dot gnu.org
@ 2022-04-22 21:55 ` mpolacek at gcc dot gnu.org
  2022-04-22 21:59 ` jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-04-22 21:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
My fix is just

--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -6315,7 +6315,7 @@ build_x_shufflevector (location_t loc, vec<tree, va_gc>
*args,
   if (processing_template_decl)
     {
       for (unsigned i = 0; i < args->length (); ++i)
-   if (type_dependent_expression_p ((*args)[i]))
+   if (instantiation_dependent_expression_p ((*args)[i]))
      {
        tree exp = build_min_nt_call_vec (NULL, args);
        CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;


so what we leave the IFN_SHUFFLEVECTOR to be processed when instantiating. 
fold_non_dependent_expr wouldn't know what to substitute T with yet.

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

* [Bug c++/105353] __builtin_shufflevector with value dependent constant
  2022-04-22 21:07 [Bug c++/105353] New: __builtin_shufflevector with template parameter fails to compile on GCC 12 but compiles on clang john_platts at hotmail dot com
                   ` (3 preceding siblings ...)
  2022-04-22 21:55 ` mpolacek at gcc dot gnu.org
@ 2022-04-22 21:59 ` jakub at gcc dot gnu.org
  2022-04-22 22:04 ` mpolacek at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-04-22 21:59 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Maybe, but for i <= 1 IMHO type_dependent_expression_p is right, that is why we
build_non_dependent_expr, c_build_shufflevector oesn't care about those exact
values.

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

* [Bug c++/105353] __builtin_shufflevector with value dependent constant
  2022-04-22 21:07 [Bug c++/105353] New: __builtin_shufflevector with template parameter fails to compile on GCC 12 but compiles on clang john_platts at hotmail dot com
                   ` (4 preceding siblings ...)
  2022-04-22 21:59 ` jakub at gcc dot gnu.org
@ 2022-04-22 22:04 ` mpolacek at gcc dot gnu.org
  2022-04-25 15:16 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-04-22 22:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Good point, I suppose this is better:

--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -6315,7 +6315,9 @@ build_x_shufflevector (location_t loc, vec<tree, va_gc>
*args,
   if (processing_template_decl)
     {
       for (unsigned i = 0; i < args->length (); ++i)
-   if (type_dependent_expression_p ((*args)[i]))
+   if (i <= 1
+       ? type_dependent_expression_p ((*args)[i])
+       : instantiation_dependent_expression_p ((*args)[i]))
      {
        tree exp = build_min_nt_call_vec (NULL, args);
        CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;

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

* [Bug c++/105353] __builtin_shufflevector with value dependent constant
  2022-04-22 21:07 [Bug c++/105353] New: __builtin_shufflevector with template parameter fails to compile on GCC 12 but compiles on clang john_platts at hotmail dot com
                   ` (5 preceding siblings ...)
  2022-04-22 22:04 ` mpolacek at gcc dot gnu.org
@ 2022-04-25 15:16 ` cvs-commit at gcc dot gnu.org
  2022-04-25 15:16 ` mpolacek at gcc dot gnu.org
  2022-10-31 19:31 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-04-25 15:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:1ba397e9f93d3abc93a6ecbabc3d873489a6fb7f

commit r12-8248-g1ba397e9f93d3abc93a6ecbabc3d873489a6fb7f
Author: Marek Polacek <polacek@redhat.com>
Date:   Fri Apr 22 19:40:27 2022 -0400

    c++: __builtin_shufflevector with value-dep expr [PR105353]

    Here we issue an error from c_build_shufflevector while parsing a template
    because it got a TEMPLATE_PARM_INDEX, but this function expects
INTEGER_CSTs
    (except the first two arguments).  It checks if any of the arguments are
    type-dependent, if so, we leave the processing for later, but it should
    also check value-dependency for the 3rd+ arguments, so as to avoid the
    problem above.

    This is not a regression -- __builtin_shufflevector was introduced in
    GCC 12, but it looks safe enough.

            PR c++/105353

    gcc/cp/ChangeLog:

            * typeck.cc (build_x_shufflevector): Use
            instantiation_dependent_expression_p except for the first two
            arguments.

    gcc/testsuite/ChangeLog:

            * g++.dg/ext/builtin-shufflevector-3.C: New test.

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

* [Bug c++/105353] __builtin_shufflevector with value dependent constant
  2022-04-22 21:07 [Bug c++/105353] New: __builtin_shufflevector with template parameter fails to compile on GCC 12 but compiles on clang john_platts at hotmail dot com
                   ` (6 preceding siblings ...)
  2022-04-25 15:16 ` cvs-commit at gcc dot gnu.org
@ 2022-04-25 15:16 ` mpolacek at gcc dot gnu.org
  2022-10-31 19:31 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-04-25 15:16 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #8 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Fixed.

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

* [Bug c++/105353] __builtin_shufflevector with value dependent constant
  2022-04-22 21:07 [Bug c++/105353] New: __builtin_shufflevector with template parameter fails to compile on GCC 12 but compiles on clang john_platts at hotmail dot com
                   ` (7 preceding siblings ...)
  2022-04-25 15:16 ` mpolacek at gcc dot gnu.org
@ 2022-10-31 19:31 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-10-31 19:31 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |12.0

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

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

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-22 21:07 [Bug c++/105353] New: __builtin_shufflevector with template parameter fails to compile on GCC 12 but compiles on clang john_platts at hotmail dot com
2022-04-22 21:21 ` [Bug c++/105353] __builtin_shufflevector with value dependent constant pinskia at gcc dot gnu.org
2022-04-22 21:40 ` mpolacek at gcc dot gnu.org
2022-04-22 21:50 ` jakub at gcc dot gnu.org
2022-04-22 21:55 ` mpolacek at gcc dot gnu.org
2022-04-22 21:59 ` jakub at gcc dot gnu.org
2022-04-22 22:04 ` mpolacek at gcc dot gnu.org
2022-04-25 15:16 ` cvs-commit at gcc dot gnu.org
2022-04-25 15:16 ` mpolacek at gcc dot gnu.org
2022-10-31 19:31 ` pinskia at gcc dot gnu.org

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).