www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5294] New: loop optimization (-O) gone crazy

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

           Summary: loop optimization (-O) gone crazy
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: blocker
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: spam extrawurst.org



---
void cpv(float x)
{}

void main(){

    int cnt;

    for(int i=0; i<30; i++)
    {
        cnt++;
        cpv(i*60 - 100); // comment this out and it makes 30 loops
    }

    writefln("%s",cnt); // compile with -O and it prints 1
}

tested with dmd2.050 using -O for optimization.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 01 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5294


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



Reduced a little:


import core.stdc.stdio: printf;
void foo(int) {}
void main() {
    int count;
    for (int i = 0; i < 2; i++) {
        count++;
        foo(i * 5 - 6); // comment this out and it makes 2 loops
    }
    printf("%d\n", count); // compile with -O and it prints 1
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 01 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5294


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au
            Version|unspecified                 |D1 & D2



Applies to all D1 and D2, even prehistoric versions (tested as far back as
DMD0.140).

Very weird. In bearophile's test case, written as 

    for (int i = 0; i < A; i++) {
        count++;
        foo(i * 5 - B); // comment this out and it makes 2 loops
    }

it fails for B = (5+1)..(5*A) inclusive (eg, 6, 7, 8, 9, 10 all fail for A==2).

And if it is foo(i*6 - B), it fails for B= 7..6*A.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 01 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5294




---


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 01 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5294




---


-- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 01 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5294




Automatic fuzzy testing like this one allows to discover compiler bugs like
that:

http://gcc.gnu.org/wiki/summit2010?action=AttachFile&do=view&target=regehr_gcc_summit_2010.pdf

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 06 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5294




---

 Automatic fuzzy testing like this
kidding me ? I wish it would have been found by any test, it appeared in an actual project. while i converted some C code to D i wondered a lot until i finally reduced it to this testcase. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 06 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5294




Bearophile -- That's an interesting link. Currently, DMD back-end bugs are
being found at the rate of about 3 per year. So yes, fuzzy testing of DMC could
probably flush out some backend bugs a bit faster.
-------------------

Here's what's happening. First, in this code:

    for (int i = 0; i < 10; i++) {
        foo(i * 5 - 6);
    }
it sees that i and 10 are always >=0, so the signed comparison "i < 10" is
replaced with an unsigned one. (This happens in the backend in constprop() ).
Then, while dealing with loop invariants, it rewrites the loop into:

for (int _i2 = -6; _i2 < 10*5 - 6; _i2 += 5)
{
  foo(_i2);
}

Fine. Except that it had changed the comparison into an unsigned one!
Particularly interesting is the case where the call is foo(i*5-50);
Then, the loop becomes:
for (int _i2 = -50; _i2 < 0; _i2 += 5)

Since an unsigned value is NEVER less than zero, it just drops the loop
completely!

Nasty.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 06 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5294


Walter Bright <bugzilla digitalmars.com> changed:

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



22:41:10 PST ---
For optimizer spelunkers, if you compile dmd with debug on, and compile with:

  -O --c

you'll get reports of the various optimizations done.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 06 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5294


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED



17:08:55 PST ---
http://www.dsource.org/projects/dmd/changeset/792

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