[comp.sys.sgi] C compiler optimization bug?

lansd@dgp.toronto.edu (Robert Lansdale) (04/08/90)

	After having run my 3d renderer for several months with the '-g2'
debug flag on, I decided to give it a spin with full optimization enabled.
I realized something was wrong when one of my texture mapped test objects
was instead being textured with a red moire pattern. The thought of finding
where this bug was in 55k lines of C without the help of dbx caused it to be 
quickly placed at the bottom of my things-to-fix list. But my renderer is now
about to be used in a large animation effort, in which a fully optimized
version of the program would be an asset. After a good number of hours of
narrowing the problem down, I found it was being caused by the following
piece of code:

---------------------------------------------------------------------------

	/* Calculate weights and Col at each of the points */
	wt0 = xLowWt * yLowWt;
	wt1 = xLowWt * yHighWt;
	wt2 = xHighWt * yLowWt;
	wt3 = xHighWt * yHighWt;
	c0 = text[yLow * size + xLow];
	c1 = text[yHigh * size + xLow];
	c2 = text[yLow * size + xHigh];
	c3 = text[yHigh * size + xHigh];

	/* Calculate averaged colour */
	col.r = (c0.r * wt0 + c1.r * wt1 + c2.r * wt2 + c3.r * wt3) / 255.0;
	col.g = (c0.g * wt0 + c1.g * wt1 + c2.g * wt2 + c3.g * wt3) / 255.0;
	col.b = (c0.b * wt0 + c1.b * wt1 + c2.b * wt2 + c3.b * wt3) / 255.0;

#if 0
printf("Weights: wt0 = %g, wt1 = %g, wt2 = %g, wt3 = %g\n", wt0, wt1, wt2, wt3);
printf("Red colours: 0 = %d, 1 = %d, 2 = %d, 3 = %d\n", c0.r, c1.r, c2.r, c3.r);
#endif
printf("Colour = %g, %g, %g\n", col.r, col.g, col.b);

	return (col);
}
----------------------------------------------------------------------------

	The printf()'s are my part of my debug code. If run as shown above 
(without the first two printf()s) then the 'col.r' is set to 0. But if either
of the first two printf()'s are included in the code, 'col.r' is set to
its correct value of 0.26. This problem only occurs with the '-O1' and '-O2'
optimization flags. It does not occur with '-g2' or '-O0'. It also only 
affects the first of the three "col.? = () / 255.0' lines. Looks like a
definite optimization bug. I also remember having a number of other similar
optimization related problems when I last ran the fully optimized version
(several months ago), but they'll have to wait until I can get this one
solved.

	The code was compiled and run on a 4D/140, software release
4D1-3.2.1. The following is the output of the '-V' C compiler option.
NOTE: the compile aborted for an unknown reason with this option enabled
(see end of this listing):

---------------------------------------------------------------------------
	cc -V -O2 -DIRS4D -DX11 -DUNIX -DSYS_V  -I/usr/include/bsd -I/usr/include/gl -L/usr/lib/X11 -c -o o_iris/nufilt.o c/nufilt.c    

/usr/lib/cpp:
	Mips Computer Systems Release 1.31
/usr/lib/ccom:
	Mips Computer Systems Release 1.31
/usr/lib/ujoin:
	Mips Computer Systems Release 1.31
/usr/bin/uld:
	Mips Computer Systems Release 1.31
/usr/lib/usplit:
	Mips Computer Systems Release 1.31
/usr/lib/umerge:
	Mips Computer Systems Release 1.31
/usr/lib/uopt:
	Mips Computer Systems Release 1.31
/usr/lib/ugen:
	Mips Computer Systems Release 1.31
/usr/lib/as0:
	Mips Computer Systems Release 1.31
/usr/lib/as1:
	Mips Computer Systems Release 1.31
/usr/bin/ld:
	Mips Computer Systems Release 1.31
/usr/lib/ftoc:
/usr/lib/cord:
	Mips Computer Systems Release 1.31
	 ldopen.c: 1.3 2/16/83
	 ldgetname.c: 1.2 2/16/83
	 ldclose.c: 1.3 2/16/83
	 ldohseek.c: 1.1 1/7/82
	 ldshread.c: 1.1 1/7/82
	 ldsseek.c: 1.1 1/7/82
	 ldnsseek.c: 1.1 1/7/82
	 ldgetname.c: 1.2 2/16/83
	 ldtbread.c: 1.1 1/7/82
	 ldrseek.c: 1.1 1/7/82
	 ldnrseek.c: 1.1 1/7/82
	 vldldptr.c: 1.1 1/8/82
	 allocldptr.c: 1.2 2/16/83
	 freeldptr.c: 1.1 1/7/82
	 ldnshread.c: 1.1 1/7/82
/usr/lib/crt1.o:
/usr/lib/crtn.o:
cc (cc)
	Mips Computer Systems 1.31
*** Error code 1


-- 
Robert Lansdale - (416) 978-6619       Dynamic Graphics Project	
Internet: lansd@dgp.toronto.edu        Computer Systems Research Institute
UUCP:   ..!uunet!dgp.toronto.edu!lansd University of Toronto
Bitnet:	  lansd@dgp.utoronto           Toronto, Ontario M5S 1A4, CANADA

deb@kea.wpd.sgi.com (Deb Ryan) (04/11/90)

In article <1990Apr7.190012.22354@jarvis.csri.toronto.edu>, lansd@dgp.toronto.edu (Robert Lansdale) writes:
> 
> ---------------------------------------------------------------------------
> 
> 	/* Calculate weights and Col at each of the points */
> 	wt0 = xLowWt * yLowWt;
> 	wt1 = xLowWt * yHighWt;
> 	wt2 = xHighWt * yLowWt;
> 	wt3 = xHighWt * yHighWt;
> 	c0 = text[yLow * size + xLow];
> 	c1 = text[yHigh * size + xLow];
> 	c2 = text[yLow * size + xHigh];
> 	c3 = text[yHigh * size + xHigh];
> 
> 	/* Calculate averaged colour */
> 	col.r = (c0.r * wt0 + c1.r * wt1 + c2.r * wt2 + c3.r * wt3) / 255.0;
> 	col.g = (c0.g * wt0 + c1.g * wt1 + c2.g * wt2 + c3.g * wt3) / 255.0;
> 	col.b = (c0.b * wt0 + c1.b * wt1 + c2.b * wt2 + c3.b * wt3) / 255.0;
> 
> #if 0
> printf("Weights: wt0 = %g, wt1 = %g, wt2 = %g, wt3 = %g\n", wt0, wt1, wt2, wt3);
> printf("Red colours: 0 = %d, 1 = %d, 2 = %d, 3 = %d\n", c0.r, c1.r, c2.r, c3.r);
> #endif
> printf("Colour = %g, %g, %g\n", col.r, col.g, col.b);
> 
> 	return (col);
> }

I've looked at an executable version of this code.  There was an 
optimizer bug in 3.2.  It has been fixed, and will be available in the
next release.


					-Deb
					 deb@sgi.com
 					 Deborah Ryan Caruso @ Silicon Graphics