home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cs.utexas.edu!swrinde!mips!odin!bananapc.csd.sgi.com!ciemo
- From: ciemo@bananapc.csd.sgi.com (Dave Ciemiewicz)
- Newsgroups: comp.sys.sgi
- Subject: Re: Optimization problem with 3.1 compilers
- Message-ID: <1992Aug26.024734.2951@odin.corp.sgi.com>
- Date: 26 Aug 92 02:47:34 GMT
- References: <1992Aug19.000616.8590@leland.Stanford.EDU> <1992Aug25.183256.3342@odin.corp.sgi.com>
- Sender: news@odin.corp.sgi.com (Net News)
- Organization: Silicon Graphics, Customer Support Division
- Lines: 80
- Nntp-Posting-Host: bananapc.csd.sgi.com
-
- In article <1992Aug25.183256.3342@odin.corp.sgi.com>, ciemo@bananapc.csd.sgi.com (Dave Ciemiewicz) writes:
- |> In article <1992Aug19.000616.8590@leland.Stanford.EDU>, dhinds@leland.Stanford.EDU (David Hinds) writes:
- |> |> I have a program that compiles and executes reliably with the MIPS 2.1
- |> |> compilers under Irix 4.0.1, but core dumps with a segmentation violation
- |> |> when a particular function is compiled at -O2 or above with the 3.1
- |> |> compilers and Irix 4.0.5A. Here is a short example with the problem:
- |> |>
- |> |> > #include <stdio.h>
- |> |> > #define MAXP 12
- |> |> >
- |> |> > void main(void)
- |> |> > {
- |> |> > int *i, *j, step[MAXP+1], k;
- |> |> > double sum;
- |> |> > float all2[MAXP+1];
- |> |> >
- |> |> > for (k = 0; k < MAXP+1; k++) {
- |> |> > step[k] = k; all2[k] = 2.0;
- |> |> > }
- |> |> >
- |> |> > sum = 0.0; k = 2;
- |> |> > for (i = &step[2]; i <= &step[MAXP]; i++, k++) {
- |> |> > printf("k = %d\n", k);
- |> |> > for (j = &step[1]; j != i; j++)
- |> |> > sum += all2[*j] - 1;
- |> |> > }
- |> |> > printf("Sum = %f\n", sum);
- |> |> > }
- |> |>
- |> |> When I compile this with 'cc -O2 bug.c' and run a.out, I get:
- |> |>
- |> |> > k = 2
- |> |> > k = 3
- |> |> > k = 4
- |> |> > k = 5
- |> |> > k = 6
- |> |> > Segmentation fault (core dumped)
- |> |>
- |>
- |> I don't know if you got a response on this or not. The problem is a bug
- |> in the 3.10 version of /usr/lib/uopt which incorrectly unrolls the inner
- |> loop "for (j = ...". There is a bug submitted on this problem.
- |>
- |> To work around this problem, turn off loop unrolling in uopt (see uopt(1)
- |> man page):
- |>
- |> cc -O2 -Wo,-loopunroll,0 bug.c -o bug.c
- |>
-
- I discovered another workaround which will allow the loopunrolling to occur.
- Change:
-
- for (j = &step[1]; j != i; j++)
- sum += all2[*j] - 1;
- }
-
- to:
-
- for (j = &step[1]; j < i; j++)
- sum += all2[*j] - 1;
- }
-
- The benefit of this change, beyond just working around the uopt bug, is that
- it changes the first for (loop) from being what the Power C analyzer considers
- to be a "while" loop into a form which is more like a tradition "for" loop.
- This allows the analyzer to do a better job of code restructuring. Besides,
- the "j < i" test is more correct than the "j != i" test because you the first
- guarantees that the address is within the subrange of the array and the other
- just tests to see if j is not the i address. Of course, all of this is should
- not be construed as an excuse for uopt not doing the right thing in the
- first place.
-
-
- --
-
- __ * __ _ __ ___
- / \ / / / / \/ \/ \ He was a man like any other man, however, not
- / / \/ / /\ / / quite like any other man.
- \___/\__/\_/ /_/ / \__/
- *
-