Bug 110378 - IPA-SRA for destructors
Summary: IPA-SRA for destructors
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: ipa (show other bugs)
Version: 13.1.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks: 109849 std::vector
  Show dependency treegraph
 
Reported: 2023-06-23 16:14 UTC by Jan Hubicka
Modified: 2024-12-21 09:16 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2023-07-31 00:00:00


Attachments
Simplest testcase (417 bytes, text/x-csrc)
2023-07-31 15:25 UTC, Martin Jambor
Details
Testcase with single inheritance (528 bytes, text/x-csrc)
2023-07-31 16:06 UTC, Martin Jambor
Details
Testcase with non-zero offset with pass-through split (566 bytes, text/x-csrc)
2023-07-31 16:08 UTC, Martin Jambor
Details
pr110378-1.C.083i.sra (1.25 KB, text/plain)
2023-08-08 16:20 UTC, Christophe Lyon
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Hubicka 2023-06-23 16:14:54 UTC
In thestcase from PR109849 built with profile feedback we get the following cleanup:

<L10>:
  D.28322 ={v} {CLOBBER};
  cur ={v} {CLOBBER(eol)};
  std::vector<std::pair<unsigned int, unsigned int> >::~vector (&stack);
  _28 = __builtin_eh_pointer (2);
  __builtin_unwind_resume (_28);

which calls destructor of std::vector. This destructor compiles to:

void std::vector<std::pair<unsigned int, unsigned int> >::~vector (struct vector * const this)
{
  struct pair * _6;
  struct pair * _7;
  long int _8;
  long unsigned int _15;

;;   basic block 2, loop depth 0, count 10000 (precise)
;;    prev block 0, next block 3, flags: (NEW, REACHABLE, VISITED)
;;    pred:       ENTRY [always]  count:10000 (precise) (FALLTHRU,EXECUTABLE)
  _6 = MEM[(struct _Vector_base *)this_3(D)]._M_impl.D.25963._M_end_of_storage;
  _7 = MEM[(struct _Vector_base *)this_3(D)]._M_impl.D.25963._M_start;
  _8 = _6 - _7;
  if (_7 != 0B)
    goto <bb 3>; [100.00%]
  else
    goto <bb 4>; [0.00%]
;;    succ:       3 [always]  count:10000 (precise) (TRUE_VALUE,EXECUTABLE)
;;                4 [never]  count:0 (precise) (FALSE_VALUE,EXECUTABLE)

;;   basic block 3, loop depth 0, count 10000 (precise)
;;    prev block 2, next block 4, flags: (NEW, REACHABLE, VISITED)
;;    pred:       2 [always]  count:10000 (precise) (TRUE_VALUE,EXECUTABLE)
  _15 = (long unsigned int) _8;
  operator delete (_7, _15); [tail call]
;;    succ:       4 [always]  count:10000 (precise) (FALLTHRU,EXECUTABLE)

;;   basic block 4, loop depth 0, count 10000 (precise)
;;    prev block 3, next block 1, flags: (NEW, REACHABLE, VISITED)
;;    pred:       2 [never]  count:0 (precise) (FALSE_VALUE,EXECUTABLE)
;;                3 [always]  count:10000 (precise) (FALLTHRU,EXECUTABLE)
  return;
;;    succ:       EXIT [always]  count:10000 (precise) (EXECUTABLE) /usr/include/c++/13/bits/stl_vector.h:735:7

}

and it is used twice in code that is considered cold. Since the code itself is longer than the call of operator delete, it makes sense to not inline it.

Before ipa-cp it is:
Released 8 names, 53.33%, removed 8 holes
void std::vector<std::pair<unsigned int, unsigned int> >::~vector (struct vector * const this)
{
  struct pair * _1;
  struct pair * _2;
  struct pair * _6;
  struct pair * _7;
  long int _8;
  long int _9;
  long unsigned int _10;
  struct _Vector_impl * _11;

;;   basic block 2, loop depth 0, count 1073741824 (estimated locally), maybe hot
;;    prev block 0, next block 3, flags: (NEW, REACHABLE, VISITED)
;;    pred:       ENTRY [always]  count:1073741824 (estimated locally) (FALLTHRU,EXECUTABLE)
  _1 = this_3(D)->D.26656._M_impl.D.25963._M_finish;
  _2 = this_3(D)->D.26656._M_impl.D.25963._M_start;
  std::_Destroy<std::pair<unsigned int, unsigned int>*> (_2, _1);
  _6 = MEM[(struct _Vector_base *)this_3(D)]._M_impl.D.25963._M_end_of_storage;
  _7 = MEM[(struct _Vector_base *)this_3(D)]._M_impl.D.25963._M_start;
  _8 = _6 - _7;
  _9 = _8 /[ex] 8;
  _10 = (long unsigned int) _9;
  if (_7 != 0B)
    goto <bb 3>; [53.47%]
  else
    goto <bb 4>; [46.53%]
;;    succ:       3 [53.5% (guessed)]  count:574129752 (estimated locally) (TRUE_VALUE,EXECUTABLE)
;;                4 [46.5% (guessed)]  count:499612072 (estimated locally) (FALSE_VALUE,EXECUTABLE)

;;   basic block 3, loop depth 0, count 574129753 (estimated locally), maybe hot
;;    prev block 2, next block 4, flags: (NEW, REACHABLE, VISITED)
;;    pred:       2 [53.5% (guessed)]  count:574129752 (estimated locally) (TRUE_VALUE,EXECUTABLE)
  _11 = &MEM[(struct _Vector_base *)this_3(D)]._M_impl;
  std::__new_allocator<std::pair<unsigned int, unsigned int> >::deallocate (_11, _7, _10);
;;    succ:       4 [always]  count:574129753 (estimated locally) (FALLTHRU,EXECUTABLE) /usr/include/c++/13/bits/alloc_traits.h:516:35

;;   basic block 4, loop depth 0, count 1073741824 (estimated locally), maybe hot
;;    prev block 3, next block 1, flags: (NEW, REACHABLE, VISITED)
;;    pred:       2 [46.5% (guessed)]  count:499612072 (estimated locally) (FALSE_VALUE,EXECUTABLE)
;;                3 [always]  count:574129753 (estimated locally) (FALLTHRU,EXECUTABLE) /usr/include/c++/13/bits/alloc_traits.h:516:35
  *this_3(D) ={v} {CLOBBER};
  return;
;;    succ:       EXIT [always]  count:1073741824 (estimated locally) (EXECUTABLE) /usr/include/c++/13/bits/stl_vector.h:735:7

}

I think it is the clobber that prevents ipa-sra :)
Comment 1 Jan Hubicka 2023-06-23 16:15:45 UTC
In general we may also learn to handle modifications to parameters that are clobbered later as read-only.
Comment 2 Martin Jambor 2023-07-31 15:25:12 UTC
Created attachment 55663 [details]
Simplest testcase

The PR 109849 testcase behavior changes over time, so I prepared three specialized for this PR.  This one is simplest, just making sure a clobber is not considered a write for the purposes of IPA-SRA parameter splitting.
Comment 3 Martin Jambor 2023-07-31 16:06:07 UTC
Created attachment 55664 [details]
Testcase with single inheritance

This testcase is somewhat more difficult and addressing will mean not
just changes in the IPA-SRA analysis but also in the modification.
Both need to deal with the following:

     _1 = &this_2(D)->D.2842;
    {anonymous}::foo::~foo (_1);

Modification must see _1 before its use to understand it is just
another name for a (split) parameter and must behave accordingly when
processing the call statement (i.e. prepare ground for call
redirection later).
Comment 4 Martin Jambor 2023-07-31 16:08:40 UTC
Created attachment 55665 [details]
Testcase with non-zero offset with pass-through split

This testcase is similar to the previous one but on top of it also deals with non-zero offsets which is currently not supported by IPA-SRA analysis and will also require some assumption adjustments on the modification side (see TODOs in ptr_parm_has_nonarg_uses and in ipa_param_body_adjustments::modify_call_stmt).
Comment 5 Martin Jambor 2023-08-01 12:45:18 UTC
I have proposed a patch addressing the simplest case of the three on the mailing list:
https://gcc.gnu.org/pipermail/gcc-patches/2023-July/625895.html
Comment 6 GCC Commits 2023-08-07 17:15:02 UTC
The master branch has been updated by Martin Jambor <jamborm@gcc.gnu.org>:

https://gcc.gnu.org/g:da1a888b524d620c7a17f368b69c46934b69495c

commit r14-3038-gda1a888b524d620c7a17f368b69c46934b69495c
Author: Martin Jambor <mjambor@suse.cz>
Date:   Mon Aug 7 19:13:41 2023 +0200

    ipa-sra: Don't consider CLOBBERS as writes preventing splitting
    
    When IPA-SRA detects whether a parameter passed by reference is
    written to, it does not special case CLOBBERs which means it often
    bails out unnecessarily, especially when dealing with C++ destructors.
    Fixed by the obvious continue in the two relevant loops and by adding
    a simple function that marks the clobbers in the transformation code
    as statements to be removed.
    
    gcc/ChangeLog:
    
    2023-08-04  Martin Jambor  <mjambor@suse.cz>
    
            PR ipa/110378
            * ipa-param-manipulation.h (class ipa_param_body_adjustments): New
            members get_ddef_if_exists_and_is_used and mark_clobbers_dead.
            * ipa-sra.cc (isra_track_scalar_value_uses): Ignore clobbers.
            (ptr_parm_has_nonarg_uses): Likewise.
            * ipa-param-manipulation.cc
            (ipa_param_body_adjustments::get_ddef_if_exists_and_is_used): New.
            (ipa_param_body_adjustments::mark_dead_statements): Move initial
            checks to get_ddef_if_exists_and_is_used.
            (ipa_param_body_adjustments::mark_clobbers_dead): New.
            (ipa_param_body_adjustments::common_initialization): Call
            mark_clobbers_dead when splitting.
    
    gcc/testsuite/ChangeLog:
    
    2023-07-31  Martin Jambor  <mjambor@suse.cz>
    
            PR ipa/110378
            * g++.dg/ipa/pr110378-1.C: New test.
Comment 7 Christophe Lyon 2023-08-08 16:20:09 UTC
The new test fails on arm:
FAIL: g++.dg/ipa/pr110378-1.C -std=gnu++14  scan-ipa-dump sra "Will split parameter 0"
FAIL: g++.dg/ipa/pr110378-1.C -std=gnu++14  scan-tree-dump-not optimized "shouldnotexist"
FAIL: g++.dg/ipa/pr110378-1.C -std=gnu++17  scan-ipa-dump sra "Will split parameter 0"
FAIL: g++.dg/ipa/pr110378-1.C -std=gnu++17  scan-tree-dump-not optimized "shouldnotexist"
FAIL: g++.dg/ipa/pr110378-1.C -std=gnu++20  scan-ipa-dump sra "Will split parameter 0"
FAIL: g++.dg/ipa/pr110378-1.C -std=gnu++20  scan-tree-dump-not optimized "shouldnotexist"
FAIL: g++.dg/ipa/pr110378-1.C -std=gnu++98  scan-ipa-dump sra "Will split parameter 0"
FAIL: g++.dg/ipa/pr110378-1.C -std=gnu++98  scan-tree-dump-not optimized "shouldnotexist"

I'm attaching pr110378-1.C.083i.sra
Comment 8 Christophe Lyon 2023-08-08 16:20:41 UTC
Created attachment 55707 [details]
pr110378-1.C.083i.sra
Comment 9 GCC Commits 2023-10-03 16:53:14 UTC
The master branch has been updated by Martin Jambor <jamborm@gcc.gnu.org>:

https://gcc.gnu.org/g:14d0c509898b0361f78284c05556035edde6d1e0

commit r14-4383-g14d0c509898b0361f78284c05556035edde6d1e0
Author: Martin Jambor <mjambor@suse.cz>
Date:   Tue Oct 3 18:44:52 2023 +0200

    ipa-sra: Allow IPA-SRA in presence of returns which will be removed
    
    Testing on 32bit arm revealed that even the simplest case of PR 110378
    was still not resolved there because destructors were rturning this
    pointer.  Needless to say, the return value of those destructors often
    is just not used, which IPA-SRA can already detect in time.  Since
    such enhancement seems generally useful, here it is.
    
    The patch simply adds two flag to respective summaries to mark down
    situations when it encounters either a simple direct use of a defaut
    definition SSA_NAME of a paramter, which means that the parameter may
    still be split when rturn value is removed, and when any derived use
    of it is returned, allowing for complete removal in that case, instead
    of discarding it as a candidate for removal or splitting like we do
    now.  The IPA phase then simply checks that we indeed plan to remove
    the return value before allowing any transformation to be considered
    in such cases.
    
    gcc/ChangeLog:
    
    2023-08-18  Martin Jambor  <mjambor@suse.cz>
    
            PR ipa/110378
            * ipa-param-manipulation.cc
            (ipa_param_body_adjustments::mark_dead_statements): Verify that any
            return uses of PARAM will be removed.
            (ipa_param_body_adjustments::mark_clobbers_dead): Likewise.
            * ipa-sra.cc (isra_param_desc): New fields
            remove_only_when_retval_removed and split_only_when_retval_removed.
            (struct gensum_param_desc): Likewise.  Fix comment long line.
            (ipa_sra_function_summaries::duplicate): Copy the new flags.
            (dump_gensum_param_descriptor): Dump the new flags.
            (dump_isra_param_descriptor): Likewise.
            (isra_track_scalar_value_uses): New parameter desc.  Set its flag
            remove_only_when_retval_removed when encountering a simple return.
            (isra_track_scalar_param_local_uses): Replace parameter call_uses_p
            with desc.  Pass it to isra_track_scalar_value_uses and set its
            call_uses.
            (ptr_parm_has_nonarg_uses): Accept parameter descriptor as a
            parameter.  If there is a direct return use, mark any..
            (create_parameter_descriptors): Pass the whole parameter descriptor to
            isra_track_scalar_param_local_uses and ptr_parm_has_nonarg_uses.
            (process_scan_results): Copy the new flags.
            (isra_write_node_summary): Stream the new flags.
            (isra_read_node_info): Likewise.
            (adjust_parameter_descriptions): Check that transformations
            requring return removal only happen when return value is removed.
            Restructure main loop.  Adjust dump message.
    
    gcc/testsuite/ChangeLog:
    
    2023-08-18  Martin Jambor  <mjambor@suse.cz>
    
            PR ipa/110378
            * gcc.dg/ipa/ipa-sra-32.c: New test.
            * gcc.dg/ipa/pr110378-4.c: Likewise.
            * gcc.dg/ipa/ipa-sra-4.c: Use a return value.
Comment 10 Jan Hubicka 2024-12-19 15:58:45 UTC
Martin, I think this is fixed?
Comment 11 Martin Jambor 2024-12-20 12:35:53 UTC
IIUC only the simplest testcase of the three was fixed.  I'll try to re-check soon-ish.