http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53667
Bug #: 53667 Summary: Cray pointer: Wrong result with optimizations Classification: Unclassified Product: gcc Version: 4.8.0 Status: UNCONFIRMED Keywords: wrong-code Severity: normal Priority: P3 Component: fortran AssignedTo: unassig...@gcc.gnu.org ReportedBy: bur...@gcc.gnu.org Cf. thread at http://gcc.gnu.org/ml/fortran/2012-06/msg00077.html For the program at http://gcc.gnu.org/ml/fortran/2012-06/txt00004.txt, GCC generates (original dump) the code: object_holder_init (&object_holder); set_vals (&object_holder); print_vals (&object_holder); When using -O2, the latter becomes (optimized dump): object_holder_init (&object_holder); print_vals (&object_holder); object_holder ={v} {CLOBBER}; Both set_vals and print_vals contains code of the form: subroutine set_vals(oh) implicit none integer*8, intent(inout):: oh integer*8 :: obj(3) pointer(pobj, obj) pobj = oh which translates into: set_vals (integer(kind=8) & restrict oh) { integer(kind=8) pobj; integer(kind=8) obj[3] [value-expr: *(integer(kind=8)[3] *) pobj]; pobj = *oh; If one marks "print_val"'s dummy argument "oh" as "target" (and, hence, removes the "restrict"), GCC inlines "set_val" and the result is correct. The program also works (at any optimization level) with -fno-inline-small-functions. Or if all functions are inlined via -fwhole-program. It also works if one has a Cray-pointer dummy argument at: subroutine print_vals(pobj) integer*8 :: obj(3) pointer(pobj, obj) ... call free(pobj) * * * Related issue: Currently, gfortran calls *oh = _gfortran_malloc (&C.1885); ... _gfortran_free ((integer(kind=8) *) oh); (Cf. iresolve.c.) If one looks into libgfortran/intrinsics/malloc.c, it uses the trivial implementation. But then a simple BUILT_IN_MALLOC / BUILT_IN_FREE would do!