https://gcc.gnu.org/g:2a7a438299b91d9d95899c2df07112198ebef8a7
commit 2a7a438299b91d9d95899c2df07112198ebef8a7 Author: Tobias Burnus <tbur...@baylibre.com> Date: Thu Jan 23 22:47:39 2025 +0100 OpenMP/C++: Fix declare variant with reference-returning functions gcc/cp/ChangeLog: * decl.cc (omp_declare_variant_finalize_one): Strip indirect ref around variant-function call when processing a variant. gcc/testsuite/ChangeLog: * g++.dg/gomp/declare-variant-9.C: New test. (cherry picked from commit f011f8908182fd05ddd9a34881507b8584c44fb2) Diff: --- gcc/cp/ChangeLog.omp | 8 ++++++++ gcc/cp/decl.cc | 3 +++ gcc/testsuite/ChangeLog.omp | 7 +++++++ gcc/testsuite/g++.dg/gomp/declare-variant-9.C | 29 +++++++++++++++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/gcc/cp/ChangeLog.omp b/gcc/cp/ChangeLog.omp index 03f4631cc565..6745dd6f0947 100644 --- a/gcc/cp/ChangeLog.omp +++ b/gcc/cp/ChangeLog.omp @@ -1,3 +1,11 @@ +2025-01-23 Tobias Burnus <tbur...@baylibre.com> + + Backported from master: + 2024-10-31 Tobias Burnus <tbur...@baylibre.com> + + * decl.cc (omp_declare_variant_finalize_one): Strip indirect ref + around variant-function call when processing a variant. + 2025-01-23 Tobias Burnus <tbur...@baylibre.com> Backported from master: diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index cf9bc12667fb..b1cf16d5f567 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -8318,6 +8318,9 @@ omp_declare_variant_finalize_one (tree decl, tree attr) if (variant == error_mark_node && !processing_template_decl) return true; + if (TREE_CODE (variant) == INDIRECT_REF) + variant = TREE_OPERAND (variant, 0); + variant = cp_get_callee_fndecl_nofold (variant); input_location = save_loc; diff --git a/gcc/testsuite/ChangeLog.omp b/gcc/testsuite/ChangeLog.omp index c8952ea407c6..b6551c7f8879 100644 --- a/gcc/testsuite/ChangeLog.omp +++ b/gcc/testsuite/ChangeLog.omp @@ -1,3 +1,10 @@ +2025-01-23 Tobias Burnus <tbur...@baylibre.com> + + Backported from master: + 2024-10-31 Tobias Burnus <tbur...@baylibre.com> + + * g++.dg/gomp/declare-variant-9.C: New test. + 2025-01-23 Tobias Burnus <tbur...@baylibre.com> Backported from master: diff --git a/gcc/testsuite/g++.dg/gomp/declare-variant-9.C b/gcc/testsuite/g++.dg/gomp/declare-variant-9.C new file mode 100644 index 000000000000..7e26d8b11aee --- /dev/null +++ b/gcc/testsuite/g++.dg/gomp/declare-variant-9.C @@ -0,0 +1,29 @@ +/* { dg-additional-options "-fdump-tree-gimple" } */ +int &variant_fn(); + +#pragma omp declare variant(variant_fn) match(user={condition(1)}) +int &bar(); + +void sub(int &a) +{ + bar(); + a = bar(); +} + +template<typename T> +T &templ_var_fn(T x); + +#pragma omp declare variant(templ_var_fn) match(user={condition(1)}) +template<typename T> +T &templ_base_fn(T x); + +void run(int &b) +{ + templ_base_fn<int>(5); + b = templ_base_fn<int>(7); +} + +/* { dg-final { scan-tree-dump " variant_fn \\(\\);" "gimple" } } */ +/* { dg-final { scan-tree-dump " _1 = variant_fn \\(\\);" "gimple" } } */ +/* { dg-final { scan-tree-dump " templ_var_fn<int> \\(5\\);" "gimple" } } */ +/* { dg-final { scan-tree-dump " _1 = templ_var_fn<int> \\(7\\);" "gimple" } } */