For recursive function, IPA does not allow constant propagation on parameter that is used to control recursion. It is common that the parameter is inc/dec (arithmetic op) a constant before entering next recursion. The following example gives a general source code pattern. int recur_fn (int i) { if (i == 6) { do_work (); return 0; } do_prepare (); recur_fn (i + 1); do_post (); return 0; } int foo() { ... recur_fn (1); ... } A straight forward optimization is to duplicate recur_fn () 6 times, which constitute a calling chain on the specialized copies as: recur_fn<i=1>() -> recur_fn<i=2>() -> ... -> recur_fn<i=6>()
Let me take a look.
(In reply to Martin Liška from comment #1) > Let me take a look. I've created a patch (https://gcc.gnu.org/ml/gcc-patches/2019-10/msg01260.html), could you take a time to review it?
(In reply to Feng Xue from comment #2) > (In reply to Martin Liška from comment #1) > > Let me take a look. > > I've created a patch > (https://gcc.gnu.org/ml/gcc-patches/2019-10/msg01260.html), could you take a > time to review it? It's domain of Martin Jambor and Honza. They will provide a review, don't worry.
Author: tkoenig Date: Sun Nov 3 22:33:53 2019 New Revision: 277760 URL: https://gcc.gnu.org/viewcvs?rev=277760&root=gcc&view=rev Log: 2019-11-03 Thomas Koenig <tkoenig@gcc.gnu.org> PR fortran/92133 * trans-decl.c (gfc_get_symbol_decl): If __def_init actually contains a value, put it into the read-only section. Modified: trunk/gcc/fortran/ChangeLog trunk/gcc/fortran/trans-decl.c
(In reply to Thomas Koenig from comment #4) > Author: tkoenig > Date: Sun Nov 3 22:33:53 2019 > New Revision: 277760 > > URL: https://gcc.gnu.org/viewcvs?rev=277760&root=gcc&view=rev > Log: > 2019-11-03 Thomas Koenig <tkoenig@gcc.gnu.org> > > PR fortran/92133 > * trans-decl.c (gfc_get_symbol_decl): If __def_init actually > contains a value, put it into the read-only section. > > > Modified: > trunk/gcc/fortran/ChangeLog > trunk/gcc/fortran/trans-decl.c Your patch's tracker number should be 92113, not 92133.
(In reply to Feng Xue from comment #5) > (In reply to Thomas Koenig from comment #4) > > Author: tkoenig > > Date: Sun Nov 3 22:33:53 2019 > > New Revision: 277760 > > > > URL: https://gcc.gnu.org/viewcvs?rev=277760&root=gcc&view=rev > > Log: > > 2019-11-03 Thomas Koenig <tkoenig@gcc.gnu.org> > > > > PR fortran/92133 > > * trans-decl.c (gfc_get_symbol_decl): If __def_init actually > > contains a value, put it into the read-only section. > > > > > > Modified: > > trunk/gcc/fortran/ChangeLog > > trunk/gcc/fortran/trans-decl.c > > Your patch's tracker number should be 92113, not 92133. Unfortunately, this is one of the things that can't be undone... Fixed in the ChangeLog, and made a remark in PR 92113.
Author: fxue Date: Mon Dec 2 06:37:30 2019 New Revision: 278893 URL: https://gcc.gnu.org/viewcvs?rev=278893&root=gcc&view=rev Log: Enable recursive function versioning 2019-12-02 Feng Xue <fxue@os.amperecomputing.com> PR ipa/92133 * doc/invoke.texi (ipa-cp-max-recursive-depth): Document new option. (ipa-cp-min-recursive-probability): Likewise. * params.opt (ipa-cp-max-recursive-depth): New. (ipa-cp-min-recursive-probability): Likewise. * ipa-cp.c (ipcp_lattice<valtype>::add_value): Add two new parameters val_p and unlimited. (self_recursively_generated_p): New function. (get_val_across_arith_op): Likewise. (propagate_vals_across_arith_jfunc): Add constant propagation for self-recursive function. (incorporate_penalties): Do not penalize pure self-recursive function. (good_cloning_opportunity_p): Dump node_is_self_scc flag. (propagate_constants_topo): Set node_is_self_scc flag for cgraph node. (get_info_about_necessary_edges): Relax hotness check for edge to self-recursive function. * ipa-prop.h (ipa_node_params): Add new field node_is_self_scc. 2019-12-02 Feng Xue <fxue@os.amperecomputing.com> PR ipa/92133 * gcc.dg/ipa/ipa-clone-2.c: New test. Added: trunk/gcc/testsuite/gcc.dg/ipa/ipa-clone-2.c Modified: trunk/gcc/ChangeLog trunk/gcc/doc/invoke.texi trunk/gcc/ipa-cp.c trunk/gcc/ipa-prop.h trunk/gcc/params.opt trunk/gcc/testsuite/ChangeLog
I believe this is now implemented, so marking as fixed.
Ok. For any followups on this, I'll create new tracker.
(In reply to Feng Xue from comment #9) > Ok. For any followups on this, I'll create new tracker. Seems "--param ipa-cp-eval-threshold=0 --param large-unit-insns=20000 -fno-inline" are required to do the recursive clone for digits_2?
--param ipa-cp-eval-threshold=1 -param ipcp-unit-growth=80 is enough.