public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug other/115581] New: missed argument forwarding in lambda
@ 2024-06-21 13:39 pobrn at protonmail dot com
  2024-06-21 17:20 ` [Bug middle-end/115581] could remove copy of struct if original otherwise not used pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: pobrn at protonmail dot com @ 2024-06-21 13:39 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 115581
           Summary: missed argument forwarding in lambda
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pobrn at protonmail dot com
  Target Milestone: ---

Consider the following piece of C++ code:
```
struct thing {
    int x[64];
};

void g(const thing &);

void f1(thing x)
{
    g(x);
}

auto *f2 = +[](thing x) {
    g(x);
};

struct f3 {
    void operator()(thing);
};
void f3::operator()(thing x) {
    g(x);
}
```

Checking the generated code on an x86-64 linux system at -O2, note that `f1()`
and `f3::operator()()` simply "forward" their by-value arguments to `g()`:

```
f1(thing):
        sub     rsp, 8
        lea     rdi, [rsp+16]
        call    g(thing const&)
        add     rsp, 8
        ret
```

Notice that this is not done in the case of `f2`, the generated function from
the lambda makes a copy of `x`:

```
f2::{lambda(thing)#1}::_FUN(thing):
        sub     rsp, 264
        movdqu  xmm0, XMMWORD PTR [rsp+272]
        mov     rdi, rsp
        movaps  XMMWORD PTR [rsp], xmm0
        movdqu  xmm0, XMMWORD PTR [rsp+288]
        [...]
        movdqu  xmm0, XMMWORD PTR [rsp+512]
        movaps  XMMWORD PTR [rsp+240], xmm0
        call    g(thing const&)
        add     rsp, 264
        ret
```

See https://gcc.godbolt.org/z/n43a6MPjY, clang does the same copy, so maybe
there is a rule in C++ that prevents this optimization and that I failed to
consider?

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

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

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-21 13:39 [Bug other/115581] New: missed argument forwarding in lambda pobrn at protonmail dot com
2024-06-21 17:20 ` [Bug middle-end/115581] could remove copy of struct if original otherwise not used pinskia at gcc dot gnu.org
2024-06-21 17:20 ` pinskia at gcc dot gnu.org
2024-06-21 17:22 ` pinskia at gcc dot gnu.org
2024-06-21 18:01 ` pobrn at protonmail dot com
2024-06-21 18:15 ` pinskia 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).