www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9320] New: Non-POD status of a struct correlated to bad inlining.

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9320

           Summary: Non-POD status of a struct correlated to bad inlining.
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: dransic gmail.com


--- Comment #0 from Nicolas Sicard <dransic gmail.com> 2013-01-14 14:08:34 PST
---
This codes defines a struct that is just a wrapper around a real x, with binary
operators.
---
struct Foo {
    real x;

    version(Constructor) {
        this(real x) {
            this.x = x;
        }
    }

    Foo opBinary(string op)(Foo other) {
        return Foo(mixin("x" ~ op ~ "other.x"));
    }
}

version(Constructor)
    static assert(!__traits(isPOD, Foo));
else
    static assert(__traits(isPOD, Foo));

Foo test(Foo a, Foo b, Foo c) {
    return (a + b) / (a * b) - c;
}

void main() {}
---

When compiled with -O -inline -release, the object code for the test function
is:
---
        push    RBP
        mov    RBP,RSP
        sub    RSP,010h
        fld    tbyte ptr 030h[RBP]
        fld    tbyte ptr 020h[RBP]
        faddp    ST(1),ST
        fld    tbyte ptr 030h[RBP]
        fld    tbyte ptr 020h[RBP]
        fmulp    ST(1),ST
        fdivp    ST(1),ST
        fld    tbyte ptr 010h[RBP]
        fsubp    ST(1),ST
        fstp    tbyte ptr [RDI]
        mov    word ptr 0Ah[RDI],0
        mov    dword ptr 0Ch[RDI],0
        mov    RAX,RDI
        mov    RSP,RBP
        pop    RBP
        ret
---

When compiled with the same flags, but with -version=Constructor, I benchmarked
the code as 5x slower (no data here) and the object code becomes:
---
        push    RBP
        mov    RBP,RSP
        sub    RSP,0D0h
        mov    -010h[RBP],RDI
        lea    RSI,020h[RBP]
        lea    RDI,-0A0h[RBP]
        movsd
        movsd
        mov    RSI,[00h][RIP]
        lea    RDI,-090h[RBP]
        movsd
        movsd
        fld    tbyte ptr 030h[RBP]
        fld    tbyte ptr -0A0h[RBP]
        faddp    ST(1),ST
        fstp    tbyte ptr -090h[RBP]
        mov    word ptr -086h[RBP],0
        mov    dword ptr -084h[RBP],0
        lea    RSI,-090h[RBP]
        lea    RDI,-0B0h[RBP]
        movsd
        movsd
        lea    RSI,020h[RBP]
        lea    RDI,-060h[RBP]
        movsd
        movsd
        mov    RSI,[00h][RIP]
        lea    RDI,-050h[RBP]
        movsd
        movsd
        fld    tbyte ptr 030h[RBP]
        fld    tbyte ptr -060h[RBP]
        fmulp    ST(1),ST
        fstp    tbyte ptr -050h[RBP]
        mov    word ptr -046h[RBP],0
        mov    dword ptr -044h[RBP],0
        lea    RSI,-050h[RBP]
        lea    RDI,-070h[RBP]
        movsd
        movsd
        lea    RSI,-070h[RBP]
        lea    RDI,-080h[RBP]
        movsd
        movsd
        mov    RSI,[00h][RIP]
        lea    RDI,-040h[RBP]
        movsd
        movsd
        fld    tbyte ptr -0B0h[RBP]
        fld    tbyte ptr -080h[RBP]
        fdivp    ST(1),ST
        fstp    tbyte ptr -040h[RBP]
        mov    word ptr -036h[RBP],0
        mov    dword ptr -034h[RBP],0
        lea    RSI,-040h[RBP]
        lea    RDI,-0C0h[RBP]
        movsd
        movsd
        lea    RSI,010h[RBP]
        lea    RDI,-030h[RBP]
        movsd
        movsd
        mov    RSI,[00h][RIP]
        lea    RDI,-020h[RBP]
        movsd
        movsd
        fld    tbyte ptr -0C0h[RBP]
        fld    tbyte ptr -030h[RBP]
        fsubp    ST(1),ST
        fstp    tbyte ptr -020h[RBP]
        mov    word ptr -016h[RBP],0
        mov    dword ptr -014h[RBP],0
        lea    RSI,-020h[RBP]
        lea    RDI,-0D0h[RBP]
        movsd
        movsd
        lea    RSI,-0D0h[RBP]
        mov    RDI,-010h[RBP]
        movsd
        movsd
        mov    RAX,-010h[RBP]
        mov    RSP,RBP
        pop    RBP
        ret
---

D2.061, OSX 10.8.2

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 14 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9320



--- Comment #1 from Nicolas Sicard <dransic gmail.com> 2013-01-16 05:19:13 PST
---
Note, LDC produces equivalent code in both cases:
---

        fld    tbyte ptr 028h[RSP]
        fld    tbyte ptr 018h[RSP]
        fld    ST(0)
        fmul    ST,ST(2)
        fxch    ST(1)
        faddp    ST(2),ST
        fdivp    ST(1),ST
        fld    tbyte ptr 8[RSP]
        fsubp    ST(1),ST
        fstp    tbyte ptr [RDI]
        mov    AX,[0Eh][RIP]
        mov    0Eh[RDI],AX
        mov    EAX,[0Ah][RIP]
        mov    0Ah[RDI],EAX
        ret
---

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 16 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9320


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com


--- Comment #2 from Walter Bright <bugzilla digitalmars.com> 2013-01-24
23:13:00 PST ---
This has nothing to do with inlining; the constructor call is inlined just
fine. It also has nothing to do with POD status, although that does illuminate
the problem.

The problem is that the optimizer does copy propagation only on basic types,
not on structs. It needs to do it on structs, too.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 24 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9320



--- Comment #3 from github-bugzilla puremagic.com 2013-01-26 19:51:13 PST ---
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/3e004c4fafcd64eda4e5ed1a257182b8d802b66f
fix
Issue 9320 - optimizer should do copy propagation on structs, too

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 26 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9320



--- Comment #4 from Walter Bright <bugzilla digitalmars.com> 2013-01-26
19:53:08 PST ---
https://github.com/D-Programming-Language/dmd/pull/1559

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 26 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9320



--- Comment #5 from github-bugzilla puremagic.com 2013-01-26 22:38:23 PST ---
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/fde7475a6f1837a32977221e9eb22df180920826
Merge pull request #1559 from WalterBright/b34

fix Issue 9320 - optimizer should do copy propagation on structs, too

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 26 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9320


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
            Version|D2                          |D1 & D2
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 26 2013