From: Andrew Stubbs <[email protected]>

This builds on the backend support patch for SIMT-DIRECTED mode, earlier in
this series, with the builtins and instructions needed to implement the full
SIMT mode.

Assisted-by: Claude Opus 5.5

Claude generated the test cases and assisted with some of the code.

gcc/ChangeLog:

        * config/gcn/gcn-builtins.def (SIMT_FIRST_LANE_P)
        (SIMT_BROADCAST_FIRST_LANE, SIMT_BROADCAST_FIRST_LANE_DI)
        (SIMT_VOTE_ANY): New builtins.
        * config/gcn/gcn-valu.md (@ensure_scalar<mode>): New vector insn.
        (omp_simt_vote_any_v64si): Combine the halves of the mask with IOR
        instead of using a BImode cstore.
        (simt_first_lane_p_di): New insn.
        * config/gcn/gcn.cc (gcn_expand_prologue): In SIMT mode, pass the
        offload mode to gomp_gcn_enter_kernel.
        (gcn_expand_builtin_1): Expand GCN_BUILTIN_SIMT_FIRST_LANE_P,
        GCN_BUILTIN_SIMT_BROADCAST_FIRST_LANE,
        GCN_BUILTIN_SIMT_BROADCAST_FIRST_LANE_DI and GCN_BUILTIN_SIMT_VOTE_ANY.
        (gcn_resolve_overloaded_builtin): New function.
        (gcn_output_ensure_scalar): Use the unit size of MODE, to allow vector
        modes.
        (TARGET_RESOLVE_OVERLOADED_BUILTIN): Define.
        * config/gcn/gcn.md (unspec): Add UNSPEC_SIMT_FIRST_LANE_P and
        UNSPEC_SIMT_BROADCAST_FIRST_LANE.
        (omp_simt_vote_any): Change to a define_insn_and_split that splits to
        a move.
        (@ensure_scalar<mode>): Set the simt_clobber attribute.
        (simt_first_lane_p_bi): New define_insn_and_split.
        (simt_broadcast_first_lane_<mode>): New expander.

gcc/testsuite/ChangeLog:

        * gcc.target/gcn/simt-broadcast-first-lane-1.c: New test.
        * gcc.target/gcn/simt-broadcast-first-lane-2.c: New test.
        * gcc.target/gcn/simt-broadcast-first-lane-3.c: New test.
        * gcc.target/gcn/simt-broadcast-first-lane-di-1.c: New test.
        * gcc.target/gcn/simt-first-lane-p-1.c: New test.
        * gcc.target/gcn/simt-vote-any-1.c: New test.
---
 gcc/config/gcn/gcn-builtins.def               |  10 +-
 gcc/config/gcn/gcn-valu.md                    |  47 ++++++-
 gcc/config/gcn/gcn.cc                         | 122 +++++++++++++++++-
 gcc/config/gcn/gcn.md                         |  42 +++++-
 .../gcn/simt-broadcast-first-lane-1.c         |  57 ++++++++
 .../gcn/simt-broadcast-first-lane-2.c         | 101 +++++++++++++++
 .../gcn/simt-broadcast-first-lane-3.c         |  16 +++
 .../gcn/simt-broadcast-first-lane-di-1.c      |  63 +++++++++
 .../gcc.target/gcn/simt-first-lane-p-1.c      |  62 +++++++++
 .../gcc.target/gcn/simt-vote-any-1.c          |  68 ++++++++++
 10 files changed, 574 insertions(+), 14 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/gcn/simt-broadcast-first-lane-1.c
 create mode 100644 gcc/testsuite/gcc.target/gcn/simt-broadcast-first-lane-2.c
 create mode 100644 gcc/testsuite/gcc.target/gcn/simt-broadcast-first-lane-3.c
 create mode 100644 
gcc/testsuite/gcc.target/gcn/simt-broadcast-first-lane-di-1.c
 create mode 100644 gcc/testsuite/gcc.target/gcn/simt-first-lane-p-1.c
 create mode 100644 gcc/testsuite/gcc.target/gcn/simt-vote-any-1.c

diff --git a/gcc/config/gcn/gcn-builtins.def b/gcc/config/gcn/gcn-builtins.def
index e732cacfee3c0..9610614d9ba95 100644
--- a/gcc/config/gcn/gcn-builtins.def
+++ b/gcc/config/gcn/gcn-builtins.def
@@ -158,12 +158,20 @@ DEF_BUILTIN (ACC_SINGLE_COPY_END, -1, "single_copy_end", 
B_INSN,
 DEF_BUILTIN (ACC_BARRIER, -1, "acc_barrier", B_INSN, _A1 (GCN_BTI_VOID),
             gcn_expand_builtin_1)
 
-/* SIMT testing.  */
+/* SIMT.  */
 
 DEF_BUILTIN (SIMT_ENTER, -1, "simt_enter", B_INSN,
             _A3 (GCN_BTI_INT, GCN_BTI_INT, GCN_BTI_INT), gcn_expand_builtin_1)
 DEF_BUILTIN (SIMT_EXIT, -1, "simt_exit", B_INSN,
             _A2 (GCN_BTI_VOID, GCN_BTI_INT), gcn_expand_builtin_1)
+DEF_BUILTIN (SIMT_FIRST_LANE_P, -1, "simt_first_lane_p", B_INSN,
+            _A1 (GCN_BTI_BOOL), gcn_expand_builtin_1)
+DEF_BUILTIN (SIMT_BROADCAST_FIRST_LANE, -1, "simt_broadcast_first_lane", 
B_INSN,
+            _A2 (GCN_BTI_INT, GCN_BTI_INT), gcn_expand_builtin_1)
+DEF_BUILTIN (SIMT_BROADCAST_FIRST_LANE_DI, -1, "simt_broadcast_first_lane_di",
+            B_INSN, _A2 (GCN_BTI_LLINT, GCN_BTI_LLINT), gcn_expand_builtin_1)
+DEF_BUILTIN (SIMT_VOTE_ANY, -1, "simt_vote_any", B_INSN,
+            _A2 (GCN_BTI_INT, GCN_BTI_INT), gcn_expand_builtin_1)
 
 /* Kernel inputs.  */
 
diff --git a/gcc/config/gcn/gcn-valu.md b/gcc/config/gcn/gcn-valu.md
index fb47edd9c1a1c..c85008308b2be 100644
--- a/gcc/config/gcn/gcn-valu.md
+++ b/gcc/config/gcn/gcn-valu.md
@@ -4728,6 +4728,24 @@
     DONE;
   })
 
+; The vector form of ensure_scalar<mode> (in gcn.md), for when the simt_vect
+; pass vectorizes one (e.g. from __builtin_gcn_simt_broadcast_first_lane):
+; copy the value of the first active lane to all active lanes.
+(define_insn "@ensure_scalar<mode>"
+  [(set (match_operand:V64_MOV 0 "register_operand" "=v")
+       (unspec:V64_MOV [(match_operand:V64_MOV 1 "register_operand" "v")]
+                       UNSPEC_ENSURE_SCALAR))
+   (clobber (match_scratch:SI 2 "=Sg"))]
+  "flag_simt_mode"
+  {
+    return gcn_output_ensure_scalar (operands, <MODE>mode);
+  }
+  [(set_attr "type" "vmult")
+   (set_attr "exec" "none")
+   (set (attr "length")
+       (symbol_ref "MAX (1, GET_MODE_UNIT_SIZE (<MODE>mode) / 4)
+                    * (TARGET_CDNA3_NOPS ? 16 : 12)"))])
+
 (define_insn_and_split "omp_simt_vote_any_v64si"
   [(set (match_operand:V64SI 0 "register_operand" "=v")
        (unspec:V64SI [(match_operand:V64SI 1 "register_operand" "v")]
@@ -4737,16 +4755,19 @@
   "can_create_pseudo_p ()"
   [(const_int 0)]
   {
-    rtx sreg = gen_reg_rtx (BImode);
+    /* The mask has a bit set for each active lane that votes.  Combine
+       its halves into a scalar that is zero iff no lane voted.  (Going
+       through a BImode cstore leaves a subreg of SCC or VCC, which LRA
+       cannot reload.)  */
+    rtx sreg = gen_reg_rtx (SImode);
     rtx vcc = gen_reg_rtx (DImode);
     emit_insn (gen_vec_cmpv64sidi (vcc, gen_rtx_NE (VOIDmode, 0, 0),
                                   operands[1],
                                   gen_const_vec_duplicate (V64SImode,
                                                            const0_rtx)));
-    emit_insn (gen_cstoredi4 (sreg, gen_rtx_NE (VOIDmode, 0, 0), vcc,
-                             const0_rtx));
-    emit_insn (gen_vec_duplicatev64si (operands[0],
-                                      gen_rtx_SUBREG (SImode, sreg, 0)));
+    emit_insn (gen_iorsi3 (sreg, gen_lowpart (SImode, vcc),
+                          gen_highpart (SImode, vcc)));
+    emit_insn (gen_vec_duplicatev64si (operands[0], sreg));
     DONE;
   }
   [(set_attr "type" "mult")])
@@ -4777,4 +4798,20 @@
   "flag_simt_mode"
   "sorry, placeholder %0 = simt_xchg_idx %1")
 
+(define_insn "simt_first_lane_p_di"
+  [(set (match_operand:DI 0 "register_operand" "=SD")
+       (unspec:DI [(reg:V64SI LANE_ID_REG)]
+                  UNSPEC_SIMT_FIRST_LANE_P))
+   (clobber (match_scratch:SI 1 "=SD"))]
+  "flag_simt_mode"
+  {
+    output_asm_insn ("v_readfirstlane_b32\t%1, v1", operands);
+    /* CDNA3: VALU writes SGPR followed by VALU reads that SGPR as a
+       constant requires 1 wait state.  */
+    if (TARGET_CDNA3_NOPS)
+      output_asm_insn ("s_nop\t0", operands);
+    output_asm_insn ("v_cmp_eq_u32\t%0, v1, %1", operands);
+    return "";
+  })
+
 ;; }}}
diff --git a/gcc/config/gcn/gcn.cc b/gcc/config/gcn/gcn.cc
index af5d92d898a6b..2c93b009b4a3a 100644
--- a/gcc/config/gcn/gcn.cc
+++ b/gcc/config/gcn/gcn.cc
@@ -3874,7 +3874,21 @@ gcn_expand_prologue ()
       rtx fn_reg = gen_rtx_REG (Pmode, FIRST_PARM_REG);
       emit_move_insn (fn_reg, gen_rtx_SYMBOL_REF (Pmode,
                                                  "gomp_gcn_enter_kernel"));
-      emit_call_insn (gen_gcn_indirect_call (fn_reg, const0_rtx));
+      rtx simt_mode_arg = NULL_RTX;
+      if (flag_simt_mode)
+       {
+         /* In SIMT mode, gomp_gcn_enter_kernel takes a parameter that
+            indicates whether the kernel was compiled in "simt" mode (1), or
+            "simt-directed" mode (0).  */
+         simt_mode_arg = gen_rtx_REG (SImode, FIRST_VPARM_REG);
+         emit_move_insn (simt_mode_arg,
+                         GEN_INT (param_offload_gpu_mode
+                                  != OFFLOAD_GPU_MODE_SIMT_DIRECTED));
+       }
+      rtx_insn *call = emit_call_insn (gen_gcn_indirect_call (fn_reg,
+                                                              const0_rtx));
+      if (simt_mode_arg)
+       use_reg (&CALL_INSN_FUNCTION_USAGE (call), simt_mode_arg);
     }
 }
 
@@ -5358,6 +5372,40 @@ gcn_expand_builtin_1 (tree exp, rtx target, rtx 
/*subtarget */ ,
        emit_insn (gen_omp_simt_exit (arg1));
        return target;
       }
+    case GCN_BUILTIN_SIMT_FIRST_LANE_P:
+      {
+       if (!target || !REG_P (target))
+         target = gen_reg_rtx (BImode);
+       emit_insn (gen_simt_first_lane_p_bi (target));
+       return target;
+      }
+    case GCN_BUILTIN_SIMT_BROADCAST_FIRST_LANE:
+      {
+       if (!target || !REG_P (target))
+         target = gen_reg_rtx (SImode);
+       rtx arg1 = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX, mode,
+                               EXPAND_NORMAL);
+       emit_insn (gen_simt_broadcast_first_lane_si (target, arg1));
+       return target;
+      }
+    case GCN_BUILTIN_SIMT_BROADCAST_FIRST_LANE_DI:
+      {
+       if (!target || !REG_P (target))
+         target = gen_reg_rtx (DImode);
+       rtx arg1 = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX, mode,
+                               EXPAND_NORMAL);
+       emit_insn (gen_simt_broadcast_first_lane_di (target, arg1));
+       return target;
+      }
+    case GCN_BUILTIN_SIMT_VOTE_ANY:
+      {
+       if (!target || !REG_P (target))
+         target = gen_reg_rtx (SImode);
+       rtx arg1 = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX, mode,
+                               EXPAND_NORMAL);
+       emit_insn (gen_omp_simt_vote_any (target, force_reg (SImode, arg1)));
+       return target;
+      }
     default:
       gcc_unreachable ();
     }
@@ -5408,6 +5456,70 @@ gcn_expand_builtin_binop (tree exp, rtx target, rtx 
/*subtarget */ ,
   return target;
 }
 
+/* Implement TARGET_RESOLVE_OVERLOADED_BUILTIN.
+
+   __builtin_gcn_simt_broadcast_first_lane is generic: it takes an integer,
+   pointer or floating-point value of up to 64 bits, and returns a value of
+   the same type.  Values of up to 32 bits go through the SImode builtin
+   itself, and larger ones through __builtin_gcn_simt_broadcast_first_lane_di.
+   Floating-point values are passed as their bits.  */
+
+static tree
+gcn_resolve_overloaded_builtin (location_t loc, tree fndecl, void *vargs,
+                               bool complain)
+{
+  if (DECL_MD_FUNCTION_CODE (fndecl) != GCN_BUILTIN_SIMT_BROADCAST_FIRST_LANE)
+    return NULL_TREE;
+
+  const char *name = "__builtin_gcn_simt_broadcast_first_lane";
+  vec<tree, va_gc> *args = (vec<tree, va_gc> *) vargs;
+  if (vec_safe_length (args) != 1)
+    {
+      if (complain)
+       error_at (loc, "%qs expects 1 argument but %d given", name,
+                 (int) vec_safe_length (args));
+      return error_mark_node;
+    }
+
+  tree arg = (*args)[0];
+  tree type = TREE_TYPE (arg);
+  if (type == error_mark_node)
+    return error_mark_node;
+  type = TYPE_MAIN_VARIANT (type);
+
+  if (!(INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)
+       || SCALAR_FLOAT_TYPE_P (type))
+      || !tree_fits_uhwi_p (TYPE_SIZE (type))
+      || tree_to_uhwi (TYPE_SIZE (type)) > 64)
+    {
+      if (complain)
+       error_at (loc, "%qs expects an integer, pointer or floating-point "
+                 "argument of at most 64 bits, not %qT", name, type);
+      return error_mark_node;
+    }
+
+  unsigned bits = tree_to_uhwi (TYPE_SIZE (type));
+  tree decl = (bits > 32
+              ? gcn_builtin_decls[GCN_BUILTIN_SIMT_BROADCAST_FIRST_LANE_DI]
+              : fndecl);
+  tree itype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)));
+
+  /* Reinterpret a floating-point value as an integer of the same size.  */
+  tree bits_type = NULL_TREE;
+  if (SCALAR_FLOAT_TYPE_P (type))
+    {
+      bits_type = build_nonstandard_integer_type (bits, 1);
+      arg = fold_build1_loc (loc, VIEW_CONVERT_EXPR, bits_type, arg);
+    }
+
+  tree call = build_call_expr_loc (loc, decl, 1,
+                                  fold_convert_loc (loc, itype, arg));
+  if (bits_type)
+    return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type,
+                           fold_convert_loc (loc, bits_type, call));
+  return fold_convert_loc (loc, type, call);
+}
+
 /* Implement TARGET_EXPAND_BUILTIN.
 
    Expand an expression EXP that calls a built-in function, with result going
@@ -8700,12 +8812,14 @@ gcn_dwarf_register_span (rtx rtl)
    operand 1 (a VGPR, valid in the first active lane) uniform across all
    lanes of operand 0, using SGPR operand 2 as a scratch.  This is output as
    a single insn because, in RTL terms, the two moves would just copy the
-   value out and back again, and later passes would delete the copy back.  */
+   value out and back again, and later passes would delete the copy back.
+   MODE may also be a vector mode (the V64 form of the pattern), whose
+   elements take the same number of VGPRs as the scalar mode.  */
 
 const char *
 gcn_output_ensure_scalar (rtx *operands, machine_mode mode)
 {
-  int partcount = MAX (1, GET_MODE_SIZE (mode) / 4);
+  int partcount = MAX (1, GET_MODE_UNIT_SIZE (mode) / 4);
   for (int p = 0; p < partcount; p++)
     {
       rtx ops[3];
@@ -8856,6 +8970,8 @@ gcn_output_ensure_scalar (rtx *operands, machine_mode 
mode)
 #define TARGET_PROMOTE_FUNCTION_MODE gcn_promote_function_mode
 #undef  TARGET_REGISTER_MOVE_COST
 #define TARGET_REGISTER_MOVE_COST gcn_register_move_cost
+#undef  TARGET_RESOLVE_OVERLOADED_BUILTIN
+#define TARGET_RESOLVE_OVERLOADED_BUILTIN gcn_resolve_overloaded_builtin
 #undef  TARGET_RETURN_IN_MEMORY
 #define TARGET_RETURN_IN_MEMORY gcn_return_in_memory
 #undef  TARGET_RTX_COSTS
diff --git a/gcc/config/gcn/gcn.md b/gcc/config/gcn/gcn.md
index 0ac727345b0dc..5513c48a19c48 100644
--- a/gcc/config/gcn/gcn.md
+++ b/gcc/config/gcn/gcn.md
@@ -96,7 +96,8 @@
   UNSPEC_SIMT_VOTE_ANY UNSPEC_SIMT_LAST_LANE UNSPEC_SIMT_XCHG_IDX
   UNSPEC_VSUBREG_WRITE UNSPEC_VSUBREG_READ
   UNSPEC_ENSURE_SCALAR
-  UNSPEC_SIMT_MASK_MERGE])
+  UNSPEC_SIMT_MASK_MERGE
+  UNSPEC_SIMT_FIRST_LANE_P UNSPEC_SIMT_BROADCAST_FIRST_LANE])
 
 ;; }}}
 ;; {{{ Attributes
@@ -2733,13 +2734,16 @@
 
 ;; Implement IFN_GOMP_SIMT_VOTE_ANY:
 ;; set operand 0 to zero iff all lanes supply zero in operand 1
-; This pseudo instruction should be replaced by the simt_vect pass
-(define_insn "omp_simt_vote_any"
+; The simt_vect pass replaces this with omp_simt_vote_any_v64si in SIMT code.
+; What remains is single-threaded (EXEC == 1), where operand 1 is the vote.
+(define_insn_and_split "omp_simt_vote_any"
   [(set (match_operand:SI 0 "register_operand" "")
        (unspec:SI [(match_operand:SI 1 "register_operand" "")]
                   UNSPEC_SIMT_VOTE_ANY))]
   "flag_simt_mode"
-  "sorry, unimplemented %0 = simt_vote_any %1")
+  "#"
+  ""
+  [(set (match_dup 0) (match_dup 1))])
 
 ;; Implement IFN_GOMP_SIMT_LAST_LANE:
 ;; set operand 0 to the value of the last active lane
@@ -2783,7 +2787,35 @@
        (if_then_else (eq_attr "alternative" "0")
                      (const_int 0)
                      (symbol_ref "MAX (1, GET_MODE_SIZE (<MODE>mode) / 4)
-                                  * (TARGET_CDNA3_NOPS ? 16 : 12)")))])
+                                  * (TARGET_CDNA3_NOPS ? 16 : 12)")))
+   (set_attr "simt_clobber" "1")])
+
+;; Return true in the lowest numbered active thread, and false in all others.
+; The simt_vect pass replaces this with simt_first_lane_p_di in SIMT code.
+; What remains is single-threaded (EXEC == 1), where the answer is true.
+(define_insn_and_split "simt_first_lane_p_bi"
+  [(set (match_operand:BI 0 "register_operand" "=r")
+       (unspec:BI [(reg:SI LANE_ID_REG)]
+                  UNSPEC_SIMT_FIRST_LANE_P))
+   (clobber (match_scratch:SI 1 "=SD"))]
+  "flag_simt_mode"
+  "#"
+  ""
+  [(set (match_dup 0) (const_int 1))]
+  ""
+  [(set_attr "simt_clobber" "1")])
+
+;; Return the value from the lowest numbered active thread in all the active
+;; threads.  This is the same thing as ensure_scalar.
+(define_expand "simt_broadcast_first_lane_<mode>"
+  [(match_operand:SIDI 0 "register_operand")
+   (match_operand:SIDI 1 "register_operand")]
+  "flag_simt_mode"
+  {
+    emit_insn (gen_ensure_scalar (<MODE>mode, operands[0],
+                                 force_reg (<MODE>mode, operands[1])));
+    DONE;
+  })
 
 ;; SIMT EXEC handling instructions that won't get optimized away
 
diff --git a/gcc/testsuite/gcc.target/gcn/simt-broadcast-first-lane-1.c 
b/gcc/testsuite/gcc.target/gcn/simt-broadcast-first-lane-1.c
new file mode 100644
index 0000000000000..5d6ad29bcb214
--- /dev/null
+++ b/gcc/testsuite/gcc.target/gcn/simt-broadcast-first-lane-1.c
@@ -0,0 +1,57 @@
+/* Check that __builtin_gcn_simt_broadcast_first_lane gives every active lane
+   the value of the first active lane, inside a SIMT region, in a
+   conditional (where the first active lane is not lane 0), and in a
+   function called from a SIMT region.  */
+
+/* { dg-do run } */
+/* { dg-require-effective-target amdgcn_simt } */
+/* { dg-additional-options "-O1" } */
+
+#ifndef __SIMT__
+#error "This test requires -msimt"
+#endif
+
+#define TID __builtin_gcn_dim_pos (2)
+
+/* Global variables aren't thread-local.  */
+int in[64], all[64], odd[64], call[64];
+
+int __attribute__((noclone,noinline))
+bcast (int x)
+{
+  return __builtin_gcn_simt_broadcast_first_lane (x);
+}
+
+void __attribute__((noclone,noinline))
+f ()
+{
+  all[TID] = __builtin_gcn_simt_broadcast_first_lane (in[TID] * 3);
+  if (TID & 1)
+    odd[TID] = __builtin_gcn_simt_broadcast_first_lane (in[TID] + 1000);
+  call[TID] = bcast (in[TID] + 100);
+}
+
+/* Note: main enters in single-threaded mode.  */
+int
+main ()
+{
+  int i, A;
+
+  for (i = 0; i < 64; i++)
+    {
+      in[i] = i + 7;
+      odd[i] = -1;
+    }
+
+  A = __builtin_gcn_simt_enter (1, 2);
+  f ();
+  __builtin_gcn_simt_exit (A);
+
+  for (i = 0; i < 64; i++)
+    if (all[i] != 21
+       || odd[i] != ((i & 1) ? 1008 : -1)
+       || call[i] != 107)
+      __builtin_abort ();
+
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/gcn/simt-broadcast-first-lane-2.c 
b/gcc/testsuite/gcc.target/gcn/simt-broadcast-first-lane-2.c
new file mode 100644
index 0000000000000..990c6f1027c72
--- /dev/null
+++ b/gcc/testsuite/gcc.target/gcn/simt-broadcast-first-lane-2.c
@@ -0,0 +1,101 @@
+/* Check that the generic __builtin_gcn_simt_broadcast_first_lane returns a
+   value of its argument's type, and gives every active lane the first active
+   lane's value, for integers of every size, an enum, a pointer and
+   floating-point values.  The odd lanes check the case where the first
+   active lane is not lane 0.  */
+
+/* { dg-do run } */
+/* { dg-require-effective-target amdgcn_simt } */
+/* { dg-additional-options "-O1" } */
+
+#ifndef __SIMT__
+#error "This test requires -msimt"
+#endif
+
+#define TID __builtin_gcn_dim_pos (2)
+#define BCAST __builtin_gcn_simt_broadcast_first_lane
+#define SAME_TYPE(X, T) \
+  __builtin_types_compatible_p (__typeof__ (BCAST ((X))), T)
+
+enum e { E0, E1 = 1000 };
+
+/* Global variables aren't thread-local.  */
+signed char c[64];
+unsigned short s[64];
+unsigned int u[64];
+long l[64];
+unsigned long ul[64];
+enum e en[64];
+int *p[64];
+float fl[64];
+double db[64];
+
+int in[64];
+
+/* The result types match the argument types.  */
+int types_ok = (SAME_TYPE ((signed char) 1, signed char)
+               && SAME_TYPE ((unsigned short) 1, unsigned short)
+               && SAME_TYPE (1u, unsigned int)
+               && SAME_TYPE (1L, long)
+               && SAME_TYPE (1UL, unsigned long)
+               && SAME_TYPE ((enum e) E1, enum e)
+               && SAME_TYPE ((const int *) 0, const int *)
+               && SAME_TYPE (1.0f, float)
+               && SAME_TYPE (1.0, double));
+
+void __attribute__((noclone,noinline))
+f1 (int i)
+{
+  int t = TID;
+  c[t] = BCAST ((signed char) (-100 + i));
+  s[t] = BCAST ((unsigned short) (60000 + i));
+  u[t] = BCAST (0x80000000u + i);
+  l[t] = BCAST (((long) (i + 7) << 32) | (0x80000000u + i));
+  ul[t] = BCAST (~0UL - i);
+  en[t] = BCAST ((enum e) (E1 + i));
+  p[t] = BCAST (&in[i]);
+  fl[t] = BCAST (-1.5f * (i + 1));
+  db[t] = BCAST (1e300 * (i + 1));
+}
+
+void __attribute__((noclone,noinline))
+f ()
+{
+  int i = TID;
+  if (i & 1)
+    f1 (i);
+  else
+    f1 (0);
+}
+
+/* Note: main enters in single-threaded mode.  */
+int
+main ()
+{
+  int i, A;
+
+  if (!types_ok)
+    __builtin_abort ();
+
+  A = __builtin_gcn_simt_enter (1, 2);
+  f ();
+  __builtin_gcn_simt_exit (A);
+
+  for (i = 0; i < 64; i++)
+    {
+      /* Even lanes broadcast lane 0's values, and odd lanes lane 1's.  */
+      int j = i & 1;
+      if (c[i] != -100 + j
+         || s[i] != 60000 + j
+         || u[i] != 0x80000000u + j
+         || l[i] != (((long) (j + 7) << 32) | (0x80000000u + j))
+         || ul[i] != ~0UL - j
+         || en[i] != E1 + j
+         || p[i] != &in[j]
+         || fl[i] != -1.5f * (j + 1)
+         || db[i] != 1e300 * (j + 1))
+       __builtin_abort ();
+    }
+
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/gcn/simt-broadcast-first-lane-3.c 
b/gcc/testsuite/gcc.target/gcn/simt-broadcast-first-lane-3.c
new file mode 100644
index 0000000000000..4596624243b0b
--- /dev/null
+++ b/gcc/testsuite/gcc.target/gcn/simt-broadcast-first-lane-3.c
@@ -0,0 +1,16 @@
+/* Check the errors for invalid arguments to the generic
+   __builtin_gcn_simt_broadcast_first_lane.  */
+
+/* { dg-do compile } */
+
+struct st { int a; };
+__extension__ typedef __int128 i128_t;
+
+void
+f (struct st x, i128_t i128)
+{
+  __builtin_gcn_simt_broadcast_first_lane (x); /* { dg-error "expects an 
integer, pointer or floating-point argument" } */
+  __builtin_gcn_simt_broadcast_first_lane (i128); /* { dg-error "expects an 
integer, pointer or floating-point argument" } */
+  __builtin_gcn_simt_broadcast_first_lane (); /* { dg-error "expects 1 
argument but 0 given" } */
+  __builtin_gcn_simt_broadcast_first_lane (1, 2); /* { dg-error "expects 1 
argument but 2 given" } */
+}
diff --git a/gcc/testsuite/gcc.target/gcn/simt-broadcast-first-lane-di-1.c 
b/gcc/testsuite/gcc.target/gcn/simt-broadcast-first-lane-di-1.c
new file mode 100644
index 0000000000000..7bdeb2bbb69c3
--- /dev/null
+++ b/gcc/testsuite/gcc.target/gcn/simt-broadcast-first-lane-di-1.c
@@ -0,0 +1,63 @@
+/* Check that __builtin_gcn_simt_broadcast_first_lane_di gives every active
+   lane the 64-bit value of the first active lane, inside a SIMT region, in a
+   conditional (where the first active lane is not lane 0), in a function
+   called from a SIMT region, and for a pointer.  */
+
+/* { dg-do run } */
+/* { dg-require-effective-target amdgcn_simt } */
+/* { dg-additional-options "-O1" } */
+
+#ifndef __SIMT__
+#error "This test requires -msimt"
+#endif
+
+#define TID __builtin_gcn_dim_pos (2)
+
+/* Global variables aren't thread-local.  */
+long in[64], all[64], odd[64], call[64];
+long *ptr[64];
+
+long __attribute__((noclone,noinline))
+bcast (long x)
+{
+  return __builtin_gcn_simt_broadcast_first_lane_di (x);
+}
+
+void __attribute__((noclone,noinline))
+f ()
+{
+  all[TID] = __builtin_gcn_simt_broadcast_first_lane_di (in[TID] * 3);
+  if (TID & 1)
+    odd[TID] = __builtin_gcn_simt_broadcast_first_lane_di (in[TID] + 1000);
+  call[TID] = bcast (in[TID] + 100);
+  ptr[TID] = (long *) __builtin_gcn_simt_broadcast_first_lane_di
+    ((long) &in[TID]);
+}
+
+/* Note: main enters in single-threaded mode.  */
+int
+main ()
+{
+  int i, A;
+
+  /* Make the high and low halves differ between lanes, and set the sign bit
+     of the low half.  */
+  for (i = 0; i < 64; i++)
+    {
+      in[i] = ((long) (i + 7) << 32) | (0x80000000u + i);
+      odd[i] = -1;
+    }
+
+  A = __builtin_gcn_simt_enter (1, 2);
+  f ();
+  __builtin_gcn_simt_exit (A);
+
+  for (i = 0; i < 64; i++)
+    if (all[i] != in[0] * 3
+       || odd[i] != ((i & 1) ? in[1] + 1000 : -1)
+       || call[i] != in[0] + 100
+       || ptr[i] != &in[0])
+      __builtin_abort ();
+
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/gcn/simt-first-lane-p-1.c 
b/gcc/testsuite/gcc.target/gcn/simt-first-lane-p-1.c
new file mode 100644
index 0000000000000..2339823f09a8a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/gcn/simt-first-lane-p-1.c
@@ -0,0 +1,62 @@
+/* Check that __builtin_gcn_simt_first_lane_p is true in the first active
+   lane only: inside a SIMT region, in a conditional (where the first active
+   lane is not lane 0), in a function called from a SIMT region, and in
+   single-threaded code.  Also check that it works as a guard.  */
+
+/* { dg-do run } */
+/* { dg-require-effective-target amdgcn_simt } */
+/* { dg-additional-options "-O1" } */
+
+#ifndef __SIMT__
+#error "This test requires -msimt"
+#endif
+
+#define TID __builtin_gcn_dim_pos (2)
+
+/* Global variables aren't thread-local.  */
+int all[64], odd[64], call[64];
+int guarded;
+
+int __attribute__((noclone,noinline))
+first_lane_p ()
+{
+  return __builtin_gcn_simt_first_lane_p ();
+}
+
+void __attribute__((noclone,noinline))
+f ()
+{
+  all[TID] = __builtin_gcn_simt_first_lane_p ();
+  if (TID & 1)
+    odd[TID] = __builtin_gcn_simt_first_lane_p ();
+  call[TID] = first_lane_p ();
+  if (__builtin_gcn_simt_first_lane_p ())
+    guarded++;
+}
+
+/* Note: main enters in single-threaded mode.  */
+int
+main ()
+{
+  int i, A;
+
+  for (i = 0; i < 64; i++)
+    odd[i] = -1;
+
+  if (!__builtin_gcn_simt_first_lane_p ())
+    __builtin_abort ();
+
+  A = __builtin_gcn_simt_enter (1, 2);
+  f ();
+  __builtin_gcn_simt_exit (A);
+
+  for (i = 0; i < 64; i++)
+    if (all[i] != (i == 0)
+       || odd[i] != ((i & 1) ? i == 1 : -1)
+       || call[i] != (i == 0))
+      __builtin_abort ();
+  if (guarded != 1)
+    __builtin_abort ();
+
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/gcn/simt-vote-any-1.c 
b/gcc/testsuite/gcc.target/gcn/simt-vote-any-1.c
new file mode 100644
index 0000000000000..7d60ad2d4d893
--- /dev/null
+++ b/gcc/testsuite/gcc.target/gcn/simt-vote-any-1.c
@@ -0,0 +1,68 @@
+/* Check that __builtin_gcn_simt_vote_any is nonzero in every active lane
+   if any active lane passes a nonzero value, and zero otherwise: inside a
+   SIMT region, in a conditional (where inactive lanes must not vote), in a
+   function called from a SIMT region, and in single-threaded code.  */
+
+/* { dg-do run } */
+/* { dg-require-effective-target amdgcn_simt } */
+/* { dg-additional-options "-O1" } */
+
+#ifndef __SIMT__
+#error "This test requires -msimt"
+#endif
+
+#define TID __builtin_gcn_dim_pos (2)
+
+/* Global variables aren't thread-local.  */
+int in[64], one[64], none[64], odd_in[64], odd_out[64], call[64];
+
+int __attribute__((noclone,noinline))
+vote_any (int x)
+{
+  return __builtin_gcn_simt_vote_any (x);
+}
+
+void __attribute__((noclone,noinline))
+f ()
+{
+  one[TID] = __builtin_gcn_simt_vote_any (in[TID] == 37);
+  none[TID] = __builtin_gcn_simt_vote_any (in[TID] == 64);
+  if (TID & 1)
+    {
+      /* Lane 2 is inactive, so its vote must not count.  */
+      odd_in[TID] = __builtin_gcn_simt_vote_any (in[TID] == 3);
+      odd_out[TID] = __builtin_gcn_simt_vote_any (in[TID] == 2);
+    }
+  call[TID] = vote_any (in[TID] == 63);
+}
+
+/* Note: main enters in single-threaded mode.  */
+int
+main ()
+{
+  int i, A;
+
+  for (i = 0; i < 64; i++)
+    {
+      in[i] = i;
+      odd_in[i] = odd_out[i] = -1;
+    }
+
+  if (!__builtin_gcn_simt_vote_any (in[5])
+      || __builtin_gcn_simt_vote_any (in[0]))
+    __builtin_abort ();
+
+  A = __builtin_gcn_simt_enter (1, 2);
+  f ();
+  __builtin_gcn_simt_exit (A);
+
+  for (i = 0; i < 64; i++)
+    if (!one[i]
+       || none[i]
+       || ((i & 1) ? !odd_in[i] : odd_in[i] != -1)
+       || ((i & 1) ? odd_out[i] : odd_out[i] != -1)
+       || !call[i])
+      __builtin_abort ();
+
+  return 0;
+}
-- 
2.56.0

Reply via email to