public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <rguenther@suse.de>
To: Jakub Jelinek <jakub@redhat.com>
Cc: Cong Hou <congh@google.com>,	GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] Fixing PR59006 and PR58921 by delaying loop invariant hoisting in vectorizer.
Date: Wed, 27 Nov 2013 14:26:00 -0000	[thread overview]
Message-ID: <alpine.LNX.2.00.1311271247470.8615@zhemvz.fhfr.qr> (raw)
In-Reply-To: <20131127113730.GZ892@tucnak.redhat.com>

On Wed, 27 Nov 2013, Jakub Jelinek wrote:

> On Wed, Nov 27, 2013 at 10:53:56AM +0100, Richard Biener wrote:
> > Hmm.  I'm still thinking that we should handle this during the regular
> > transform step.
> 
> I wonder if it can't be done instead just in vectorizable_load,
> if LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo) and the load is
> invariant, just emit the (broadcasted) load not inside of the loop, but on
> the loop preheader edge.

It is safe even for !LOOP_REQUIRES_VERSIONING_FOR_ALIAS.  It's just
a missed optimization I even noted when originally implementing
support for invariant loads ...

> > *** gcc/tree-vect-data-refs.c	(revision 205435)
> > --- gcc/tree-vect-data-refs.c	(working copy)
> > *************** again:
> > *** 3668,3673 ****
> > --- 3668,3682 ----
> >   	    }
> >   	  STMT_VINFO_STRIDE_LOAD_P (stmt_info) = true;
> >   	}
> > +       else if (loop_vinfo
> > + 	       && integer_zerop (DR_STEP (dr)))
> > + 	{
> > + 	  /* All loads from a non-varying address will be disambiguated
> > + 	     by data-ref analysis or via a runtime alias check and thus
> > + 	     they will become invariant.  Force them to be vectorized
> > + 	     as external.  */
> > + 	  STMT_VINFO_DEF_TYPE (stmt_info) = vect_external_def;
> > + 	}
> 
> I think this is unsafe for simd loops.
> I'd say:
> int a[1024], b[1024];
> 
> int foo (void)
> {
>   int i;
>   #pragma omp simd safelen(8)
>   for (i = 0; i < 1024; i++)
>     {
>       a[i] = i;
>       b[i] = a[0];
>     }
> }
> 
> is valid testcase, the loop behaves the same if you execute it
> sequentially, or vectorize using SIMD (hardware or emulated) instructions
> with vectorization factor of 2, 4 or 8, as long as you do all memory
> operations (either using scalar insns or simd instructions) in the order
> they were written, which I believe the vectorizer right now handles
> correctly, but the hoisting this patch wants to perform is not fine,
> unless data ref analysis would prove that it can't alias.  For non-simd
> loops we of course perform that data ref analysis and either version for
> alias, or prove that the drs can't alias, but for simd loops we take as
> given that the loop is safe to be vectorized.  It is, but not for hoisting.

Ick.  I hate this behind-the-back stuff - so safelen doesn't mean
that a[i] and a[0] do not alias.  Note that this will break with
SLP stuff at least as that will re-order reads/writes.  Not sure
how safelen applies to SLP though.  That is

    a[i] = i;
    b[i] = a[0];
    a[i+1] = i+1;
    b[i+1] = a[1];

will eventually end up re-ordering reads/writes in non-obvious
ways.

> So, the above say with emulated SIMD is safe to be executed as:
>   for (i = 0; i < 1024; i += 8)
>     {
>       int tmp;
>       for (tmp = i; tmp < i + 8; tmp++)
> 	a[tmp] = tmp;
>       for (tmp = i; tmp < i + 8; tmp++)
> 	b[tmp] = a[0];
>     }
> but not as:
>   int tempa[8], tmp;
>   /* Hoisted HW or emulated load + splat.  */
>   for (tmp = 0; tmp < 8; tmp++)
>     tempa[tmp] = a[0];
>   for (i = 0; i < 1024; i += 8)
>     {
>       for (tmp = i; tmp < i + 8; tmp++)
> 	a[tmp] = tmp;
>       for (tmp = i; tmp < i + 8; tmp++)
> 	b[tmp] = tempa[tmp];
>     }
> 
> 	Jakub
> 
> 

-- 
Richard Biener <rguenther@suse.de>
SUSE / SUSE Labs
SUSE LINUX Products GmbH - Nuernberg - AG Nuernberg - HRB 16746
GF: Jeff Hawn, Jennifer Guild, Felix Imend"orffer

  reply	other threads:[~2013-11-27 11:54 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-23 14:13 Cong Hou
2013-11-27 11:37 ` Richard Biener
2013-11-27 12:45   ` Jakub Jelinek
2013-11-27 14:26     ` Richard Biener [this message]
2013-11-27 15:35       ` Jakub Jelinek
2013-11-27 16:08         ` Richard Biener
2014-01-13 13:38     ` Richard Biener
2014-01-13 13:45       ` Jakub Jelinek
2014-01-14  3:42       ` Cong Hou
2014-01-14  9:01         ` Richard Biener
2014-01-14 10:51           ` Richard Biener
2014-01-14 13:17           ` Jakub Jelinek
2014-01-14 13:37             ` Richard Biener
2014-01-16 14:19       ` H.J. Lu
2013-11-28  4:39   ` Cong Hou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LNX.2.00.1311271247470.8615@zhemvz.fhfr.qr \
    --to=rguenther@suse.de \
    --cc=congh@google.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).