Bug 287 – DMD optimization bug arround dynamic array length

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2006-08-14T10:07:00Z
Last change time
2014-02-15T13:19:45Z
Keywords
wrong-code
Assigned to
bugzilla
Creator
iceelyne

Comments

Comment #0 by iceelyne — 2006-08-14T10:07:32Z
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; }
Comment #1 by godaves — 2006-08-14T10:15:55Z
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).
Comment #2 by thomas-dloop — 2006-08-15T07:25:25Z
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 [email protected] 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-----
Comment #3 by matti.niemenmaa+dbugzilla — 2006-08-19T04:10:50Z
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; } }
Comment #4 by thomas-dloop — 2006-08-26T06:15:29Z
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 [email protected] 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-----
Comment #5 by bugzilla — 2006-09-19T15:21:45Z
Not reproducible with DMC 0.167.