From 4ed0f84e48a1806da7e046dc226ae1997c6fcfb9 Mon Sep 17 00:00:00 2001 From: Thomas Koenig Date: Sun, 10 Mar 2019 11:26:25 +0000 Subject: [PATCH] re PR fortran/71544 (gfortran compiler optimization bug when dealing with c-style pointers) 2019-03-10 Thomas Koenig PR fortran/71544 Backport from trunk * trans-types.c (gfc_typenode_for_spec) Set ts->is_c_interop of C_PTR and C_FUNPTR. (create_fn_spec): Mark argument as escaping if ts->is_c_interop is set. 2019-03-10 Thomas Koenig PR fortran/71544 Backport from trunk * gfortran.dg/c_ptr_tests_19.f90: New test. From-SVN: r269552 --- gcc/fortran/ChangeLog | 8 +++++ gcc/fortran/trans-types.c | 6 ++-- gcc/testsuite/ChangeLog | 6 ++++ gcc/testsuite/gfortran.dg/c_ptr_tests_19.f90 | 36 ++++++++++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/c_ptr_tests_19.f90 diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index 12fd22b974b8..1346f4a5d36c 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,11 @@ +2019-03-10 Thomas Koenig + + PR fortran/71544 + Backport from trunk + * trans-types.c (gfc_typenode_for_spec) Set ts->is_c_interop of + C_PTR and C_FUNPTR. + (create_fn_spec): Mark argument as escaping if ts->is_c_interop is set. + 2019-03-10 Thomas Koenig Steven G. Kargl diff --git a/gcc/fortran/trans-types.c b/gcc/fortran/trans-types.c index 6f8bcfdaaca1..385be8a2c97d 100644 --- a/gcc/fortran/trans-types.c +++ b/gcc/fortran/trans-types.c @@ -1139,7 +1139,8 @@ gfc_typenode_for_spec (gfc_typespec * spec, int codim) { spec->type = BT_INTEGER; spec->kind = gfc_index_integer_kind; - spec->f90_type = BT_VOID; + spec->f90_type = BT_VOID; + spec->is_c_interop = 1; /* Mark as escaping later. */ } break; case BT_VOID: @@ -2884,7 +2885,8 @@ create_fn_spec (gfc_symbol *sym, tree fntype) || f->sym->ts.u.derived->attr.pointer_comp)) || (f->sym->ts.type == BT_CLASS && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp - || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp))) + || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp)) + || (f->sym->ts.type == BT_INTEGER && f->sym->ts.is_c_interop)) spec[spec_len++] = '.'; else if (f->sym->attr.intent == INTENT_IN) spec[spec_len++] = 'r'; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index a878fd58fab7..c7bfd6d27bd9 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2019-03-10 Thomas Koenig + + PR fortran/71544 + Backport from trunk + * gfortran.dg/c_ptr_tests_19.f90: New test. + 2019-03-10 Thomas Koenig PR fortran/87734 diff --git a/gcc/testsuite/gfortran.dg/c_ptr_tests_19.f90 b/gcc/testsuite/gfortran.dg/c_ptr_tests_19.f90 new file mode 100644 index 000000000000..2cb0b183c88a --- /dev/null +++ b/gcc/testsuite/gfortran.dg/c_ptr_tests_19.f90 @@ -0,0 +1,36 @@ +! { dg-do run } + +! PR 71544 - this failed with some optimization options due to a +! pointer not being marked as escaping. + +module store_cptr + use, intrinsic :: iso_c_binding + implicit none + public + type(c_ptr), save :: cptr +end module store_cptr + +subroutine init() + use, intrinsic :: iso_c_binding + implicit none + integer(c_int), pointer :: a + allocate(a) + call save_cptr(c_loc(a)) + a = 100 +end subroutine init + +subroutine save_cptr(cptr_in) + use store_cptr + implicit none + type(c_ptr), intent(in) :: cptr_in + cptr = cptr_in +end subroutine save_cptr + +program init_fails + use store_cptr + implicit none + integer(c_int), pointer :: val + call init() + call c_f_pointer(cptr,val) + if (val /= 100) stop 1 +end program init_fails -- 2.47.2