public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jakub at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/112733] [14 Regression] ICE: Segmentation fault in wide-int.cc during GIMPLE pass: sccp
Date: Tue, 28 Nov 2023 15:56:39 +0000	[thread overview]
Message-ID: <bug-112733-4-yPA7c8tvXC@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-112733-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112733

--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
As for the actual crash, I have again multiple possible fixes:
--- gcc/wide-int.cc.jj  2023-10-16 14:24:46.360204472 +0200
+++ gcc/wide-int.cc     2023-11-28 16:33:35.737394223 +0100
@@ -1297,6 +1297,8 @@ wi_pack (HOST_WIDE_INT *result,
   unsigned int j = 0;
   unsigned int blocks_needed = BLOCKS_NEEDED (precision);

+  if (in_len > blocks_needed * 2)
+    in_len = blocks_needed * 2;
   while (i + 1 < in_len)
     {
       result[j++] = ((unsigned HOST_WIDE_INT) input[i]
Or another option is ensure that in_len is never larger than blocks_needed * 2.
The problem with the wide-int.cc wi_pack calls for remainder is that
the in_len argument in that case is n (which is:
  n = divisor_blocks_needed;
  while (n > 1 && b_divisor[n - 1] == 0)
    n--;
i.e. at most 2 * BLOCKS_NEEDED (divisor_prec), but the last
argument is dividend_prec rather than divisor_prec.
In reality, I think the remainder is bound by both minimum precision
of dividend and divisor, because it can't be in absolute value larger
than the divisor and can't be in absolute value larger than dividend.
--- gcc/wide-int.h.jj   2023-10-14 11:08:20.790471175 +0200
+++ gcc/wide-int.h      2023-11-28 16:44:26.576265870 +0100
@@ -3179,6 +3179,10 @@ wi::div_floor (const T1 &x, const T2 &y,
       else
        est_len = xi.len + 1;
       quotient_val = quotient.write_val (est_len);
+      if (sgn == UNSIGNED && yi.val[yi.len - 1] < 0)
+       est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
+      else
+       est_len = yi.len + 1;
       remainder_val = remainder.write_val (est_len);
     }
   quotient.set_len (divmod_internal (quotient_val,
@@ -3231,6 +3235,10 @@ wi::div_ceil (const T1 &x, const T2 &y,
       else
        est_len = xi.len + 1;
       quotient_val = quotient.write_val (est_len);
+      if (sgn == UNSIGNED && yi.val[yi.len - 1] < 0)
+       est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
+      else
+       est_len = yi.len + 1;
       remainder_val = remainder.write_val (est_len);
     }
   quotient.set_len (divmod_internal (quotient_val,
@@ -3274,6 +3282,10 @@ wi::div_round (const T1 &x, const T2 &y,
       else
        est_len = xi.len + 1;
       quotient_val = quotient.write_val (est_len);
+      if (sgn == UNSIGNED && yi.val[yi.len - 1] < 0)
+       est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
+      else
+       est_len = yi.len + 1;
       remainder_val = remainder.write_val (est_len);
     }
   quotient.set_len (divmod_internal (quotient_val,
@@ -3327,6 +3339,10 @@ wi::divmod_trunc (const T1 &x, const T2
       else
        est_len = xi.len + 1;
       quotient_val = quotient.write_val (est_len);
+      if (sgn == UNSIGNED && yi.val[yi.len - 1] < 0)
+       est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
+      else
+       est_len = yi.len + 1;
       remainder_val = remainder.write_val (est_len);
     }
   quotient.set_len (divmod_internal (quotient_val,
@@ -3375,10 +3391,10 @@ wi::mod_trunc (const T1 &x, const T2 &y,
   unsigned int remainder_len;
   if (remainder.needs_write_val_arg)
     remainder_val = remainder.write_val ((sgn == UNSIGNED
-                                         && xi.val[xi.len - 1] < 0)
+                                         && yi.val[yi.len - 1] < 0)
                                         ? CEIL (precision,
                                                 HOST_BITS_PER_WIDE_INT) + 1
-                                        : xi.len + 1);
+                                        : yi.len + 1);
   divmod_internal (0, &remainder_len, remainder_val,
                   xi.val, xi.len, precision,
                   yi.val, yi.len, yi.precision, sgn, overflow);
@@ -3427,6 +3443,10 @@ wi::mod_floor (const T1 &x, const T2 &y,
       else
        est_len = xi.len + 1;
       quotient_val = quotient.write_val (est_len);
+      if (sgn == UNSIGNED && yi.val[yi.len - 1] < 0)
+       est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
+      else
+       est_len = yi.len + 1;
       remainder_val = remainder.write_val (est_len);
     }
   quotient.set_len (divmod_internal (quotient_val,
@@ -3473,6 +3493,10 @@ wi::mod_ceil (const T1 &x, const T2 &y,
       else
        est_len = xi.len + 1;
       quotient_val = quotient.write_val (est_len);
+      if (sgn == UNSIGNED && yi.val[yi.len - 1] < 0)
+       est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
+      else
+       est_len = yi.len + 1;
       remainder_val = remainder.write_val (est_len);
     }
   quotient.set_len (divmod_internal (quotient_val,
@@ -3509,6 +3533,10 @@ wi::mod_round (const T1 &x, const T2 &y,
       else
        est_len = xi.len + 1;
       quotient_val = quotient.write_val (est_len);
+      if (sgn == UNSIGNED && yi.val[yi.len - 1] < 0)
+       est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
+      else
+       est_len = yi.len + 1;
       remainder_val = remainder.write_val (est_len);
     }
   quotient.set_len (divmod_internal (quotient_val,
--- gcc/wide-int.cc.jj  2023-10-16 14:24:46.360204472 +0200
+++ gcc/wide-int.cc     2023-11-28 16:36:09.227244087 +0100
@@ -1985,11 +1985,11 @@ wi::divmod_internal (HOST_WIDE_INT *quot

   if (remainder)
     {
-      *remainder_len = wi_pack (remainder, b_remainder, n, dividend_prec);
+      *remainder_len = wi_pack (remainder, b_remainder, n, divisor_prec);
       /* The remainder is always the same sign as the dividend.  */
       if (dividend_neg)
        *remainder_len = wi::sub_large (remainder, zeros, 1, remainder,
-                                       *remainder_len, dividend_prec,
+                                       *remainder_len, divisor_prec,
                                        UNSIGNED, 0);
     }

The advantage of the first patch is that it doesn't increase the code needed
to be emitted in widest_int wi::div_{floor,ceil,round} and wi::divmod_trunc,
wi::mod_{floor,ceil,round}, at the expense of one comparison in wide-int.cc
(done for all large multiplications, divisions, modulo).

  parent reply	other threads:[~2023-11-28 15:56 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-27 19:12 [Bug middle-end/112733] New: " patrick at rivosinc dot com
2023-11-27 19:16 ` [Bug middle-end/112733] " pinskia at gcc dot gnu.org
2023-11-27 19:16 ` pinskia at gcc dot gnu.org
2023-11-27 19:21 ` pinskia at gcc dot gnu.org
2023-11-27 19:25 ` pinskia at gcc dot gnu.org
2023-11-28  9:02 ` rguenth at gcc dot gnu.org
2023-11-28  9:35 ` jakub at gcc dot gnu.org
2023-11-28 14:34 ` jakub at gcc dot gnu.org
2023-11-28 14:48 ` jakub at gcc dot gnu.org
2023-11-28 15:56 ` jakub at gcc dot gnu.org [this message]
2023-11-28 16:01 ` jakub at gcc dot gnu.org
2023-11-28 17:37 ` jakub at gcc dot gnu.org
2023-11-28 17:43 ` jakub at gcc dot gnu.org
2023-11-29  9:29 ` jakub at gcc dot gnu.org
2023-11-29 11:27 ` cvs-commit at gcc dot gnu.org
2023-11-30  8:15 ` cvs-commit at gcc dot gnu.org
2023-11-30  8:16 ` jakub at gcc dot gnu.org
2023-12-15 14:05 ` cvs-commit at gcc dot gnu.org
2023-12-16  0:38 ` cvs-commit at gcc dot gnu.org
2023-12-17 13:55 ` cvs-commit at gcc dot gnu.org

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=bug-112733-4-yPA7c8tvXC@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).