Bug 33354 - [4.2 only] MINLOC in combination with SUM gives wrong result
Summary: [4.2 only] MINLOC in combination with SUM gives wrong result
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.2.1
: P3 normal
Target Milestone: ---
Assignee: Thomas Koenig
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2007-09-08 13:02 UTC by Oskar Enoksson
Modified: 2007-10-15 18:24 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.3.0 4.2.3
Known to fail: 4.2.2
Last reconfirmed: 2007-10-02 18:09:13


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Oskar Enoksson 2007-09-08 13:02:44 UTC
A simple program should print "1" but prints "2". Of some reason MINLOC fails to find the minium location (first location) for the SUM expression.

PROGRAM TST
  IMPLICIT NONE
  REAL :: A(1,3)
  A(:,1) = 10
  A(:,2) = 20
  A(:,3) = 30
  WRITE(*,*) SUM(A(:,1:3),1)
  WRITE(*,*) MINLOC(SUM(A(:,1:3),1),1)
END PROGRAM TST

bash%> gfortran -o tst tst.f90
bash%> ./tst
   10.00000       20.00000       30.00000
           2
bash%>
Comment 1 kargls 2007-09-08 15:31:40 UTC
The code compiles and runs correctly with trunk (aka 4.3).
I'm guessing that Thomas had fixed the problem and that
he did not back port the fix because the bug isn't a
regression.  If Thomas does not comment on the PR in a
reasonable time, I'm inclinded to cloase the PR.  Thanks
for the bug report.
Comment 2 Tobias Burnus 2007-09-08 16:16:45 UTC
I want to add that you can find gfortran 4.3.0 binaries at
   http://gcc.gnu.org/wiki/GFortranBinaries

I'm actually unsure which patch fixed it. It might be Paul's PR32298 even though it is about PARAMETER arrays.
Comment 3 Paul Thomas 2007-09-11 18:37:31 UTC
(In reply to comment #2)
> I want to add that you can find gfortran 4.3.0 binaries at
>    http://gcc.gnu.org/wiki/GFortranBinaries
> 
> I'm actually unsure which patch fixed it. It might be Paul's PR32298 even
> though it is about PARAMETER arrays.
> 

Nah! I plead not guilty:-)

Paul
Comment 4 Tobias Schlüter 2007-09-29 07:57:52 UTC
Subject: Bug 33354

Author: tobi
Date: Sat Sep 29 07:57:37 2007
New Revision: 128882

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=128882
Log:
PR fortran/33354
* gfortran.dg/minmaxloc_4.f90: New.

Added:
    trunk/gcc/testsuite/gfortran.dg/minmaxloc_4.f90
Modified:
    trunk/gcc/testsuite/ChangeLog

Comment 5 Tobias Schlüter 2007-09-29 08:00:13 UTC
I added a testcase for this.  Can this bug be closed or does anyone feel strongly enough about it to fix it in 4.2?
Comment 6 Thomas Koenig 2007-09-29 13:12:18 UTC
(In reply to comment #5)
> I added a testcase for this.

Thanks!

> Can this bug be closed or does anyone feel
> strongly enough about it to fix it in 4.2?

If we can identify which patch fixed this, I'd be in favor
of backporting (even though we'll miss 4.2.2).

Thomas

Comment 7 Oskar Enoksson 2007-09-30 20:56:22 UTC
(In reply to comment #6)
> (In reply to comment #5)
> > I added a testcase for this.
> Thanks!
> > Can this bug be closed or does anyone feel
> > strongly enough about it to fix it in 4.2?
> If we can identify which patch fixed this, I'd be in favor
> of backporting (even though we'll miss 4.2.2).
> Thomas

I think it's a pretty serious bug. Everyone who uses MINLOC or MAXLOC will silently get wrong result in certain cases and those intrinsics are fairly commonly used. It would sure be nice if this could be fixed in 4.2.3, to bad its to late for 4.2.2. Let me know if theres anything I can do to help.
Comment 8 Thomas Koenig 2007-09-30 21:03:23 UTC
I am currently trying to find the patch responsible
for fixing this.  This could indeed be Paul's
fix for PR 32298 and 31726.
Comment 9 Thomas Koenig 2007-10-02 18:09:12 UTC
I can confirm that

http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=125983

fixes the problem for 4.2.  As this is a particularly
bad (i.e. silent wrong-code) bug, I propose to commit
Paul's patch once 4.2 reopens.
Comment 10 Tobias Schlüter 2007-10-02 18:51:30 UTC
There are only two later patches to min/maxloc, namely those for PRs 33297 and 32954, which both seem unrelated.  So I agree that this should be safe to backport.
Comment 11 Thomas Koenig 2007-10-15 18:23:54 UTC
Subject: Bug 33354

Author: tkoenig
Date: Mon Oct 15 18:23:39 2007
New Revision: 129365

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=129365
Log:
2007-10-25  Thomas Koenig  <tkoenig@gcc.gnu.org>
	    Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/32298
	PR fortran/31726
	PR fortran/33354
	Backport from trunk
	* trans-intrinsic.c (gfc_conv_intrinsic_minmaxloc): Calculate
	the offset between the loop counter and the position as
	defined. Add the offset within the loop so that the mask acts
	correctly.  Do not advance the location on the basis that it
	is zero.

2007-10-15  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/33354
	* gfortran.dg/minmaxloc_4.f90:  New test.



Added:
    branches/gcc-4_2-branch/gcc/testsuite/gfortran.dg/minmaxloc_4.f90
Modified:
    branches/gcc-4_2-branch/gcc/fortran/ChangeLog
    branches/gcc-4_2-branch/gcc/fortran/trans-intrinsic.c
    branches/gcc-4_2-branch/gcc/testsuite/ChangeLog

Comment 12 Thomas Koenig 2007-10-15 18:24:17 UTC
Fixed.