vec.h: Properly destruct elements in auto_vec auto storage [PR118400]

Message ID Z4govmz+I5CFxIi1@tucnak
State New
Headers
Series vec.h: Properly destruct elements in auto_vec auto storage [PR118400] |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gcc_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gcc_check--master-aarch64 fail Patch failed to apply

Commit Message

Jakub Jelinek Jan. 15, 2025, 9:29 p.m. UTC
  Hi!

For T with non-trivial destructors, we were destructing objects in the
vector on release only when not using auto storage of auto_vec.

The following patch calls truncate (0) instead of m_vecpfx.m_num clearing,
and truncate takes care of that destruction:
  unsigned l = length ();
  gcc_checking_assert (l >= size);
  if (!std::is_trivially_destructible <T>::value)
    vec_destruct (address () + size, l - size);
  m_vecpfx.m_num = size;

Bootstrapped/regtested on x86_64-linux and i686-linux, plus tested on the
testcase from the PR under valgrind, ok for trunk?

2025-01-15  Jakub Jelinek  <jakub@redhat.com>

	PR ipa/118400
	* vec.h (vec<T, va_heap, vl_ptr>::release): Call m_vec->truncate (0)
	instead of clearing m_vec->m_vecpfx.m_num.


	Jakub
  

Comments

Richard Biener Jan. 16, 2025, 7:54 a.m. UTC | #1
On Wed, 15 Jan 2025, Jakub Jelinek wrote:

> Hi!
> 
> For T with non-trivial destructors, we were destructing objects in the
> vector on release only when not using auto storage of auto_vec.
> 
> The following patch calls truncate (0) instead of m_vecpfx.m_num clearing,
> and truncate takes care of that destruction:
>   unsigned l = length ();
>   gcc_checking_assert (l >= size);
>   if (!std::is_trivially_destructible <T>::value)
>     vec_destruct (address () + size, l - size);
>   m_vecpfx.m_num = size;
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, plus tested on the
> testcase from the PR under valgrind, ok for trunk?

OK and thanks for catching this - it escaped my search for the problem...

Richard.

> 2025-01-15  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR ipa/118400
> 	* vec.h (vec<T, va_heap, vl_ptr>::release): Call m_vec->truncate (0)
> 	instead of clearing m_vec->m_vecpfx.m_num.
> 
> --- gcc/vec.h.jj	2025-01-15 08:43:16.259249515 +0100
> +++ gcc/vec.h	2025-01-15 09:32:25.755433708 +0100
> @@ -2020,7 +2020,7 @@ vec<T, va_heap, vl_ptr>::release (void)
>  
>    if (using_auto_storage ())
>      {
> -      m_vec->m_vecpfx.m_num = 0;
> +      m_vec->truncate (0);
>        return;
>      }
>  
> 
> 	Jakub
> 
>
  

Patch

--- gcc/vec.h.jj	2025-01-15 08:43:16.259249515 +0100
+++ gcc/vec.h	2025-01-15 09:32:25.755433708 +0100
@@ -2020,7 +2020,7 @@  vec<T, va_heap, vl_ptr>::release (void)
 
   if (using_auto_storage ())
     {
-      m_vec->m_vecpfx.m_num = 0;
+      m_vec->truncate (0);
       return;
     }