D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 15745 - Inline Assembly stomped on by Profiling
Summary: Inline Assembly stomped on by Profiling
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 normal
Assignee: No Owner
URL:
Keywords: iasm, pull
Depends on:
Blocks:
 
Reported: 2016-03-02 17:13 UTC by Matthew Dudley
Modified: 2020-08-24 14:09 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Matthew Dudley 2016-03-02 17:13:09 UTC
I posted this on the learn section of the forum earlier in the week, but I'm reasonably certain this is a compiler bug. 

Essentially, when I run some code which has inline assembly through the profiler, I get unexpected results.

Here's a reproduction:

// bsf is Bitscan Forward. It should return the index of the least significant bit which is on.
ubyte LS1B(ulong board)
{
  asm
  {
    bsf RAX, board;
  }
}

void main()
{
    import std.conv;

    for (int i = 0; i < 63; ++i)
    {
        auto slide = (1UL << i);
        auto ls1b = slide.LS1B;
        assert(ls1b == i, "Incorrect LS1B ~ expected: " ~ i.to!string ~ " got: " ~ ls1b.to!string);
    }
}



If I run the above like so:

dmd -debug app.d; ./app

It doesn't assert. But if I run it like so:

dmd -profile app.d; ./app

It asserts, and comes back with a different answer every time.
Comment 1 Matthew Dudley 2016-03-02 17:14:09 UTC
DMD 2.070 on Arch Linux for reference.
Comment 2 Walter Bright 2020-08-23 10:04:14 UTC
The trouble is the profiler inserts function calls to the profiler library code in each function. This interferes with the register contents of RAX.
Comment 3 Dlang Bot 2020-08-23 10:20:23 UTC
@WalterBright created dlang/dmd pull request #11608 "fix Issue 15745 - Inline Assembly stomped on by Profiling" fixing this issue:

- fix Issue 15745 - Inline Assembly stomped on by Profiling

https://github.com/dlang/dmd/pull/11608
Comment 4 Dlang Bot 2020-08-24 14:09:40 UTC
dlang/dmd pull request #11608 "fix Issue 15745 - Inline Assembly stomped on by Profiling" was merged into master:

- c557bcbcb2b52cd3faaa4bb698c0a3b0a9fdffe8 by Walter Bright:
  fix Issue 15745 - Inline Assembly stomped on by Profiling

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