public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/110381] New: Incorrect loop unrolling for structs of floating point types
@ 2023-06-23 18:06 lennox.ho at intel dot com
  2023-06-23 18:08 ` [Bug c++/110381] " lennox.ho at intel dot com
                   ` (20 more replies)
  0 siblings, 21 replies; 22+ messages in thread
From: lennox.ho at intel dot com @ 2023-06-23 18:06 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110381
           Summary: Incorrect loop unrolling for structs of floating point
                    types
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lennox.ho at intel dot com
  Target Milestone: ---

We believe gcc is incorrectly unrolling loops while performing summation of
structs with floating point members:

Here's a minimal example:

```
#include <iostream>

using value_type = double;

struct FOO {
   value_type a = 0;
   value_type b = 0;
   value_type c = 0;
};

value_type sum_8_foos(const FOO* foos) {
    value_type sum = 0;

    for (int i = 0; i < 8; ++i) {
        auto foo = foos[i];

        sum += foo.c;
        sum += foo.b;
        sum += foo.a;
    }

    return sum;
}

int main() {
    FOO foos[8];
    foos[0].b = 5;

    std::cout << sum_8_foos(foos) << '\n';
    return 0;
}
```
With -O1, we get 5.
With -O2, we get 10.

godbolt link: https://godbolt.org/z/7cxeb3Gsv

Slightly reorganising the assembly output for the loop,
```
.L2
        add     rdi, 48

        addsd   sum, QWORD PTR [rdi-48] // c
        addsd   sum, QWORD PTR [rdi-40] // b
        addsd   sum, QWORD PTR [rdi-32] // a

        addsd   sum, QWORD PTR [rdi-24] // c
        addsd   sum, QWORD PTR [rdi-16] // b
        addsd   sum, QWORD PTR [rdi-8]  // a

        add     rax, 24

        addsd   sum, QWORD PTR [rax-16] // b
        addsd   sum, QWORD PTR [rax-24] // c

        cmp     rdi, end
        jne     .L2
```

There appears to be duplicate additions for the members b and c.

This behaviour appears on gcc 12.1 and is still present in gcc 13.1.

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

end of thread, other threads:[~2023-07-07 12:06 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-23 18:06 [Bug c++/110381] New: Incorrect loop unrolling for structs of floating point types lennox.ho at intel dot com
2023-06-23 18:08 ` [Bug c++/110381] " lennox.ho at intel dot com
2023-06-23 19:48 ` [Bug tree-optimization/110381] [11/12/13/14 Regression] double counting for sum of " pinskia at gcc dot gnu.org
2023-06-23 20:01 ` lennox.ho at intel dot com
2023-06-23 20:07 ` pinskia at gcc dot gnu.org
2023-06-23 22:34 ` mpolacek at gcc dot gnu.org
2023-06-26  8:58 ` rguenth at gcc dot gnu.org
2023-06-26  9:33 ` rguenth at gcc dot gnu.org
2023-06-26  9:41 ` rguenth at gcc dot gnu.org
2023-06-26 10:50 ` rguenth at gcc dot gnu.org
2023-06-26 12:18 ` cvs-commit at gcc dot gnu.org
2023-06-26 12:18 ` [Bug tree-optimization/110381] [11/12/13 " rguenth at gcc dot gnu.org
2023-06-29 16:01 ` clyon at gcc dot gnu.org
2023-06-30  6:35 ` cvs-commit at gcc dot gnu.org
2023-06-30  6:40 ` rguenth at gcc dot gnu.org
2023-06-30 14:05 ` clyon at gcc dot gnu.org
2023-06-30 20:50 ` pinskia at gcc dot gnu.org
2023-07-03  7:54 ` clyon at gcc dot gnu.org
2023-07-03  7:57 ` pinskia at gcc dot gnu.org
2023-07-03  8:03 ` cvs-commit at gcc dot gnu.org
2023-07-03  8:05 ` clyon at gcc dot gnu.org
2023-07-07 12:06 ` [Bug tree-optimization/110381] [11/12 " cvs-commit 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).