From: chenglulu <chenglulu@loongson.cn>
To: Li Wei <liwei@loongson.cn>, gcc-patches@gcc.gnu.org
Cc: xry111@xry111.site, i@xen0n.name, xuchenghua@loongson.cn
Subject: Re: [pushed][PATCH v1 1/2] LoongArch: Accelerate optimization of scalar signed/unsigned popcount.
Date: Sat, 2 Dec 2023 16:53:05 +0800 [thread overview]
Message-ID: <56f2b35d-11d3-46bd-840a-b1d740b667be@loongson.cn> (raw)
In-Reply-To: <20231128073837.2451935-1-liwei@loongson.cn>
Pushed to r14-6072.
在 2023/11/28 下午3:38, Li Wei 写道:
> In LoongArch, the vector popcount has corresponding instructions, while
> the scalar does not. Currently, the scalar popcount is calculated
> through a loop, and the value of a non-power of two needs to be iterated
> several times, so the vector popcount instruction is considered for
> optimization.
>
> gcc/ChangeLog:
>
> * config/loongarch/loongarch.md (v2di): Used to simplify the
> following templates.
> (popcount<mode>2): New.
>
> gcc/testsuite/ChangeLog:
>
> * gcc.target/loongarch/popcnt.c: New test.
> * gcc.target/loongarch/popcount.c: New test.
> ---
> gcc/config/loongarch/loongarch.md | 27 +++++++++++-
> gcc/testsuite/gcc.target/loongarch/popcnt.c | 41 +++++++++++++++++++
> gcc/testsuite/gcc.target/loongarch/popcount.c | 17 ++++++++
> 3 files changed, 83 insertions(+), 2 deletions(-)
> create mode 100644 gcc/testsuite/gcc.target/loongarch/popcnt.c
> create mode 100644 gcc/testsuite/gcc.target/loongarch/popcount.c
>
> diff --git a/gcc/config/loongarch/loongarch.md b/gcc/config/loongarch/loongarch.md
> index cd4ed495697..c440d9c348f 100644
> --- a/gcc/config/loongarch/loongarch.md
> +++ b/gcc/config/loongarch/loongarch.md
> @@ -1515,7 +1515,30 @@ (define_insn "truncdfsf2"
> (set_attr "cnv_mode" "D2S")
> (set_attr "mode" "SF")])
>
> -\f
> +;; In vector registers, popcount can be implemented directly through
> +;; the vector instruction [X]VPCNT. For GP registers, we can implement
> +;; it through the following method. Compared with loop implementation
> +;; of popcount, the following method has better performance.
> +
> +;; This attribute used for get connection of scalar mode and corresponding
> +;; vector mode.
> +(define_mode_attr cntmap [(SI "v4si") (DI "v2di")])
> +
> +(define_expand "popcount<mode>2"
> + [(set (match_operand:GPR 0 "register_operand")
> + (popcount:GPR (match_operand:GPR 1 "register_operand")))]
> + "ISA_HAS_LSX"
> +{
> + rtx in = operands[1];
> + rtx out = operands[0];
> + rtx vreg = <MODE>mode == SImode ? gen_reg_rtx (V4SImode) :
> + gen_reg_rtx (V2DImode);
> + emit_insn (gen_lsx_vinsgr2vr_<size> (vreg, in, vreg, GEN_INT (1)));
> + emit_insn (gen_popcount<cntmap>2 (vreg, vreg));
> + emit_insn (gen_lsx_vpickve2gr_<size> (out, vreg, GEN_INT (0)));
> + DONE;
> +})
> +
> ;;
> ;; ....................
> ;;
> @@ -3882,7 +3905,7 @@ (define_peephole2
> (any_extend:SI (match_dup 3)))])]
> "")
>
> -\f
> +
>
> (define_mode_iterator QHSD [QI HI SI DI])
>
> diff --git a/gcc/testsuite/gcc.target/loongarch/popcnt.c b/gcc/testsuite/gcc.target/loongarch/popcnt.c
> new file mode 100644
> index 00000000000..a10fca42092
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/loongarch/popcnt.c
> @@ -0,0 +1,41 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -mlsx" } */
> +/* { dg-final { scan-assembler-not {popcount} } } */
> +/* { dg-final { scan-assembler-times "vpcnt.d" 2 { target { loongarch64*-*-* } } } } */
> +/* { dg-final { scan-assembler-times "vpcnt.w" 4 { target { loongarch64*-*-* } } } } */
> +
> +int
> +foo (int x)
> +{
> + return __builtin_popcount (x);
> +}
> +
> +long
> +foo1 (long x)
> +{
> + return __builtin_popcountl (x);
> +}
> +
> +long long
> +foo2 (long long x)
> +{
> + return __builtin_popcountll (x);
> +}
> +
> +int
> +foo3 (int *p)
> +{
> + return __builtin_popcount (*p);
> +}
> +
> +unsigned
> +foo4 (int x)
> +{
> + return __builtin_popcount (x);
> +}
> +
> +unsigned long
> +foo5 (int x)
> +{
> + return __builtin_popcount (x);
> +}
> diff --git a/gcc/testsuite/gcc.target/loongarch/popcount.c b/gcc/testsuite/gcc.target/loongarch/popcount.c
> new file mode 100644
> index 00000000000..390ff067617
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/loongarch/popcount.c
> @@ -0,0 +1,17 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -mlsx -fdump-tree-optimized" } */
> +/* { dg-final { scan-tree-dump-times "__builtin_popcount|\\.POPCOUNT" 1 "optimized" } } */
> +
> +int
> +PopCount (long b)
> +{
> + int c = 0;
> +
> + while (b)
> + {
> + b &= b - 1;
> + c++;
> + }
> +
> + return c;
> +}
prev parent reply other threads:[~2023-12-02 8:53 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-28 7:38 [PATCH " Li Wei
2023-12-02 8:53 ` chenglulu [this message]
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=56f2b35d-11d3-46bd-840a-b1d740b667be@loongson.cn \
--to=chenglulu@loongson.cn \
--cc=gcc-patches@gcc.gnu.org \
--cc=i@xen0n.name \
--cc=liwei@loongson.cn \
--cc=xry111@xry111.site \
--cc=xuchenghua@loongson.cn \
/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).