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/110449] New: Vect: use a small step to calculate the loop induction if the loop is unrolled during loop vectorization
Date: Wed, 28 Jun 2023 09:22:40 +0000	[thread overview]
Message-ID: <bug-110449-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 110449
           Summary: Vect: use a small step to calculate the loop induction
                    if the loop is unrolled during loop vectorization
           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 is inspired by clang. Compile the follwing case with "-mcpu=neoverse-n2
-O3":

void foo(int *arr, int val, int step) {
  for (int i = 0; i < 1024; i++) {
    arr[i] = val;
    val += step;
  }
}

It will be unrolled by 2 during vectorization. GCC generates code:
        fmov    s29, w2                 # step
        shl     v27.2s, v29.2s, 3       # 8*step
        shl     v28.2s, v29.2s, 2       # 4*step
        ...
.L2:
        mov     v30.16b, v31.16b
        add     v31.4s, v31.4s, v27.4s  # += 8*step
        add     v29.4s, v30.4s, v28.4s  # += 4*step
        stp     q30, q29, [x0]
        add     x0, x0, 32
        cmp     x1, x0
        bne     .L2

The v27 (i.e. "8*step") is actually not necessary. We can use v29 + v28 (i.e.
"+ 4*step") and generate simpler code:
        fmov    s29, w2                 # step
        shl     v28.2s, v29.2s, 2       # 4*step
        ...
.L2:
        add     v29.4s, v30.4s, v28.4s  # += 4*step
        stp     q30, q29, [x0]
        add     x0, x0, 32
        add     v30.4s, v29.4s, v28.4s  # += 4*step
        cmp     x1, x0
        bne     .L2

This has two benefits:
(1) Save 1 vector register and one "mov" instructon
(2) For floating point, the result value of small step should be closer to the
original scalar result value than large step. I.e. "A + 4*step + ... + 4*step"
should be closer to "A + step + ... + step" than "A + 8*step + ... 8*step".

Do you think if this is reasonable? 

I have a simple patch to enhance the tree-vect-loop.cc
"vectorizable_induction()" to achieve this. Will send out the patch for code
review later.

             reply	other threads:[~2023-06-28  9:22 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-28  9:22 hliu at amperecomputing dot com [this message]
2023-06-28 20:11 ` [Bug tree-optimization/110449] " rsandifo at gcc dot gnu.org
2023-06-29  3:15 ` hliu at amperecomputing dot com
2023-07-06 16:05 ` 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-110449-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).