Thread overview
[Issue 8095] [64 bit] Wrong code generation with spilled register, -m64 -O
[Issue 8095] [64 bit] Wrong code generation with ref parameters, -m64 -O -inline
May 15, 2012
Don
[Issue 8095] [64 bit] Wrong code generation with ref parameters, -m64 -O
May 15, 2012
Don
May 15, 2012
Kenji Hara
May 16, 2012
Don
May 16, 2012
Don
May 16, 2012
Walter Bright
May 15, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8095


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
           Severity|normal                      |critical


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 15, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8095


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[64 bit] Wrong code         |[64 bit] Wrong code
                   |generation with ref         |generation with ref
                   |parameters, -m64 -O -inline |parameters, -m64 -O


--- Comment #1 from Don <clugdbug@yahoo.com.au> 2012-05-15 04:14:08 PDT ---
'Reduced' test case doesn't require -inline. This is a pure optimizer bug. It requires a nested function, but the nested function isn't actually used.

void bug8095(int z, ref int p1, ref int p2, int xxx, ref int p4, ref int p5)
{
    int x = z / 3;
    void never_used()
    {
        p4 = 0;
        int unused = 0;
        if (x == 4) unused = 1;
    }
    if (z >= 0) {
        p1 = 0;
        p4 = 0;
        p5 = 0;
        int c = 0;
        if ( z / 5 )
            c = 1;
        p2 = c;
        x = c;
    }

}

void main()
{
    int  x1, x2, x4, x5;
    bug8095(0, x1, x2, 0, x4, x5);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 15, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8095



--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2012-05-15 04:41:04 PDT ---
This is similar to bug 8093.
It occurs in Windows 7 64-bit, and the cause is accessing ref variable in outer
scope from nested function.

int g;
ref int foo() {
   ref int __result;
   Container c;
   switch(c.opApply((ref int n){
       __result = g;   // accessing ref variable from nested function
       return 2;
       return 0;
   })){
       default: break;
       case 2: return __vresult;
   }
   return g;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8095


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[64 bit] Wrong code         |[64 bit] Wrong code
                   |generation with ref         |generation with spilled
                   |parameters, -m64 -O         |register, -m64 -O


--- Comment #4 from Don <clugdbug@yahoo.com.au> 2012-05-16 01:43:57 PDT ---
A little further reduced. Doesn't need ref. The complicated thing about this test case is the third parameter, which goes into EDX. The two divisions, one in each section, prevent any variable from being stored in RDX. Probably there is some simpler way of forcing it to run out of registers.

Compiling with --r gives the message:

symbol 'z' spilled in reg R9

and this is where the problem happens.
edx must be the third last parameter, since RDX is used in the division. All
the other parameters (and hence registers) can be shuffled around.

---------------------------
void bug8095(int p0, int *p1, int z, int edx, int *p4, int p5)
{
    int x = z / 3;
    if (z) {
        int c = p5 + *p1 + p0; // *p1 segfaults -- it does *z !!
        if ( z / 5 )
            c = 1;
        *p4 = c;
        x = c;
    }
    void never_used() {
        ++x;
        int * unused = p1; // kills p4 somehow
    }
}

void main() {
    int x, y;
    bug8095(0, &x, 1, 0, &y, 0);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8095



--- Comment #5 from Don <clugdbug@yahoo.com.au> 2012-05-16 08:34:18 PDT ---
As far as I can tell, the problem is with spilling registers.
When a spill occurs ( creg_map(), called from cgreg_assign() line cgreg.c:925)
I don't see how it marks the fastpar register as not being valid anymore.

Then in cod1.c, loaddata(), line 3818, it decides that "we can use register
that parameter was passed in".
So it doesn't bother to reload it.

Not sure how to fix this.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8095



--- Comment #6 from github-bugzilla@puremagic.com 2012-05-16 16:13:57 PDT ---
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/5916afbef515147d44d0a58b45a29c9a17788e98 fix Issue 8095 - [64 bit] Wrong code generation with spilled register, -m64 -O

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8095



--- Comment #7 from github-bugzilla@puremagic.com 2012-05-16 16:14:29 PDT ---
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/405a22f79dc0d2a0830944bfd20d8dadc85fca2e fix Issue 8095 - [64 bit] Wrong code generation with spilled register, -m64 -O

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8095


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


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