mbox series

[V5,0/2] PR target/105325: Fix constraint issue with power10 fusion

Message ID ZFu6I9FafoZHRYYi@toto.the-meissners.org
Headers show
Series PR target/105325: Fix constraint issue with power10 fusion | expand

Message

Michael Meissner May 10, 2023, 3:37 p.m. UTC
I have posted 4 previous versions of this patch (April 26th, March 28th, March
24th, and March 21st).

In this patch, rather than just add changes to the existing code in
genfusion.pl, I rewrote the function completely.  There are two patches within
this patch set:

    * The first patch rewrites the perl function to be more readable.  This
      patch produces the same output for fusion.md that the current version
      generates.

    * The second patch then using the rewrite in the first patch adds the
      changes to fix the problem.

The issue with the original bug is the power10 load GPR + cmpi -1/0/1 fusion
optimization generates illegal assembler code when the -fstack-protector option
is used.

Ultimately the code was dying because the fusion load + compare -1/0/1 patterns
did not handle the possibility that the load might be prefixed.

The main cause is the constraints for the individual loads in the fusion did not
match the machine.  In particular, LWA is a ds format instruction when it is
unprefixed.  The code did not also set the prefixed attribute correctly.

These patch hav been tested on:

    * Little endian power9 with both IEEE and IBM long double
    * Little endian power10
    * Big endian power8 using both 32-bit and 64-bit code generation.

Can I check these into the master branch?  Assuming I can check this in, I will
also commit to the active GCC branches after a burn-in period.

Comments

Michael Meissner May 10, 2023, 3:38 p.m. UTC | #1
This patch rewrites the gen_ld_cmpi_p10 function in genfusion.pl to be clearer.
The resulting fusion.md file that this patch generates is exactly the same
output that the previous version of genfusion.pl generated.  The next patch in
this series will fix PR target/105325 (provide correct predicates and
constraints for power10 fusion of load and compare immediate).

This patch has been tested on:

    * Little endian power9 with both IEEE and IBM long double
    * Little endian power10
    * Big endian power8 using both 32-bit and 64-bit code generation.

Can I check this into the master branch?  Assuming I can check this in, I will
also commit to the active GCC branches after a burn-in period.

2023-05-10   Michael Meissner  <meissner@linux.ibm.com>

gcc/

	PR target/105325
	* config/rs6000/genfusion.pl (mode_to_ldst_char): Delete.
	(print_ld_cmpi_p10): New function, split off from gen_ld_cmpi_p10.
	(gen_ld_cmpi_p10): Rewrite completely.
---
 gcc/config/rs6000/genfusion.pl | 248 +++++++++++++++++++++------------
 1 file changed, 157 insertions(+), 91 deletions(-)

diff --git a/gcc/config/rs6000/genfusion.pl b/gcc/config/rs6000/genfusion.pl
index e4db352e0ce..81ba4b33940 100755
--- a/gcc/config/rs6000/genfusion.pl
+++ b/gcc/config/rs6000/genfusion.pl
@@ -45,103 +45,169 @@ print <<'EOF';
 
 EOF
 
-sub mode_to_ldst_char
+# Print the insns for load and compare with -1/0/1.
+# Arguments:
+# lmode      -- Integer mode ("DI", "SI", "HI", or "QI").
+# result     -- "clobber", "GPR", or $lmode
+# ccmode     -- Sign vs. unsigned ("CC" or "CCUNS").
+# mem_format -- Memory format ("d" or "ds").
+# cmpl       -- Suffix for compare ("l" or "")
+# const_pred -- Predicate for constant (i.e. -1/0/1 or 0/1).
+# extend     -- "sign", "zero", or "none".
+# echr       -- Suffix for load ("a", "z", or "").
+# load       -- Load instruction (i.e. "ld", "lwa", "lwz", etc.)
+# np         -- enum non_prefixed_form for memory type
+# constraint -- constraint to use
+# mem_pred   -- predicate for the memory operation
+
+sub print_ld_cmpi_p10
 {
-    my ($mode) = @_;
-    my %x = (DI => 'd', SI => 'w', HI => 'h', QI => 'b');
-    return $x{$mode} if exists $x{$mode};
-    return '?';
+  my ($lmode, $result, $ccmode, $cmpl, $const_pred,
+      $extend, $load, $np, $constraint, $mem_pred) = @_;
+
+  # For clobber, we need a SI/DI reg in case we split because we have to
+  # sign/zero extend.
+  my $clobbermode = ($lmode =~ m/^[HQ]I$/) ? "GPR" : $lmode;
+
+  # Break long print statements into smaller lines.
+  my $info = join (" ",
+		   "load mode is ${lmode} result mode is ${result}",
+		   "compare mode is ${ccmode} extend is ${extend}");
+
+  my $name = join ("",
+		   "${load}_cmp${cmpl}di_cr0_${lmode}",
+		   "_${result}_${ccmode}_${extend}");
+
+  my $cmp_op1 = "(match_operand:${lmode} 1 \"${mem_pred}\" \"${constraint}\")";
+
+  my $spaces = " " x (length ($ccmode) + 18);
+
+  print ";; load-cmpi fusion pattern generated by gen_ld_cmpi_p10\n";
+  print ";; ${info}\n";
+  print "(define_insn_and_split \"*${name}\"\n";
+  print "  [(set (match_operand:${ccmode} 2 \"cc_reg_operand\" \"=x\")\n";
+  print "        (compare:${ccmode} ${cmp_op1}\n";
+  print "${spaces}(match_operand:${lmode} 3 \"${const_pred}\" \"n\")))\n";
+
+  if ($result eq "clobber")
+    {
+      print "   (clobber (match_scratch:${clobbermode} 0 \"=r\"))]\n";
+    }
+
+  else
+    {
+      my $load_op0 = "(match_operand:${result} 0 \"gpc_reg_operand\" \"=r\")";
+      my $load_op1 = (($result eq $lmode)
+		      ? "(match_dup 1)"
+		      : "(${extend}_extend:${result} (match_dup 1))");
+      print "   (set ${load_op0} ${load_op1})]\n";
+    }
+
+  # Do not match prefixed loads.  The machine only fuses non-prefixed loads
+  # with compare immediate.  Take into account whether the load is a ds-form
+  # or a d-form instruction.
+  print "  \"(TARGET_P10_FUSION)\"\n";
+  print "  \"${load}%X1 %0,%1\\;cmp${cmpl}di %2,%0,%3\"\n";
+  print "  \"&& reload_completed\n";
+  print "   && (cc_reg_not_cr0_operand (operands[2], CCmode)\n";
+  print "       || !address_is_non_pfx_d_or_x (XEXP (operands[1], 0),\n";
+  print "                                      ${lmode}mode, ${np}))\"\n";
+
+  if ($extend eq "none")
+    {
+      print "  [(set (match_dup 0) (match_dup 1))\n";
+    }
+
+  else
+    {
+      my $resultmode = ($result eq "clobber") ? $clobbermode : $result;
+      print "  [(set (match_dup 0) (${extend}_extend:${resultmode} (match_dup 1)))\n";
+    }
+
+  print "   (set (match_dup 2)\n";
+  print "        (compare:${ccmode} (match_dup 0) (match_dup 3)))]\n";
+  print "  \"\"\n";
+  print "  [(set_attr \"type\" \"fused_load_cmpi\")\n";
+  print "   (set_attr \"cost\" \"8\")\n";
+  print "   (set_attr \"length\" \"8\")])\n";
+  print "\n";
 }
 
 sub gen_ld_cmpi_p10
 {
-    my ($lmode, $ldst, $clobbermode, $result, $cmpl, $echr, $constpred,
-	$mempred, $ccmode, $np, $extend, $resultmode);
-  LMODE: foreach $lmode ('DI','SI','HI','QI') {
-      $ldst = mode_to_ldst_char($lmode);
-      $clobbermode = $lmode;
-      # For clobber, we need a SI/DI reg in case we
-      # split because we have to sign/zero extend.
-      if ($lmode eq 'HI' || $lmode eq 'QI') { $clobbermode = "GPR"; }
-    RESULT: foreach $result ('clobber', $lmode,  "EXT".$lmode) {
-	# EXTDI does not exist, and we cannot directly produce HI/QI results.
-	next RESULT if $result eq "EXTDI" || $result eq "HI" || $result eq "QI";
-	# Don't allow EXTQI because that would allow HI result which we can't do.
-	$result = "GPR" if $result eq "EXTQI";
-      CCMODE: foreach $ccmode ('CC','CCUNS') {
-	  $np = "NON_PREFIXED_D";
-	  $mempred = "non_update_memory_operand";
-	  if ( $ccmode eq 'CC' ) {
-	      next CCMODE if $lmode eq 'QI';
-	      if ( $lmode eq 'DI' || $lmode eq 'SI' ) {
-		  # ld and lwa are both DS-FORM.
-		  $np = "NON_PREFIXED_DS";
-		  $mempred = "ds_form_mem_operand";
-	      }
-	      $cmpl = "";
-	      $echr = "a";
-	      $constpred = "const_m1_to_1_operand";
-	  } else {
-	      if ( $lmode eq 'DI' ) {
-		  # ld is DS-form, but lwz is not.
-		  $np = "NON_PREFIXED_DS";
-		  $mempred = "ds_form_mem_operand";
-	      }
-	      $cmpl = "l";
-	      $echr = "z";
-	      $constpred = "const_0_to_1_operand";
-	  }
-	  if ($lmode eq 'DI') { $echr = ""; }
-	  if ($result =~ m/^EXT/ || $result eq 'GPR' || $clobbermode eq 'GPR') {
-	      # We always need extension if result > lmode.
-	      if ( $ccmode eq 'CC' ) {
-		  $extend = "sign";
-	      } else {
-		  $extend = "zero";
-	      }
-	  } else {
-	      # Result of SI/DI does not need sign extension.
-	      $extend = "none";
-	  }
-	  print ";; load-cmpi fusion pattern generated by gen_ld_cmpi_p10\n";
-	  print ";; load mode is $lmode result mode is $result compare mode is $ccmode extend is $extend\n";
-
-	  print "(define_insn_and_split \"*l${ldst}${echr}_cmp${cmpl}di_cr0_${lmode}_${result}_${ccmode}_${extend}\"\n";
-	  print "  [(set (match_operand:${ccmode} 2 \"cc_reg_operand\" \"=x\")\n";
-	  print "        (compare:${ccmode} (match_operand:${lmode} 1 \"${mempred}\" \"m\")\n";
-	  if ($ccmode eq 'CCUNS') { print "   "; }
-	  print "                    (match_operand:${lmode} 3 \"${constpred}\" \"n\")))\n";
-	  if ($result eq 'clobber') {
-	      print "   (clobber (match_scratch:${clobbermode} 0 \"=r\"))]\n";
-	  } elsif ($result eq $lmode) {
-	      print "   (set (match_operand:${result} 0 \"gpc_reg_operand\" \"=r\") (match_dup 1))]\n";
-	  } else {
-	      print "   (set (match_operand:${result} 0 \"gpc_reg_operand\" \"=r\") (${extend}_extend:${result} (match_dup 1)))]\n";
-	  }
-	  print "  \"(TARGET_P10_FUSION)\"\n";
-	  print "  \"l${ldst}${echr}%X1 %0,%1\\;cmp${cmpl}di %2,%0,%3\"\n";
-	  print "  \"&& reload_completed\n";
-	  print "   && (cc_reg_not_cr0_operand (operands[2], CCmode)\n";
-	  print "       || !address_is_non_pfx_d_or_x (XEXP (operands[1], 0),\n";
-	  print "                                      ${lmode}mode, ${np}))\"\n";
-
-	  if ($extend eq "none") {
-	      print "  [(set (match_dup 0) (match_dup 1))\n";
-	  } else {
-	      $resultmode = $result;
-	      if ( $result eq 'clobber' ) { $resultmode = $clobbermode }
-	      print "  [(set (match_dup 0) (${extend}_extend:${resultmode} (match_dup 1)))\n";
-	  }
-	  print "   (set (match_dup 2)\n";
-	  print "        (compare:${ccmode} (match_dup 0) (match_dup 3)))]\n";
-	  print "  \"\"\n";
-	  print "  [(set_attr \"type\" \"fused_load_cmpi\")\n";
-	  print "   (set_attr \"cost\" \"8\")\n";
-	  print "   (set_attr \"length\" \"8\")])\n";
-	  print "\n";
-      }
+  my ($lmode, $result, $mem_format, $extend);
+
+  # Map mode to load instruction
+  my %signed_load = ("DI" => "ld",
+		     "SI" => "lwa",
+		     "HI" => "lha");
+
+  my %unsigned_load = ("DI" => "ld",
+		       "SI" => "lwz",
+		       "HI" => "lhz",
+		       "QI" => "lbz");
+
+  # Memory predicate to use.
+  my %signed_memory_predicate = ("DI" => "ds_form_mem_operand",
+				 "SI" => "ds_form_mem_operand",
+				 "HI" => "non_update_memory_operand");
+
+  my %unsigned_memory_predicate = ("DI" => "ds_form_mem_operand",
+				   "SI" => "non_update_memory_operand",
+				   "HI" => "non_update_memory_operand",
+				   "QI" => "non_update_memory_operand");
+
+  # Internal format of the memory instruction (enum non_prefixed_form) to use.
+  my %np = ("ds" => "NON_PREFIXED_DS",
+	    "d"  => "NON_PREFIXED_D");
+
+  # Result modes to use. Clobber is used when you are comparing the load to
+  # -1/0/1, but you are not using it otherwise.  EXTDI does not exist. We
+  # cannot directly use HI/QI results because we only have word and double word
+  # compared.  For promotion, don't allow EXTQI because that would allow HI
+  # results which we can't do (use GPR instead).
+  my %result_modes = ("DI" => ["clobber", "DI"],
+		      "SI" => ["clobber", "SI", "EXTSI" ],
+		      "HI" => ["clobber", "EXTHI" ],
+		      "QI" => ["clobber", "GPR" ]);
+
+  foreach $lmode ("DI", "SI", "HI", "QI")
+    {
+      foreach $result (@{ $result_modes{$lmode} })
+	{
+	  # Handle CCmode (sign extended compares to -1, 0, or 1).  We don't
+	  # have  a LBA instruction, so skip QImode.  Both LD and LWA are
+	  # DS-form instructions for signed loads.
+	  if ($lmode ne "QI")
+	    {
+	      $mem_format = ($lmode =~ m/^[DS]I$/) ? "ds" : "d";
+	      $extend = (($lmode eq "DI"
+			  || $lmode eq $result
+			  || ($lmode eq "SI" && $result eq "clobber"))
+			 ? "none"
+			 : "sign");
+
+	      print_ld_cmpi_p10 ($lmode, $result, "CC", "",
+				 "const_m1_to_1_operand", $extend,
+				 $signed_load{$lmode}, $np{$mem_format}, "m",
+				 $signed_memory_predicate{$lmode});
+	    }
+
+	  # Handle CCUNS mode (zero extended compares to 0 or 1.
+	  # LD is DS-form, but LWZ is not for unsigned loads.
+	  $mem_format = ($lmode eq "DI") ? "ds" : "d";
+	  $extend = (($lmode eq "DI"
+		      || $lmode eq $result
+		      || ($lmode eq "SI" && $result eq "clobber"))
+		     ? "none"
+		     : "zero");
+
+	  print_ld_cmpi_p10 ($lmode, $result, "CCUNS", "l",
+			     "const_0_to_1_operand", $extend,
+			     $unsigned_load{$lmode}, $np{$mem_format}, "m",
+			     $unsigned_memory_predicate{$lmode});
+	}
     }
-  }
 }
 
 sub gen_logical_addsubf
Michael Meissner May 15, 2023, 6:22 p.m. UTC | #2
Ping both patches:

Patch #1, rewrite genfusion.pl's code for load and compare immediate fusion to
be more readable.  This patch produces the same output as the current sources.

| Date: Wed, 10 May 2023 11:38:55 -0400
| Subject: Re: [PATCH V5, 1/2] PR target/105325: Rewrite genfusion.pl's gen_ld_cmpi_p10 function.
| Message-ID: <ZFu6j7SZaCUZpTYb@toto.the-meissners.org>

Patch #2, implement the fix for PR target/105325:

| Date: Wed, 10 May 2023 11:40:00 -0400
| Subject: [PATCH V5, 2/2] PR target/105325: Fix memory constraints for power10 fusion.
| Message-ID: <ZFu60EPEOJTV/GA1@toto.the-meissners.org>
Segher Boessenkool May 26, 2023, 2:35 p.m. UTC | #3
Hi Mike,

On Wed, May 10, 2023 at 11:38:55AM -0400, Michael Meissner wrote:
> This patch rewrites the gen_ld_cmpi_p10 function in genfusion.pl to be clearer.

That is not at all what I asked for, even if I would agree the code is
nicer to read now (I don't).

What I asked for, what is needed, is for your patches to be readable.
This is a prerequisite for them to be reviewable, which is a
prerequisite for them to be approvable.  One way to do that is to split
out refactorings (which I asked for) and rewrites (which you did) to
earlier patches in the series.  Pure refactoring are easy to review:
they change exactly nothing in what code is executed.  Rewrites are much
harder to review.  But even then we can hope you didn't slip up once in
a hundred lines of code, sure.

The later patches can then be much more readable because there isn't so
much noise mixed in.

> Assuming I can check this in, I will
> also commit to the active GCC branches after a burn-in period.

No, you will never do that.  You always need approval for that.  We have
these procedures for a reason.  We do not want other things than what
was approved committed, doubly so if *nothing* was approved.

> 	* config/rs6000/genfusion.pl (mode_to_ldst_char): Delete.

This is a regression.

> +# Print the insns for load and compare with -1/0/1.
> +# Arguments:
> +# lmode      -- Integer mode ("DI", "SI", "HI", or "QI").
> +# result     -- "clobber", "GPR", or $lmode
> +# ccmode     -- Sign vs. unsigned ("CC" or "CCUNS").
> +# mem_format -- Memory format ("d" or "ds").
> +# cmpl       -- Suffix for compare ("l" or "")
> +# const_pred -- Predicate for constant (i.e. -1/0/1 or 0/1).
> +# extend     -- "sign", "zero", or "none".
> +# echr       -- Suffix for load ("a", "z", or "").
> +# load       -- Load instruction (i.e. "ld", "lwa", "lwz", etc.)
> +# np         -- enum non_prefixed_form for memory type
> +# constraint -- constraint to use
> +# mem_pred   -- predicate for the memory operation

If you need a huge block comment for your sub argument, that is a
not-so-subtle hint that you need to refactor.  Or if this was supposed
to be a refactoring, that something went terribly wrong :-(


Segher