public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [RFC] tree-optimization: fix optimize-out variables passed into func to alloc
@ 2023-01-17 15:35 Alexey Lapshin
  2023-01-17 21:40 ` Andrew Pinski
  0 siblings, 1 reply; 3+ messages in thread
From: Alexey Lapshin @ 2023-01-17 15:35 UTC (permalink / raw)
  To: gcc-patches
  Cc: Michael Xiao (XIAOXUFENG),
	Alexey Gerenkov, Anton Maklakov, d, hubicka, Ivan Grokhotkov

After updating to GCC newer than 11.4.0 we found that some code started
to fail if it was built with size optimization (-Os).
You can find testsuite for reproduction in the attached patch.

The simplified version affected code looks like this:

void alloc_function (unsigned char **data_p) {
  *data_p = malloc (8);
  assert(*data_p != NULL);
}
int main () {
  int *data;
  alloc_function (&data);
  printf ("data pointer is %p", data); // prints NULL(compile with -Os)
}

If the type of passed argument is equal to the type in alloc_function
declaration it works perfectly. Also helps change one or both types to
void.

I found that issue started to appear from commit
d119f34c952f8718fdbabc63e2f369a16e92fa07
if-statement which leads to this issue was found and after being
removed seems it works well.

Could you please elaborate on what cases exactly this checking should
optimize?
I think it should also contain at least one more check for accessing
variable's memory to write..


---
 gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c | 17 +++++++++++++++++
 gcc/tree-ssa-alias.cc                         |  2 --
 2 files changed, 17 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c
b/gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c
new file mode 100644
index 00000000000..b30c1cedcb9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c
@@ -0,0 +1,17 @@
+/* { dg-do run } */
+/* { dg-options "-Os" } */
+
+#define assert(x) if (!(x)) __builtin_abort ()
+
+static inline void alloc_function (unsigned char **data_p)
+{
+    *data_p = (unsigned char *) __builtin_malloc (10);
+    assert (*data_p != (void *)0);
+}
+
+int main ()
+{
+    int *data = (void *)0;
+    alloc_function ((unsigned char **) &data);
+    assert (data != (void *)0);
+}
diff --git a/gcc/tree-ssa-alias.cc b/gcc/tree-ssa-alias.cc
index b8f107dfa52..9068db300e5 100644
--- a/gcc/tree-ssa-alias.cc
+++ b/gcc/tree-ssa-alias.cc
@@ -2608,8 +2608,6 @@ modref_may_conflict (const gcall *stmt,
 	  if (num_tests >= max_tests)
 	    return true;
 	  alias_stats.modref_tests++;
-	  if (!alias_sets_conflict_p (base_set, base_node->base))
-	    continue;
 	  num_tests++;
 	}
 
-- 
2.34.1


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

* Re: [RFC] tree-optimization: fix optimize-out variables passed into func to alloc
  2023-01-17 15:35 [RFC] tree-optimization: fix optimize-out variables passed into func to alloc Alexey Lapshin
@ 2023-01-17 21:40 ` Andrew Pinski
  2023-01-18  7:05   ` Richard Biener
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Pinski @ 2023-01-17 21:40 UTC (permalink / raw)
  To: Alexey Lapshin
  Cc: gcc-patches, Michael Xiao (XIAOXUFENG),
	Alexey Gerenkov, Anton Maklakov, d, hubicka, Ivan Grokhotkov

On Tue, Jan 17, 2023 at 7:36 AM Alexey Lapshin via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> After updating to GCC newer than 11.4.0 we found that some code started
> to fail if it was built with size optimization (-Os).
> You can find testsuite for reproduction in the attached patch.
>
> The simplified version affected code looks like this:
>
> void alloc_function (unsigned char **data_p) {
>   *data_p = malloc (8);
>   assert(*data_p != NULL);
> }
> int main () {
>   int *data;
>   alloc_function (&data);
>   printf ("data pointer is %p", data); // prints NULL(compile with -Os)
> }

This code is violating C/C++ aliasing rules.
You store to data via a "unsigned char*" and then do a load via "int*".
You can just use -fno-strict-aliasing if you want your code to just work.

Thanks,
Andrew Pinski

>
> If the type of passed argument is equal to the type in alloc_function
> declaration it works perfectly. Also helps change one or both types to
> void.
>
> I found that issue started to appear from commit
> d119f34c952f8718fdbabc63e2f369a16e92fa07
> if-statement which leads to this issue was found and after being
> removed seems it works well.
>
> Could you please elaborate on what cases exactly this checking should
> optimize?
> I think it should also contain at least one more check for accessing
> variable's memory to write..
>
>
> ---
>  gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c | 17 +++++++++++++++++
>  gcc/tree-ssa-alias.cc                         |  2 --
>  2 files changed, 17 insertions(+), 2 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c
>
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c
> b/gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c
> new file mode 100644
> index 00000000000..b30c1cedcb9
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c
> @@ -0,0 +1,17 @@
> +/* { dg-do run } */
> +/* { dg-options "-Os" } */
> +
> +#define assert(x) if (!(x)) __builtin_abort ()
> +
> +static inline void alloc_function (unsigned char **data_p)
> +{
> +    *data_p = (unsigned char *) __builtin_malloc (10);
> +    assert (*data_p != (void *)0);
> +}
> +
> +int main ()
> +{
> +    int *data = (void *)0;
> +    alloc_function ((unsigned char **) &data);
> +    assert (data != (void *)0);
> +}
> diff --git a/gcc/tree-ssa-alias.cc b/gcc/tree-ssa-alias.cc
> index b8f107dfa52..9068db300e5 100644
> --- a/gcc/tree-ssa-alias.cc
> +++ b/gcc/tree-ssa-alias.cc
> @@ -2608,8 +2608,6 @@ modref_may_conflict (const gcall *stmt,
>           if (num_tests >= max_tests)
>             return true;
>           alias_stats.modref_tests++;
> -         if (!alias_sets_conflict_p (base_set, base_node->base))
> -           continue;
>           num_tests++;
>         }
>
> --
> 2.34.1
>

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

* Re: [RFC] tree-optimization: fix optimize-out variables passed into func to alloc
  2023-01-17 21:40 ` Andrew Pinski
@ 2023-01-18  7:05   ` Richard Biener
  0 siblings, 0 replies; 3+ messages in thread
From: Richard Biener @ 2023-01-18  7:05 UTC (permalink / raw)
  To: Andrew Pinski
  Cc: Alexey Lapshin, gcc-patches, Michael Xiao (XIAOXUFENG),
	Alexey Gerenkov, Anton Maklakov, d, hubicka, Ivan Grokhotkov

On Tue, Jan 17, 2023 at 10:41 PM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> On Tue, Jan 17, 2023 at 7:36 AM Alexey Lapshin via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
> >
> > After updating to GCC newer than 11.4.0 we found that some code started
> > to fail if it was built with size optimization (-Os).
> > You can find testsuite for reproduction in the attached patch.
> >
> > The simplified version affected code looks like this:
> >
> > void alloc_function (unsigned char **data_p) {
> >   *data_p = malloc (8);
> >   assert(*data_p != NULL);
> > }
> > int main () {
> >   int *data;
> >   alloc_function (&data);
> >   printf ("data pointer is %p", data); // prints NULL(compile with -Os)
> > }
>
> This code is violating C/C++ aliasing rules.
> You store to data via a "unsigned char*" and then do a load via "int*".
> You can just use -fno-strict-aliasing if you want your code to just work.

You can also use void **data_p as a workaround, GCC treats void *
similar to a character type for aliasing rules (note that this is a GCC
extension and not guaranteed to work by the C/C++ standards).

Richard.

> Thanks,
> Andrew Pinski
>
> >
> > If the type of passed argument is equal to the type in alloc_function
> > declaration it works perfectly. Also helps change one or both types to
> > void.
> >
> > I found that issue started to appear from commit
> > d119f34c952f8718fdbabc63e2f369a16e92fa07
> > if-statement which leads to this issue was found and after being
> > removed seems it works well.
> >
> > Could you please elaborate on what cases exactly this checking should
> > optimize?
> > I think it should also contain at least one more check for accessing
> > variable's memory to write..
> >
> >
> > ---
> >  gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c | 17 +++++++++++++++++
> >  gcc/tree-ssa-alias.cc                         |  2 --
> >  2 files changed, 17 insertions(+), 2 deletions(-)
> >  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c
> >
> > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c
> > b/gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c
> > new file mode 100644
> > index 00000000000..b30c1cedcb9
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c
> > @@ -0,0 +1,17 @@
> > +/* { dg-do run } */
> > +/* { dg-options "-Os" } */
> > +
> > +#define assert(x) if (!(x)) __builtin_abort ()
> > +
> > +static inline void alloc_function (unsigned char **data_p)
> > +{
> > +    *data_p = (unsigned char *) __builtin_malloc (10);
> > +    assert (*data_p != (void *)0);
> > +}
> > +
> > +int main ()
> > +{
> > +    int *data = (void *)0;
> > +    alloc_function ((unsigned char **) &data);
> > +    assert (data != (void *)0);
> > +}
> > diff --git a/gcc/tree-ssa-alias.cc b/gcc/tree-ssa-alias.cc
> > index b8f107dfa52..9068db300e5 100644
> > --- a/gcc/tree-ssa-alias.cc
> > +++ b/gcc/tree-ssa-alias.cc
> > @@ -2608,8 +2608,6 @@ modref_may_conflict (const gcall *stmt,
> >           if (num_tests >= max_tests)
> >             return true;
> >           alias_stats.modref_tests++;
> > -         if (!alias_sets_conflict_p (base_set, base_node->base))
> > -           continue;
> >           num_tests++;
> >         }
> >
> > --
> > 2.34.1
> >

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

end of thread, other threads:[~2023-01-18  7:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-17 15:35 [RFC] tree-optimization: fix optimize-out variables passed into func to alloc Alexey Lapshin
2023-01-17 21:40 ` Andrew Pinski
2023-01-18  7:05   ` 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).