www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 287] New: DMD optimization bug arround dynamic array length

reply d-bugmail puremagic.com writes:
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;
}


-- 
Aug 14 2006
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=287






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).


-- 
Aug 14 2006
prev sibling next sibling parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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 -----BEGIN PGP SIGNATURE----- iD8DBQFE4bJ+LK5blCcjpWoRAiFDAJwNAxYqylFxBbE0DHKz8lv6ouPUJQCgo8Ap m1e4mdLdEGVvRNeiS5Fm7zE= =tGKg -----END PGP SIGNATURE-----
Aug 15 2006
prev sibling next sibling parent reply d-bugmail puremagic.com writes:
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





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;
        }
}


-- 
Aug 19 2006
parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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 -----BEGIN PGP SIGNATURE----- iD8DBQFE8DBuLK5blCcjpWoRAhfOAJ9zAK6uzszmTi+RBXOPRSihgQDAGgCggcdl Ubdg1lNxTseYx1uaiOKaTq8= =OVF5 -----END PGP SIGNATURE-----
Aug 26 2006
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=287


bugzilla digitalmars.com changed:

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





Not reproducible with DMC 0.167.


-- 
Sep 19 2006