Dump if a pattern fails after having printed applying it

Message ID 20230523231601.2130715-1-apinski@marvell.com
State Accepted
Headers
Series Dump if a pattern fails after having printed applying it |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Andrew Pinski May 23, 2023, 11:16 p.m. UTC
  While trying to understand how to use the ! operand for match
patterns, I noticed that the debug dumps would print out applying
a pattern but nothing when it was rejected in the end. This was confusing
me.
This adds that by emitting a dump for the failed case.
Note the patch is little more complex as we don't want to print out
if debug counter rejected the pattern and then we need to fix up
when we mark needing a label or not.

OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

gcc/ChangeLog:

	* genmatch.cc (needs_label): New variable
	(expr::gen_transform): Set needs_label
	if we use the local_label.
	(dt_simplify::gen_1): Use `_1` for the debug count label.
	After the local label, emit debug print for the failure.
	Emit `_1` label if needed.
---
 gcc/genmatch.cc | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
  

Comments

Richard Biener May 24, 2023, 9 a.m. UTC | #1
On Wed, May 24, 2023 at 1:16 AM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> While trying to understand how to use the ! operand for match
> patterns, I noticed that the debug dumps would print out applying
> a pattern but nothing when it was rejected in the end. This was confusing
> me.
> This adds that by emitting a dump for the failed case.
> Note the patch is little more complex as we don't want to print out
> if debug counter rejected the pattern and then we need to fix up
> when we mark needing a label or not.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

Hmm, can we maybe simply move the

                                                if (UNLIKELY
(debug_dump)) fprintf (dump_file, "Applying pattern %s:%d, %s:%d\n",
"match.pd", 1157, __FILE__, __LINE__);

right before the return true; instead?

> gcc/ChangeLog:
>
>         * genmatch.cc (needs_label): New variable
>         (expr::gen_transform): Set needs_label
>         if we use the local_label.
>         (dt_simplify::gen_1): Use `_1` for the debug count label.
>         After the local label, emit debug print for the failure.
>         Emit `_1` label if needed.
> ---
>  gcc/genmatch.cc | 28 ++++++++++++++++++++++++----
>  1 file changed, 24 insertions(+), 4 deletions(-)
>
> diff --git a/gcc/genmatch.cc b/gcc/genmatch.cc
> index 177c13d87cb..2ea80d341a2 100644
> --- a/gcc/genmatch.cc
> +++ b/gcc/genmatch.cc
> @@ -2433,6 +2433,7 @@ capture_info::walk_c_expr (c_expr *e)
>  /* The current label failing the current matched pattern during
>     code generation.  */
>  static char *fail_label;
> +static bool needs_label;
>
>  /* Code generation off the decision tree and the refered AST nodes.  */
>
> @@ -2611,6 +2612,7 @@ expr::gen_transform (FILE *f, int indent, const char *dest, bool gimple,
>        fprintf_indent (f, indent,
>                       "if (!_r%d) goto %s;\n",
>                       depth, fail_label);
> +      needs_label = true;
>        if (*opr == CONVERT_EXPR)
>         {
>           indent -= 4;
> @@ -2640,11 +2642,13 @@ expr::gen_transform (FILE *f, int indent, const char *dest, bool gimple,
>         {
>           fprintf_indent (f, indent, "if (!_r%d)\n", depth);
>           fprintf_indent (f, indent, "  goto %s;\n", fail_label);
> +         needs_label = true;
>         }
>        if (force_leaf)
>         {
>           fprintf_indent (f, indent, "if (EXPR_P (_r%d))\n", depth);
>           fprintf_indent (f, indent, "  goto %s;\n", fail_label);
> +         needs_label = true;
>         }
>        if (*opr == CONVERT_EXPR)
>         {
> @@ -3409,7 +3413,8 @@ dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
>    char local_fail_label[256];
>    snprintf (local_fail_label, 256, "next_after_fail%u", ++fail_label_cnt);
>    fail_label = local_fail_label;
> -  bool needs_label = false;
> +  needs_label = false;
> +  bool needs_label_1 = false;
>
>    /* Analyze captures and perform early-outs on the incoming arguments
>       that cover cases we cannot handle.  */
> @@ -3484,8 +3489,8 @@ dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
>
>    if (s->kind == simplify::SIMPLIFY)
>      {
> -      fprintf_indent (f, indent, "if (UNLIKELY (!dbg_cnt (match))) goto %s;\n", fail_label);
> -      needs_label = true;
> +      fprintf_indent (f, indent, "if (UNLIKELY (!dbg_cnt (match))) goto %s_1;\n", fail_label);
> +      needs_label_1 = true;
>      }
>
>    fprintf_indent (f, indent, "if (UNLIKELY (debug_dump)) "
> @@ -3718,7 +3723,22 @@ dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
>    indent -= 2;
>    fprintf_indent (f, indent, "}\n");
>    if (needs_label)
> -    fprintf (f, "%s:;\n", fail_label);
> +    {
> +      fprintf (f, "%s:;\n", fail_label);
> +      if (s->kind == simplify::SIMPLIFY)
> +       {
> +         fprintf_indent (f, indent, "if (UNLIKELY (debug_dump)) "
> +                         "fprintf (dump_file, \"Pattern failed ");
> +         fprintf (f, "%%s:%%d, %%s:%%d\\n\", ");
> +         output_line_directive (f,
> +                                result ? result->location : s->match->location, true,
> +                                true);
> +         fprintf (f, ", __FILE__, __LINE__);\n");
> +       }
> +    }
> +  if (needs_label_1)
> +    fprintf (f, "%s_1:;\n", fail_label);
> +  needs_label = false;
>    fail_label = NULL;
>  }
>
> --
> 2.31.1
>
  
Andrew Pinski May 24, 2023, 4:42 p.m. UTC | #2
On Wed, May 24, 2023 at 2:03 AM Richard Biener via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> On Wed, May 24, 2023 at 1:16 AM Andrew Pinski via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
> >
> > While trying to understand how to use the ! operand for match
> > patterns, I noticed that the debug dumps would print out applying
> > a pattern but nothing when it was rejected in the end. This was confusing
> > me.
> > This adds that by emitting a dump for the failed case.
> > Note the patch is little more complex as we don't want to print out
> > if debug counter rejected the pattern and then we need to fix up
> > when we mark needing a label or not.
> >
> > OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
>
> Hmm, can we maybe simply move the
>
>                                                 if (UNLIKELY
> (debug_dump)) fprintf (dump_file, "Applying pattern %s:%d, %s:%d\n",
> "match.pd", 1157, __FILE__, __LINE__);
>
> right before the return true; instead?

Yes that should work. Let me do a patch for that.

Thanks,
Andrew

>
> > gcc/ChangeLog:
> >
> >         * genmatch.cc (needs_label): New variable
> >         (expr::gen_transform): Set needs_label
> >         if we use the local_label.
> >         (dt_simplify::gen_1): Use `_1` for the debug count label.
> >         After the local label, emit debug print for the failure.
> >         Emit `_1` label if needed.
> > ---
> >  gcc/genmatch.cc | 28 ++++++++++++++++++++++++----
> >  1 file changed, 24 insertions(+), 4 deletions(-)
> >
> > diff --git a/gcc/genmatch.cc b/gcc/genmatch.cc
> > index 177c13d87cb..2ea80d341a2 100644
> > --- a/gcc/genmatch.cc
> > +++ b/gcc/genmatch.cc
> > @@ -2433,6 +2433,7 @@ capture_info::walk_c_expr (c_expr *e)
> >  /* The current label failing the current matched pattern during
> >     code generation.  */
> >  static char *fail_label;
> > +static bool needs_label;
> >
> >  /* Code generation off the decision tree and the refered AST nodes.  */
> >
> > @@ -2611,6 +2612,7 @@ expr::gen_transform (FILE *f, int indent, const char *dest, bool gimple,
> >        fprintf_indent (f, indent,
> >                       "if (!_r%d) goto %s;\n",
> >                       depth, fail_label);
> > +      needs_label = true;
> >        if (*opr == CONVERT_EXPR)
> >         {
> >           indent -= 4;
> > @@ -2640,11 +2642,13 @@ expr::gen_transform (FILE *f, int indent, const char *dest, bool gimple,
> >         {
> >           fprintf_indent (f, indent, "if (!_r%d)\n", depth);
> >           fprintf_indent (f, indent, "  goto %s;\n", fail_label);
> > +         needs_label = true;
> >         }
> >        if (force_leaf)
> >         {
> >           fprintf_indent (f, indent, "if (EXPR_P (_r%d))\n", depth);
> >           fprintf_indent (f, indent, "  goto %s;\n", fail_label);
> > +         needs_label = true;
> >         }
> >        if (*opr == CONVERT_EXPR)
> >         {
> > @@ -3409,7 +3413,8 @@ dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
> >    char local_fail_label[256];
> >    snprintf (local_fail_label, 256, "next_after_fail%u", ++fail_label_cnt);
> >    fail_label = local_fail_label;
> > -  bool needs_label = false;
> > +  needs_label = false;
> > +  bool needs_label_1 = false;
> >
> >    /* Analyze captures and perform early-outs on the incoming arguments
> >       that cover cases we cannot handle.  */
> > @@ -3484,8 +3489,8 @@ dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
> >
> >    if (s->kind == simplify::SIMPLIFY)
> >      {
> > -      fprintf_indent (f, indent, "if (UNLIKELY (!dbg_cnt (match))) goto %s;\n", fail_label);
> > -      needs_label = true;
> > +      fprintf_indent (f, indent, "if (UNLIKELY (!dbg_cnt (match))) goto %s_1;\n", fail_label);
> > +      needs_label_1 = true;
> >      }
> >
> >    fprintf_indent (f, indent, "if (UNLIKELY (debug_dump)) "
> > @@ -3718,7 +3723,22 @@ dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
> >    indent -= 2;
> >    fprintf_indent (f, indent, "}\n");
> >    if (needs_label)
> > -    fprintf (f, "%s:;\n", fail_label);
> > +    {
> > +      fprintf (f, "%s:;\n", fail_label);
> > +      if (s->kind == simplify::SIMPLIFY)
> > +       {
> > +         fprintf_indent (f, indent, "if (UNLIKELY (debug_dump)) "
> > +                         "fprintf (dump_file, \"Pattern failed ");
> > +         fprintf (f, "%%s:%%d, %%s:%%d\\n\", ");
> > +         output_line_directive (f,
> > +                                result ? result->location : s->match->location, true,
> > +                                true);
> > +         fprintf (f, ", __FILE__, __LINE__);\n");
> > +       }
> > +    }
> > +  if (needs_label_1)
> > +    fprintf (f, "%s_1:;\n", fail_label);
> > +  needs_label = false;
> >    fail_label = NULL;
> >  }
> >
> > --
> > 2.31.1
> >
  

Patch

diff --git a/gcc/genmatch.cc b/gcc/genmatch.cc
index 177c13d87cb..2ea80d341a2 100644
--- a/gcc/genmatch.cc
+++ b/gcc/genmatch.cc
@@ -2433,6 +2433,7 @@  capture_info::walk_c_expr (c_expr *e)
 /* The current label failing the current matched pattern during
    code generation.  */
 static char *fail_label;
+static bool needs_label;
 
 /* Code generation off the decision tree and the refered AST nodes.  */
 
@@ -2611,6 +2612,7 @@  expr::gen_transform (FILE *f, int indent, const char *dest, bool gimple,
       fprintf_indent (f, indent,
 		      "if (!_r%d) goto %s;\n",
 		      depth, fail_label);
+      needs_label = true;
       if (*opr == CONVERT_EXPR)
 	{
 	  indent -= 4;
@@ -2640,11 +2642,13 @@  expr::gen_transform (FILE *f, int indent, const char *dest, bool gimple,
 	{
 	  fprintf_indent (f, indent, "if (!_r%d)\n", depth);
 	  fprintf_indent (f, indent, "  goto %s;\n", fail_label);
+	  needs_label = true;
 	}
       if (force_leaf)
 	{
 	  fprintf_indent (f, indent, "if (EXPR_P (_r%d))\n", depth);
 	  fprintf_indent (f, indent, "  goto %s;\n", fail_label);
+	  needs_label = true;
 	}
       if (*opr == CONVERT_EXPR)
 	{
@@ -3409,7 +3413,8 @@  dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
   char local_fail_label[256];
   snprintf (local_fail_label, 256, "next_after_fail%u", ++fail_label_cnt);
   fail_label = local_fail_label;
-  bool needs_label = false;
+  needs_label = false;
+  bool needs_label_1 = false;
 
   /* Analyze captures and perform early-outs on the incoming arguments
      that cover cases we cannot handle.  */
@@ -3484,8 +3489,8 @@  dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
 
   if (s->kind == simplify::SIMPLIFY)
     {
-      fprintf_indent (f, indent, "if (UNLIKELY (!dbg_cnt (match))) goto %s;\n", fail_label);
-      needs_label = true;
+      fprintf_indent (f, indent, "if (UNLIKELY (!dbg_cnt (match))) goto %s_1;\n", fail_label);
+      needs_label_1 = true;
     }
 
   fprintf_indent (f, indent, "if (UNLIKELY (debug_dump)) "
@@ -3718,7 +3723,22 @@  dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
   indent -= 2;
   fprintf_indent (f, indent, "}\n");
   if (needs_label)
-    fprintf (f, "%s:;\n", fail_label);
+    {
+      fprintf (f, "%s:;\n", fail_label);
+      if (s->kind == simplify::SIMPLIFY)
+	{
+	  fprintf_indent (f, indent, "if (UNLIKELY (debug_dump)) "
+			  "fprintf (dump_file, \"Pattern failed ");
+	  fprintf (f, "%%s:%%d, %%s:%%d\\n\", ");
+	  output_line_directive (f,
+				 result ? result->location : s->match->location, true,
+				 true);
+	  fprintf (f, ", __FILE__, __LINE__);\n");
+	}
+    }
+  if (needs_label_1)
+    fprintf (f, "%s_1:;\n", fail_label);
+  needs_label = false;
   fail_label = NULL;
 }