public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] stor-layout: Create DECL_BIT_FIELD_REPRESENTATIVE even for bitfields in unions [PR101062]
@ 2021-06-16  7:45 Jakub Jelinek
  2021-06-16  9:54 ` Richard Biener
  2021-06-18  8:30 ` [PATCH] stor-layout: Don't create DECL_BIT_FIELD_REPRESENTATIVE for QUAL_UNION_TYPE [PR101062] Jakub Jelinek
  0 siblings, 2 replies; 4+ messages in thread
From: Jakub Jelinek @ 2021-06-16  7:45 UTC (permalink / raw)
  To: Richard Biener; +Cc: gcc-patches

Hi!

The following testcase is miscompiled on x86_64-linux, the bitfield store
is implemented as a RMW 64-bit operation at d+24 when the d variable has
size of only 28 bytes and scheduling moves in between the R and W part
a store to a different variable that happens to be right after the d
variable.

The reason for this is that we weren't creating
DECL_BIT_FIELD_REPRESENTATIVEs for bitfields in unions.

The following patch does create them, but treats all such bitfields as if
they were in a structure where the particular bitfield is the only field.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2021-06-16  Jakub Jelinek  <jakub@redhat.com>

	PR middle-end/101062
	* stor-layout.c (finish_bitfield_representative): For fields in unions
	assume nextf is always NULL.
	(finish_bitfield_layout): Compute bit field representatives also in
	unions, but handle it as if each bitfield was the only field in the
	aggregate.

	* gcc.dg/pr101062.c: New test.

--- gcc/stor-layout.c.jj	2021-03-30 18:11:52.537092233 +0200
+++ gcc/stor-layout.c	2021-06-15 10:58:59.244353965 +0200
@@ -2072,9 +2072,14 @@ finish_bitfield_representative (tree rep
   bitsize = (bitsize + BITS_PER_UNIT - 1) & ~(BITS_PER_UNIT - 1);
 
   /* Now nothing tells us how to pad out bitsize ...  */
-  nextf = DECL_CHAIN (field);
-  while (nextf && TREE_CODE (nextf) != FIELD_DECL)
-    nextf = DECL_CHAIN (nextf);
+  if (TREE_CODE (DECL_CONTEXT (field)) == RECORD_TYPE)
+    {
+      nextf = DECL_CHAIN (field);
+      while (nextf && TREE_CODE (nextf) != FIELD_DECL)
+	nextf = DECL_CHAIN (nextf);
+    }
+  else
+    nextf = NULL_TREE;
   if (nextf)
     {
       tree maxsize;
@@ -2167,13 +2172,6 @@ finish_bitfield_layout (tree t)
   tree field, prev;
   tree repr = NULL_TREE;
 
-  /* Unions would be special, for the ease of type-punning optimizations
-     we could use the underlying type as hint for the representative
-     if the bitfield would fit and the representative would not exceed
-     the union in size.  */
-  if (TREE_CODE (t) != RECORD_TYPE)
-    return;
-
   for (prev = NULL_TREE, field = TYPE_FIELDS (t);
        field; field = DECL_CHAIN (field))
     {
@@ -2233,7 +2231,13 @@ finish_bitfield_layout (tree t)
       if (repr)
 	DECL_BIT_FIELD_REPRESENTATIVE (field) = repr;
 
-      prev = field;
+      if (TREE_CODE (t) == RECORD_TYPE)
+	prev = field;
+      else if (repr)
+	{
+	  finish_bitfield_representative (repr, field);
+	  repr = NULL_TREE;
+	}
     }
 
   if (repr)
--- gcc/testsuite/gcc.dg/pr101062.c.jj	2021-06-15 10:42:58.642919880 +0200
+++ gcc/testsuite/gcc.dg/pr101062.c	2021-06-15 10:42:40.897171191 +0200
@@ -0,0 +1,29 @@
+/* PR middle-end/101062 */
+/* { dg-do run } */
+/* { dg-options "-O2 -fno-toplevel-reorder -frename-registers" } */
+
+union U { signed b : 5; };
+int c;
+volatile union U d[7] = { { 8 } };
+short e = 1;
+
+__attribute__((noipa)) void
+foo ()
+{
+  d[6].b = 0;
+  d[6].b = 0;
+  d[6].b = 0;
+  d[6].b = 0;
+  d[6].b = 0;
+  e = 0;
+  c = 0;
+}
+
+int
+main ()
+{
+  foo ();
+  if (e != 0)
+    __builtin_abort ();
+  return 0;
+}

	Jakub


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] stor-layout: Create DECL_BIT_FIELD_REPRESENTATIVE even for bitfields in unions [PR101062]
  2021-06-16  7:45 [PATCH] stor-layout: Create DECL_BIT_FIELD_REPRESENTATIVE even for bitfields in unions [PR101062] Jakub Jelinek
@ 2021-06-16  9:54 ` Richard Biener
  2021-06-18  8:30 ` [PATCH] stor-layout: Don't create DECL_BIT_FIELD_REPRESENTATIVE for QUAL_UNION_TYPE [PR101062] Jakub Jelinek
  1 sibling, 0 replies; 4+ messages in thread
From: Richard Biener @ 2021-06-16  9:54 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches

On Wed, 16 Jun 2021, Jakub Jelinek wrote:

> Hi!
> 
> The following testcase is miscompiled on x86_64-linux, the bitfield store
> is implemented as a RMW 64-bit operation at d+24 when the d variable has
> size of only 28 bytes and scheduling moves in between the R and W part
> a store to a different variable that happens to be right after the d
> variable.
> 
> The reason for this is that we weren't creating
> DECL_BIT_FIELD_REPRESENTATIVEs for bitfields in unions.
> 
> The following patch does create them, but treats all such bitfields as if
> they were in a structure where the particular bitfield is the only field.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK.

Thanks,
Richard.

> 2021-06-16  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR middle-end/101062
> 	* stor-layout.c (finish_bitfield_representative): For fields in unions
> 	assume nextf is always NULL.
> 	(finish_bitfield_layout): Compute bit field representatives also in
> 	unions, but handle it as if each bitfield was the only field in the
> 	aggregate.
> 
> 	* gcc.dg/pr101062.c: New test.
> 
> --- gcc/stor-layout.c.jj	2021-03-30 18:11:52.537092233 +0200
> +++ gcc/stor-layout.c	2021-06-15 10:58:59.244353965 +0200
> @@ -2072,9 +2072,14 @@ finish_bitfield_representative (tree rep
>    bitsize = (bitsize + BITS_PER_UNIT - 1) & ~(BITS_PER_UNIT - 1);
>  
>    /* Now nothing tells us how to pad out bitsize ...  */
> -  nextf = DECL_CHAIN (field);
> -  while (nextf && TREE_CODE (nextf) != FIELD_DECL)
> -    nextf = DECL_CHAIN (nextf);
> +  if (TREE_CODE (DECL_CONTEXT (field)) == RECORD_TYPE)
> +    {
> +      nextf = DECL_CHAIN (field);
> +      while (nextf && TREE_CODE (nextf) != FIELD_DECL)
> +	nextf = DECL_CHAIN (nextf);
> +    }
> +  else
> +    nextf = NULL_TREE;
>    if (nextf)
>      {
>        tree maxsize;
> @@ -2167,13 +2172,6 @@ finish_bitfield_layout (tree t)
>    tree field, prev;
>    tree repr = NULL_TREE;
>  
> -  /* Unions would be special, for the ease of type-punning optimizations
> -     we could use the underlying type as hint for the representative
> -     if the bitfield would fit and the representative would not exceed
> -     the union in size.  */
> -  if (TREE_CODE (t) != RECORD_TYPE)
> -    return;
> -
>    for (prev = NULL_TREE, field = TYPE_FIELDS (t);
>         field; field = DECL_CHAIN (field))
>      {
> @@ -2233,7 +2231,13 @@ finish_bitfield_layout (tree t)
>        if (repr)
>  	DECL_BIT_FIELD_REPRESENTATIVE (field) = repr;
>  
> -      prev = field;
> +      if (TREE_CODE (t) == RECORD_TYPE)
> +	prev = field;
> +      else if (repr)
> +	{
> +	  finish_bitfield_representative (repr, field);
> +	  repr = NULL_TREE;
> +	}
>      }
>  
>    if (repr)
> --- gcc/testsuite/gcc.dg/pr101062.c.jj	2021-06-15 10:42:58.642919880 +0200
> +++ gcc/testsuite/gcc.dg/pr101062.c	2021-06-15 10:42:40.897171191 +0200
> @@ -0,0 +1,29 @@
> +/* PR middle-end/101062 */
> +/* { dg-do run } */
> +/* { dg-options "-O2 -fno-toplevel-reorder -frename-registers" } */
> +
> +union U { signed b : 5; };
> +int c;
> +volatile union U d[7] = { { 8 } };
> +short e = 1;
> +
> +__attribute__((noipa)) void
> +foo ()
> +{
> +  d[6].b = 0;
> +  d[6].b = 0;
> +  d[6].b = 0;
> +  d[6].b = 0;
> +  d[6].b = 0;
> +  e = 0;
> +  c = 0;
> +}
> +
> +int
> +main ()
> +{
> +  foo ();
> +  if (e != 0)
> +    __builtin_abort ();
> +  return 0;
> +}
> 
> 	Jakub
> 
> 

-- 
Richard Biener <rguenther@suse.de>
SUSE Software Solutions Germany GmbH, Maxfeldstrasse 5, 90409 Nuernberg,
Germany; GF: Felix Imendörffer; HRB 36809 (AG Nuernberg)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] stor-layout: Don't create DECL_BIT_FIELD_REPRESENTATIVE for QUAL_UNION_TYPE [PR101062]
  2021-06-16  7:45 [PATCH] stor-layout: Create DECL_BIT_FIELD_REPRESENTATIVE even for bitfields in unions [PR101062] Jakub Jelinek
  2021-06-16  9:54 ` Richard Biener
@ 2021-06-18  8:30 ` Jakub Jelinek
  2021-06-18  9:17   ` Richard Biener
  1 sibling, 1 reply; 4+ messages in thread
From: Jakub Jelinek @ 2021-06-18  8:30 UTC (permalink / raw)
  To: Richard Biener, Eric Botcazou; +Cc: gcc-patches

On Wed, Jun 16, 2021 at 09:45:17AM +0200, Jakub Jelinek via Gcc-patches wrote:
> The following patch does create them, but treats all such bitfields as if
> they were in a structure where the particular bitfield is the only field.

While the patch passed bootstrap/regtest on the trunk, when trying to
backport it to 11 branch the bootstrap failed with
atree.ads:3844:34: size for "Node_Record" too small
errors.  Turns out the error is not about size being too small, but actually
about size being non-constant, and comes from:
/* In a FIELD_DECL of a RECORD_TYPE, this is a pointer to the storage
   representative FIELD_DECL.  */
#define DECL_BIT_FIELD_REPRESENTATIVE(NODE) \
  (FIELD_DECL_CHECK (NODE)->field_decl.qualifier)

/* For a FIELD_DECL in a QUAL_UNION_TYPE, records the expression, which
   if nonzero, indicates that the field occupies the type.  */
#define DECL_QUALIFIER(NODE) (FIELD_DECL_CHECK (NODE)->field_decl.qualifier)
so by setting up DECL_BIT_FIELD_REPRESENTATIVE in QUAL_UNION_TYPE we
actually set or modify DECL_QUALIFIER and then construct size as COND_EXPRs
with those bit field representatives (e.g. with array type) as conditions
which doesn't fold into constant.

The following patch fixes it by not creating DECL_BIT_FIELD_REPRESENTATIVEs
for QUAL_UNION_TYPE as there is nowhere to store them,

Bootstrapped/regtested on x86_64-linux and i686-linux (both trunk and
11 branch - there on top of the earlier patch backport).  Ok for trunk and
the backport?

Shall we change tree.h to document that DECL_BIT_FIELD_REPRESENTATIVE
is valid also on UNION_TYPE?
I see:
tree-ssa-alias.c-  if (TREE_CODE (type1) == RECORD_TYPE
tree-ssa-alias.c:      && DECL_BIT_FIELD_REPRESENTATIVE (field1))
tree-ssa-alias.c:    field1 = DECL_BIT_FIELD_REPRESENTATIVE (field1);
tree-ssa-alias.c-  if (TREE_CODE (type2) == RECORD_TYPE
tree-ssa-alias.c:      && DECL_BIT_FIELD_REPRESENTATIVE (field2))
tree-ssa-alias.c:    field2 = DECL_BIT_FIELD_REPRESENTATIVE (field2);
Shall we change that to || == UNION_TYPE or do we assume all fields
are overlapping in a UNION_TYPE already?
At other spots (asan, ubsan, expr.c) it is unclear what will happen
if they see a QUAL_UNION_TYPE with a DECL_QUALIFIER (or does the Ada FE
lower that somehow)?

2021-06-18  Jakub Jelinek  <jakub@redhat.com>

	PR middle-end/101062
	* stor-layout.c (finish_bitfield_layout): Don't add bitfield
	representatives in QUAL_UNION_TYPE.

--- gcc/stor-layout.c.jj	2021-06-16 12:17:49.254221954 +0200
+++ gcc/stor-layout.c	2021-06-17 11:36:13.011822974 +0200
@@ -2172,6 +2172,9 @@ finish_bitfield_layout (tree t)
   tree field, prev;
   tree repr = NULL_TREE;
 
+  if (TREE_CODE (t) == QUAL_UNION_TYPE)
+    return;
+
   for (prev = NULL_TREE, field = TYPE_FIELDS (t);
        field; field = DECL_CHAIN (field))
     {


	Jakub


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] stor-layout: Don't create DECL_BIT_FIELD_REPRESENTATIVE for QUAL_UNION_TYPE [PR101062]
  2021-06-18  8:30 ` [PATCH] stor-layout: Don't create DECL_BIT_FIELD_REPRESENTATIVE for QUAL_UNION_TYPE [PR101062] Jakub Jelinek
@ 2021-06-18  9:17   ` Richard Biener
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Biener @ 2021-06-18  9:17 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: Eric Botcazou, gcc-patches

On Fri, 18 Jun 2021, Jakub Jelinek wrote:

> On Wed, Jun 16, 2021 at 09:45:17AM +0200, Jakub Jelinek via Gcc-patches wrote:
> > The following patch does create them, but treats all such bitfields as if
> > they were in a structure where the particular bitfield is the only field.
> 
> While the patch passed bootstrap/regtest on the trunk, when trying to
> backport it to 11 branch the bootstrap failed with
> atree.ads:3844:34: size for "Node_Record" too small
> errors.  Turns out the error is not about size being too small, but actually
> about size being non-constant, and comes from:
> /* In a FIELD_DECL of a RECORD_TYPE, this is a pointer to the storage
>    representative FIELD_DECL.  */
> #define DECL_BIT_FIELD_REPRESENTATIVE(NODE) \
>   (FIELD_DECL_CHECK (NODE)->field_decl.qualifier)
> 
> /* For a FIELD_DECL in a QUAL_UNION_TYPE, records the expression, which
>    if nonzero, indicates that the field occupies the type.  */
> #define DECL_QUALIFIER(NODE) (FIELD_DECL_CHECK (NODE)->field_decl.qualifier)
> so by setting up DECL_BIT_FIELD_REPRESENTATIVE in QUAL_UNION_TYPE we
> actually set or modify DECL_QUALIFIER and then construct size as COND_EXPRs
> with those bit field representatives (e.g. with array type) as conditions
> which doesn't fold into constant.
> 
> The following patch fixes it by not creating DECL_BIT_FIELD_REPRESENTATIVEs
> for QUAL_UNION_TYPE as there is nowhere to store them,
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux (both trunk and
> 11 branch - there on top of the earlier patch backport).  Ok for trunk and
> the backport?
> 
> Shall we change tree.h to document that DECL_BIT_FIELD_REPRESENTATIVE
> is valid also on UNION_TYPE?

I think so.

> I see:
> tree-ssa-alias.c-  if (TREE_CODE (type1) == RECORD_TYPE
> tree-ssa-alias.c:      && DECL_BIT_FIELD_REPRESENTATIVE (field1))
> tree-ssa-alias.c:    field1 = DECL_BIT_FIELD_REPRESENTATIVE (field1);
> tree-ssa-alias.c-  if (TREE_CODE (type2) == RECORD_TYPE
> tree-ssa-alias.c:      && DECL_BIT_FIELD_REPRESENTATIVE (field2))
> tree-ssa-alias.c:    field2 = DECL_BIT_FIELD_REPRESENTATIVE (field2);
> Shall we change that to || == UNION_TYPE or do we assume all fields
> are overlapping in a UNION_TYPE already?

We don't assume that - for example the Fortran FE uses union types
to lay out equivalences which have multiple members at non-zero offsets,
eventually partly overlapping.  IIRC at least.

But Fortran doesn't have bitfields (does it?).  Still I guess updating
the checks would be prefered.

> At other spots (asan, ubsan, expr.c) it is unclear what will happen
> if they see a QUAL_UNION_TYPE with a DECL_QUALIFIER (or does the Ada FE
> lower that somehow)?

I don't think we see implicit uses of DECL_QUALIFIER, instead I think
that's reflected into DECL_FIELD_OFFSET.

OK.

Richard.

> 2021-06-18  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR middle-end/101062
> 	* stor-layout.c (finish_bitfield_layout): Don't add bitfield
> 	representatives in QUAL_UNION_TYPE.
> 
> --- gcc/stor-layout.c.jj	2021-06-16 12:17:49.254221954 +0200
> +++ gcc/stor-layout.c	2021-06-17 11:36:13.011822974 +0200
> @@ -2172,6 +2172,9 @@ finish_bitfield_layout (tree t)
>    tree field, prev;
>    tree repr = NULL_TREE;
>  
> +  if (TREE_CODE (t) == QUAL_UNION_TYPE)
> +    return;
> +
>    for (prev = NULL_TREE, field = TYPE_FIELDS (t);
>         field; field = DECL_CHAIN (field))
>      {
> 
> 
> 	Jakub
> 
> 

-- 
Richard Biener <rguenther@suse.de>
SUSE Software Solutions Germany GmbH, Maxfeldstrasse 5, 90409 Nuernberg,
Germany; GF: Felix Imendörffer; HRB 36809 (AG Nuernberg)

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-06-18  9:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-16  7:45 [PATCH] stor-layout: Create DECL_BIT_FIELD_REPRESENTATIVE even for bitfields in unions [PR101062] Jakub Jelinek
2021-06-16  9:54 ` Richard Biener
2021-06-18  8:30 ` [PATCH] stor-layout: Don't create DECL_BIT_FIELD_REPRESENTATIVE for QUAL_UNION_TYPE [PR101062] Jakub Jelinek
2021-06-18  9:17   ` Richard Biener

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).