Issue 24836 - struct return by hidden struct arg does not need copy to become an rvalue
Summary: struct return by hidden struct arg does not need copy to become an rvalue
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: backend, pull
Depends on:
Blocks:
 
Reported: 2024-10-26 06:19 UTC by Walter Bright
Modified: 2024-10-29 18:20 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Walter Bright 2024-10-26 06:19:44 UTC
Related: 9666

Given:

  import core.stdc.stdio;

  struct S {
    this(ref S) { printf("this(ref S)\n"); }
    void opAssign(ref S) { printf("opAssign(ref S)\n"); }
    void opAssign(S) { printf("opAssign(S)\n"); }
    void opMove(ref S) { printf("opMove(ref S)\n"); }
  }

  void main() {
    S s;
    S t;

    S test() { S* p = &s; return *p; }
    t = test();
  }

The code generated for main() is:

_Dmain:
    push      RBP
    mov       RBP,RSP
    sub       RSP,010h
    xor       EAX,EAX
    mov       -8[RBP],AL  // S s;
    mov       -7[RBP],AL  // S t;
    lea       RSI,-6[RBP] // address of struct return value
    mov       RDI,RBP     // static link
    call      test
    mov       RSI,RAX     // address of struct return value as source
    lea       RDI,-5[RBP] // address of temporary
    movsb                 // copy return value to temporary
    lea       RSI,-5[RBP] // address of temporary
    lea       RDI,-7[RBP] // address of t
    call      opAssign    // opAssign(S)
    xor       EAX,EAX
    leave
    ret

The copy is not necessary, and is a bug.
Comment 1 Dlang Bot 2024-10-27 03:32:24 UTC
@WalterBright created dlang/dmd pull request #17033 "fix bugzilla Issue 24836 - struct return by hidden struct arg does no…" fixing this issue:

- fix bugzilla Issue 24836 - struct return by hidden struct arg does not need copy to become an rvalue

https://github.com/dlang/dmd/pull/17033
Comment 2 Dlang Bot 2024-10-29 18:20:12 UTC
dlang/dmd pull request #17033 "fix bugzilla Issue 24836 - struct return by hidden struct arg does no…" was merged into master:

- 252291627196b0bcc9bc836bf94ad4adb2c46e5a by Walter Bright:
  fix bugzilla Issue 24836 - struct return by hidden struct arg does not need copy to become an rvalue

https://github.com/dlang/dmd/pull/17033