https://gcc.gnu.org/g:1851bbcd6e31fcc037a2e3917ff284b08d47d2fd

commit 1851bbcd6e31fcc037a2e3917ff284b08d47d2fd
Author: Andrew Stubbs <[email protected]>
Date:   Thu Oct 29 17:47:16 2026 +0000

    openmp, amdgcn: SIMT reduction optimization
    
    The "full" SIMT mode presents itself as threads, to OpenMP, so the standard
    lowering would use thread-style synchronization for reductions.  We still 
need
    this across the multiple wavefronts, but the reduction can use a 
vector-style
    reduction for the threads within a wavefront.
    
    Vector reductions require that all lanes are active, so a new internal 
function
    IFN_GOMP_SIMT_THREADS_UNIFORM is introduced to guard the vector path at
    runtime.
    
    IFN_GOMP_SIMT_THREADS_REDUCE eventually ends up identical to
    IFN_GOMP_SIMT_REDUCE, but differs in that it is a no-op on devices that 
support
    only SIMT_DIRECTED mode (currently NVPTX).
    
    IFN_GOMP_SIMT_THREADS_FIRST gives the middle end access to
    __builtin_gcn_simt_first_lane_p in a target independent way.  It need not be
    implemented if IFN_GOMP_SIMT_THREADS_UNIFORM is not implemented.
    
    Assisted-by: Claude Opus 5.5
    
    Claude generated the testcase and assisted with the code.
    
    gcc/ChangeLog:
    
            * config/gcn/gcn-valu.md (omp_simt_threads_uniform_v64si): New.
            * config/gcn/gcn.md (UNSPEC_SIMT_THREADS_UNIFORM): New unspec value.
            (omp_simt_threads_uniform): New.
            (omp_simt_threads_first): New.
            * internal-fn.cc (expand_GOMP_SIMT_THREADS_UNIFORM): New function.
            (expand_GOMP_SIMT_THREADS_REDUCE): New function.
            (expand_GOMP_SIMT_THREADS_FIRST): New function.
            * internal-fn.def (GOMP_SIMT_THREADS_UNIFORM): New internal 
function.
            (GOMP_SIMT_THREADS_REDUCE): Likewise.
            (GOMP_SIMT_THREADS_FIRST): Likewise.
            * omp-expand.cc (execute_expand_omp): Clear PROP_gimple_lomp_dev in
            "simt" offload mode.
            * omp-low.cc (omp_simt_threads_reduction_p): New function.
            (lower_reduction_clauses): In "simt" offload mode, combine a single
            atomic reduction across the wavefront first, when the lanes all
            merge into the same variable.
            * omp-offload.cc (execute_omp_device_lower): Fold
            IFN_GOMP_SIMT_THREADS_UNIFORM, IFN_GOMP_SIMT_THREADS_REDUCE and
            IFN_GOMP_SIMT_THREADS_FIRST if the target has no
            omp_simt_threads_uniform pattern.
            * target-insns.def (omp_simt_threads_uniform): New.
            (omp_simt_threads_first): New.
            * tree-cfg.cc (gimple_can_duplicate_bb_p): Do not duplicate the
            IFN_GOMP_SIMT_THREADS_* calls.
    
    libgomp/ChangeLog:
    
            * testsuite/libgomp.c-c++-common/simt-reduction-amdgcn-3.c: New 
test.

Diff:
---
 gcc/config/gcn/gcn-valu.md                         |  45 +++++++
 gcc/config/gcn/gcn.md                              |  28 +++++
 gcc/internal-fn.cc                                 |  51 ++++++++
 gcc/internal-fn.def                                |   6 +
 gcc/omp-expand.cc                                  |   6 +
 gcc/omp-low.cc                                     | 101 ++++++++++++++++
 gcc/omp-offload.cc                                 |  14 +++
 gcc/target-insns.def                               |   2 +
 gcc/tree-cfg.cc                                    |   8 +-
 .../libgomp.c-c++-common/simt-reduction-amdgcn-3.c | 130 +++++++++++++++++++++
 10 files changed, 389 insertions(+), 2 deletions(-)

diff --git a/gcc/config/gcn/gcn-valu.md b/gcc/config/gcn/gcn-valu.md
index 1d87cb689f3e..5f090d8e6984 100644
--- a/gcc/config/gcn/gcn-valu.md
+++ b/gcc/config/gcn/gcn-valu.md
@@ -4759,6 +4759,51 @@
   }
   [(set_attr "type" "mult")])
   
+(define_insn_and_split "omp_simt_threads_uniform_v64si"
+  [(set (match_operand:V64SI 0 "register_operand" "=v")
+       (unspec:V64SI [(match_operand:V64DI 1 "register_operand" "v")]
+                     UNSPEC_SIMT_THREADS_UNIFORM))]
+  "flag_simt_mode && TARGET_DPP_FULL"
+  "#"
+  "can_create_pseudo_p ()"
+  [(const_int 0)]
+  {
+    /* Make a mask with a bit set for each lane that is inactive or whose
+       operand differs from lane 0's, and combine its halves into a scalar
+       that is zero iff there are none.  Lane 0 can be read even if it is
+       inactive, because then the answer is false anyway.  */
+    rtx first = gen_reg_rtx (DImode);
+    emit_insn (gen_vec_extractv64didi (first, operands[1], const0_rtx));
+    rtx vfirst = gen_reg_rtx (V64DImode);
+    emit_insn (gen_vec_duplicatev64di (vfirst, first));
+    rtx differ = gen_reg_rtx (DImode);
+    emit_insn (gen_vec_cmpv64didi (differ, gen_rtx_NE (VOIDmode, 0, 0),
+                                  operands[1], vfirst));
+    rtx inactive = gen_reg_rtx (DImode);
+    emit_insn (gen_one_cmpldi2 (inactive, gen_rtx_REG (DImode, EXEC_REG)));
+    rtx bad = gen_reg_rtx (DImode);
+    emit_insn (gen_iordi3 (bad, differ, inactive));
+    rtx sreg = gen_reg_rtx (SImode);
+    emit_insn (gen_iorsi3 (sreg, gen_lowpart (SImode, bad),
+                          gen_highpart (SImode, bad)));
+
+    /* Set each lane to 1 iff SREG is zero.  */
+    rtx vbad = gen_reg_rtx (V64SImode);
+    emit_insn (gen_vec_duplicatev64si (vbad, sreg));
+    rtx ok = gen_reg_rtx (DImode);
+    emit_insn (gen_vec_cmpv64sidi (ok, gen_rtx_EQ (VOIDmode, 0, 0), vbad,
+                                  gen_const_vec_duplicate (V64SImode,
+                                                           const0_rtx)));
+    rtx ones = force_reg (V64SImode,
+                         gen_const_vec_duplicate (V64SImode, const1_rtx));
+    emit_insn (gen_vcond_mask_v64sidi (operands[0], ones,
+                                      gen_const_vec_duplicate (V64SImode,
+                                                               const0_rtx),
+                                      ok));
+    DONE;
+  }
+  [(set_attr "type" "mult")])
+
 (define_insn_and_split "omp_simt_last_lane_v64si"
   [(set (match_operand:V64SI 0 "register_operand" "=v")
         (unspec:V64SI [(match_operand:V64SI 1 "register_operand" "v")]
diff --git a/gcc/config/gcn/gcn.md b/gcc/config/gcn/gcn.md
index 2d020c0bf640..0692cb1fd54a 100644
--- a/gcc/config/gcn/gcn.md
+++ b/gcc/config/gcn/gcn.md
@@ -94,6 +94,7 @@
   UNSPEC_DIV_SCALE UNSPEC_DIV_FMAS UNSPEC_DIV_FIXUP
   UNSPEC_PAIR
   UNSPEC_SIMT_VOTE_ANY UNSPEC_SIMT_LAST_LANE UNSPEC_SIMT_XCHG_IDX
+  UNSPEC_SIMT_THREADS_UNIFORM
   UNSPEC_VSUBREG_WRITE UNSPEC_VSUBREG_READ
   UNSPEC_ENSURE_SCALAR
   UNSPEC_SIMT_MASK_MERGE
@@ -2745,6 +2746,33 @@
   ""
   [(set (match_dup 0) (match_dup 1))])
 
+;; Implement IFN_GOMP_SIMT_THREADS_UNIFORM: set operand 0 to nonzero iff
+;; all the lanes of the wavefront are active and operand 1 is the same in all
+;; of them.
+; The simt_vect pass replaces this with omp_simt_threads_uniform_v64si in
+; SIMT code.  What remains is single-threaded (EXEC == 1), where the answer
+; is false.
+(define_insn_and_split "omp_simt_threads_uniform"
+  [(set (match_operand:SI 0 "register_operand" "")
+       (unspec:SI [(match_operand:DI 1 "register_operand" "")]
+                  UNSPEC_SIMT_THREADS_UNIFORM))]
+  "flag_simt_mode && TARGET_DPP_FULL"
+  "#"
+  ""
+  [(set (match_dup 0) (const_int 0))])
+
+;; Implement IFN_GOMP_SIMT_THREADS_FIRST: set operand 0 to nonzero in the
+;; first active lane only.
+(define_expand "omp_simt_threads_first"
+  [(match_operand:SI 0 "register_operand")]
+  "flag_simt_mode && TARGET_DPP_FULL"
+  {
+    rtx first = gen_reg_rtx (BImode);
+    emit_insn (gen_simt_first_lane_p_bi (first));
+    convert_move (operands[0], first, 1);
+    DONE;
+  })
+
 ;; Implement IFN_GOMP_SIMT_LAST_LANE:
 ;; set operand 0 to the value of the last active lane
 ; This pseudo instruction should be replace by the simt_vect pass
diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc
index dc2dc3396791..c9055af71871 100644
--- a/gcc/internal-fn.cc
+++ b/gcc/internal-fn.cc
@@ -647,6 +647,57 @@ expand_GOMP_SIMT_REDUCE (internal_fn, gcall *stmt)
   assign_call_lhs (lhs, target, &ops[0]);
 }
 
+/* The OpenMP threads are SIMT lanes: return nonzero if the encountering
+   threads are all the lanes of a wavefront, and the address in argument 0
+   is the same in all of them.  */
+
+static void
+expand_GOMP_SIMT_THREADS_UNIFORM (internal_fn, gcall *stmt)
+{
+  tree lhs = gimple_call_lhs (stmt);
+  if (!lhs)
+    return;
+
+  rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
+  tree arg0 = gimple_call_arg (stmt, 0);
+  rtx addr = expand_normal (arg0);
+  machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
+  class expand_operand ops[2];
+  create_call_lhs_operand (&ops[0], target, mode);
+  create_input_operand (&ops[1], addr, TYPE_MODE (TREE_TYPE (arg0)));
+  gcc_assert (targetm.have_omp_simt_threads_uniform ());
+  expand_insn (targetm.code_for_omp_simt_threads_uniform, 2, ops);
+  assign_call_lhs (lhs, target, &ops[0]);
+}
+
+/* The OpenMP threads are SIMT lanes: reduce across them, as for
+   IFN_GOMP_SIMT_REDUCE.  */
+
+static void
+expand_GOMP_SIMT_THREADS_REDUCE (internal_fn fn, gcall *stmt)
+{
+  expand_GOMP_SIMT_REDUCE (fn, stmt);
+}
+
+/* The OpenMP threads are SIMT lanes: return nonzero in the first active
+   one.  */
+
+static void
+expand_GOMP_SIMT_THREADS_FIRST (internal_fn, gcall *stmt)
+{
+  tree lhs = gimple_call_lhs (stmt);
+  if (!lhs)
+    return;
+
+  rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
+  machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
+  class expand_operand ops[1];
+  create_call_lhs_operand (&ops[0], target, mode);
+  gcc_assert (targetm.have_omp_simt_threads_first ());
+  expand_insn (targetm.code_for_omp_simt_threads_first, 1, ops);
+  assign_call_lhs (lhs, target, &ops[0]);
+}
+
 /* 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 2886305d663f..2d632fb01cd5 100644
--- a/gcc/internal-fn.def
+++ b/gcc/internal-fn.def
@@ -485,6 +485,12 @@ DEF_INTERNAL_FN (GOMP_SIMT_ORDERED_PRED, ECF_LEAF | 
ECF_NOTHROW, NULL)
 DEF_INTERNAL_FN (GOMP_SIMT_VOTE_ANY, ECF_NOVOPS | ECF_LEAF | ECF_NOTHROW, NULL)
 DEF_INTERNAL_FN (GOMP_SIMT_REDUCE, ECF_NOVOPS | ECF_LEAF | ECF_NOTHROW, NULL)
 DEF_INTERNAL_FN (GOMP_SIMT_XCHG_IDX, ECF_NOVOPS | ECF_LEAF | ECF_NOTHROW, NULL)
+DEF_INTERNAL_FN (GOMP_SIMT_THREADS_UNIFORM, ECF_NOVOPS | ECF_LEAF | 
ECF_NOTHROW,
+                NULL)
+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_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-expand.cc b/gcc/omp-expand.cc
index 0a6b1d91918f..47c5f1f42ad3 100644
--- a/gcc/omp-expand.cc
+++ b/gcc/omp-expand.cc
@@ -11061,6 +11061,12 @@ execute_expand_omp (void)
 
   remove_exit_barriers (root_omp_region);
 
+  /* In "simt" offload mode, lower_reduction_clauses emits
+     IFN_GOMP_SIMT_THREADS_* calls, which pass_omp_device_lower must
+     resolve.  The functions outlined below inherit this.  */
+  if (param_offload_gpu_mode == OFFLOAD_GPU_MODE_SIMT)
+    cfun->curr_properties &= ~PROP_gimple_lomp_dev;
+
   expand_omp (root_omp_region);
 
   omp_free_regions ();
diff --git a/gcc/omp-low.cc b/gcc/omp-low.cc
index 73ccf27ce8de..9e1c736c61de 100644
--- a/gcc/omp-low.cc
+++ b/gcc/omp-low.cc
@@ -8475,6 +8475,42 @@ lower_oacc_reductions (location_t loc, tree clauses, 
tree level, bool inner,
   gimple_seq_add_seq (join_seq, after_join);
 }
 
+/* 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
+   the cases that IFN_GOMP_SIMT_THREADS_REDUCE supports.  The logical
+   operators are combined as "int" truth values of any scalar or complex
+   type.  */
+
+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))
+    return false;
+
+  if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
+    {
+      if (TREE_CODE (type) == COMPLEX_TYPE)
+       type = TREE_TYPE (type);
+      return INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type);
+    }
+
+  machine_mode mode = TYPE_MODE (type);
+  if (INTEGRAL_TYPE_P (type) && TREE_CODE (type) != BOOLEAN_TYPE)
+    return ((mode == QImode || mode == HImode || mode == SImode
+            || mode == DImode)
+           && (code == PLUS_EXPR || code == MULT_EXPR
+               || code == MIN_EXPR || code == MAX_EXPR
+               || code == BIT_AND_EXPR || code == BIT_IOR_EXPR
+               || code == BIT_XOR_EXPR));
+  if (SCALAR_FLOAT_TYPE_P (type))
+    return ((mode == SFmode || mode == DFmode)
+           && (code == PLUS_EXPR || code == MULT_EXPR));
+  return false;
+}
+
 /* Generate code to implement the REDUCTION clauses, append it
    to STMT_SEQP.  CLIST if non-NULL is a pointer to a sequence
    that should be emitted also inside of the critical section,
@@ -8590,6 +8626,69 @@ lower_reduction_clauses (tree clauses, gimple_seq 
*stmt_seqp,
              ref2 = fold_build2_loc (clause_loc, NE_EXPR, boolean_type_node,
                                      ref, zero);
            }
+         tree label = NULL_TREE;
+         if (omp_simt_threads_reduction_p (code, TREE_TYPE (new_var), ctx))
+           {
+             /* The OpenMP threads may be the lanes of a wavefront.  If they
+                all merge into the same variable, combine their values
+                across the wavefront first, so that only one of them
+                needs to do the atomic update.  The logical operators
+                combine the truth values, as "int".  */
+             tree type = TREE_TYPE (new_var);
+             enum tree_code rcode = code;
+             if (is_truth_op)
+               {
+                 type = integer_type_node;
+                 rcode = (code == TRUTH_ANDIF_EXPR
+                          ? BIT_AND_EXPR : BIT_IOR_EXPR);
+               }
+             tree uniform = create_tmp_var (integer_type_node);
+             tree first = create_tmp_var (integer_type_node);
+             tree tmp = create_tmp_var (type);
+             tree lab1 = create_artificial_label (UNKNOWN_LOCATION);
+             tree lab3 = create_artificial_label (UNKNOWN_LOCATION);
+             label = create_artificial_label (UNKNOWN_LOCATION);
+             gimplify_assign (tmp, fold_convert (type, new_var2), stmt_seqp);
+             tree a = create_tmp_var (TREE_TYPE (addr));
+             gimplify_assign (a, addr, stmt_seqp);
+             gcall *g
+               = gimple_build_call_internal (IFN_GOMP_SIMT_THREADS_UNIFORM,
+                                             1, a);
+             gimple_call_set_lhs (g, uniform);
+             gimple_seq_add_stmt (stmt_seqp, g);
+             gimple_seq_add_stmt (stmt_seqp,
+                                  gimple_build_cond (NE_EXPR, uniform,
+                                                     integer_zero_node,
+                                                     lab1, lab3));
+             gimple_seq_add_stmt (stmt_seqp, gimple_build_label (lab1));
+             g = gimple_build_call_internal (IFN_GOMP_SIMT_THREADS_REDUCE,
+                                             2, tmp,
+                                             build_int_cst (unsigned_type_node,
+                                                            rcode));
+             gimple_call_set_lhs (g, tmp);
+             gimple_seq_add_stmt (stmt_seqp, g);
+             g = gimple_build_call_internal (IFN_GOMP_SIMT_THREADS_FIRST, 0);
+             gimple_call_set_lhs (g, first);
+             gimple_seq_add_stmt (stmt_seqp, g);
+             gimple_seq_add_stmt (stmt_seqp,
+                                  gimple_build_cond (NE_EXPR, first,
+                                                     integer_zero_node,
+                                                     lab3, label));
+             gimple_seq_add_stmt (stmt_seqp, gimple_build_label (lab3));
+             new_var2 = tmp;
+             ref = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (a)), a);
+             ref2 = ref;
+             addr = a;
+             if (is_truth_op)
+               {
+                 new_var2 = fold_build2_loc (clause_loc, NE_EXPR,
+                                             boolean_type_node, tmp,
+                                             integer_zero_node);
+                 ref2 = fold_build2_loc (clause_loc, NE_EXPR,
+                                         boolean_type_node, ref,
+                                         build_zero_cst (TREE_TYPE (ref)));
+               }
+           }
          x = fold_build2_loc (clause_loc, code, TREE_TYPE (new_var2), ref2,
                               new_var2);
          if (is_truth_op)
@@ -8597,6 +8696,8 @@ lower_reduction_clauses (tree clauses, gimple_seq 
*stmt_seqp,
          x = build2 (OMP_ATOMIC, void_type_node, addr, x);
          OMP_ATOMIC_MEMORY_ORDER (x) = OMP_MEMORY_ORDER_RELAXED;
          gimplify_and_add (x, stmt_seqp);
+         if (label)
+           gimple_seq_add_stmt (stmt_seqp, gimple_build_label (label));
          return;
        }
       else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
diff --git a/gcc/omp-offload.cc b/gcc/omp-offload.cc
index 7ef3fabe4211..29265a1742d4 100644
--- a/gcc/omp-offload.cc
+++ b/gcc/omp-offload.cc
@@ -3389,6 +3389,20 @@ execute_omp_device_lower ()
          case IFN_GOMP_SIMT_XCHG_IDX:
            rhs = vf == 1 ? gimple_call_arg (stmt, 0) : NULL_TREE;
            break;
+         /* These do not depend on VF: OpenMP threads are SIMT lanes only
+            if the target says so, and each thread is on its own otherwise.  */
+         case IFN_GOMP_SIMT_THREADS_UNIFORM:
+           rhs = (targetm.have_omp_simt_threads_uniform ()
+                  ? NULL_TREE : build_zero_cst (type));
+           break;
+         case IFN_GOMP_SIMT_THREADS_REDUCE:
+           rhs = (targetm.have_omp_simt_threads_uniform ()
+                  ? NULL_TREE : gimple_call_arg (stmt, 0));
+           break;
+         case IFN_GOMP_SIMT_THREADS_FIRST:
+           rhs = (targetm.have_omp_simt_threads_uniform ()
+                  ? NULL_TREE : build_one_cst (type));
+           break;
          case IFN_GOMP_SIMD_LANE:
          case IFN_GOMP_SIMD_LAST_LANE:
            rhs = vf != 1 ? build_zero_cst (type) : NULL_TREE;
diff --git a/gcc/target-insns.def b/gcc/target-insns.def
index 44d4f699870f..f0b0f312d022 100644
--- a/gcc/target-insns.def
+++ b/gcc/target-insns.def
@@ -82,6 +82,8 @@ DEF_TARGET_INSN (omp_simt_ordered, (rtx x0, rtx x1))
 DEF_TARGET_INSN (omp_simt_vote_any, (rtx x0, rtx x1))
 DEF_TARGET_INSN (omp_simt_reduce, (rtx x0, rtx x1, rtx x2))
 DEF_TARGET_INSN (omp_simt_xchg_idx, (rtx x0, rtx x1, rtx x2))
+DEF_TARGET_INSN (omp_simt_threads_uniform, (rtx x0, rtx x1))
+DEF_TARGET_INSN (omp_simt_threads_first, (rtx x0))
 DEF_TARGET_INSN (prefetch, (rtx x0, rtx x1, rtx x2))
 DEF_TARGET_INSN (probe_stack, (rtx x0))
 DEF_TARGET_INSN (probe_stack_address, (rtx x0))
diff --git a/gcc/tree-cfg.cc b/gcc/tree-cfg.cc
index 3653f2806629..c505afc240a9 100644
--- a/gcc/tree-cfg.cc
+++ b/gcc/tree-cfg.cc
@@ -6451,13 +6451,17 @@ gimple_can_duplicate_bb_p (const_basic_block bb)
         duplicated as part of its group, or not at all.
         The IFN_GOMP_SIMT_VOTE_ANY, IFN_GOMP_SIMT_XCHG_IDX and
         IFN_GOMP_SIMT_REDUCE are part of such a group, so the same holds
-        there.  */
+        there.  The IFN_GOMP_SIMT_THREADS_* calls depend on the active
+        lanes, so must not be duplicated either.  */
       if (is_gimple_call (g)
          && (gimple_call_internal_p (g, IFN_GOMP_SIMT_ENTER_ALLOC)
              || gimple_call_internal_p (g, IFN_GOMP_SIMT_EXIT)
              || gimple_call_internal_p (g, IFN_GOMP_SIMT_VOTE_ANY)
              || gimple_call_internal_p (g, IFN_GOMP_SIMT_REDUCE)
-             || gimple_call_internal_p (g, IFN_GOMP_SIMT_XCHG_IDX)))
+             || gimple_call_internal_p (g, IFN_GOMP_SIMT_XCHG_IDX)
+             || gimple_call_internal_p (g, IFN_GOMP_SIMT_THREADS_UNIFORM)
+             || gimple_call_internal_p (g, IFN_GOMP_SIMT_THREADS_REDUCE)
+             || gimple_call_internal_p (g, IFN_GOMP_SIMT_THREADS_FIRST)))
        return false;
 
       if (is_gimple_call (g) && simt_no_duplicate_call_p (g))
diff --git a/libgomp/testsuite/libgomp.c-c++-common/simt-reduction-amdgcn-3.c 
b/libgomp/testsuite/libgomp.c-c++-common/simt-reduction-amdgcn-3.c
new file mode 100644
index 000000000000..739a79b4e8a6
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/simt-reduction-amdgcn-3.c
@@ -0,0 +1,130 @@
+/* Check reductions over OpenMP threads on an AMD GCN device in "simt"
+   offload mode, where each lane of a wavefront is a thread.  When all the
+   lanes merge into the same variable, the compiler combines them across
+   the wavefront first, and only one lane does the atomic update.  Check
+   each supported type and operator, a combined "teams distribute parallel
+   for" as in BabelStream, and nested regions, where each lane is a team of
+   one with its own variable, so the lanes must not be combined.  */
+
+/* { 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 MAXT 1024
+
+int
+main (void)
+{
+  for (int k = 0; k < 2; k++)
+    {
+      int nt = k ? 1024 : 64;
+      int nthr = 0;
+      long isum = 0, imin = N, imax = -1;
+      unsigned iand = ~0U, ior = 0, ixor = 0;
+      long long lprod = 1;
+      double dsum = 0, dprod = 1;
+      float fsum = 0;
+
+      #pragma omp target teams num_teams(1) firstprivate(nt) \
+                 map(tofrom: nthr, isum, imin, imax, iand, ior, ixor, \
+                             lprod, dsum, dprod, fsum)
+      #pragma omp parallel num_threads(nt)
+      {
+       #pragma omp single
+       nthr = omp_get_num_threads ();
+
+       #pragma omp for reduction(+: isum)
+       for (int i = 0; i < N; i++)
+         isum += i;
+       #pragma omp for reduction(min: imin)
+       for (int i = 0; i < N; i++)
+         imin = (i + 7) % N < imin ? (i + 7) % N : imin;
+       #pragma omp for reduction(max: imax)
+       for (int i = 0; i < N; i++)
+         imax = (i * 3) % N > imax ? (i * 3) % N : imax;
+       #pragma omp for reduction(&: iand)
+       for (int i = 0; i < N; i++)
+         iand &= i == 100 ? 0xffff0fffU : ~0U;
+       #pragma omp for reduction(|: ior)
+       for (int i = 0; i < N; i++)
+         ior |= (i % 37 == 0) << (i % 32);
+       #pragma omp for reduction(^: ixor)
+       for (int i = 0; i < N; i++)
+         ixor ^= i;
+       #pragma omp for reduction(*: lprod)
+       for (int i = 0; i < N; i++)
+         lprod *= (i % 512 == 3 ? 3 : 1);
+       #pragma omp for reduction(+: dsum)
+       for (int i = 0; i < N; i++)
+         dsum += 0.5 * i;
+       #pragma omp for reduction(*: dprod)
+       for (int i = 0; i < N; i++)
+         dprod *= (i % 1024 == 1 ? 2.0 : 1.0);
+       #pragma omp for reduction(+: fsum)
+       for (int i = 0; i < N; i++)
+         fsum += 1.0f;
+      }
+
+      if (nthr != nt
+         || isum != (long) N * (N - 1) / 2
+         || imin != 0 || imax != N - 1
+         || iand != 0xffff0fffU
+         || lprod != 6561 || dsum != 0.25 * N * (N - 1)
+         || dprod != 16.0 || fsum != (float) N)
+       abort ();
+
+      unsigned eor = 0, exor = 0;
+      for (int i = 0; i < N; i++)
+       {
+         eor |= (i % 37 == 0) << (i % 32);
+         exor ^= i;
+       }
+      if (ior != eor || ixor != exor)
+       abort ();
+
+      /* Like BabelStream's "dot".  */
+      double dot = 0;
+      #pragma omp target teams distribute parallel for map(tofrom: dot) \
+                 reduction(+: dot) num_teams(4) thread_limit(nt)
+      for (int i = 0; i < N * 4; i++)
+       dot += 2.0 * i;
+      if (dot != 2.0 * (N * 4) * (N * 4 - 1) / 2)
+       abort ();
+
+      /* Nested regions: each lane is a team of one with its own SUM.  */
+      int res[MAXT], bad = 0;
+      #pragma omp target teams num_teams(1) firstprivate(nt) \
+                 map(from: res) map(tofrom: bad)
+      #pragma omp parallel num_threads(nt)
+      {
+       int t = omp_get_thread_num ();
+       long sum = t, sum2 = t;
+       #pragma omp parallel
+       {
+         #pragma omp for reduction(+: sum)
+         for (int i = 0; i < 10; i++)
+           sum += i + t;
+       }
+       #pragma omp parallel reduction(+: sum2)
+       sum2 += 45 + 10 * t;
+       if (t < MAXT && sum == sum2)
+         res[t] = sum;
+       else
+         {
+           #pragma omp atomic
+           bad++;
+         }
+      }
+      if (bad)
+       abort ();
+      for (int t = 0; t < nt; t++)
+       if (res[t] != t + 45 + 10 * t)
+         abort ();
+    }
+
+  return 0;
+}

Reply via email to