public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/105968] New: GCC vectorizes but reports that it did not vectorize
@ 2022-06-14  8:55 steveire at gmail dot com
  2022-06-14  9:02 ` [Bug c++/105968] " crazylht at gmail dot com
  2022-06-14 10:22 ` rguenth at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: steveire at gmail dot com @ 2022-06-14  8:55 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105968
           Summary: GCC vectorizes but reports that it did not vectorize
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: steveire at gmail dot com
  Target Milestone: ---

I'm looking for a way to know if GCC autovectorizes some code.

Starting with this testcase which I picked up somewhere:

```
#define N 10000
#define NTIMES 100000

double a[N] __attribute__ ((aligned (16)));
double b[N] __attribute__ ((aligned (16)));
double c[N] __attribute__ ((aligned (16)));
double r[N] __attribute__ ((aligned (16)));

int muladd (void) {
  int i, times;
  for (times = 0; times < NTIMES; times++) {
#if 1
    // count up
    for (i = 0; i < N; ++i)
        r[i] = (a[i] + b[i]) * c[i];
#else
    // count down (old gcc won't auto-vectorize)
    for (i = N-1; i >= 0; --i)
      r[i] = (a[i] + b[i]) * c[i];
#endif
  }
  return 0;
}
```

the command

```
g++ -O2 -ftree-vectorize -fno-verbose-asm -mavx2 -fopt-info-vec-all -c test.cpp
```

reports 

```
test.cpp:9:5: note: vectorized 1 loops in function.
```

However, with -O3, GCC reports that it did not vectorize:

```
g++ -O3 -ftree-vectorize -fno-verbose-asm -mavx2 -fopt-info-vec-all -c test.cpp
```

output:

```
test.cpp:9:5: note: vectorized 0 loops in function.
```

even though vector instructions are generated.

Demo https://godbolt.org/z/3o41r7jWc

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

* [Bug c++/105968] GCC vectorizes but reports that it did not vectorize
  2022-06-14  8:55 [Bug c++/105968] New: GCC vectorizes but reports that it did not vectorize steveire at gmail dot com
@ 2022-06-14  9:02 ` crazylht at gmail dot com
  2022-06-14 10:22 ` rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: crazylht at gmail dot com @ 2022-06-14  9:02 UTC (permalink / raw)
  To: gcc-bugs

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

Hongtao.liu <crazylht at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |crazylht at gmail dot com

--- Comment #1 from Hongtao.liu <crazylht at gmail dot com> ---

> even though vector instructions are generated.
> 

No it's scalar instructions, but the issue here is why vectorizer is ok for -O2
-O2 -ftree-vectorize -mavx2 but not for -O3 -ftree-vectorize -mavx2


muladd():
        xor     eax, eax
.L2:
        vmovsd  xmm0, QWORD PTR a[rax]
        vaddsd  xmm0, xmm0, QWORD PTR b[rax]
        add     rax, 8
        vmulsd  xmm0, xmm0, QWORD PTR c[rax-8]
        vmovsd  QWORD PTR r[rax-8], xmm0
        cmp     rax, 80000
        jne     .L2
        xor     eax, eax
        ret

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

* [Bug c++/105968] GCC vectorizes but reports that it did not vectorize
  2022-06-14  8:55 [Bug c++/105968] New: GCC vectorizes but reports that it did not vectorize steveire at gmail dot com
  2022-06-14  9:02 ` [Bug c++/105968] " crazylht at gmail dot com
@ 2022-06-14 10:22 ` rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-06-14 10:22 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |WONTFIX

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
> ./cc1 -quiet t.c -O3 -mavx2 -fopt-info
t.c:11:25: optimized: loops interchanged in loop nest
> ./cc1 -quiet t.c -O2 -mavx2 -fopt-info
t.c:14:19: optimized: loop vectorized using 32 byte vectors

so we interchange the loop to

    for (i = 0; i < N; ++i)
      for (times = 0; times < NTIMES; times++)
        r[i] = (a[i] + b[i]) * c[i];

which is indeed good for memory locality (now, we should then eliminate
the inner loop completely but we have no such facility - only unrolling
and DSE/DCE would do this but nothing on the high-level loop form).

"Benchmark" issue.  The outer loop should have a memory clobber.

Oh, and we should in theory be able to vectorize the outer loop if
N is a multiple of the vector element count.  But:

t.c:11:25: note:   === vect_analyze_data_ref_accesses ===
t.c:11:25: note:   zero step in inner loop of nest
t.c:11:25: missed:   not vectorized: complicated access pattern.
t.c:15:14: missed:   not vectorized: complicated access pattern.
t.c:11:25: missed:  bad data access.

so we don't handle this exact issue (maybe the offending check can
simply be elided - assuming dependence checking handles zero steps
correctly).

Putting

    __asm__ volatile ("" : : : "memory");

at the end of the outer loop vectorizes with -O3 as well (but doesn't
interchange).

Not a bug I think unless you want to make it a bug about not vectorizing
the outer loop after interchange.

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

end of thread, other threads:[~2022-06-14 10:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-14  8:55 [Bug c++/105968] New: GCC vectorizes but reports that it did not vectorize steveire at gmail dot com
2022-06-14  9:02 ` [Bug c++/105968] " crazylht at gmail dot com
2022-06-14 10:22 ` rguenth at gcc dot gnu.org

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