public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* How to represent a fallthrough condtion (with no else) in "Match and Simplify"?
@ 2024-06-12  6:56 Hanke Zhang
  2024-06-12  7:17 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Hanke Zhang @ 2024-06-12  6:56 UTC (permalink / raw)
  To: GCC Development

Hi,

I'm trying to study "Match and Simplify" recently, and I had this sample code:

int main() {
  int n = 1000;
  int *a = malloc (sizeof(int) * n);
  int *b = malloc (sizeof(int) * n);
  int *c = malloc (sizeof(int) * n);
  for (int i = 0; i < n; i++) {
    if (a[i] & b[i]) {
      a[i] ^= c[i];
    }
  }
}

But this code cannot be vectorized very well. I hope it can become like this:

int main() {
  int n = 1000;
  int *a = malloc (sizeof(int) * n);
  int *b = malloc (sizeof(int) * n);
  int *c = malloc (sizeof(int) * n);
  for (int i = 0; i < n; i++) {
    int cond = ((a[i] & b[i]) == 1);
    unsigned int mask = cond ? -1 : 0;
    a[i] ^= (c[i] & mask);
  }
}


This can finally result in concise and efficient vectorized
instructions. But I want to know if this can be achieved through
"Match and Simplify"? Because when I tried to write the pattern, I
found that the condtional statement here seemed not to be matched
well, as there is not an else block.

Or is this not possible with "Match and Simplify"? Is it possible to
implement it in if-conversion?

Thanks
Hanke Zhang

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

* Re: How to represent a fallthrough condtion (with no else) in "Match and Simplify"?
  2024-06-12  6:56 How to represent a fallthrough condtion (with no else) in "Match and Simplify"? Hanke Zhang
@ 2024-06-12  7:17 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2024-06-12  7:17 UTC (permalink / raw)
  To: Hanke Zhang; +Cc: GCC Development

On Wed, Jun 12, 2024 at 8:57 AM Hanke Zhang via Gcc <gcc@gcc.gnu.org> wrote:
>
> Hi,
>
> I'm trying to study "Match and Simplify" recently, and I had this sample code:
>
> int main() {
>   int n = 1000;
>   int *a = malloc (sizeof(int) * n);
>   int *b = malloc (sizeof(int) * n);
>   int *c = malloc (sizeof(int) * n);
>   for (int i = 0; i < n; i++) {
>     if (a[i] & b[i]) {
>       a[i] ^= c[i];
>     }
>   }
> }
>
> But this code cannot be vectorized very well. I hope it can become like this:
>
> int main() {
>   int n = 1000;
>   int *a = malloc (sizeof(int) * n);
>   int *b = malloc (sizeof(int) * n);
>   int *c = malloc (sizeof(int) * n);
>   for (int i = 0; i < n; i++) {
>     int cond = ((a[i] & b[i]) == 1);
>     unsigned int mask = cond ? -1 : 0;
>     a[i] ^= (c[i] & mask);
>   }
> }
>
>
> This can finally result in concise and efficient vectorized
> instructions. But I want to know if this can be achieved through
> "Match and Simplify"? Because when I tried to write the pattern, I
> found that the condtional statement here seemed not to be matched
> well, as there is not an else block.
>
> Or is this not possible with "Match and Simplify"? Is it possible to
> implement it in if-conversion?

It's not possible to perform this transform in match-and-simplify,
if-conversion does this but it considers 'a' to be possibly not
writable and thus the conditional store has to be preserved.  It
should use a .MASK_STORE here and I verified it does with
-mavx2.  Note your testcase is optimized away as it's full
of dead code.

int foo (int n, int *a, int *b, int *c) {
  for (int i = 0; i < n; i++) {
    if (a[i] & b[i]) {
      a[i] ^= c[i];
    }
  }
}

is what I tried.  I suppose other compilers do not consider
read-only memory mappings?  Note there's also store data races
to be considered (but -Ofast might help with that).

In my testcase the c[i] access could also trap, requiring .MASK_LOAD
(I'm quite sure we can't analyze allocated array bounds when the
allocation stmt is seen as in your case).

Richard.

> Thanks
> Hanke Zhang

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

end of thread, other threads:[~2024-06-12  7:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-12  6:56 How to represent a fallthrough condtion (with no else) in "Match and Simplify"? Hanke Zhang
2024-06-12  7:17 ` Richard Biener

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