public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/110452] New: Bad vectorization of invariant masks
@ 2023-06-28 11:11 rguenth at gcc dot gnu.org
  2023-06-28 11:12 ` [Bug tree-optimization/110452] " rguenth at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-06-28 11:11 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110452
           Summary: Bad vectorization of invariant masks
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rguenth at gcc dot gnu.org
  Target Milestone: ---

When we have loop like

double a[1024], b[1024], c[1024];

void foo (int flag, int n)
{
  _Bool x = flag == 3;
  for (int i = 0; i < n; ++i)
    a[i] = (x ? b[i] : c[i]) * 42.;
}

and build it with -O2 -ftree-vectorize -march=znver4 (to avoid unswitching)
we get

  _55 = _2 ? -1 : 0;
  vect_cst__56 = {_55, _55, _55, _55, _55, _55, _55, _55};

  <bb 3> [local count: 567644343]:
  # i_14 = PHI <i_11(9), 0(21)>
  # vectp_b.10_49 = PHI <vectp_b.10_50(9), &b(21)>
  # vectp_c.13_52 = PHI <vectp_c.13_53(9), &c(21)>
  # vectp_a.18_62 = PHI <vectp_a.18_63(9), &a(21)>
  # ivtmp_65 = PHI <ivtmp_66(9), 0(21)>
  vect_iftmp.12_51 = MEM <vector(8) double> [(double *)vectp_b.10_49];
  iftmp.0_9 = b[i_14];
  vect_iftmp.15_54 = MEM <vector(8) double> [(double *)vectp_c.13_52];
  iftmp.0_8 = c[i_14];
  vect_patt_13.16_59 = VEC_COND_EXPR <vect_cst__56, vect_iftmp.12_51,
vect_iftmp.15_54>;
  iftmp.0_3 = _2 ? iftmp.0_9 : iftmp.0_8;

so the invariant but not constant condition _2 on the COND_EXPR is vectorized
as

  _55 = _2 ? -1 : 0;
  vect_cst__56 = {_55, _55, _55, _55, _55, _55, _55, _55};

unfortunately that leads to very bad generated code

        cmpl    $3, %edi
        sete    %cl
        movl    %ecx, %esi
        leal    (%rsi,%rsi), %eax
        leal    0(,%rsi,4), %r9d
        leal    0(,%rsi,8), %r8d
        orl     %esi, %eax
        orl     %r9d, %eax
        movl    %ecx, %r9d
        orl     %r8d, %eax
        movl    %ecx, %r8d
        sall    $4, %r9d
        sall    $5, %r8d
        sall    $6, %esi
        orl     %r9d, %eax
        orl     %r8d, %eax
        movl    %ecx, %r8d
        orl     %esi, %eax
        sall    $7, %r8d
        orl     %r8d, %eax
        kmovb   %eax, %k1

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

* [Bug tree-optimization/110452] Bad vectorization of invariant masks
  2023-06-28 11:11 [Bug tree-optimization/110452] New: Bad vectorization of invariant masks rguenth at gcc dot gnu.org
@ 2023-06-28 11:12 ` rguenth at gcc dot gnu.org
  2023-06-29  8:34 ` cvs-commit at gcc dot gnu.org
  2023-06-29  8:35 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-06-28 11:12 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |rguenth at gcc dot gnu.org
           Keywords|                            |missed-optimization
   Last reconfirmed|                            |2023-06-28
             Target|                            |x86_64-*-*

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

* [Bug tree-optimization/110452] Bad vectorization of invariant masks
  2023-06-28 11:11 [Bug tree-optimization/110452] New: Bad vectorization of invariant masks rguenth at gcc dot gnu.org
  2023-06-28 11:12 ` [Bug tree-optimization/110452] " rguenth at gcc dot gnu.org
@ 2023-06-29  8:34 ` cvs-commit at gcc dot gnu.org
  2023-06-29  8:35 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-06-29  8:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Richard Biener <rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:6d2eddf456f2d6494cac490c4aa3e7d089926098

commit r14-2183-g6d2eddf456f2d6494cac490c4aa3e7d089926098
Author: Richard Biener <rguenther@suse.de>
Date:   Wed Jun 28 14:05:55 2023 +0200

    middle-end/110452 - bad code generation with AVX512 mask splat

    The following adds an alternate way of expanding a uniform
    mask vector constructor like

      _55 = _2 ? -1 : 0;
      vect_cst__56 = {_55, _55, _55, _55, _55, _55, _55, _55};

    when the mask mode is a scalar int mode like for AVX512 or GCN.
    Instead of piecewise building the result via shifts and ors
    we can take advantage of uniformity and signedness of the
    component and simply sign-extend to the result.

    Instead of

            cmpl    $3, %edi
            sete    %cl
            movl    %ecx, %esi
            leal    (%rsi,%rsi), %eax
            leal    0(,%rsi,4), %r9d
            leal    0(,%rsi,8), %r8d
            orl     %esi, %eax
            orl     %r9d, %eax
            movl    %ecx, %r9d
            orl     %r8d, %eax
            movl    %ecx, %r8d
            sall    $4, %r9d
            sall    $5, %r8d
            sall    $6, %esi
            orl     %r9d, %eax
            orl     %r8d, %eax
            movl    %ecx, %r8d
            orl     %esi, %eax
            sall    $7, %r8d
            orl     %r8d, %eax
            kmovb   %eax, %k1

    we then get

            cmpl    $3, %edi
            sete    %cl
            negl    %ecx
            kmovb   %ecx, %k1

    Code generation for non-uniform masks remains bad, but at least
    I see no easy way out for the most general case here.

            PR middle-end/110452
            * expr.cc (store_constructor): Handle uniform boolean
            vectors with integer mode specially.

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

* [Bug tree-optimization/110452] Bad vectorization of invariant masks
  2023-06-28 11:11 [Bug tree-optimization/110452] New: Bad vectorization of invariant masks rguenth at gcc dot gnu.org
  2023-06-28 11:12 ` [Bug tree-optimization/110452] " rguenth at gcc dot gnu.org
  2023-06-29  8:34 ` cvs-commit at gcc dot gnu.org
@ 2023-06-29  8:35 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-06-29  8:35 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

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

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
Fixed for GCC 14.

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

end of thread, other threads:[~2023-06-29  8:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-28 11:11 [Bug tree-optimization/110452] New: Bad vectorization of invariant masks rguenth at gcc dot gnu.org
2023-06-28 11:12 ` [Bug tree-optimization/110452] " rguenth at gcc dot gnu.org
2023-06-29  8:34 ` cvs-commit at gcc dot gnu.org
2023-06-29  8:35 ` rguenth 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).