public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Cupertino Miranda <cupertino.miranda@oracle.com>
To: gcc-patches@gcc.gnu.org
Cc: jose.marchesi@oracle.com, david.faust@oracle.com,
	elena.zannoni@oracle.com, indu.bhagat@oracle.com,
	Cupertino Miranda <cupertino.miranda@oracle.com>
Subject: [PATCH] ctf: Fix multi-dimentional array types ordering in CTF
Date: Wed, 28 Feb 2024 15:03:41 +0000	[thread overview]
Message-ID: <20240228150341.54713-1-cupertino.miranda@oracle.com> (raw)

Hi everyone,

In order to facilitate reviewing, I include a copy of the function in
this email, since the code structure changes are too hard to analyse
in the patch itself.

Looking forward to your comments.

Regards,
Cupertino

=== Function changes ===

/* Generate CTF for an ARRAY_TYPE.
   C argument is used as the iterator for the recursive calls to
   gen_ctf_array_type. C is the current die within the recursion.
   When C is NULL, it means it is the first call to gen_ctf_array_type.
   C should always be NULL when called from other functions.  */

static ctf_id_t
gen_ctf_array_type (ctf_container_ref ctfc,
		    dw_die_ref array_type,
		    dw_die_ref c = NULL)
{
  int vector_type_p = get_AT_flag (array_type, DW_AT_GNU_vector);
  if (vector_type_p)
    return CTF_NULL_TYPEID;

  if (c == dw_get_die_child (array_type))
    {
      dw_die_ref array_elems_type = ctf_get_AT_type (array_type);
      return gen_ctf_type (ctfc, array_elems_type);
    }
  else
    {
      ctf_arinfo_t arinfo;
      ctf_id_t array_node_type_id = CTF_NULL_TYPEID;
      if (c == NULL)
	c = dw_get_die_child (array_type);

      c = dw_get_die_sib (c);

      ctf_id_t child_id = gen_ctf_array_type (ctfc, array_type, c);

      dw_die_ref array_index_type;
      uint32_t array_num_elements;

      if (dw_get_die_tag (c) == DW_TAG_subrange_type)
	{
	  dw_attr_node *upper_bound_at;

	  array_index_type = ctf_get_AT_type (c);

	  /* When DW_AT_upper_bound is used to specify the size of an
	     array in DWARF, it is usually an unsigned constant
	     specifying the upper bound index of the array.  However,
	     for unsized arrays, such as foo[] or bar[0],
	     DW_AT_upper_bound is a signed integer constant
	     instead.  */

	  upper_bound_at = get_AT (c, DW_AT_upper_bound);
	  if (upper_bound_at
	      && AT_class (upper_bound_at) == dw_val_class_unsigned_const)
	    /* This is the upper bound index.  */
	    array_num_elements = get_AT_unsigned (c, DW_AT_upper_bound) + 1;
	  else if (get_AT (c, DW_AT_count))
	    array_num_elements = get_AT_unsigned (c, DW_AT_count);
	  else
	    {
	      /* This is a VLA of some kind.  */
	      array_num_elements = 0;
	    }
	}
      else if (dw_get_die_tag (c) == DW_TAG_enumeration_type)
	{
	  array_index_type = 0;
	  array_num_elements = 0;
	  /* XXX writeme. */
	  gcc_assert (1);
	}
      else
	gcc_unreachable ();

      /* Ok, mount and register the array type.  Note how the array
	 type we register here is the type of the elements in
	 subsequent "dimensions", if there are any.  */

      arinfo.ctr_nelems = array_num_elements;
      if (array_index_type)
	arinfo.ctr_index = gen_ctf_type (ctfc, array_index_type);
      else
	arinfo.ctr_index = gen_ctf_type (ctfc, ctf_array_index_die);

      arinfo.ctr_contents = child_id;
      if (!ctf_type_exists (ctfc, c, &array_node_type_id))
	array_node_type_id = ctf_add_array (ctfc, CTF_ADD_ROOT, &arinfo,
					    c);
      return array_node_type_id;
    }
}

=== The patch ===

Multi-dimentional array types would be linked in reverse order to the
expected C standard ordering. In other words, CTF would define the type
of "char [a][b][c]" as if it was an array of "char [c][b][a]"
dimentions.

gcc/ChangeLog:

	* dwarf2ctf.cc (gen_ctf_array_type): Correct order in which CTF
	multi-dimentional array types are linked.

gcc/testsuite/ChangeLog:
	* gcc.dg/debug/ctf/ctf-array-6.c: Add test.
---
 gcc/dwarf2ctf.cc                             | 67 ++++++++------------
 gcc/testsuite/gcc.dg/debug/ctf/ctf-array-6.c | 14 ++++
 2 files changed, 41 insertions(+), 40 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/debug/ctf/ctf-array-6.c

diff --git a/gcc/dwarf2ctf.cc b/gcc/dwarf2ctf.cc
index 93e5619933fa..95ceca196217 100644
--- a/gcc/dwarf2ctf.cc
+++ b/gcc/dwarf2ctf.cc
@@ -349,42 +349,40 @@ gen_ctf_pointer_type (ctf_container_ref ctfc, dw_die_ref ptr_type)
   return ptr_type_id;
 }
 
-/* Generate CTF for an array type.  */
+/* Generate CTF for an ARRAY_TYPE.
+   C argument is used as the iterator for the recursive calls to
+   gen_ctf_array_type. C is the current die within the recursion.
+   When C is NULL, it means it is the first call to gen_ctf_array_type.
+   C should always be NULL when called from other functions.  */
 
 static ctf_id_t
-gen_ctf_array_type (ctf_container_ref ctfc, dw_die_ref array_type)
+gen_ctf_array_type (ctf_container_ref ctfc,
+		    dw_die_ref array_type,
+		    dw_die_ref c = NULL)
 {
-  dw_die_ref c;
-  ctf_id_t array_elems_type_id = CTF_NULL_TYPEID;
-
   int vector_type_p = get_AT_flag (array_type, DW_AT_GNU_vector);
   if (vector_type_p)
-    return array_elems_type_id;
-
-  dw_die_ref array_elems_type = ctf_get_AT_type (array_type);
+    return CTF_NULL_TYPEID;
 
-  /* First, register the type of the array elements if needed.  */
-  array_elems_type_id = gen_ctf_type (ctfc, array_elems_type);
+  if (c == dw_get_die_child (array_type))
+    {
+      dw_die_ref array_elems_type = ctf_get_AT_type (array_type);
+      return gen_ctf_type (ctfc, array_elems_type);
+    }
+  else
+    {
+      ctf_arinfo_t arinfo;
+      ctf_id_t array_node_type_id = CTF_NULL_TYPEID;
+      if (c == NULL)
+	c = dw_get_die_child (array_type);
 
-  /* DWARF array types pretend C supports multi-dimensional arrays.
-     So for the type int[N][M], the array type DIE contains two
-     subrange_type children, the first with upper bound N-1 and the
-     second with upper bound M-1.
+      c = dw_get_die_sib (c);
 
-     CTF, on the other hand, just encodes each array type in its own
-     array type CTF struct.  Therefore we have to iterate on the
-     children and create all the needed types.  */
+      ctf_id_t child_id = gen_ctf_array_type (ctfc, array_type, c);
 
-  c = dw_get_die_child (array_type);
-  gcc_assert (c);
-  do
-    {
-      ctf_arinfo_t arinfo;
       dw_die_ref array_index_type;
       uint32_t array_num_elements;
 
-      c = dw_get_die_sib (c);
-
       if (dw_get_die_tag (c) == DW_TAG_subrange_type)
 	{
 	  dw_attr_node *upper_bound_at;
@@ -431,23 +429,12 @@ gen_ctf_array_type (ctf_container_ref ctfc, dw_die_ref array_type)
       else
 	arinfo.ctr_index = gen_ctf_type (ctfc, ctf_array_index_die);
 
-      arinfo.ctr_contents = array_elems_type_id;
-      if (!ctf_type_exists (ctfc, c, &array_elems_type_id))
-	array_elems_type_id = ctf_add_array (ctfc, CTF_ADD_ROOT, &arinfo,
-					     c);
+      arinfo.ctr_contents = child_id;
+      if (!ctf_type_exists (ctfc, c, &array_node_type_id))
+	array_node_type_id = ctf_add_array (ctfc, CTF_ADD_ROOT, &arinfo,
+					    c);
+      return array_node_type_id;
     }
-  while (c != dw_get_die_child (array_type));
-
-#if 0
-  /* Type de-duplication.
-     Consult the ctfc_types hash again before adding the CTF array type because
-     there can be cases where an array_type type may have been added by the
-     gen_ctf_type call above.  */
-  if (!ctf_type_exists (ctfc, array_type, &type_id))
-    type_id = ctf_add_array (ctfc, CTF_ADD_ROOT, &arinfo, array_type);
-#endif
-
-  return array_elems_type_id;
 }
 
 /* Generate CTF for a typedef.  */
diff --git a/gcc/testsuite/gcc.dg/debug/ctf/ctf-array-6.c b/gcc/testsuite/gcc.dg/debug/ctf/ctf-array-6.c
new file mode 100644
index 000000000000..9391d6ba2eb5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/ctf/ctf-array-6.c
@@ -0,0 +1,14 @@
+/* CTF generation for multidimentional array.  */
+
+/* { dg-do compile )  */
+/* { dg-options "-O0 -gctf -dA" } */
+
+/* { dg-final { scan-assembler-times "\[\t \]+0x2\[\t \]+\[^\n\]*cta_nelems" 1 } } */
+/* { dg-final { scan-assembler-times "\[\t \]+0x3\[\t \]+\[^\n\]*cta_nelems" 1 } } */
+/* { dg-final { scan-assembler-times "\[\t \]+0x4\[\t \]+\[^\n\]*cta_nelems" 1 } } */
+
+/* { dg-final { scan-assembler-times "\t.4byte\t0x1\[\t \]+# cta_contents\[\\r\\n\]+\t.4byte\t0x2\[\t \]+# cta_index\[\\r\\n\]+\t.4byte\t0x4\[\t \]+# cta_nelems" 1 } } */
+/* { dg-final { scan-assembler-times "\t.4byte\t0x3\[\t \]+# cta_contents\[\\r\\n\]+\t.4byte\t0x2\[\t \]+# cta_index\[\\r\\n\]+\t.4byte\t0x3\[\t \]+# cta_nelems" 1 } } */
+/* { dg-final { scan-assembler-times "\t.4byte\t0x4\[\t \]+# cta_contents\[\\r\\n\]+\t.4byte\t0x2\[\t \]+# cta_index\[\\r\\n\]+\t.4byte\t0x2\[\t \]+# cta_nelems" 1 } } */
+
+int a[2][3][4];
-- 
2.30.2


             reply	other threads:[~2024-02-28 15:03 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-28 15:03 Cupertino Miranda [this message]
2024-03-04 18:00 ` [PATCH,V2] ctf: fix incorrect CTF for multi-dimensional array types Indu Bhagat
2024-03-04 19:46   ` David Faust
2024-03-05  8:47   ` [PATCH, V3] " Indu Bhagat
2024-03-05 17:41     ` David Faust

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=20240228150341.54713-1-cupertino.miranda@oracle.com \
    --to=cupertino.miranda@oracle.com \
    --cc=david.faust@oracle.com \
    --cc=elena.zannoni@oracle.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=indu.bhagat@oracle.com \
    --cc=jose.marchesi@oracle.com \
    /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).