https://gcc.gnu.org/g:3fae4f8cfce53dfec04be6451a776d998813b932
commit 3fae4f8cfce53dfec04be6451a776d998813b932 Author: Andrew Stubbs <[email protected]> Date: Mon Nov 2 15:07:12 2026 +0000 openmp: fix multiple reductions in full-SIMT mode Individual thread-level reductions in SIMT mode work, but multiple reductions on the same loop need special handling because two threads in the same wave can't hold a lock at the same time. Basically, this doesn't work: for (....) { if (cond_that_only_matches_Zone_thread_at_a_time) { /* This is an exit block. It will be deferred until the loop is done iterating (all threads have a exit condition) and then all threads that exit this way will execute it together, efficiently. Only one thread can take the lock, so all will hang. */ lock (); do_stuff (); unlock (); break; } } But this does work: for (....) { if (cond_that_only_matches_one_thread_at_a_time) { /* This runs repeatedly inside the loop for a different lane each time. */ lock (); do_stuff (); unlock (); if (always_true_but_the_compiler_doesn't_know ()) /* This is now the exit block, but the work is already done. */ break; } } Assisted-by: Claude Opus 5.5 Claude generated the testcase and assisted with analysing the problem. gcc/ChangeLog: * internal-fn.cc (expand_GOMP_SIMT_THREADS_LANE): New function. * internal-fn.def (GOMP_SIMT_THREADS_LANE): New internal function. * omp-low.cc (omp_simt_threads_p): New function. (omp_simt_threads_reduction_p): Use it. (lower_reduction_clauses): In "simt" offload mode, merge the reductions under the lock one lane at a time. * omp-offload.cc (execute_omp_device_lower): Fold IFN_GOMP_SIMT_THREADS_LANE if the target has no omp_simt_threads_uniform pattern. libgomp/ChangeLog: * testsuite/libgomp.c-c++-common/simt-reduction-amdgcn-4.c: New test. Diff: --- gcc/internal-fn.cc | 15 +++ gcc/internal-fn.def | 1 + gcc/omp-low.cc | 56 ++++++++++- gcc/omp-offload.cc | 6 ++ .../libgomp.c-c++-common/simt-reduction-amdgcn-4.c | 112 +++++++++++++++++++++ 5 files changed, 188 insertions(+), 2 deletions(-) diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc index c9055af71871..86b2ea224e45 100644 --- a/gcc/internal-fn.cc +++ b/gcc/internal-fn.cc @@ -698,6 +698,21 @@ expand_GOMP_SIMT_THREADS_FIRST (internal_fn, gcall *stmt) assign_call_lhs (lhs, target, &ops[0]); } +/* The OpenMP threads are SIMT lanes: return the lane number. The call is + also a memory barrier in GIMPLE. */ + +static void +expand_GOMP_SIMT_THREADS_LANE (internal_fn, gcall *stmt) +{ + tree lhs = gimple_call_lhs (stmt); + if (!lhs) + return; + + rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE); + gcc_assert (targetm.have_omp_simt_lane ()); + emit_insn (targetm.gen_omp_simt_lane (target)); +} + /* Exchange between SIMT lanes according to given source lane index. */ static void diff --git a/gcc/internal-fn.def b/gcc/internal-fn.def index 2d632fb01cd5..2485433911ce 100644 --- a/gcc/internal-fn.def +++ b/gcc/internal-fn.def @@ -491,6 +491,7 @@ DEF_INTERNAL_FN (GOMP_SIMT_THREADS_REDUCE, ECF_NOVOPS | ECF_LEAF | ECF_NOTHROW, NULL) DEF_INTERNAL_FN (GOMP_SIMT_THREADS_FIRST, ECF_NOVOPS | ECF_LEAF | ECF_NOTHROW, NULL) +DEF_INTERNAL_FN (GOMP_SIMT_THREADS_LANE, ECF_LEAF | ECF_NOTHROW, NULL) DEF_INTERNAL_FN (GOMP_SIMD_LANE, ECF_NOVOPS | ECF_LEAF | ECF_NOTHROW, NULL) DEF_INTERNAL_FN (GOMP_SIMD_VF, ECF_CONST | ECF_LEAF | ECF_NOTHROW, NULL) DEF_INTERNAL_FN (GOMP_SIMD_LAST_LANE, ECF_CONST | ECF_LEAF | ECF_NOTHROW, NULL) diff --git a/gcc/omp-low.cc b/gcc/omp-low.cc index 9e1c736c61de..3b7a1c6468c5 100644 --- a/gcc/omp-low.cc +++ b/gcc/omp-low.cc @@ -8475,6 +8475,16 @@ lower_oacc_reductions (location_t loc, tree clauses, tree level, bool inner, gimple_seq_add_seq (join_seq, after_join); } +/* Return true if the OpenMP threads that run CTX may be the lanes of a + wavefront, as in "simt" offload mode. */ + +static bool +omp_simt_threads_p (omp_context *ctx) +{ + return (param_offload_gpu_mode == OFFLOAD_GPU_MODE_SIMT + && omp_maybe_offloaded_ctx (ctx)); +} + /* Return true if a reduction with operator CODE of a variable of TYPE in CTX should first be combined across the wavefront, for "simt" offload mode, where the OpenMP threads are the lanes of a wavefront. These are @@ -8486,8 +8496,7 @@ static bool omp_simt_threads_reduction_p (enum tree_code code, tree type, omp_context *ctx) { - if (param_offload_gpu_mode != OFFLOAD_GPU_MODE_SIMT - || !omp_maybe_offloaded_ctx (ctx)) + if (!omp_simt_threads_p (ctx)) return false; if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR) @@ -8869,6 +8878,34 @@ lower_reduction_clauses (tree clauses, gimple_seq *stmt_seqp, 0); gimple_seq_add_stmt (stmt_seqp, stmt); + /* The OpenMP threads may be the lanes of a wavefront, which hold the lock + together. Merge one lane at a time: lane L merges in iteration L. + IFN_GOMP_SIMT_THREADS_LANE is called again in every iteration: it is a + memory barrier, which keeps the merges inside the loop, and because the + compiler cannot tell that it returns the same value, it cannot see that + the lane that merged leaves the loop next, and thread the merge to the + loop exit, where every lane would run it at once. */ + tree lane = NULL_TREE, l = NULL_TREE; + tree lane_body = NULL_TREE, lane_next = NULL_TREE, lane_end = NULL_TREE; + if (omp_simt_threads_p (ctx)) + { + lane = create_tmp_var (integer_type_node); + l = create_tmp_var (integer_type_node); + lane_body = create_artificial_label (UNKNOWN_LOCATION); + lane_next = create_artificial_label (UNKNOWN_LOCATION); + lane_end = create_artificial_label (UNKNOWN_LOCATION); + tree lane_merge = create_artificial_label (UNKNOWN_LOCATION); + gimplify_assign (l, integer_zero_node, stmt_seqp); + gcall *g = gimple_build_call_internal (IFN_GOMP_SIMT_THREADS_LANE, 0); + gimple_call_set_lhs (g, lane); + gimple_seq_add_stmt (stmt_seqp, g); + gimple_seq_add_stmt (stmt_seqp, gimple_build_label (lane_body)); + gimple_seq_add_stmt (stmt_seqp, + gimple_build_cond (EQ_EXPR, lane, l, lane_merge, + lane_next)); + gimple_seq_add_stmt (stmt_seqp, gimple_build_label (lane_merge)); + } + gimple_seq_add_seq (stmt_seqp, sub_seq); if (clist) @@ -8877,6 +8914,21 @@ lower_reduction_clauses (tree clauses, gimple_seq *stmt_seqp, *clist = NULL; } + if (lane_end) + { + gimple_seq_add_stmt (stmt_seqp, gimple_build_label (lane_next)); + gimple_seq_add_stmt (stmt_seqp, + gimple_build_assign (l, PLUS_EXPR, l, + integer_one_node)); + gcall *g = gimple_build_call_internal (IFN_GOMP_SIMT_THREADS_LANE, 0); + gimple_call_set_lhs (g, lane); + gimple_seq_add_stmt (stmt_seqp, g); + gimple_seq_add_stmt (stmt_seqp, + gimple_build_cond (LE_EXPR, l, lane, lane_body, + lane_end)); + gimple_seq_add_stmt (stmt_seqp, gimple_build_label (lane_end)); + } + stmt = gimple_build_call (builtin_decl_explicit (BUILT_IN_GOMP_REDUCTION_END), 0); gimple_seq_add_stmt (stmt_seqp, stmt); diff --git a/gcc/omp-offload.cc b/gcc/omp-offload.cc index 29265a1742d4..5db9d5261438 100644 --- a/gcc/omp-offload.cc +++ b/gcc/omp-offload.cc @@ -3403,6 +3403,12 @@ execute_omp_device_lower () rhs = (targetm.have_omp_simt_threads_uniform () ? NULL_TREE : build_one_cst (type)); break; + case IFN_GOMP_SIMT_THREADS_LANE: + rhs = (targetm.have_omp_simt_threads_uniform () + ? NULL_TREE : build_zero_cst (type)); + if (rhs || !lhs) + unlink_stmt_vdef (stmt); + break; case IFN_GOMP_SIMD_LANE: case IFN_GOMP_SIMD_LAST_LANE: rhs = vf != 1 ? build_zero_cst (type) : NULL_TREE; diff --git a/libgomp/testsuite/libgomp.c-c++-common/simt-reduction-amdgcn-4.c b/libgomp/testsuite/libgomp.c-c++-common/simt-reduction-amdgcn-4.c new file mode 100644 index 000000000000..d02b9db04109 --- /dev/null +++ b/libgomp/testsuite/libgomp.c-c++-common/simt-reduction-amdgcn-4.c @@ -0,0 +1,112 @@ +/* Check reductions that merge under a lock on an AMD GCN device in "simt" + offload mode, where each lane of a wavefront is a thread: more than one + reduction clause, array sections and user-defined reductions. The lanes + of a wavefront hold the lock together, so they must merge one at a time. + Check full and partial wavefronts, worksharing loops, and nested regions, + where each lane is a team of one. */ + +/* { 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 +#define M 8 + +struct pair { int lo, hi; }; + +#pragma omp declare reduction (pairmax : struct pair : \ + omp_out.lo = omp_in.lo < omp_out.lo ? omp_in.lo : omp_out.lo, \ + omp_out.hi = omp_in.hi > omp_out.hi ? omp_in.hi : omp_out.hi) \ + initializer (omp_priv = { 1 << 30, -1 }) + +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]; + long a = 0, b = 0, c = 0; + int mx = -1, arr[M] = { 0 }; + double d = 0; + struct pair p = { 1 << 30, -1 }; + + #pragma omp target teams num_teams(1) firstprivate(nt) \ + map(tofrom: a, b, c, mx, arr, d, p) + { + /* Two clauses on a parallel region: one merge per thread. */ + #pragma omp parallel num_threads(nt) reduction(+: a, b) + { + a += omp_get_thread_num () + 1; + b += 2; + } + + /* Mixed clauses on a worksharing loop. */ + #pragma omp parallel num_threads(nt) + #pragma omp for reduction(+: c, d) reduction(max: mx) + for (int i = 0; i < N; i++) + { + c += i; + d += 0.5; + mx = i > mx ? i : mx; + } + + /* An array section. */ + #pragma omp parallel for num_threads(nt) reduction(+: arr[:M]) + for (int i = 0; i < N; i++) + arr[i % M] += i; + + /* A user-defined reduction. */ + #pragma omp parallel for num_threads(nt) reduction(pairmax: p) + for (int i = 0; i < N; i++) + { + int v = (i * 7) % N; + p.lo = v < p.lo ? v : p.lo; + p.hi = v > p.hi ? v : p.hi; + } + } + + if (a != (long) nt * (nt + 1) / 2 || b != 2L * nt) + abort (); + if (c != (long) N * (N - 1) / 2 || d != 0.5 * N || mx != N - 1) + abort (); + for (int j = 0; j < M; j++) + { + long e = 0; + for (int i = j; i < N; i += M) + e += i; + if (arr[j] != e) + abort (); + } + if (p.lo != 0 || p.hi != N - 1) + abort (); + } + + /* Nested regions: each lane is a team of one, merging into its own + variables. */ + int bad = 0; + #pragma omp target teams num_teams(1) map(tofrom: bad) + #pragma omp parallel num_threads(128) + { + int t = omp_get_thread_num (); + long x = t, y = 0; + #pragma omp parallel reduction(+: x, y) + { + x += 10; + y += 1; + } + if (x != t + 10 || y != 1) + { + #pragma omp atomic + bad++; + } + } + if (bad) + abort (); + + return 0; +}
