public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "juzhe.zhong at rivai dot ai" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/113134] New: Middle end early break vectorization: Fail to vectorize a simple early break code
Date: Mon, 25 Dec 2023 11:29:28 +0000	[thread overview]
Message-ID: <bug-113134-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 113134
           Summary: Middle end early break vectorization: Fail to
                    vectorize a simple early break code
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: juzhe.zhong at rivai dot ai
  Target Milestone: ---

Hi, as reference shows:

https://compiler-explorer.com/z/zMzba7WT1

void add(int N, int *__restrict a, int *__restrict b, int *__restrict c) {
  for (int i = 0; i < N; i++) {
    c[i] = a[i] + b[i];
    if (i > 1000) {
        break;
    }
  }
}

GCC failed to vectorize it:

add:
        cmp     w0, 0
        ble     .L1
        sbfiz   x6, x0, 2, 32
        mov     x4, 0
.L3:
        ldr     w0, [x1, x4]
        ldr     w5, [x2, x4]
        add     w0, w0, w5
        str     w0, [x3, x4]
        cmp     x4, 4004
        beq     .L1
        add     x4, x4, 4
        cmp     x6, x4
        bne     .L3
.L1:
        ret

But clang is able to vectorize it:

add:                                    // @add
        cmp     w0, #1
        b.lt    .LBB0_8
        mov     w8, w0
        mov     w9, #1001                       // =0x3e9
        sub     x8, x8, #1
        cmp     x8, #1001
        csel    x9, x8, x9, lo
        add     x10, x9, #1
        cnth    x9
        cmp     x10, x9
        b.hs    .LBB0_3
        mov     x9, xzr
        b       .LBB0_6
.LBB0_3:
        ptrue   p0.s
        neg     x9, x9
        mov     x11, xzr
        and     x9, x10, x9
        addvl   x12, x1, #1
        addvl   x13, x2, #1
        addvl   x14, x3, #1
.LBB0_4:                                // =>This Inner Loop Header: Depth=1
        ld1w    { z0.s }, p0/z, [x1, x11, lsl #2]
        ld1w    { z1.s }, p0/z, [x2, x11, lsl #2]
        ld1w    { z2.s }, p0/z, [x12, x11, lsl #2]
        ld1w    { z3.s }, p0/z, [x13, x11, lsl #2]
        add     z0.s, z1.s, z0.s
        add     z1.s, z3.s, z2.s
        st1w    { z0.s }, p0, [x3, x11, lsl #2]
        st1w    { z1.s }, p0, [x14, x11, lsl #2]
        inch    x11
        cmp     x9, x11
        b.ne    .LBB0_4
        cmp     x10, x9
        b.eq    .LBB0_8
.LBB0_6:                                // =>This Inner Loop Header: Depth=1
        lsl     x10, x9, #2
        cmp     x9, #1001
        ldr     w11, [x1, x10]
        ldr     w12, [x2, x10]
        add     w11, w12, w11
        str     w11, [x3, x10]
        b.eq    .LBB0_8
        cmp     x8, x9
        add     x9, x9, #1
        b.ne    .LBB0_6
.LBB0_8:
        ret

             reply	other threads:[~2023-12-25 11:29 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-25 11:29 juzhe.zhong at rivai dot ai [this message]
2023-12-25 12:35 ` [Bug c/113134] " tnfchris at gcc dot gnu.org
2023-12-27 15:21 ` [Bug c/113134] gcc does not version loops with side-effect early breaks tnfchris at gcc dot gnu.org
2023-12-28  1:21 ` [Bug c/113134] gcc does not version loops with early break conditions that don't have side-effects juzhe.zhong at rivai dot ai
2023-12-28  3:48 ` tnfchris at gcc dot gnu.org
2023-12-28  3:55 ` juzhe.zhong at rivai dot ai
2023-12-28  4:02 ` tnfchris at gcc dot gnu.org
2023-12-28  4:05 ` tnfchris at gcc dot gnu.org
2023-12-28  4:23 ` juzhe.zhong at rivai dot ai
2023-12-28  4:30 ` tnfchris at gcc dot gnu.org
2023-12-28  4:35 ` juzhe.zhong at rivai dot ai
2023-12-28  4:45 ` tnfchris at gcc dot gnu.org
2023-12-28  4:46 ` juzhe.zhong at rivai dot ai
2023-12-28  4:49 ` tnfchris at gcc dot gnu.org
2023-12-28  4:51 ` juzhe.zhong at rivai dot ai
2023-12-28  4:53 ` tnfchris at gcc dot gnu.org
2023-12-28  5:08 ` tnfchris at gcc dot gnu.org
2023-12-28  9:11 ` juzhe.zhong at rivai dot ai
2023-12-28 21:20 ` [Bug tree-optimization/113134] " pinskia at gcc dot gnu.org
2024-01-08  8:11 ` rguenth at gcc dot gnu.org
2024-01-31 11:48 ` juzhe.zhong at rivai dot ai
2024-01-31 12:04 ` rguenth at gcc dot gnu.org
2024-02-02  3:38 ` juzhe.zhong at rivai dot ai
2024-02-02  8:49 ` juzhe.zhong at rivai dot ai

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-113134-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).