public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] RAII auto_mpfr and autp_mpz
@ 2023-04-18 13:39 Richard Biener
  2023-04-22  8:02 ` [PATCH] Fix up bootstrap with GCC 4.[89] after RAII auto_mpfr and autp_mpz [PR109589] Jakub Jelinek
  0 siblings, 1 reply; 3+ messages in thread
From: Richard Biener @ 2023-04-18 13:39 UTC (permalink / raw)
  To: gcc-patches

The following adds two RAII classes, one for mpz_t and one for mpfr_t
making object lifetime management easier.  Both formerly require
explicit initialization with {mpz,mpfr}_init and release with
{mpz,mpfr}_clear.

I've converted two example places (where lifetime is trivial).

As discussed earlier and improved upon comments.  Re-bootstrapped
and tested on x86_64-unknown-linux-gnu, pushed to trunk.

	* system.h (class auto_mpz): New,
	* realmpfr.h (class auto_mpfr): Likewise.
	* fold-const-call.cc (do_mpfr_arg1): Use auto_mpfr.
	(do_mpfr_arg2): Likewise.
	* tree-ssa-loop-niter.cc (bound_difference): Use auto_mpz;
---
 gcc/fold-const-call.cc     |  8 ++------
 gcc/realmpfr.h             | 20 ++++++++++++++++++++
 gcc/system.h               | 18 ++++++++++++++++++
 gcc/tree-ssa-loop-niter.cc | 10 +---------
 4 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/gcc/fold-const-call.cc b/gcc/fold-const-call.cc
index 43819c1f984..fa0b287cc8a 100644
--- a/gcc/fold-const-call.cc
+++ b/gcc/fold-const-call.cc
@@ -130,14 +130,12 @@ do_mpfr_arg1 (real_value *result,
 
   int prec = format->p;
   mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
-  mpfr_t m;
 
-  mpfr_init2 (m, prec);
+  auto_mpfr m (prec);
   mpfr_from_real (m, arg, MPFR_RNDN);
   mpfr_clear_flags ();
   bool inexact = func (m, m, rnd);
   bool ok = do_mpfr_ckconv (result, m, inexact, format);
-  mpfr_clear (m);
 
   return ok;
 }
@@ -224,14 +222,12 @@ do_mpfr_arg2 (real_value *result,
 
   int prec = format->p;
   mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
-  mpfr_t m;
 
-  mpfr_init2 (m, prec);
+  auto_mpfr m (prec);
   mpfr_from_real (m, arg1, MPFR_RNDN);
   mpfr_clear_flags ();
   bool inexact = func (m, arg0.to_shwi (), m, rnd);
   bool ok = do_mpfr_ckconv (result, m, inexact, format);
-  mpfr_clear (m);
 
   return ok;
 }
diff --git a/gcc/realmpfr.h b/gcc/realmpfr.h
index 5e032c05f25..3824e62da19 100644
--- a/gcc/realmpfr.h
+++ b/gcc/realmpfr.h
@@ -24,6 +24,26 @@
 #include <mpfr.h>
 #include <mpc.h>
 
+class auto_mpfr
+{
+public:
+  auto_mpfr () { mpfr_init (m_mpfr); }
+  explicit auto_mpfr (mpfr_prec_t prec) { mpfr_init2 (m_mpfr, prec); }
+  ~auto_mpfr () { mpfr_clear (m_mpfr); }
+
+  operator mpfr_t& () { return m_mpfr; }
+
+  auto_mpfr (const auto_mpfr &) = delete;
+  auto_mpfr &operator= (const auto_mpfr &) = delete;
+
+  friend void mpfr_clear (auto_mpfr&) = delete;
+  friend void mpfr_init (auto_mpfr&) = delete;
+  friend void mpfr_init2 (auto_mpfr&, mpfr_prec_t) = delete;
+
+private:
+  mpfr_t m_mpfr;
+};
+
 /* Convert between MPFR and REAL_VALUE_TYPE.  The caller is
    responsible for initializing and clearing the MPFR parameter.  */
 
diff --git a/gcc/system.h b/gcc/system.h
index cf45db3f97e..71d8a040353 100644
--- a/gcc/system.h
+++ b/gcc/system.h
@@ -701,6 +701,24 @@ extern int vsnprintf (char *, size_t, const char *, va_list);
 /* Do not introduce a gmp.h dependency on the build system.  */
 #ifndef GENERATOR_FILE
 #include <gmp.h>
+
+class auto_mpz
+{
+public:
+  auto_mpz () { mpz_init (m_mpz); }
+  ~auto_mpz () { mpz_clear (m_mpz); }
+
+  operator mpz_t& () { return m_mpz; }
+
+  auto_mpz (const auto_mpz &) = delete;
+  auto_mpz &operator= (const auto_mpz &) = delete;
+
+  friend void mpz_clear (auto_mpz&) = delete;
+  friend void mpz_init (auto_mpz&) = delete;
+
+private:
+  mpz_t m_mpz;
+};
 #endif
 
 /* Get libiberty declarations.  */
diff --git a/gcc/tree-ssa-loop-niter.cc b/gcc/tree-ssa-loop-niter.cc
index dc4c7a418f6..dcfba2fc7ae 100644
--- a/gcc/tree-ssa-loop-niter.cc
+++ b/gcc/tree-ssa-loop-niter.cc
@@ -722,7 +722,6 @@ bound_difference (class loop *loop, tree x, tree y, bounds *bnds)
   tree type = TREE_TYPE (x);
   tree varx, vary;
   mpz_t offx, offy;
-  mpz_t minx, maxx, miny, maxy;
   int cnt = 0;
   edge e;
   basic_block bb;
@@ -754,19 +753,12 @@ bound_difference (class loop *loop, tree x, tree y, bounds *bnds)
     {
       /* Otherwise, use the value ranges to determine the initial
 	 estimates on below and up.  */
-      mpz_init (minx);
-      mpz_init (maxx);
-      mpz_init (miny);
-      mpz_init (maxy);
+      auto_mpz minx, maxx, miny, maxy;
       determine_value_range (loop, type, varx, offx, minx, maxx);
       determine_value_range (loop, type, vary, offy, miny, maxy);
 
       mpz_sub (bnds->below, minx, maxy);
       mpz_sub (bnds->up, maxx, miny);
-      mpz_clear (minx);
-      mpz_clear (maxx);
-      mpz_clear (miny);
-      mpz_clear (maxy);
     }
 
   /* If both X and Y are constants, we cannot get any more precise.  */
-- 
2.35.3

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

* [PATCH] Fix up bootstrap with GCC 4.[89] after RAII auto_mpfr and autp_mpz [PR109589]
  2023-04-18 13:39 [PATCH] RAII auto_mpfr and autp_mpz Richard Biener
@ 2023-04-22  8:02 ` Jakub Jelinek
  2023-04-22 17:37   ` Richard Biener
  0 siblings, 1 reply; 3+ messages in thread
From: Jakub Jelinek @ 2023-04-22  8:02 UTC (permalink / raw)
  To: Richard Biener; +Cc: gcc-patches

Hi!

On Tue, Apr 18, 2023 at 03:39:41PM +0200, Richard Biener via Gcc-patches wrote:
> The following adds two RAII classes, one for mpz_t and one for mpfr_t
> making object lifetime management easier.  Both formerly require
> explicit initialization with {mpz,mpfr}_init and release with
> {mpz,mpfr}_clear.

This unfortunately broke bootstrap when using GCC 4.8.x or 4.9.x as
it uses deleted friends which weren't supported until PR62101 fixed
them in 2014 for GCC 5.

The following patch adds an workaround, not deleting those friends
for those old versions.
While it means if people add those mp*_{init{,2},clear} calls on auto_mp*
objects they won't notice when doing non-bootstrap builds using
very old system compilers, people should be bootstrapping their changes
and it will be caught during bootstraps even when starting with those
old compilers, plus most people actually use much newer compilers
when developing.

Bootstrapped/regtested on x86_64-linux and i686-linux (with gcc 12.1.1
as system compiler) and powerpc64-linux and powerpc64le-linux (with gcc
4.8.5 as system compiler, where it previously failed bootstrap).
Ok for trunk?

2023-04-22  Jakub Jelinek  <jakub@redhat.com>

	PR bootstrap/109589
	* system.h (class auto_mpz): Workaround PR62101 bug in GCC 4.8 and 4.9.
	* realmpfr.h (class auto_mpfr): Likewise.

--- gcc/system.h.jj	2023-04-20 09:36:09.097375720 +0200
+++ gcc/system.h	2023-04-21 20:13:09.212049563 +0200
@@ -714,8 +714,11 @@ public:
   auto_mpz (const auto_mpz &) = delete;
   auto_mpz &operator= (const auto_mpz &) = delete;
 
+#if GCC_VERSION < 4008 || GCC_VERSION >= 5000
+  /* GCC 4.8 and 4.9 don't support this, only fixed in PR62101 for 5.0.  */
   friend void mpz_clear (auto_mpz&) = delete;
   friend void mpz_init (auto_mpz&) = delete;
+#endif
 
 private:
   mpz_t m_mpz;
--- gcc/realmpfr.h.jj	2023-04-20 09:36:09.066376175 +0200
+++ gcc/realmpfr.h	2023-04-21 20:13:36.191663089 +0200
@@ -37,9 +37,12 @@ public:
   auto_mpfr (const auto_mpfr &) = delete;
   auto_mpfr &operator= (const auto_mpfr &) = delete;
 
+#if GCC_VERSION < 4008 || GCC_VERSION >= 5000
+  /* GCC 4.8 and 4.9 don't support this, only fixed in PR62101 for 5.0.  */
   friend void mpfr_clear (auto_mpfr&) = delete;
   friend void mpfr_init (auto_mpfr&) = delete;
   friend void mpfr_init2 (auto_mpfr&, mpfr_prec_t) = delete;
+#endif
 
 private:
   mpfr_t m_mpfr;


	Jakub


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

* Re: [PATCH] Fix up bootstrap with GCC 4.[89] after RAII auto_mpfr and autp_mpz [PR109589]
  2023-04-22  8:02 ` [PATCH] Fix up bootstrap with GCC 4.[89] after RAII auto_mpfr and autp_mpz [PR109589] Jakub Jelinek
@ 2023-04-22 17:37   ` Richard Biener
  0 siblings, 0 replies; 3+ messages in thread
From: Richard Biener @ 2023-04-22 17:37 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches



> Am 22.04.2023 um 10:03 schrieb Jakub Jelinek <jakub@redhat.com>:
> 
> Hi!
> 
>> On Tue, Apr 18, 2023 at 03:39:41PM +0200, Richard Biener via Gcc-patches wrote:
>> The following adds two RAII classes, one for mpz_t and one for mpfr_t
>> making object lifetime management easier.  Both formerly require
>> explicit initialization with {mpz,mpfr}_init and release with
>> {mpz,mpfr}_clear.
> 
> This unfortunately broke bootstrap when using GCC 4.8.x or 4.9.x as
> it uses deleted friends which weren't supported until PR62101 fixed
> them in 2014 for GCC 5.
> 
> The following patch adds an workaround, not deleting those friends
> for those old versions.
> While it means if people add those mp*_{init{,2},clear} calls on auto_mp*
> objects they won't notice when doing non-bootstrap builds using
> very old system compilers, people should be bootstrapping their changes
> and it will be caught during bootstraps even when starting with those
> old compilers, plus most people actually use much newer compilers
> when developing.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux (with gcc 12.1.1
> as system compiler) and powerpc64-linux and powerpc64le-linux (with gcc
> 4.8.5 as system compiler, where it previously failed bootstrap).
> Ok for trunk?

Ok

> 2023-04-22  Jakub Jelinek  <jakub@redhat.com>
> 
>    PR bootstrap/109589
>    * system.h (class auto_mpz): Workaround PR62101 bug in GCC 4.8 and 4.9.
>    * realmpfr.h (class auto_mpfr): Likewise.
> 
> --- gcc/system.h.jj    2023-04-20 09:36:09.097375720 +0200
> +++ gcc/system.h    2023-04-21 20:13:09.212049563 +0200
> @@ -714,8 +714,11 @@ public:
>   auto_mpz (const auto_mpz &) = delete;
>   auto_mpz &operator= (const auto_mpz &) = delete;
> 
> +#if GCC_VERSION < 4008 || GCC_VERSION >= 5000
> +  /* GCC 4.8 and 4.9 don't support this, only fixed in PR62101 for 5.0.  */
>   friend void mpz_clear (auto_mpz&) = delete;
>   friend void mpz_init (auto_mpz&) = delete;
> +#endif
> 
> private:
>   mpz_t m_mpz;
> --- gcc/realmpfr.h.jj    2023-04-20 09:36:09.066376175 +0200
> +++ gcc/realmpfr.h    2023-04-21 20:13:36.191663089 +0200
> @@ -37,9 +37,12 @@ public:
>   auto_mpfr (const auto_mpfr &) = delete;
>   auto_mpfr &operator= (const auto_mpfr &) = delete;
> 
> +#if GCC_VERSION < 4008 || GCC_VERSION >= 5000
> +  /* GCC 4.8 and 4.9 don't support this, only fixed in PR62101 for 5.0.  */
>   friend void mpfr_clear (auto_mpfr&) = delete;
>   friend void mpfr_init (auto_mpfr&) = delete;
>   friend void mpfr_init2 (auto_mpfr&, mpfr_prec_t) = delete;
> +#endif
> 
> private:
>   mpfr_t m_mpfr;
> 
> 
>    Jakub
> 

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

end of thread, other threads:[~2023-04-22 17:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-18 13:39 [PATCH] RAII auto_mpfr and autp_mpz Richard Biener
2023-04-22  8:02 ` [PATCH] Fix up bootstrap with GCC 4.[89] after RAII auto_mpfr and autp_mpz [PR109589] Jakub Jelinek
2023-04-22 17:37   ` 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).