https://gcc.gnu.org/g:73701e14e91c57cd3aefcddf835aec003d8ce49f
commit 73701e14e91c57cd3aefcddf835aec003d8ce49f Author: Paul-Antoine Arras <par...@baylibre.com> Date: Mon Jan 6 17:00:10 2025 +0100 Only apply adjust_args in OpenMP dispatch if variant substitution occurs This is a followup to 084ea8ad584 OpenMP: middle-end support for dispatch + adjust_args. This patch fixes a bug that caused arguments in an OpenMP dispatch call to be modified even when no variant substitution occurred. gcc/ChangeLog: * gimplify.cc (gimplify_call_expr): Create variable variant_substituted_p to control whether adjust_args applies. gcc/testsuite/ChangeLog: * c-c++-common/gomp/adjust-args-4.c: New test. (cherry picked from commit aa688dd6302fd9fd4a6ede232bbe63781e672ae9) Diff: --- gcc/ChangeLog.omp | 8 ++++++++ gcc/gimplify.cc | 11 +++++++++-- gcc/testsuite/ChangeLog.omp | 7 +++++++ gcc/testsuite/c-c++-common/gomp/adjust-args-4.c | 24 ++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/gcc/ChangeLog.omp b/gcc/ChangeLog.omp index bda3e3de82fd..5b5017ee56ac 100644 --- a/gcc/ChangeLog.omp +++ b/gcc/ChangeLog.omp @@ -1,3 +1,11 @@ +2025-01-27 Paul-Antoine Arras <par...@baylibre.com> + + Backported from master: + 2025-01-07 Paul-Antoine Arras <par...@baylibre.com> + + * gimplify.cc (gimplify_call_expr): Create variable + variant_substituted_p to control whether adjust_args applies. + 2025-01-27 Paul-Antoine Arras <par...@baylibre.com> Backported from master: diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc index 06995d9172d2..977275eb9c99 100644 --- a/gcc/gimplify.cc +++ b/gcc/gimplify.cc @@ -4075,7 +4075,8 @@ gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback) enum gimplify_status ret; int i, nargs; gcall *call; - bool builtin_va_start_p = false, omp_dispatch_p = false; + bool builtin_va_start_p = false, omp_dispatch_p = false, + variant_substituted_p = false; location_t loc = EXPR_LOCATION (*expr_p); gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR); @@ -4264,6 +4265,12 @@ gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback) stop here. */ if (*expr_p != orig) return ret; + /* FIXME: This is not adequate because of the above case. Argument + mangling for variant functions needs to happen where we generate + the new calls in gimplify_variant_call_expr instead of below in + this function. */ + if (get_callee_fndecl (*expr_p) != fndecl) + variant_substituted_p = true; } /* There is a sequence point before the call, so any side effects in @@ -4411,7 +4418,7 @@ gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback) device_num = OMP_CLAUSE_OPERAND (c, 0); } - if (!is_device_ptr) + if (variant_substituted_p && !is_device_ptr) { if (device_num == NULL_TREE) { diff --git a/gcc/testsuite/ChangeLog.omp b/gcc/testsuite/ChangeLog.omp index 70a249e0ea00..f662215ac739 100644 --- a/gcc/testsuite/ChangeLog.omp +++ b/gcc/testsuite/ChangeLog.omp @@ -1,3 +1,10 @@ +2025-01-27 Paul-Antoine Arras <par...@baylibre.com> + + Backported from master: + 2025-01-07 Paul-Antoine Arras <par...@baylibre.com> + + * c-c++-common/gomp/adjust-args-4.c: New test. + 2025-01-27 Paul-Antoine Arras <par...@baylibre.com> Backported from master: diff --git a/gcc/testsuite/c-c++-common/gomp/adjust-args-4.c b/gcc/testsuite/c-c++-common/gomp/adjust-args-4.c new file mode 100644 index 000000000000..377932e1b9cc --- /dev/null +++ b/gcc/testsuite/c-c++-common/gomp/adjust-args-4.c @@ -0,0 +1,24 @@ +/* { dg-do compile } */ +/* { dg-additional-options "-fdump-tree-gimple" } */ + +/* Ensure that adjust_args is only applied when variant substitution happens. */ + +void h(int *); +void f(int *); +#pragma omp declare variant(f) match(construct={dispatch}) adjust_args(need_device_ptr : x) +void g(int *x); + +void foo(int *y) +{ + #pragma omp dispatch + h(y); + #pragma omp dispatch + f(y); + #pragma omp dispatch + g(y); +} + +/* { dg-final { scan-tree-dump-times "h \\(y\\);" 1 "gimple" } } */ +/* { dg-final { scan-tree-dump-times "f \\(y\\);" 1 "gimple" } } */ +/* { dg-final { scan-tree-dump-times "D\.\[0-9]+ = __builtin_omp_get_mapped_ptr \\(y, D\.\[0-9]+\\);" 1 "gimple" } } */ +/* { dg-final { scan-tree-dump-times "f \\(D\.\[0-9]+\\);" 1 "gimple" } } */