Hi,
this is a revised patch adding a new param for controlling the creation
of callback edges. The previous version added a binary param which only
affected builtins and internal functions. After a discussion with Martin,
we decided that letting the param take values 0,1,2 and making it
control the creation of all callback edges is better. This allows the
user to control not only the edges of builtins and internal functions,
but also edges of dispatchers from code they might not be able to edit,
e.g. system headers.
Bootstrapped and regtested on x86_64-linux without regressions. OK for
master?
Best regards,
Josef
-- >8 --
This patch adds a new param which allows users to disable callback
edge creation for builtins, internal and callback_only annotated functions.
Up until now, the only way to avoid them is to pass -fno-ipa-cp, which
is not ideal. 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.
Assisted-by: Claude Opus 5.5 (docs)
gcc/ChangeLog:
* attr-callback.cc (callback_edge_creation_gate): New function,
returns TRUE if a callback edge can be created for the given
call statement.
* attr-callback.h (callback_edge_creation_gate): Add decl.
* doc/params.texi: Document ipa-callback-edges.
* ipa-prop.cc (ipa_compute_jump_functions_for_edge): Use
callback_edge_creation_gate.
(ipa_analyze_call_uses): Likewise.
* params.opt: Add ipa-callback-edges param.
gcc/testsuite/ChangeLog:
* gcc.dg/callback-edges-param-0.c: New test.
* gcc.dg/callback-edges-param-1.c: New test.
* gcc.dg/callback-edges-param-2.c: New test.
Signed-off-by: Josef Melcr <[email protected]>
---
gcc/attr-callback.cc | 20 +++++++++
gcc/attr-callback.h | 4 ++
gcc/doc/params.texi | 9 ++++
gcc/ipa-prop.cc | 15 ++++---
gcc/params.opt | 4 ++
gcc/testsuite/gcc.dg/callback-edges-param-0.c | 39 +++++++++++++++++
gcc/testsuite/gcc.dg/callback-edges-param-1.c | 42 +++++++++++++++++++
gcc/testsuite/gcc.dg/callback-edges-param-2.c | 42 +++++++++++++++++++
8 files changed, 169 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
create mode 100644 gcc/testsuite/gcc.dg/callback-edges-param-2.c
diff --git a/gcc/attr-callback.cc b/gcc/attr-callback.cc
index 6496844738f..65e2fb50152 100644
--- a/gcc/attr-callback.cc
+++ b/gcc/attr-callback.cc
@@ -197,3 +197,23 @@ 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)
+{
+ gcc_checking_assert (cfun);
+
+ /* All edges allowed. */
+ if (param_ipa_callback_edges == 2)
+ return true;
+
+ /* Only builtins. */
+ tree decl = gimple_call_fndecl (call_stmt);
+ if (gimple_call_internal_p (call_stmt, IFN_ASSUME)
+ || (decl && fndecl_built_in_p (decl)))
+ return param_ipa_callback_edges == 1;
+
+ return false;
+}
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 43a1397971d..483428e326d 100644
--- a/gcc/doc/params.texi
+++ b/gcc/doc/params.texi
@@ -1234,6 +1234,15 @@ 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-callback-edges
+@item ipa-callback-edges
+Controls the creation of callback edges, which allow interprocedural
+optimizations of functions passed as callbacks to builtins, internal
+functions and @code{callback_only} annotated functions. A value of 0
+disables all callback edges, a value of 1 allows only callback edges
+for builtins and internal functions, and a value of 2 allows all
+callback edges. The default is 2.
+
@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 61b2dbd5d6b..edd5a4020c5 100644
--- a/gcc/ipa-prop.cc
+++ b/gcc/ipa-prop.cc
@@ -2813,7 +2813,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. */
@@ -3432,11 +3432,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 e6a53950acf..4ba3fe8ef2e 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-callback-edges=
+Common Joined UInteger Var(param_ipa_callback_edges) Init(2) IntegerRange(0,
2) Param Optimization
+Control creation of callback edges: 0 disables them, 1 allows them only for
builtins and internal functions, 2 allows them also for callback_only annotated
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..a540a488d34
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/callback-edges-param-0.c
@@ -0,0 +1,39 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fipa-cp -fdump-ipa-cp-details -fopenmp
--param=ipa-callback-edges=0" } */
+
+int test ();
+
+void test2 (void *);
+
+void do_something ();
+
+void
+openmp_functions ()
+{
+/* Regular OpenMP builtin. */
+#pragma omp parallel
+ test ();
+
+/* Special-cased OpenMP builtin. */
+#pragma omp task
+ test ();
+}
+
+void
+assumes ()
+{
+ __attribute__ ((assume (test ())));
+ if (test ())
+ do_something ();
+}
+
+__attribute__ ((callback_only (1, 2)))
+void dispatcher (void (*) (void *), void *);
+
+void
+user_specified (void *ptr)
+{
+ dispatcher (test2, ptr);
+}
+
+/* { 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..78f67c90faf
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/callback-edges-param-1.c
@@ -0,0 +1,42 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fipa-cp -fdump-ipa-cp-details -fopenmp
--param=ipa-callback-edges=1" } */
+
+int test ();
+
+void test2 (void *);
+
+void do_something ();
+
+void
+openmp_functions ()
+{
+/* Regular OpenMP builtin. */
+#pragma omp parallel
+ test ();
+
+/* Special-cased OpenMP builtin. */
+#pragma omp task
+ test ();
+}
+
+void
+assumes ()
+{
+ __attribute__ ((assume (test ())));
+ if (test ())
+ do_something ();
+}
+
+__attribute__ ((callback_only (1, 2)))
+void dispatcher (void (*) (void *), void *);
+
+void
+user_specified (void *ptr)
+{
+ dispatcher (test2, ptr);
+}
+
+/* { dg-final { scan-ipa-dump "Created callback edge \[^\\n\]* \\.ASSUME" "cp"
} } */
+/* { dg-final { scan-ipa-dump "Created callback edge \[^\\n\]*
__builtin_GOMP_parallel" "cp" } } */
+/* { dg-final { scan-ipa-dump "Created callback edge \[^\\n\]*
__builtin_GOMP_task" "cp" } } */
+/* { dg-final { scan-ipa-dump-not "Created callback edge \[^\\n\]* dispatcher"
"cp" } } */
diff --git a/gcc/testsuite/gcc.dg/callback-edges-param-2.c
b/gcc/testsuite/gcc.dg/callback-edges-param-2.c
new file mode 100644
index 00000000000..c24f93a46a9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/callback-edges-param-2.c
@@ -0,0 +1,42 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fipa-cp -fdump-ipa-cp-details -fopenmp
--param=ipa-callback-edges=2" } */
+
+int test ();
+
+void test2 (void *);
+
+void do_something ();
+
+void
+openmp_functions ()
+{
+/* Regular OpenMP builtin. */
+#pragma omp parallel
+ test ();
+
+/* Special-cased OpenMP builtin. */
+#pragma omp task
+ test ();
+}
+
+void
+assumes ()
+{
+ __attribute__ ((assume (test ())));
+ if (test ())
+ do_something ();
+}
+
+__attribute__ ((callback_only (1, 2)))
+void dispatcher (void (*) (void *), void *);
+
+void
+user_specified (void *ptr)
+{
+ dispatcher (test2, ptr);
+}
+
+/* { dg-final { scan-ipa-dump "Created callback edge \[^\\n\]* \\.ASSUME" "cp"
} } */
+/* { dg-final { scan-ipa-dump "Created callback edge \[^\\n\]*
__builtin_GOMP_parallel" "cp" } } */
+/* { dg-final { scan-ipa-dump "Created callback edge \[^\\n\]*
__builtin_GOMP_task" "cp" } } */
+/* { dg-final { scan-ipa-dump "Created callback edge \[^\\n\]* dispatcher"
"cp" } } */
--
2.56.0