From patchwork Thu Sep 13 12:35:43 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [Fortran] PR54556 - fix (4.6/4.7/) 4.8 regression: wrong code with implicit_pure procedures Date: Thu, 13 Sep 2012 02:35:43 -0000 From: Tobias Burnus X-Patchwork-Id: 183621 Message-Id: <5051D31F.8090408@net-b.de> To: gcc patches , gfortran gfortran wrongly marks some procedures as implicit_pure which aren't pure. implicit_pure exists since 2011-01-08 (= GCC 4.6), but was only used internally (FE optimization and trans*.c to avoid temporaries). Since 2012-08-28, implicit_pure also implies DECL_PURE_P. The later change exposes a bug and miscompiles CP2K. The reason for the the bug is that gfc_impure_variable() checks at some point whether it is invoked in a PURE procedure. For implicit_pure procedures, the answer is no - thus that check never triggered. I have now removed the check - the callee already takes care of that. (Which is also implied by the function name.) I additionally allow VALUE for implicit_pure. That's in line with PURE where VALUE is allowed since Fortran 2008. (I think since F2008's first technical corriendum.) Build and regtested on x86-64-gnu-linux. OK for the trunk and for the 4.6/4.7 branches? Tobias, who hopes that no additional implicit_pure bugs exist. 2012-09-13 Tobias Burnus PR fortran/54556 * resolve.c (resolve_formal_arglist): Allow VALUE arguments with implicit_pure. (gfc_impure_variable): Don't check gfc_pure such that the function also works for gfc_implicit_pure procedures. 2012-09-13 Tobias Burnus PR fortran/54556 * gfortran.dg/implicit_pure_3.f90: New. diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c index 28eea5d..0748b6a 100644 --- a/gcc/fortran/resolve.c +++ b/gcc/fortran/resolve.c @@ -426,10 +426,12 @@ resolve_formal_arglist (gfc_symbol *proc) } else if (!sym->attr.pointer) { - if (proc->attr.function && sym->attr.intent != INTENT_IN) + if (proc->attr.function && sym->attr.intent != INTENT_IN + && !sym->value) proc->attr.implicit_pure = 0; - if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN) + if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN + && !sym->value) proc->attr.implicit_pure = 0; } } @@ -13565,10 +13567,9 @@ gfc_impure_variable (gfc_symbol *sym) } proc = sym->ns->proc_name; - if (sym->attr.dummy && gfc_pure (proc) - && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN) - || - proc->attr.function)) + if (sym->attr.dummy + && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN) + || proc->attr.function)) return 1; /* TODO: Sort out what can be storage associated, if anything, and include --- /dev/null 2012-09-13 07:07:28.691771313 +0200 +++ gcc/gcc/testsuite/gfortran.dg/implicit_pure_3.f90 2012-09-13 14:17:18.000000000 +0200 @@ -0,0 +1,109 @@ +! { dg-do compile } +! { dg-options "-O2 -fdump-tree-optimized" } +! +! PR fortran/54556 +! +! Contributed by Joost VandeVondele +! +MODULE parallel_rng_types + + IMPLICIT NONE + + ! Global parameters in this module + INTEGER, PARAMETER :: dp=8 + + TYPE rng_stream_type + PRIVATE + CHARACTER(LEN=40) :: name + INTEGER :: distribution_type + REAL(KIND=dp), DIMENSION(3,2) :: bg,cg,ig + LOGICAL :: antithetic,extended_precision + REAL(KIND=dp) :: buffer + LOGICAL :: buffer_filled + END TYPE rng_stream_type + + REAL(KIND=dp), DIMENSION(3,3) :: a1p0,a1p76,a1p127,& + a2p0,a2p76,a2p127,& + inv_a1,inv_a2 + + INTEGER, PARAMETER :: GAUSSIAN = 1,& + UNIFORM = 2 + + REAL(KIND=dp), PARAMETER :: norm = 2.328306549295727688e-10_dp,& + m1 = 4294967087.0_dp,& + m2 = 4294944443.0_dp,& + a12 = 1403580.0_dp,& + a13n = 810728.0_dp,& + a21 = 527612.0_dp,& + a23n = 1370589.0_dp,& + two17 = 131072.0_dp,& ! 2**17 + two53 = 9007199254740992.0_dp,& ! 2**53 + fact = 5.9604644775390625e-8_dp ! 1/2**24 + + +CONTAINS + + FUNCTION rn32(rng_stream) RESULT(u) + + TYPE(rng_stream_type), POINTER :: rng_stream + REAL(KIND=dp) :: u + + INTEGER :: k + REAL(KIND=dp) :: p1, p2 + +! ------------------------------------------------------------------------- +! Component 1 + + p1 = a12*rng_stream%cg(2,1) - a13n*rng_stream%cg(1,1) + k = INT(p1/m1) + p1 = p1 - k*m1 + IF (p1 < 0.0_dp) p1 = p1 + m1 + rng_stream%cg(1,1) = rng_stream%cg(2,1) + rng_stream%cg(2,1) = rng_stream%cg(3,1) + rng_stream%cg(3,1) = p1 + + ! Component 2 + + p2 = a21*rng_stream%cg(3,2) - a23n*rng_stream%cg(1,2) + k = INT(p2/m2) + p2 = p2 - k*m2 + IF (p2 < 0.0_dp) p2 = p2 + m2 + rng_stream%cg(1,2) = rng_stream%cg(2,2) + rng_stream%cg(2,2) = rng_stream%cg(3,2) + rng_stream%cg(3,2) = p2 + + ! Combination + + IF (p1 > p2) THEN + u = (p1 - p2)*norm + ELSE + u = (p1 - p2 + m1)*norm + END IF + + IF (rng_stream%antithetic) u = 1.0_dp - u + + END FUNCTION rn32 + +! ***************************************************************************** + FUNCTION rn53(rng_stream) RESULT(u) + + TYPE(rng_stream_type), POINTER :: rng_stream + REAL(KIND=dp) :: u + + u = rn32(rng_stream) + + IF (rng_stream%antithetic) THEN + u = u + (rn32(rng_stream) - 1.0_dp)*fact + IF (u < 0.0_dp) u = u + 1.0_dp + ELSE + u = u + rn32(rng_stream)*fact + IF (u >= 1.0_dp) u = u - 1.0_dp + END IF + + END FUNCTION rn53 + +END MODULE + +! { dg-final { scan-module-absence "parallel_rng_types" "IMPLICIT_PURE" } } +! { dg-final { scan-tree-dump-times "rn32 \\(rng_stream" 3 "optimized" } } +! { dg-final { cleanup-tree-dump "optimized" } }