From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id A40A63858C52 for ; Mon, 6 Mar 2023 11:01:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A40A63858C52 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 2728E1FE0B; Mon, 6 Mar 2023 11:01:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1678100479; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=4aWSf6w/0pfbdnvgnoWFNqm/FMy58tPdIx4wMkhQg0g=; b=z8IJufF+L6kcFdWdYc/5LgNAzJjyBS3kdGECBCgGjn1/vEOZtWwfyvIKKnjrgch8ERu9/O hBB62vCHKIX3T2jEEPPTmiSHx0/J9i1D+/Bfh27weSIOPmUhswDkEvVxLhonskuzxNKoy+ jQ1jg5ICnm09yyl40m50kbX3lUF5uTc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1678100479; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=4aWSf6w/0pfbdnvgnoWFNqm/FMy58tPdIx4wMkhQg0g=; b=ke6+eu/F5NQdwVTOQH1Bf45MnI/Ln5/MA3m1agWFwG00vyWe8cik65SyYa+jCJG8a256VK nWnLEYGiJmQChzBQ== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 176272C141; Mon, 6 Mar 2023 11:01:19 +0000 (UTC) Date: Mon, 6 Mar 2023 11:01:18 +0000 (UTC) From: Richard Biener To: Jonathan Wakely cc: gcc-patches@gcc.gnu.org, Jakub Jelinek Subject: Re: [PATCH] [RFC] RAII auto_mpfr and autp_mpz In-Reply-To: Message-ID: References: <20230306101121.3CFDA13A66@imap2.suse-dmz.suse.de> User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Mon, 6 Mar 2023, Jonathan Wakely wrote: > On Mon, 6 Mar 2023 at 10:11, Richard Biener 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. > > > > I've converted two example places (where lifetime is trivial). > > > > I've sofar only build cc1 with the change. Any comments? > > > > Thanks, > > Richard. > > > > * 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 | 15 +++++++++++++++ > > gcc/system.h | 14 ++++++++++++++ > > gcc/tree-ssa-loop-niter.cc | 10 +--------- > > 4 files changed, 32 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..2db2ecc94d4 100644 > > --- a/gcc/realmpfr.h > > +++ b/gcc/realmpfr.h > > @@ -24,6 +24,21 @@ > > #include > > #include > > > > +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; } > > > This implicit conversion makes the following mistake possible, if code > is incorrectly converted to use it: > > auto_mpfr m (prec); > // ... > mpfr_clear (m); // oops! > > You could prevent that by adding this to the class body: > > friend void mpfr_clear (auto_mpfr&) = delete; > > This will be a better match for calls to mpfr_clear(m) than using the > implicit conversion then calling the real function, and will give an > error if used: > auto.cc:20:13: error: use of deleted function 'void mpfr_clear(auto_mpfr&)' > > This deleted friend will not be a candidate for calls to mpfr_clear > with an argument of any other type, only for calls with an argument of > type auto_mpfr. OK, it might be OK to mpfr_clear() twice and/or mpfr_clear/mpfr_init again. Quite possibly mpfr_init should get the same treatmen, mixing auto_* with explicit lifetime management is bad. > > + > > + auto_mpfr (const auto_mpfr &) = delete; > > This class has an implicit-defined assignment operator, which would > result in a leaks and double-frees. > You should add: > auto_mpfr &operator=(const auto_mpfr &) = delete; > This ensures it can't becopied by construction or assignment. > > The same two comments apply to auto_mpz. Thanks a lot, I've adjusted the patch to the one below. Richard. >From c2736b929a3d0440432f31e65f5c89f4ec9dc21d Mon Sep 17 00:00:00 2001 From: Richard Biener Date: Mon, 6 Mar 2023 11:06:38 +0100 Subject: [PATCH] [RFC] RAII auto_mpfr and autp_mpz To: gcc-patches@gcc.gnu.org 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). * 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..a91cd2b6321 100644 --- a/gcc/realmpfr.h +++ b/gcc/realmpfr.h @@ -24,6 +24,26 @@ #include #include +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 64cd5a49258..b16c384df6d 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 + +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