public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "hliu at amperecomputing dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/112774] New: Vectorize the loop by inferring nonwrapping information from arrays
Date: Thu, 30 Nov 2023 09:01:36 +0000	[thread overview]
Message-ID: <bug-112774-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 112774
           Summary: Vectorize the loop by inferring nonwrapping
                    information from arrays
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hliu at amperecomputing dot com
  Target Milestone: ---

This case extracted from another benchmark and it is simpler than the case in
PR101450, as it has the additional boundary information from the array:

    int A[1024 * 2];

    int foo (unsigned offset, unsigned N) 
    {
      int sum = 0;

      for (unsigned i = 0; i < N; i++)
        sum += A[i + offset];

      return sum;
    }

The Gimple before the vectorization pass is:

    <bb 3> [local count: 955630224]:
    # sum_12 = PHI <sum_9(6), 0(5)>
    # i_14 = PHI <i_10(6), 0(5)>
    _1 = offset_8(D) + i_14;
    _2 = A[_1];
    sum_9 = _2 + sum_12;
    i_10 = i_14 + 1;

GCC failed to vectorize it as it the chrec "{offset_8, +, 1}_1" may
overflow/wrap. I summarized more details in the email:
https://gcc.gnu.org/pipermail/gcc/2023-November/242854.html

Actually, GCC already knows it won't by inferring the range from the array
(in estimate_numbers_of_iterations -> infer_loop_bounds_from_undefined ->
infer_loop_bounds_from_array):

    Induction variable (unsigned int) offset_8(D) + 1 * iteration does not wrap
in statement _2 = A[_1];
     in loop 1.
    Statement _2 = A[_1];
     is executed at most 2047 (bounded by 2047) + 1 times in loop 1.

We can use re-use this information to vectorize this case. I already have a
simple patch to achieve this, and will send it out later (after doing more
tests).

             reply	other threads:[~2023-11-30  9:01 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-30  9:01 hliu at amperecomputing dot com [this message]
2023-11-30 12:27 ` [Bug tree-optimization/112774] " rguenth at gcc dot gnu.org
2023-12-08  3:20 ` cvs-commit at gcc dot gnu.org
2024-01-18  7:52 ` cvs-commit 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-112774-4@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).