Thread overview
Very strange bug causing Access Violation with -O and -release
Aug 06, 2005
Deewiant
Aug 06, 2005
Thomas Kühne
Aug 11, 2005
Walter
August 06, 2005
DMD 0.128 on Windows XP SP2.
--
void foo(inout real[] arr) {
	// i = 0 doesn't trigger the bug, everything bigger seems to
	// also, making this const fixes the bug
	size_t i = 1;

	for (size_t j = i; j >= i; j -= i) {
		// dummy line which fixes the bug goes here
		// it can be anything that accesses j in some way
		// such as any of:
		//size_t x = j;
		//printf("%d", j);
		//j = i;

		// interesting results follow from this:
		//printf("%d", i);
		// it prints a _lot_ of ones

		arr[j] = arr[j - i];
	}
}

void main()
{
	real[] array;
	array.length = 2; // whatever, as long as it's more than 1

	foreach (inout real i; array)
		i = 1; // just something

	foo(array);
}
--
IMO this bug is strange because:
	It is only triggered if the array is of type cdouble, real, ireal, or
creal. (FWIW, my machine's a 1.2 GHz Duron - reals on other CPUs might
not trigger the bug?)
	The code must be compiled with both -release and -O to trigger the bug.
	What's explained in the comments... a dummy line accessing j inside the
for loop fixes the bug, if i is const the bug is fixed, if you print i
within the for loop you get _many_ of them. The number seems to depend
on the type of the array (cdouble, real, ireal, creal) and on its
length. The smallest number I got was 402 (creal, array length 2) and
the largest was 1227 (real, array length 2).
August 06, 2005
Deewiant schrieb:

> DMD 0.128 on Windows XP SP2.
> --
> void foo(inout real[] arr) {
> 	// i = 0 doesn't trigger the bug, everything bigger seems to
> 	// also, making this const fixes the bug
> 	size_t i = 1;
> 
> 	for (size_t j = i; j >= i; j -= i) {
> 		// dummy line which fixes the bug goes here
> 		// it can be anything that accesses j in some way
> 		// such as any of:
> 		//size_t x = j;
> 		//printf("%d", j);
> 		//j = i;
> 
> 		// interesting results follow from this:
> 		//printf("%d", i);
> 		// it prints a _lot_ of ones
> 
> 		arr[j] = arr[j - i];
> 	}
> }
> 
> void main()
> {
> 	real[] array;
> 	array.length = 2; // whatever, as long as it's more than 1
> 
> 	foreach (inout real i; array)
> 		i = 1; // just something
> 
> 	foo(array);
> }
> --
> IMO this bug is strange because:
> 	It is only triggered if the array is of type cdouble, real, ireal, or
> creal. (FWIW, my machine's a 1.2 GHz Duron - reals on other CPUs might
> not trigger the bug?)
> 	The code must be compiled with both -release and -O to trigger the bug.
> 	What's explained in the comments... a dummy line accessing j inside the
> for loop fixes the bug, if i is const the bug is fixed, if you print i
> within the for loop you get _many_ of them. The number seems to depend
> on the type of the array (cdouble, real, ireal, creal) and on its
> length. The smallest number I got was 402 (creal, array length 2) and
> the largest was 1227 (real, array length 2).

Added to DStress as
http://dstress.kuehne.cn/run/f/for_06_A.d
...
http://dstress.kuehne.cn/run/f/for_06_R.d

Thomas
August 11, 2005
You've found a bug in the optimizer. It'll get fixed in the next update. Thanks, -Walter