https://gcc.gnu.org/g:9233cd010db7c924d83f7bd5511ee3e8aa13a5ec
commit 9233cd010db7c924d83f7bd5511ee3e8aa13a5ec Author: Andrew Stubbs <[email protected]> Date: Mon Nov 9 11:12:40 2026 +0000 openmp: fix reductions of complex types The new vector-optimized reductions did allow for complex types, and caused a regression on SIMD. This patch fixes the problem by doing two separate reductions, where possible, and otherwise disabling the optimized reductions. Assisted-by: Claude Opus 5.5 Claude generated the testcases and assisted with the code. gcc/ChangeLog: * omp-low.cc (lower_rec_simd_input_clauses): Disable SIMT for complex multiplication reductions. (lower_rec_input_clauses): Reduce the real and imaginary parts of complex values separately. (lower_reduction_clauses): In "simt" offload mode, merge a single complex reduction clause one lane at a time under the lock, unless it is combined across the wavefront. libgomp/ChangeLog: * testsuite/libgomp.c-c++-common/simt-reduction-amdgcn-5.c: New test. * testsuite/libgomp.c-c++-common/target-simd-reduction-complex-1.c: New test. Diff: --- gcc/omp-low.cc | 54 ++++++++- .../libgomp.c-c++-common/simt-reduction-amdgcn-5.c | 64 +++++++++++ .../target-simd-reduction-complex-1.c | 122 +++++++++++++++++++++ 3 files changed, 237 insertions(+), 3 deletions(-) diff --git a/gcc/omp-low.cc b/gcc/omp-low.cc index 3b7a1c6468c5..8deb12b89fae 100644 --- a/gcc/omp-low.cc +++ b/gcc/omp-low.cc @@ -5192,6 +5192,18 @@ lower_rec_simd_input_clauses (tree new_var, omp_context *ctx, sctx->max_vf = 1; break; } + + tree type = TREE_TYPE (OMP_CLAUSE_DECL (c)); + if (omp_privatize_by_reference (OMP_CLAUSE_DECL (c))) + type = TREE_TYPE (type); + if (OMP_CLAUSE_REDUCTION_CODE (c) == MULT_EXPR + && TREE_CODE (type) == COMPLEX_TYPE) + { + /* IFN_GOMP_SIMT_REDUCE combines the parts of a complex + value separately, which is wrong for multiplication. */ + sctx->max_vf = 1; + break; + } } } if (maybe_gt (sctx->max_vf, 1U)) @@ -7283,9 +7295,29 @@ lower_rec_input_clauses (tree clauses, gimple_seq *ilist, gimple_seq *dlist, if (sctx.is_simt) { tree op = build_int_cst (unsigned_type_node, code); - x = build_call_expr_internal_loc - (UNKNOWN_LOCATION, IFN_GOMP_SIMT_REDUCE, - TREE_TYPE (ivar), 2, ivar, op); + tree type = TREE_TYPE (ivar); + if (TREE_CODE (type) == COMPLEX_TYPE) + { + /* Reduce the real and imaginary parts + separately. Only addition gets here; see + lower_rec_simd_input_clauses. */ + gcc_checking_assert (code == PLUS_EXPR); + tree ptype = TREE_TYPE (type); + tree re = build1 (REALPART_EXPR, ptype, ivar); + tree im = build1 (IMAGPART_EXPR, ptype, + unshare_expr (ivar)); + re = build_call_expr_internal_loc + (UNKNOWN_LOCATION, IFN_GOMP_SIMT_REDUCE, + ptype, 2, re, op); + im = build_call_expr_internal_loc + (UNKNOWN_LOCATION, IFN_GOMP_SIMT_REDUCE, + ptype, 2, im, op); + x = build2 (COMPLEX_EXPR, type, re, im); + } + else + x = build_call_expr_internal_loc + (UNKNOWN_LOCATION, IFN_GOMP_SIMT_REDUCE, + type, 2, ivar, op); gimplify_assign (ivar, x, &llist[2]); } tree ivar2 = ivar; @@ -8562,6 +8594,22 @@ lower_reduction_clauses (tree clauses, gimple_seq *stmt_seqp, break; } count++; + if (!omp_simt_threads_p (ctx)) + continue; + /* The OpenMP threads may be the lanes of a wavefront. OMP_ATOMIC of + a complex value uses a lock, which the lanes would hold together, + so merge them one lane at a time instead, unless they are combined + across the wavefront first. */ + tree type = TREE_TYPE (OMP_CLAUSE_DECL (c)); + if (omp_privatize_by_reference (OMP_CLAUSE_DECL (c))) + type = TREE_TYPE (type); + if (TREE_CODE (type) == COMPLEX_TYPE + && !omp_simt_threads_reduction_p (OMP_CLAUSE_REDUCTION_CODE (c), + type, ctx)) + { + count = -1; + break; + } } if (count == 0) diff --git a/libgomp/testsuite/libgomp.c-c++-common/simt-reduction-amdgcn-5.c b/libgomp/testsuite/libgomp.c-c++-common/simt-reduction-amdgcn-5.c new file mode 100644 index 000000000000..bdc63461a5b0 --- /dev/null +++ b/libgomp/testsuite/libgomp.c-c++-common/simt-reduction-amdgcn-5.c @@ -0,0 +1,64 @@ +/* Check single-clause _Complex reductions on an AMD GCN device in "simt" + offload mode, where each lane of a wavefront is a thread. The update + of a complex value uses a lock, which the lanes of a wavefront hold + together, so they must merge one at a time. Check full and partial + wavefronts, parallel regions and worksharing loops. */ + +/* { dg-do run } */ +/* { dg-require-effective-target offload_device_gcn } */ +/* { dg-additional-options "--param=offload-gpu-mode=simt" { target offload_target_amdgcn } } */ + +#include <stdlib.h> +#include <omp.h> + +#define N 4096 + +static const int sizes[] = { 1, 64, 100, 1024 }; + +int +main (void) +{ + for (int k = 0; k < (int) (sizeof (sizes) / sizeof (sizes[0])); k++) + { + int nt = sizes[k]; + _Complex float sf = 0, pf = 1; + _Complex double sd = 0, pd = 1; + _Complex int si = 0; + + #pragma omp target teams num_teams(1) firstprivate(nt) \ + map(tofrom: sf, pf, sd, pd, si) + { + #pragma omp parallel num_threads(nt) reduction(+: sf) + sf += (omp_get_thread_num () + 1) + 2.0fi; + + #pragma omp parallel num_threads(nt) reduction(+: sd) + sd += (omp_get_thread_num () + 1) + 2.0i; + + #pragma omp parallel for num_threads(nt) reduction(+: si) + for (int i = 0; i < N; i++) + si += i + 1i; + + /* Eight factors of 2i: the product is 256, exactly. */ + #pragma omp parallel for num_threads(nt) reduction(*: pf) + for (int i = 0; i < N; i++) + pf *= (i % (N / 8) == 0) ? 2.0fi : 1.0f; + + #pragma omp parallel for num_threads(nt) reduction(*: pd) + for (int i = 0; i < N; i++) + pd *= (i % (N / 8) == 0) ? 2.0i : 1.0; + } + + if (__real__ sf != (float) nt * (nt + 1) / 2 || __imag__ sf != 2 * nt) + abort (); + if (__real__ sd != (double) nt * (nt + 1) / 2 || __imag__ sd != 2 * nt) + abort (); + if (__real__ si != N * (N - 1) / 2 || __imag__ si != N) + abort (); + if (__real__ pf != 256 || __imag__ pf != 0) + abort (); + if (__real__ pd != 256 || __imag__ pd != 0) + abort (); + } + + return 0; +} diff --git a/libgomp/testsuite/libgomp.c-c++-common/target-simd-reduction-complex-1.c b/libgomp/testsuite/libgomp.c-c++-common/target-simd-reduction-complex-1.c new file mode 100644 index 000000000000..284994163d04 --- /dev/null +++ b/libgomp/testsuite/libgomp.c-c++-common/target-simd-reduction-complex-1.c @@ -0,0 +1,122 @@ +/* { dg-additional-options "-foffload-options=nvptx-none=-latomic" { target { offload_target_nvptx } } } */ + +/* Complex-typed simd reductions in offloaded code. SIMT targets combine + these across the lanes of a warp or wavefront. */ + +#define N 1024 + +_Complex double cd[N]; +_Complex float cf[N]; +_Complex int ci[N]; + +static void +init (void) +{ + for (int i = 0; i < N; i++) + { + cd[i] = i + (N - i) * 1.0i; + cf[i] = i + (N - i) * 1.0fi; + ci[i] = i + (N - i) * 1i; + } +} + +static int +plus (void) +{ + _Complex double sd = 0; + _Complex float sf = 0; + _Complex int si = 0; + + #pragma omp target simd reduction(+: sd) map(tofrom: sd) + for (int i = 0; i < N; i++) + sd += cd[i]; + + #pragma omp target teams distribute parallel for simd reduction(+: sf) \ + map(tofrom: sf) + for (int i = 0; i < N; i++) + sf += cf[i]; + + #pragma omp target simd reduction(-: si) map(tofrom: si) /* { dg-warning "'-' operator for reductions deprecated" } */ + for (int i = 0; i < N; i++) + si -= ci[i]; + + /* Sum of i is N * (N - 1) / 2, and sum of N - i is N * (N + 1) / 2. */ + if (__real__ sd != N * (N - 1) / 2 || __imag__ sd != N * (N + 1) / 2) + return 1; + if (__real__ sf != N * (N - 1) / 2 || __imag__ sf != N * (N + 1) / 2) + return 2; + if (__real__ si != -(N * (N - 1) / 2) || __imag__ si != -(N * (N + 1) / 2)) + return 3; + return 0; +} + +static int +mult (void) +{ + _Complex double pd = 1; + _Complex float pf = 1; + + /* Eight factors of 2i: the product is 256, exactly. Multiplying the + real and imaginary parts separately would give 0. */ + #pragma omp target simd reduction(*: pd) map(tofrom: pd) + for (int i = 0; i < N; i++) + pd *= (i % (N / 8) == 0) ? 2.0i : 1.0; + + #pragma omp target teams distribute parallel for simd reduction(*: pf) \ + map(tofrom: pf) + for (int i = 0; i < N; i++) + pf *= (i % (N / 8) == 0) ? 2.0fi : 1.0f; + + if (__real__ pd != 256 || __imag__ pd != 0) + return 4; + if (__real__ pf != 256 || __imag__ pf != 0) + return 5; + return 0; +} + +static int +logical (void) +{ + _Complex double ord = 0, andd = 1; + _Complex float orf = 0, andf = 1; + + #pragma omp target simd reduction(||: ord) map(tofrom: ord) + for (int i = 0; i < N; i++) + ord = ord || (i == N - 3 ? cd[i] : 0); + + #pragma omp target simd reduction(&&: andd) map(tofrom: andd) + for (int i = 0; i < N; i++) + andd = andd && (i == 5 ? 0 : cd[i] + 1); + + #pragma omp target teams distribute parallel for simd reduction(||: orf) \ + map(tofrom: orf) + for (int i = 0; i < N; i++) + orf = orf || (i == 7 ? 1.0fi : 0); + + #pragma omp target teams distribute parallel for simd reduction(&&: andf) \ + map(tofrom: andf) + for (int i = 0; i < N; i++) + andf = andf && cf[i]; + + if (__real__ ord != 1 || __imag__ ord != 0) + return 6; + if (__real__ andd != 0 || __imag__ andd != 0) + return 7; + if (__real__ orf != 1 || __imag__ orf != 0) + return 8; + if (__real__ andf != 1 || __imag__ andf != 0) + return 9; + return 0; +} + +int +main (void) +{ + int r; + + init (); + + if ((r = plus ()) || (r = mult ()) || (r = logical ())) + return r; + return 0; +}
