public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/111791] New: RISC-V: Strange loop vectorizaion on popcount function
@ 2023-10-12 19:56 kito at gcc dot gnu.org
  2023-10-12 20:49 ` [Bug tree-optimization/111791] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: kito at gcc dot gnu.org @ 2023-10-12 19:56 UTC (permalink / raw)
  To: gcc-bugs

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

```

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-10-19  2:26 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-12 19:56 [Bug tree-optimization/111791] New: RISC-V: Strange loop vectorizaion on popcount function kito at gcc dot gnu.org
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

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