public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "kito at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/111791] New: RISC-V: Strange loop vectorizaion on popcount function
Date: Thu, 12 Oct 2023 19:56:25 +0000	[thread overview]
Message-ID: <bug-111791-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 111791
           Summary: RISC-V: Strange loop vectorizaion on popcount function
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kito at gcc dot gnu.org
  Target Milestone: ---

Symptom:

A typical popcount implementation with Brian Kernighan’s algorithm, vectorizer
has recognized that as popcount, but...come with strange vectorization result,
I  know that might because I add -fno-vect-cost-model, but I still don't
understand why it vectorized, so I guess maybe it's something worth to report.

NOTE:
Those bad/strange code gen will gone once scalar popcount instruction
available. 

Case:
```
int popcount(unsigned long value)
  {
    int nbits;
    for (nbits = 0; value != 0; value &= value - 1)
      nbits++;
    return nbits;
  }

```

Command to reproduce:
```
$ riscv64-unknown-linux-gnu-gcc x.c -march=rv64gcv -o - -S -fno-vect-cost-model
-O3
```

Sha1: g:faae30c49560f1481f036061fa2f894b0f7257f8 (some random point of top of
trunk)

Current output:
```
        .globl  popcount
        .type   popcount, @function
popcount:
.LFB0:
        .cfi_startproc
        beq     a0,zero,.L4
        addi    sp,sp,-16
        .cfi_def_cfa_offset 16
        sd      ra,8(sp)
        .cfi_offset 1, -8
        call    __popcountdi2
        csrr    a2,vlenb
        sext.w  a0,a0
        srli    a2,a2,2
        vsetvli a3,zero,e32,m1,ta,ma
        vid.v   v1
.L3:
        vsetvli a5,a0,e8,mf4,ta,ma
        sub     a0,a0,a5
        vsetvli a3,zero,e32,m1,ta,ma
        vmv1r.v v3,v1
        vmv.v.x v2,a2
        vadd.vv v1,v1,v2
        bne     a0,zero,.L3
        ld      ra,8(sp)
        .cfi_restore 1
        addi    a5,a5,-1
        vadd.vi v3,v3,1
        vslidedown.vx   v3,v3,a5
        addi    sp,sp,16
        .cfi_def_cfa_offset 0
        vmv.x.s a0,v3
        jr      ra
.L4:
        li      a0,0
        ret
        .cfi_endproc
.LFE0:
        .size   popcount, .-popcount

```

             reply	other threads:[~2023-10-12 19:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-12 19:56 kito at gcc dot gnu.org [this message]
2023-10-12 20:49 ` [Bug tree-optimization/111791] " pinskia at gcc dot gnu.org
2023-10-13  6:54 ` rguenth at gcc dot gnu.org
2023-10-13 23:13 ` juzhe.zhong at rivai dot ai
2023-10-18 19:53 ` rdapp at gcc dot gnu.org
2023-10-18 23:15 ` vineetg at gcc dot gnu.org
2023-10-18 23:17 ` pinskia at gcc dot gnu.org
2023-10-19  2:26 ` 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-111791-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).