ipa: Add param for callback edge creation for builtins

Message ID 20261103131625.101620-2-josef.melcr@suse.com
State New
Headers
Series ipa: Add param for callback edge creation for builtins |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-aarch64-bootstrap success Build passed

Commit Message

Josef Melcr Nov. 3, 2026, 1:16 p.m. UTC
  Hi,
this patch adds a new param which would allow users to disable callback
edge creation for builtin functions.  Currently, the only way to avoid
them is to pass -fno-ipa-cp, which is not great.  The main use case of
this param is to give users the ability to disable them if needed, in
cases when there is a bug with them, it degrades performance instead of
improving it and so on.

Bootstrapped and regtested on x86_64-linux without any new regressions.
OK for master?

Best regards,
Josef

-- >8 --

Add a new param --param=ipa-builtin-callback-edges, which allows users
to disable the creation of callback edges for builtin functions and
internal calls, e.g. IFN_ASSUME.  That way, if a user finds a bug or
other undesired behavior related to callback edges, they can just
disable them directly instead of passing -fno-ipa-cp.

gcc/ChangeLog:

	* attr-callback.cc (callback_edge_creation_gate): New function.
	* attr-callback.h (callback_edge_creation_gate): Add decl.
	* doc/params.texi: Document the param.
	* ipa-prop.cc (ipa_compute_jump_functions_for_edge): Use the gate.
	(ipa_analyze_call_uses): Likewise.
	* params.opt: Add --param=ipa-builtin-callback-edges.

gcc/testsuite/ChangeLog:

	* gcc.dg/callback-edges-param-0.c: New test.
	* gcc.dg/callback-edges-param-1.c: New test.

Signed-off-by: Josef Melcr <josef.melcr@suse.com>
---
 gcc/attr-callback.cc                          | 11 +++++++++
 gcc/attr-callback.h                           |  4 ++++
 gcc/doc/params.texi                           |  6 +++++
 gcc/ipa-prop.cc                               | 15 +++++++-----
 gcc/params.opt                                |  4 ++++
 gcc/testsuite/gcc.dg/callback-edges-param-0.c | 23 +++++++++++++++++++
 gcc/testsuite/gcc.dg/callback-edges-param-1.c | 23 +++++++++++++++++++
 7 files changed, 80 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/callback-edges-param-0.c
 create mode 100644 gcc/testsuite/gcc.dg/callback-edges-param-1.c
  

Patch

diff --git a/gcc/attr-callback.cc b/gcc/attr-callback.cc
index 6496844738f..7e34fd20251 100644
--- a/gcc/attr-callback.cc
+++ b/gcc/attr-callback.cc
@@ -197,3 +197,14 @@  callback_redirect_edge (cgraph_edge *e)
   tree new_addr = build_fold_addr_expr_loc (loc, e->callee->decl);
   gimple_call_set_arg (e->call_stmt, ci->fn_idx, new_addr);
 }
+
+/* Returns TRUE if a callback edge is allowed for a dispatching function based
+   on its CALL_STMT.  */
+bool
+callback_edge_creation_gate (const gcall *call_stmt)
+{
+  if (gimple_call_internal_p (call_stmt)
+      || fndecl_built_in_p (gimple_call_fndecl (call_stmt)))
+    return param_ipa_builtin_callback_edges;
+  return true;
+}
diff --git a/gcc/attr-callback.h b/gcc/attr-callback.h
index b969b713a20..d826f6742cb 100644
--- a/gcc/attr-callback.h
+++ b/gcc/attr-callback.h
@@ -81,4 +81,8 @@  size_t callback_num_args (tree attr);
    edge.  */
 void callback_redirect_edge (cgraph_edge *e);
 
+/* Returns TRUE if a callback edge is allowed for a dispatching function based
+   on its CALL_STMT.  */
+bool callback_edge_creation_gate (const gcall *call_stmt);
+
 #endif /* ATTR_CALLBACK_H  */
diff --git a/gcc/doc/params.texi b/gcc/doc/params.texi
index 090af64a7e7..52614a74a5b 100644
--- a/gcc/doc/params.texi
+++ b/gcc/doc/params.texi
@@ -1233,6 +1233,12 @@  loop in the loop nest by a given number of iterations.  The strip
 length can be changed using the @option{loop-block-tile-size}
 parameter.
 
+@paindex ipa-builtin-callback-edges
+@item ipa-builtin-callback-edges
+Enables interprocedural optimization opportunities of callback
+functions passed to builtins and internal functions.  Enabled by
+default, disable it with @option{--param=ipa-builtin-callback-edges=0}.
+
 @paindex ipa-jump-function-lookups
 @item ipa-jump-function-lookups
 Specifies number of statements visited during jump function offset discovery.
diff --git a/gcc/ipa-prop.cc b/gcc/ipa-prop.cc
index c614ddccbbb..43a497bb5a3 100644
--- a/gcc/ipa-prop.cc
+++ b/gcc/ipa-prop.cc
@@ -2600,7 +2600,7 @@  ipa_compute_jump_functions_for_edge (struct ipa_func_body_info *fbi,
 	    {
 	      tree pointee = TREE_OPERAND (arg, 0);
 	      if (TREE_CODE (pointee) == FUNCTION_DECL && !cs->callback
-		  && cs->callee)
+		  && cs->callee && callback_edge_creation_gate (call))
 		{
 		  /* Argument is a pointer to a function. Look for a callback
 		     attribute describing this argument.  */
@@ -3219,11 +3219,14 @@  ipa_analyze_call_uses (struct ipa_func_body_info *fbi, gcall *call)
      ipa_compute_jump_functions_for_edge doesn't analyze call statements.  */
   if (callback_ifn_assume_call_p (call))
     {
-      tree callee_decl = TREE_OPERAND (gimple_call_arg (call, 0), 0);
-      cgraph_node *target = cgraph_node::get_create (callee_decl);
-      cgraph_edge *cbei
-	= cgraph_edge::make_callback_internal (fbi->node, target, call);
-      ipa_compute_jump_functions_for_edge (fbi, cbei);
+      if (callback_edge_creation_gate (call))
+	{
+	  tree callee_decl = TREE_OPERAND (gimple_call_arg (call, 0), 0);
+	  cgraph_node *target = cgraph_node::get_create (callee_decl);
+	  cgraph_edge *cbei
+	    = cgraph_edge::make_callback_internal (fbi->node, target, call);
+	  ipa_compute_jump_functions_for_edge (fbi, cbei);
+	}
       return;
     }
 
diff --git a/gcc/params.opt b/gcc/params.opt
index 29a5c7018a8..c8b49d90a2e 100644
--- a/gcc/params.opt
+++ b/gcc/params.opt
@@ -253,6 +253,10 @@  How much can given compilation unit grow because of the inlining (as a percentag
 Common Joined UInteger Var(param_integer_share_limit) Init(251) IntegerRange(2, 65536) Param
 The upper bound for sharing integer constants.
 
+-param=ipa-builtin-callback-edges=
+Common Joined UInteger Var(param_ipa_builtin_callback_edges) Init(1) IntegerRange(0, 1) Param Optimization
+Enable optimizations of callback functions passed to builtins and internal functions.
+
 -param=ipa-cp-eval-threshold=
 Common Joined UInteger Var(param_ipa_cp_eval_threshold) Init(1100) Param Optimization
 Threshold ipa-cp opportunity evaluation that is still considered beneficial to clone.
diff --git a/gcc/testsuite/gcc.dg/callback-edges-param-0.c b/gcc/testsuite/gcc.dg/callback-edges-param-0.c
new file mode 100644
index 00000000000..ecbb40fcebe
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/callback-edges-param-0.c
@@ -0,0 +1,23 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fipa-cp -fdump-ipa-cp-details -fopenmp --param=ipa-builtin-callback-edges=0"  } */
+
+int test ();
+
+void do_something ();
+
+void
+openmp_functions ()
+{
+#pragma omp parallel
+  test ();
+}
+
+void
+assumes ()
+{
+  __attribute__ ((assume (test ())));
+  if (test ())
+    do_something ();
+}
+
+/* { dg-final { scan-ipa-dump-not "Created callback edge" "cp" } } */
diff --git a/gcc/testsuite/gcc.dg/callback-edges-param-1.c b/gcc/testsuite/gcc.dg/callback-edges-param-1.c
new file mode 100644
index 00000000000..7ba83f86c63
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/callback-edges-param-1.c
@@ -0,0 +1,23 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fipa-cp -fdump-ipa-cp-details -fopenmp --param=ipa-builtin-callback-edges=1"  } */
+
+int test ();
+
+void do_something ();
+
+void
+openmp_functions ()
+{
+#pragma omp parallel
+  test ();
+}
+
+void
+assumes ()
+{
+  __attribute__ ((assume (test ())));
+  if (test ())
+    do_something ();
+}
+
+/* { dg-final { scan-ipa-dump-times "Created callback edge" 2 "cp" } } */