public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cvs-commit at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/114061] GCC fails vectorization when using __builtin_prefetch
Date: Wed, 12 Jun 2024 13:39:26 +0000	[thread overview]
Message-ID: <bug-114061-4-6MU52u57EP@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-114061-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #6 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Victor Do Nascimento
<victorldn@gcc.gnu.org>:

https://gcc.gnu.org/g:adcc815a01ae009d2768b6afb546e357bd37bbd2

commit r15-1211-gadcc815a01ae009d2768b6afb546e357bd37bbd2
Author: Victor Do Nascimento <victor.donascimento@arm.com>
Date:   Wed May 22 12:14:11 2024 +0100

    middle-end: Drop __builtin_prefetch calls in autovectorization [PR114061]

    At present the autovectorizer fails to vectorize simple loops
    involving calls to `__builtin_prefetch'.  A simple example of such
    loop is given below:

    void foo(double * restrict a, double * restrict b, int n){
      int i;
      for(i=0; i<n; ++i){
        a[i] = a[i] + b[i];
        __builtin_prefetch(&(b[i+8]));
      }
    }

    The failure stems from two issues:

    1. Given that it is typically not possible to fully reason about a
       function call due to the possibility of side effects, the
       autovectorizer does not attempt to vectorize loops which make such
       calls.

       Given the memory reference passed to `__builtin_prefetch', in the
       absence of assurances about its effect on the passed memory
       location the compiler deems the function unsafe to vectorize,
       marking it as clobbering memory in `vect_find_stmt_data_reference'.
       This leads to the failure in autovectorization.

    2. Notwithstanding the above issue, though the prefetch statement
       would be classed as `vect_unused_in_scope', the loop invariant that
       is used in the address of the prefetch is the scalar loop's and not
       the vector loop's IV. That is, it still uses `i' and not `vec_iv'
       because the instruction wasn't vectorized, causing DCE to think the
       value is live, such that we now have both the vector and scalar loop
       invariant actively used in the loop.

    This patch addresses both of these:

    1. About the issue regarding the memory clobber, data prefetch does
       not generate faults if its address argument is invalid and does not
       write to memory.  Therefore, it does not alter the internal state
       of the program or its control flow under any circumstance.  As
       such, it is reasonable that the function be marked as not affecting
       memory contents.

       To achieve this, we add the necessary logic to
       `get_references_in_stmt' to ensure that builtin functions are given
       given the same treatment as internal functions.  If the gimple call
       is to a builtin function and its function code is
       `BUILT_IN_PREFETCH', we mark `clobbers_memory' as false.

    2. Finding precedence in the way clobber statements are handled,
       whereby the vectorizer drops these from both the scalar and
       vectorized versions of a given loop, we choose to drop prefetch
       hints in a similar fashion.  This seems appropriate given how
       software prefetch hints are typically ignored by processors across
       architectures, as they seldom lead to performance gain over their
       hardware counterparts.

    gcc/ChangeLog:

            PR tree-optimization/114061
            * tree-data-ref.cc (get_references_in_stmt): set
            `clobbers_memory' to false for __builtin_prefetch.
            * tree-vect-loop.cc (vect_transform_loop): Drop all
            __builtin_prefetch calls from loops.

    gcc/testsuite/ChangeLog:

            * gcc.dg/vect/vect-prefetch-drop.c: New test.
            * gcc.target/aarch64/vect-prefetch-drop.c: Likewise.

  parent reply	other threads:[~2024-06-12 13:39 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-22 19:04 [Bug tree-optimization/114061] New: " tnfchris at gcc dot gnu.org
2024-02-22 19:07 ` [Bug tree-optimization/114061] " pinskia at gcc dot gnu.org
2024-02-22 19:09 ` tnfchris at gcc dot gnu.org
2024-02-22 19:11 ` pinskia at gcc dot gnu.org
2024-02-22 19:21 ` tnfchris at gcc dot gnu.org
2024-02-23  7:01 ` rguenth at gcc dot gnu.org
2024-04-08 14:01 ` victorldn at gcc dot gnu.org
2024-06-12 13:39 ` cvs-commit at gcc dot gnu.org [this message]
2024-06-12 17:15 ` pinskia at gcc dot gnu.org

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=bug-114061-4-6MU52u57EP@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).