Thread overview
[Issue 287] New: DMD optimization bug arround dynamic array length
Aug 14, 2006
d-bugmail
Aug 14, 2006
d-bugmail
Aug 15, 2006
Thomas Kuehne
Aug 19, 2006
d-bugmail
Aug 26, 2006
Thomas Kuehne
Sep 19, 2006
d-bugmail
August 14, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=287

           Summary: DMD optimization bug arround dynamic array length
           Product: D
           Version: 0.163
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: iceelyne@gmail.com


build -release -O -clean sample.d
got wrong optimized exe.

import std.stdio;
import std.process;

char[] replace(char[] subject) {
                char[] s = new char[subject.length];
                int lng = 0;
                char[] sa;
                int lngnew;
                int i = 0;
                while(i<10) {
                        i++;
                        sa = "repxxxxxxxxxxxx";
                        lngnew = lng + sa.length;
                        if(lngnew > s.length) s.length = lngnew * 2; // expand
size
                        writefln(lng, \t, lngnew, \t, s.length, \t, sa.length);
                        s[lng .. lngnew] = sa[0 .. $];
                        lng = lngnew;

                        //volatile{//OK

                        sa = "repzzzzzzzzzzzzzzzzzzzzzz";
                        //lngnew = lng-1 + sa.length+1;//OK
                        //synchronized lngnew = lng + sa.length;//OK
                        //lngnew += sa.length;//OK
                        lngnew = lng + sa.length;// 2*sa.length?
                        if(lngnew > s.length) s.length = lngnew * 2; // expand
size
                        writefln(lng, \t, lngnew, \t, s.length, \t, sa.length);
                        s[lng .. lngnew] = sa[0 .. $];
                        lng = lngnew;

                        //}

                }
                return s[0 .. lng];
}

void demo() {
        int lng = 0;
        int lngnew = 0;
        int i = 0;
        char[] sa = "xxxx";//it seem to be around the Dynamic Array's length
property.

        // class SA {
                // int _a;
                // this(int a) {_a = a;}
                // int length() {return _a;}
        // }
        // SA sa = new SA(4);//OK
        while(i<10) {
                i++;
                lngnew = lng + sa.length;
                writefln(lng, \t, lngnew, \t, sa.length);
                lng = lngnew;

                lngnew = lng + sa.length;
                writefln(lng, \t, lngnew, \t, sa.length);
                lng = lngnew;
        }
}

int main(char[][] args) {
        try {
                demo();
                writefln(replace("012345678901234567890"));
        } catch(Exception e) {
                writefln(e.msg);
        }
        system("pause");
        return 0;
}


-- 

August 14, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=287





------- Comment #1 from godaves@yahoo.com  2006-08-14 10:15 -------
This also happens w/o build w/ the -O flag (build isn't needed nor does it seem to have anything to do with the bug).


-- 

August 15, 2006
d-bugmail@puremagic.com schrieb am 2006-08-14:
> http://d.puremagic.com/issues/show_bug.cgi?id=287

> build -release -O -clean sample.d
> got wrong optimized exe.
>
> import std.stdio;
> import std.process;
>
> char[] replace(char[] subject) {
>                 char[] s = new char[subject.length];
>                 int lng = 0;
>                 char[] sa;
>                 int lngnew;
>                 int i = 0;
>                 while(i<10) {
>                         i++;
>                         sa = "repxxxxxxxxxxxx";
>                         lngnew = lng + sa.length;
>                         if(lngnew > s.length) s.length = lngnew * 2; // expand
> size
>                         writefln(lng, \t, lngnew, \t, s.length, \t, sa.length);
>                         s[lng .. lngnew] = sa[0 .. $];
>                         lng = lngnew;
>
>                         //volatile{//OK
>
>                         sa = "repzzzzzzzzzzzzzzzzzzzzzz";
>                         //lngnew = lng-1 + sa.length+1;//OK
>                         //synchronized lngnew = lng + sa.length;//OK
>                         //lngnew += sa.length;//OK
>                         lngnew = lng + sa.length;// 2*sa.length?
>                         if(lngnew > s.length) s.length = lngnew * 2; // expand
> size
>                         writefln(lng, \t, lngnew, \t, s.length, \t, sa.length);
>                         s[lng .. lngnew] = sa[0 .. $];
>                         lng = lngnew;
>
>                         //}
>
>                 }
>                 return s[0 .. lng];
> }
>
> void demo() {
>         int lng = 0;
>         int lngnew = 0;
>         int i = 0;
>         char[] sa = "xxxx";//it seem to be around the Dynamic Array's length
> property.
>
>         // class SA {
>                 // int _a;
>                 // this(int a) {_a = a;}
>                 // int length() {return _a;}
>         // }
>         // SA sa = new SA(4);//OK
>         while(i<10) {
>                 i++;
>                 lngnew = lng + sa.length;
>                 writefln(lng, \t, lngnew, \t, sa.length);
>                 lng = lngnew;
>
>                 lngnew = lng + sa.length;
>                 writefln(lng, \t, lngnew, \t, sa.length);
>                 lng = lngnew;
>         }
> }
>
> int main(char[][] args) {
>         try {
>                 demo();
>                 writefln(replace("012345678901234567890"));
>         } catch(Exception e) {
>                 writefln(e.msg);
>         }
>         system("pause");
>         return 0;
> }

Added to DStress as http://dstress.kuehne.cn/run/o/odd_bug_07_A.d http://dstress.kuehne.cn/run/o/odd_bug_07_B.d

Thomas


August 19, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=287


deewiant@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |deewiant@gmail.com
           Keywords|                            |wrong-code
            Version|0.163                       |0.164




------- Comment #3 from deewiant@gmail.com  2006-08-19 04:10 -------
My reduced version, with some inline comments:

import std.stdio : writefln;

void main() {
        int lng, lngnew;

        int[] arr = new int[1];

        for (int i = 10; i--;) {
                // removing one of the pairs of assignments to lng/lngnew
                // fixes the bug
                lngnew = lng + arr.length;
                lng = lngnew;

                lngnew = lng + arr.length;

                // moving this writefln to anywhere except between either pair
                // of assignments to lng/lngnew fixes the bug
                writefln("%2d %2d %2d", lng, lngnew, arr.length);

                // changing either "lng = lngnew" to the equivalent statement
                // "lng += arr.length" fixes the bug
                lng = lngnew;
        }
}


-- 

August 26, 2006
d-bugmail@puremagic.com schrieb am 2006-08-19:
> http://d.puremagic.com/issues/show_bug.cgi?id=287

> My reduced version, with some inline comments:
<snip>

Added to DStress as http://dstress.kuehne.cn/run/o/odd_bug_07_C.d http://dstress.kuehne.cn/run/o/odd_bug_07_D.d http://dstress.kuehne.cn/run/o/odd_bug_07_E.d

Thomas


September 19, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=287


bugzilla@digitalmars.com changed:

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




------- Comment #5 from bugzilla@digitalmars.com  2006-09-19 15:21 -------
Not reproducible with DMC 0.167.


--