public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jan Hubicka <hubicka@ucw.cz>
To: Richard Biener <rguenther@suse.de>
Cc: Jan Hubicka <hubicka@ucw.cz>, gcc-patches@gcc.gnu.org, burnus@net-b.de
Subject: Re: Fix more of C/fortran canonical type issues
Date: Sat, 10 Oct 2015 19:45:00 -0000	[thread overview]
Message-ID: <20151010194526.GB87460@kam.mff.cuni.cz> (raw)
In-Reply-To: <alpine.LSU.2.11.1510080937180.6516@zhemvz.fhfr.qr>

Hi,
this is a variant of patch I commited (adding the suggested predicate)

Honza

	* tree.c (type_with_interoperable_signedness): New.
	(gimple_canonical_types_compatible_p): Use it.
	* tree.h (type_with_interoperable_signedness): Declare

	* lto.c (hash_canonical_type): Honor
	type_with_interoperable_signedness.

	* gfortran.dg/lto/bind_c-2_0.f90: New testcase.
	* gfortran.dg/lto/bind_c-2_1.c: New testcase.
	* gfortran.dg/lto/bind_c-3_0.f90: New testcase.
	* gfortran.dg/lto/bind_c-3_1.c: New testcase.
	* gfortran.dg/lto/bind_c-4_0.f90: New testcase.
	* gfortran.dg/lto/bind_c-4_1.c: New testcase.
	* gfortran.dg/lto/bind_c-5_0.f90: New testcase.
	* gfortran.dg/lto/bind_c-5_1.c: New testcase.
Index: testsuite/gfortran.dg/lto/bind_c-2_1.c
===================================================================
--- testsuite/gfortran.dg/lto/bind_c-2_1.c	(revision 0)
+++ testsuite/gfortran.dg/lto/bind_c-2_1.c	(revision 0)
@@ -0,0 +1,36 @@
+#include <stdlib.h>
+/* interopse with myftype_1 */
+typedef struct {
+   unsigned char chr;
+   signed char chr2;
+} myctype_t;
+
+
+extern void abort(void);
+void types_test(void);
+/* declared in the fortran module */
+extern myctype_t myVar;
+
+int main(int argc, char **argv)
+{
+   myctype_t *cchr;
+   asm("":"=r"(cchr):"0"(&myVar));
+   cchr->chr = 1;
+   cchr->chr2 = 2;
+
+   types_test();
+
+   if(cchr->chr != 2)
+      abort();
+   if(cchr->chr2 != 2)
+      abort();
+   myVar.chr2 = 3;
+   types_test();
+
+   if(myVar.chr != 3)
+      abort();
+   if(myVar.chr2 != 3)
+      abort();
+   return 0;
+}
+
Index: testsuite/gfortran.dg/lto/bind_c-3_1.c
===================================================================
--- testsuite/gfortran.dg/lto/bind_c-3_1.c	(revision 0)
+++ testsuite/gfortran.dg/lto/bind_c-3_1.c	(revision 0)
@@ -0,0 +1,78 @@
+#include <stdlib.h>
+#include <stdint.h>
+/* interopse with myftype_1 */
+typedef struct {
+  int val1;
+  short int val2;
+  long int val3;
+  long long int val4;
+  size_t val5;
+  int8_t val6;
+  int16_t val7;
+  int32_t val8;
+  int64_t val9;
+  int_least8_t val10;
+  int_least16_t val11;
+  int_least32_t val12;
+  int_least64_t val13;
+  int_fast8_t val14;
+  int_fast16_t val15;
+  int_fast32_t val16;
+  int_fast64_t val17;
+  intmax_t val18;
+  intptr_t val19;
+} myctype_t;
+
+
+extern void abort(void);
+void types_test1(void);
+void types_test2(void);
+void types_test3(void);
+void types_test4(void);
+void types_test5(void);
+void types_test6(void);
+void types_test7(void);
+void types_test8(void);
+void types_test9(void);
+void types_test10(void);
+void types_test11(void);
+void types_test12(void);
+void types_test13(void);
+void types_test14(void);
+void types_test15(void);
+void types_test16(void);
+void types_test17(void);
+void types_test18(void);
+void types_test19(void);
+/* declared in the fortran module */
+extern myctype_t myVar;
+
+#define test(n)\
+  cchr->val##n = 1; types_test##n (); if (cchr->val##n != 2) abort ();
+
+int main(int argc, char **argv)
+{
+   myctype_t *cchr;
+   asm("":"=r"(cchr):"0"(&myVar));
+   test(1);
+   test(2);
+   test(3);
+   test(4);
+   test(5);
+   test(6);
+   test(7);
+   test(8);
+   test(9);
+   test(10);
+   test(11);
+   test(12);
+   test(13);
+   test(14);
+   test(15);
+   test(16);
+   test(17);
+   test(18);
+   test(19);
+   return 0;
+}
+
Index: testsuite/gfortran.dg/lto/bind_c-4_1.c
===================================================================
--- testsuite/gfortran.dg/lto/bind_c-4_1.c	(revision 0)
+++ testsuite/gfortran.dg/lto/bind_c-4_1.c	(revision 0)
@@ -0,0 +1,46 @@
+#include <stdlib.h>
+#include <stdint.h>
+/* interopse with myftype_1 */
+typedef struct {
+  float val1;
+  double val2;
+  long double val3;
+  float _Complex val4;
+  double _Complex val5;
+  long double _Complex val6;
+  _Bool val7;
+  /* FIXME: Fortran define c_char as array of size 1.
+     char val8;  */
+} myctype_t;
+
+
+extern void abort(void);
+void types_test1(void);
+void types_test2(void);
+void types_test3(void);
+void types_test4(void);
+void types_test5(void);
+void types_test6(void);
+void types_test7(void);
+void types_test8(void);
+/* declared in the fortran module */
+extern myctype_t myVar;
+
+#define test(n)\
+  cchr->val##n = 1; types_test##n (); if (cchr->val##n != 2) abort ();
+
+int main(int argc, char **argv)
+{
+   myctype_t *cchr;
+   asm("":"=r"(cchr):"0"(&myVar));
+   test(1);
+   test(2);
+   test(3);
+   test(4);
+   test(5);
+   test(6);
+   cchr->val7 = 0; types_test7 (); if (cchr->val7 != 1) abort ();
+   /*cchr->val8 = 0; types_test8 (); if (cchr->val8 != 'a') abort ();*/
+   return 0;
+}
+
Index: testsuite/gfortran.dg/lto/bind_c-5_1.c
===================================================================
--- testsuite/gfortran.dg/lto/bind_c-5_1.c	(revision 0)
+++ testsuite/gfortran.dg/lto/bind_c-5_1.c	(revision 0)
@@ -0,0 +1,31 @@
+#include <stdlib.h>
+/* declared in the fortran module */
+extern int (*myVar) (int);
+extern float (*myVar2) (float);
+void types_test(void);
+
+
+extern void abort(void);
+
+int main(int argc, char **argv)
+{
+   int (**myptr) (int);
+   float (**myptr2) (float);
+   asm("":"=r"(myptr):"0"(&myVar));
+   asm("":"=r"(myptr2):"0"(&myVar2));
+   *myptr = (int (*) (int)) (size_t) (void *)1;
+   *myptr2 = (float (*) (float)) (size_t) (void *)2;
+   types_test();
+   if (*myptr != (int (*) (int)) (size_t) (void *)2)
+	abort ();
+   if (*myptr2 != (float (*) (float)) (size_t) (void *)2)
+	abort ();
+   *myptr2 = (float (*) (float)) (size_t) (void *)3;
+   types_test();
+   if (*myptr != (int (*) (int)) (size_t) (void *)3)
+	abort ();
+   if (*myptr2 != (float (*) (float)) (size_t) (void *)3)
+	abort ();
+   return 0;
+}
+
Index: testsuite/gfortran.dg/lto/bind_c-2_0.f90
===================================================================
--- testsuite/gfortran.dg/lto/bind_c-2_0.f90	(revision 0)
+++ testsuite/gfortran.dg/lto/bind_c-2_0.f90	(revision 0)
@@ -0,0 +1,21 @@
+! { dg-lto-do run }
+! { dg-lto-options {{ -O3 -flto }} }
+! This testcase will abort if C_PTR is not interoperable with both int *
+! and float *
+module lto_type_merge_test
+  use, intrinsic :: iso_c_binding
+  implicit none
+
+  type, bind(c) :: MYFTYPE_1
+     integer(c_signed_char) :: chr
+     integer(c_signed_char) :: chrb
+  end type MYFTYPE_1
+
+  type(myftype_1), bind(c, name="myVar") :: myVar
+
+contains
+  subroutine types_test() bind(c)
+    myVar%chr = myVar%chrb
+  end subroutine types_test
+end module lto_type_merge_test
+
Index: testsuite/gfortran.dg/lto/bind_c-3_0.f90
===================================================================
--- testsuite/gfortran.dg/lto/bind_c-3_0.f90	(revision 0)
+++ testsuite/gfortran.dg/lto/bind_c-3_0.f90	(revision 0)
@@ -0,0 +1,91 @@
+! { dg-lto-do run }
+! { dg-lto-options {{ -O3 -flto }} }
+! This testcase will abort if integer types are not interoperable.
+module lto_type_merge_test
+  use, intrinsic :: iso_c_binding
+  implicit none
+
+  type, bind(c) :: MYFTYPE_1
+    integer(c_int) :: val_int
+    integer(c_short) :: val_short
+    integer(c_long) :: val_long
+    integer(c_long_long) :: val_long_long
+    integer(c_size_t) :: val_size_t
+    integer(c_int8_t) :: val_int8_t
+    integer(c_int16_t) :: val_int16_t
+    integer(c_int32_t) :: val_int32_t
+    integer(c_int64_t) :: val_int64_t
+    integer(c_int_least8_t) :: val_intleast_8_t
+    integer(c_int_least16_t) :: val_intleast_16_t
+    integer(c_int_least32_t) :: val_intleast_32_t
+    integer(c_int_least64_t) :: val_intleast_64_t
+    integer(c_int_fast8_t) :: val_intfast_8_t
+    integer(c_int_fast16_t) :: val_intfast_16_t
+    integer(c_int_fast32_t) :: val_intfast_32_t
+    integer(c_int_fast64_t) :: val_intfast_64_t
+    integer(c_intmax_t) :: val_intmax_t
+    integer(c_intptr_t) :: val_intptr_t
+  end type MYFTYPE_1
+
+  type(myftype_1), bind(c, name="myVar") :: myVar
+
+contains
+  subroutine types_test1() bind(c)
+    myVar%val_int = 2
+  end subroutine types_test1
+  subroutine types_test2() bind(c)
+    myVar%val_short = 2
+  end subroutine types_test2
+  subroutine types_test3() bind(c)
+    myVar%val_long = 2
+  end subroutine types_test3
+  subroutine types_test4() bind(c)
+    myVar%val_long_long = 2
+  end subroutine types_test4
+  subroutine types_test5() bind(c)
+    myVar%val_size_t = 2
+  end subroutine types_test5
+  subroutine types_test6() bind(c)
+    myVar%val_int8_t = 2
+  end subroutine types_test6
+  subroutine types_test7() bind(c)
+    myVar%val_int16_t = 2
+  end subroutine types_test7
+  subroutine types_test8() bind(c)
+    myVar%val_int32_t = 2
+  end subroutine types_test8
+  subroutine types_test9() bind(c)
+    myVar%val_int64_t = 2
+  end subroutine types_test9
+  subroutine types_test10() bind(c)
+    myVar%val_intleast_8_t = 2
+  end subroutine types_test10
+  subroutine types_test11() bind(c)
+    myVar%val_intleast_16_t = 2
+  end subroutine types_test11
+  subroutine types_test12() bind(c)
+    myVar%val_intleast_32_t = 2
+  end subroutine types_test12
+  subroutine types_test13() bind(c)
+    myVar%val_intleast_64_t = 2
+  end subroutine types_test13
+  subroutine types_test14() bind(c)
+    myVar%val_intfast_8_t = 2
+  end subroutine types_test14
+  subroutine types_test15() bind(c)
+    myVar%val_intfast_16_t = 2
+  end subroutine types_test15
+  subroutine types_test16() bind(c)
+    myVar%val_intfast_32_t = 2
+  end subroutine types_test16
+  subroutine types_test17() bind(c)
+    myVar%val_intfast_64_t = 2
+  end subroutine types_test17
+  subroutine types_test18() bind(c)
+    myVar%val_intmax_t = 2
+  end subroutine types_test18
+  subroutine types_test19() bind(c)
+    myVar%val_intptr_t = 2
+  end subroutine types_test19
+end module lto_type_merge_test
+
Index: testsuite/gfortran.dg/lto/bind_c-4_0.f90
===================================================================
--- testsuite/gfortran.dg/lto/bind_c-4_0.f90	(revision 0)
+++ testsuite/gfortran.dg/lto/bind_c-4_0.f90	(revision 0)
@@ -0,0 +1,48 @@
+! { dg-lto-do run }
+! { dg-lto-options {{ -O3 -flto }} }
+! This testcase will abort if real/complex/boolean/character types are not interoperable
+module lto_type_merge_test
+  use, intrinsic :: iso_c_binding
+  implicit none
+
+  type, bind(c) :: MYFTYPE_1
+    real(c_float) :: val_1
+    real(c_double) :: val_2
+    real(c_long_double) :: val_3
+    complex(c_float_complex) :: val_4
+    complex(c_double_complex) :: val_5
+    complex(c_long_double_complex) :: val_6
+    logical(c_bool) :: val_7
+    !FIXME: Fortran define c_char as array of size 1.
+    !character(c_char) :: val_8
+  end type MYFTYPE_1
+
+  type(myftype_1), bind(c, name="myVar") :: myVar
+
+contains
+  subroutine types_test1() bind(c)
+    myVar%val_1 = 2
+  end subroutine types_test1
+  subroutine types_test2() bind(c)
+    myVar%val_2 = 2
+  end subroutine types_test2
+  subroutine types_test3() bind(c)
+    myVar%val_3 = 2
+  end subroutine types_test3
+  subroutine types_test4() bind(c)
+    myVar%val_4 = 2
+  end subroutine types_test4
+  subroutine types_test5() bind(c)
+    myVar%val_5 = 2
+  end subroutine types_test5
+  subroutine types_test6() bind(c)
+    myVar%val_6 = 2
+  end subroutine types_test6
+  subroutine types_test7() bind(c)
+    myVar%val_7 = myVar%val_7 .or. .not. myVar%val_7
+  end subroutine types_test7
+  !subroutine types_test8() bind(c)
+    !myVar%val_8 = "a"
+  !end subroutine types_test8
+end module lto_type_merge_test
+
Index: testsuite/gfortran.dg/lto/bind_c-5_0.f90
===================================================================
--- testsuite/gfortran.dg/lto/bind_c-5_0.f90	(revision 0)
+++ testsuite/gfortran.dg/lto/bind_c-5_0.f90	(revision 0)
@@ -0,0 +1,17 @@
+! { dg-lto-do run }
+! { dg-lto-options {{ -O3 -flto }} }
+! This testcase will abort if C_FUNPTR is not interoperable with both int *
+! and float *
+module lto_type_merge_test
+  use, intrinsic :: iso_c_binding
+  implicit none
+
+  type(c_funptr), bind(c, name="myVar") :: myVar
+  type(c_funptr), bind(c, name="myVar2") :: myVar2
+
+contains
+  subroutine types_test() bind(c)
+    myVar = myVar2
+  end subroutine types_test
+end module lto_type_merge_test
+
Index: lto/lto.c
===================================================================
--- lto/lto.c	(revision 228625)
+++ lto/lto.c	(working copy)
@@ -288,6 +288,7 @@ static hashval_t
 hash_canonical_type (tree type)
 {
   inchash::hash hstate;
+  enum tree_code code;
 
   /* We compute alias sets only for types that needs them.
      Be sure we do not recurse to something else as we can not hash incomplete
@@ -299,7 +300,8 @@ hash_canonical_type (tree type)
      smaller sets; when searching for existing matching types to merge,
      only existing types having the same features as the new type will be
      checked.  */
-  hstate.add_int (tree_code_for_canonical_type_merging (TREE_CODE (type)));
+  code = tree_code_for_canonical_type_merging (TREE_CODE (type));
+  hstate.add_int (code);
   hstate.add_int (TYPE_MODE (type));
 
   /* Incorporate common features of numerical types.  */
@@ -309,8 +311,9 @@ hash_canonical_type (tree type)
       || TREE_CODE (type) == OFFSET_TYPE
       || POINTER_TYPE_P (type))
     {
-      hstate.add_int (TYPE_UNSIGNED (type));
       hstate.add_int (TYPE_PRECISION (type));
+      if (!type_with_interoperable_signedness (type))
+        hstate.add_int (TYPE_UNSIGNED (type));
     }
 
   if (VECTOR_TYPE_P (type))
Index: tree.c
===================================================================
--- tree.c	(revision 228625)
+++ tree.c	(working copy)
@@ -13012,6 +13012,23 @@ verify_type_variant (const_tree t, tree
    back to pointer-comparison of TYPE_CANONICAL for aggregates
    for example.  */
 
+/* Return true if TYPE_UNSIGNED of TYPE should be ignored for canonical
+   type calculation because we need to allow inter-operability between signed
+   and unsigned variants.  */
+
+bool
+type_with_interoperable_signedness (const_tree type)
+{
+  /* Fortran standard require C_SIGNED_CHAR to be interoperable with both
+     signed char and unsigned char.  Similarly fortran FE builds
+     C_SIZE_T as signed type, while C defines it unsigned.  */
+
+  return tree_code_for_canonical_type_merging (TREE_CODE (type))
+	   == INTEGER_TYPE
+         && (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node)
+	     || TYPE_PRECISION (type) == TYPE_PRECISION (size_type_node));
+}
+
 /* Return true iff T1 and T2 are structurally identical for what
    TBAA is concerned.  
    This function is used both by lto.c canonical type merging and by the
@@ -13062,8 +13079,8 @@ gimple_canonical_types_compatible_p (con
     return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
 
   /* Can't be the same type if the types don't have the same code.  */
-  if (tree_code_for_canonical_type_merging (TREE_CODE (t1))
-      != tree_code_for_canonical_type_merging (TREE_CODE (t2)))
+  enum tree_code code = tree_code_for_canonical_type_merging (TREE_CODE (t1));
+  if (code != tree_code_for_canonical_type_merging (TREE_CODE (t2)))
     return false;
 
   /* Qualifiers do not matter for canonical type comparison purposes.  */
@@ -13086,9 +13103,14 @@ gimple_canonical_types_compatible_p (con
       || TREE_CODE (t1) == OFFSET_TYPE
       || POINTER_TYPE_P (t1))
     {
-      /* Can't be the same type if they have different sign or precision.  */
-      if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
-	  || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
+      /* Can't be the same type if they have different recision.  */
+      if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
+	return false;
+
+      /* In some cases the signed and unsigned types are required to be
+	 inter-operable.  */
+      if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2)
+	  && !type_with_interoperable_signedness (t1))
 	return false;
 
       /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
Index: tree.h
===================================================================
--- tree.h	(revision 228625)
+++ tree.h	(working copy)
@@ -4609,6 +4609,7 @@ extern int tree_map_base_marked_p (const
 extern void DEBUG_FUNCTION verify_type (const_tree t);
 extern bool gimple_canonical_types_compatible_p (const_tree, const_tree,
 						 bool trust_type_canonical = true);
+extern bool type_with_interoperable_signedness (const_tree);
 /* Return simplified tree code of type that is used for canonical type merging.  */
 inline enum tree_code
 tree_code_for_canonical_type_merging (enum tree_code code)

  parent reply	other threads:[~2015-10-10 19:45 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-08  1:23 Jan Hubicka
2015-06-08  5:45 ` Jan Hubicka
2015-06-08  7:25   ` Jan Hubicka
2015-06-08 13:43     ` Richard Biener
2015-06-08 14:07       ` Joseph Myers
2015-06-08 14:32         ` Richard Biener
2015-06-08 14:44           ` Joseph Myers
2015-06-08 14:52             ` Jan Hubicka
2015-06-08 14:54               ` Richard Biener
2015-06-08 15:11                 ` Jan Hubicka
2015-06-08 15:32                   ` Fortran's C_CHAR type Jan Hubicka
2015-06-10 11:50                     ` Mikael Morin
2015-06-10 14:55                       ` Jan Hubicka
2015-06-10 16:37                         ` Mikael Morin
2015-06-11 18:19                           ` Jan Hubicka
2015-06-09  9:50                   ` Fix more of C/fortran canonical type issues Richard Biener
2015-06-09 17:24                     ` Jan Hubicka
2015-06-11 17:58                       ` Jan Hubicka
2015-06-22  7:25                       ` Jan Hubicka
2015-06-22 15:09                         ` Richard Biener
2015-06-22 16:17                           ` Jan Hubicka
2015-06-08 15:08       ` Jan Hubicka
2015-06-08 16:54         ` Joseph Myers
2015-06-08 16:57           ` Jan Hubicka
2015-06-08 17:03             ` Joseph Myers
2015-06-08 22:06               ` Jan Hubicka
2015-06-08 23:01       ` Jan Hubicka
2015-10-08  3:47     ` Jan Hubicka
2015-10-08  7:44       ` Richard Biener
2015-10-08 16:17         ` Jan Hubicka
2015-10-10 19:45         ` Jan Hubicka [this message]
2015-06-08 13:37   ` Richard Biener
2015-10-11  8:09 Dominique d'Humières
2015-10-12  7:10 ` Jan Hubicka
2015-10-12  7:41   ` Richard Biener

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20151010194526.GB87460@kam.mff.cuni.cz \
    --to=hubicka@ucw.cz \
    --cc=burnus@net-b.de \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=rguenther@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).