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

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

--- Comment #4 from Robin Dapp <rdapp at gcc dot gnu.org> ---
This is a scalar popcount and as Kito already noted we will just emit

  cpop a0, a0

once the zbb extension is present.

As to the question what is actually being vectorized here, I'm not so sure :D
It looks like we're generating a vectorized scalar popcount by something like a
reduction?  But we already did the call to __popcountdi2?

Analyzing loop at pr111791.c:8
pr111791.c:8:25: note:  === analyze_loop_nest ===
pr111791.c:8:25: note:   === vect_analyze_loop_form ===
pr111791.c:8:25: note:    === get_loop_niters ===
Matching expression match.pd:1919, generic-match-8.cc:27
Applying pattern match.pd:1975, generic-match-2.cc:4670
Matching expression match.pd:2707, generic-match-4.cc:36
Matching expression match.pd:2710, generic-match-3.cc:53
Matching expression match.pd:2717, generic-match-2.cc:23
Matching expression match.pd:2707, generic-match-4.cc:36
Matching expression match.pd:2710, generic-match-3.cc:53
Matching expression match.pd:2717, generic-match-2.cc:23
Matching expression match.pd:2707, generic-match-4.cc:36
Matching expression match.pd:2710, generic-match-3.cc:53
Matching expression match.pd:2717, generic-match-2.cc:23
Matching expression match.pd:148, generic-match-10.cc:27
Matching expression match.pd:148, generic-match-10.cc:27
Applying pattern match.pd:4519, generic-match-4.cc:2923
Applying pattern match.pd:201, generic-match-4.cc:3103
Applying pattern match.pd:3393, generic-match-2.cc:182
pr111791.c:8:25: note:   Symbolic number of iterations is (unsigned intD.4)
__builtin_popcountlD.1952 (value_4(D))

Ah, interesting: ranger(?) recognizes that the loop runs "popcount" iterations.
Shouldn't that still be 64?  Well, it probably knows better :)

Regardless, we use this symbolic value as number of iterations:
  _5 = __builtin_popcountlD.1952 (value_4(D));
  niters.4_9 = (unsigned intD.4) _5;
  _2 = __builtin_popcountlD.1952 (value_4(D));
  bnd.5_3 = (unsigned intD.4) _2;
  _23 = (unsigned long) bnd.5_3;

Then, it gets funnier:

  # nbits_11 = PHI <nbits_7(6), 0(5)>
  # vect_vec_iv_.6_15 = PHI <_16(6), { 0, 1, 2, ... }(5)>
  # ivtmp_24 = PHI <ivtmp_25(6), _23(5)>
  _26 = .SELECT_VL (ivtmp_24, POLY_INT_CST [4, 4]);
  _16 = vect_vec_iv_.6_15 + { POLY_INT_CST [4, 4], ... };
  vect_nbits_7.7_18 = vect_vec_iv_.6_15 + { 1, ... };
  # RANGE [irange] int [1, 65]
  nbits_7 = nbits_11 + 1;
  # RANGE [irange] long unsigned int [0, 18446744073709551614]
  _1 = value_10 + 18446744073709551615;
  # RANGE [irange] long unsigned int [0, 18446744073709551614]
  value_8 = _1 & value_10;
  ivtmp_25 = ivtmp_24 - _26;

i.e. we have a vector IV that we add to the vectorized nbits.  Finally we
extract the niter-th (=popcount) element from that vector only to get -
popcount :)

Still not sure why that happens but a vector-mode popcount expander doesn't
help here as everything is scalar.  Maybe the explanation is simple in that we
would vectorize such a loop anyway and here it just looks particularly bad
because we already know the result via ranger?

  parent reply	other threads:[~2023-10-18 19:53 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-12 19:56 [Bug tree-optimization/111791] New: " 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 [this message]
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-kHPoJp4TzE@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).