D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20126 - codegen reloads parameter from register when iasm changed the backing memory
Summary: codegen reloads parameter from register when iasm changed the backing memory
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 regression
Assignee: No Owner
URL:
Keywords: iasm, pull
Depends on:
Blocks:
 
Reported: 2019-08-13 05:24 UTC by Manu
Modified: 2019-08-19 08:47 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Manu 2019-08-13 05:24:50 UTC
On my local 2.087, this code works, but in master, it generates bad code:

asmtest.d
---------

extern(C) float floop(float* r, float x)
{
  asm
  {
    mov EAX, x;
    mov RCX, r;
    xchg [RCX], EAX;
    mov x, EAX;
  }
  return x;
}


> dmd asmtest.d -c -m64

Dumping this emits:

0000000000000000 <floop>:
   0:   55                      push   %rbp
   1:   48 8b ec                mov    %rsp,%rbp
   4:   48 83 ec 10             sub    $0x10,%rsp
   8:   48 89 7d f0             mov    %rdi,-0x10(%rbp)
   c:   f3 0f 11 45 f8          movss  %xmm0,-0x8(%rbp)

  11:   8b 45 f8                mov    -0x8(%rbp),%eax
  14:   48 8b 4d f0             mov    -0x10(%rbp),%rcx
  18:   87 01                   xchg   %eax,(%rcx)
  1a:   89 45 f8                mov    %eax,-0x8(%rbp)

  1d:   c9                      leaveq
  1e:   c3                      retq


It's missing this before the return:
  movss 0x8(%rbp),%xmm0

You can see where it loads from the argument in xmm0 to -0x8(%rbp) to transfer to RAX in the asm code, then the asm code as written.
It writes the modified value back to the stack, but the `return x;` statement should load `x` back into xmm0 to return the value, but it just skips that.

I presume that it assumes xmm0 was not clobbered by the asm block and elides the return code, but the variable was written...

This fails on x64 in master on both windows and linux, but it works correctly for -m32 (does emit an fld before return).
It also used to work on x64 in 2.087.0 (which i'm running locally)
Comment 1 Dlang Bot 2019-08-17 21:48:50 UTC
@WalterBright created dlang/dmd pull request #10316 "fix Issue 20126 - codegen reloads parameter from register when iasm c…" fixing this issue:

- fix Issue 20126 - codegen reloads parameter from register when iasm changed the backing memory

https://github.com/dlang/dmd/pull/10316
Comment 2 Manu 2019-08-19 00:34:04 UTC
**Bug report.
Comment 3 Dlang Bot 2019-08-19 08:47:03 UTC
dlang/dmd pull request #10316 "fix Issue 20126 - codegen reloads parameter from register when iasm c…" was merged into master:

- d51b3ec1a9cb3f1d9d1a5701b2237be7a31f61f4 by Walter Bright:
  fix Issue 20126 - codegen reloads parameter from register when iasm changed the backing memory

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