Re: [PATCH 4/4] ipa-cp: Better opportunity evaluation and ranking

2026-01-08 Thread Jan Hubicka
> gcc/ChangeLog:
> 
> 2025-11-16  Martin Jambor  
> 
>   * ipa-cp.cc (good_cloning_opportunity_p): Dump a message when
>   bailing out early too.
>   (find_more_scalar_values_for_callers_subset): Rename to
>   find_scalar_values_for_callers_subset, collect constants regardless of
>   what is already in the vector.  Remove dumping.
>   (find_more_contexts_for_caller_subset): Rename to
>   find_contexts_for_caller_subset, collect contexts regardless of what
>   is already in the vector.  Remove dumping.
>   (find_aggregate_values_for_callers_subset): Rename to
>   find_aggregate_values_for_callers_subset_gc, implement using new
>   functions.
>   (find_aggregate_values_for_callers_subset_1): New function.
>   (find_aggregate_values_for_callers_subset): Likewise.
>   (copy_known_vectors_add_val): Removed.
>   (dump_reestimation_message): New function.
>   (decide_about_value): Remove formal parameter avals, compute it
>   independently, and use it to estimate local cloning effects.
>   (struct cloning_opportunity_ranking): New type.
>   (compare_cloning_opportunities): New function.
>   (cloning_opportunity_ranking_evaluation): Likewise.
>   (decide_whether_version_node): Pre-sort candidates for cloning before
>   really evaluating them.  Calculate context independent values only
>   when considering versioning for all contexts.
> 
> gcc/testsuite/ChangeLog:
> 
> 2025-11-16  Martin Jambor  
> 
>   * gcc.dg/ipa/ipcp-agg-2.c: Adjust dump test.
>   * gcc.dg/ipa/ipcp-agg-3.c: Likewise.
>   * gcc.dg/ipa/ipcp-agg-4.c: Likewise.
>   * gcc.dg/vect/pr101145_1.c: Compile with -fno-ipa-cp.
>   * gcc.dg/vect/pr101145_2.c: Likewise.
>   * gcc.dg/vect/pr101145_3.c: Likewise.

OK,
can you please add a testcase (incrementally) that tests the new
functionality.
Something like
void
foo (int a, int b)
{
  if (a == 0 && b == 0)
return;
  
}

and later use foo (0, 0)

Thanks,
Honza


Re: [PATCH 4/4] ipa-cp: Better opportunity evaluation and ranking

2025-12-04 Thread Martin Jambor
Hello,

On Sun, Nov 16 2025, Martin Jambor wrote:
> Hi,
>
> this modifies the decision making stage of IPA-CP in two ways:
>

I'd like to ping this patch.  I have however fixed an omission in a
checking assert, so I'm reposting the updated patch below.

Thanks,

Martin


This modifies the decision making stage of IPA-CP in two ways:

Previously, local effects of the cloning were estimated only for the
constant that was being considered, even though the calls which bring
it also carry other constants.  With this patch, all knowsn constants
for the given subset of caller edges are considered and the heuritics
should therefore have more information and generally work better.

Also, when evaluating the opportunities for a given node, IPA-CP
previously just iterate over the parameters starting with the first
one and if any opportunity looked profitable, it was carried out and
associated calling edges were redirected, even if this precludes some
even better opportunity.  The patch tries to mitigate this by first
using the initial estimates to sort all cloning candidates and then
iterate in that order.

The one difference from the version I posted before is that I have
extended the checking assert making sure the value we clone for is
indeed used to also work for non-aggregate constants and polymorphic
contexts.

gcc/ChangeLog:

2025-12-01  Martin Jambor  

* ipa-cp.cc (good_cloning_opportunity_p): Dump a message when
bailing out early too.
(find_more_scalar_values_for_callers_subset): Rename to
find_scalar_values_for_callers_subset, collect constants regardless of
what is already in the vector.  Remove dumping.
(find_more_contexts_for_caller_subset): Rename to
find_contexts_for_caller_subset, collect contexts regardless of what
is already in the vector.  Remove dumping.
(find_aggregate_values_for_callers_subset): Rename to
find_aggregate_values_for_callers_subset_gc, implement using new
functions.
(find_aggregate_values_for_callers_subset_1): New function.
(find_aggregate_values_for_callers_subset): Likewise.
(copy_known_vectors_add_val): Removed.
(dump_reestimation_message): New function.
(decide_about_value): Remove formal parameter avals, compute it
independently, and use it to estimate local cloning effects.
(struct cloning_opportunity_ranking): New type.
(compare_cloning_opportunities): New function.
(cloning_opportunity_ranking_evaluation): Likewise.
(decide_whether_version_node): Pre-sort candidates for cloning before
really evaluating them.  Calculate context independent values only
when considering versioning for all contexts.
(ipcp_val_agg_replacement_ok_p): Renamed to
ipcp_val_replacement_ok_p, check also non-aggregate values.

gcc/testsuite/ChangeLog:

2025-11-16  Martin Jambor  

* gcc.dg/ipa/ipcp-agg-2.c: Adjust dump test.
* gcc.dg/ipa/ipcp-agg-3.c: Likewise.
* gcc.dg/ipa/ipcp-agg-4.c: Likewise.
* gcc.dg/vect/pr101145_1.c: Compile with -fno-ipa-cp.
* gcc.dg/vect/pr101145_2.c: Likewise.
* gcc.dg/vect/pr101145_3.c: Likewise.
---
 gcc/ipa-cp.cc  | 501 +
 gcc/testsuite/gcc.dg/ipa/ipcp-agg-2.c  |   2 +-
 gcc/testsuite/gcc.dg/ipa/ipcp-agg-3.c  |   4 +-
 gcc/testsuite/gcc.dg/ipa/ipcp-agg-4.c  |   2 +-
 gcc/testsuite/gcc.dg/vect/pr101145_1.c |   2 +-
 gcc/testsuite/gcc.dg/vect/pr101145_2.c |   2 +-
 gcc/testsuite/gcc.dg/vect/pr101145_3.c |   2 +-
 7 files changed, 345 insertions(+), 170 deletions(-)

diff --git a/gcc/ipa-cp.cc b/gcc/ipa-cp.cc
index f4959178321..65784761360 100644
--- a/gcc/ipa-cp.cc
+++ b/gcc/ipa-cp.cc
@@ -3403,7 +3403,13 @@ good_cloning_opportunity_p (struct cgraph_node *node, 
sreal time_benefit,
   /* If there is no call which was executed in profiling or where
 profile is missing, we do not want to clone.  */
   || (!called_without_ipa_profile && !count_sum.nonzero_p ()))
-return false;
+{
+  if (dump_file && (dump_flags & TDF_DETAILS))
+   fprintf (dump_file, " good_cloning_opportunity_p (time: %g, "
+"size: %i): Definitely not good or prohibited.\n",
+time_benefit.to_double (), size_cost);
+  return false;
+}
 
   gcc_assert (size_cost > 0);
 
@@ -5244,24 +5250,29 @@ self_recursive_agg_pass_through_p (const cgraph_edge 
*cs,
KNOWN_CSTS with constants that are also known for all of the CALLERS.  */
 
 static void
-find_more_scalar_values_for_callers_subset (struct cgraph_node *node,
-   vec &known_csts,
-   const vec &callers)
+find_scalar_values_for_callers_subset (vec &known_csts,
+  ipa_node_params *info,
+  const vec &callers)
 {
-  ipa_node_params *info = ipa_no