From: Andrew Stubbs <[email protected]>
In full SIMT mode, the "omp single" directive must be applied to vector lanes
in addition to wavefront threads.
The regular algorthm uses barriers to allow the secondary threads to wait for
the "single" thread to complete, but in SIMT mode the other threads in the wave
need to continue to "run" but in a masked mode. The SIMT code generation will
take care of this with a simple "if", so it's mostly enough to avoid the
barriers.
A problem case is handled by a new hook TARGET_SIMT_CAN_DUPLICATE_CALLS, that
ensures calls are SIMT-safe.
Assisted-by: Claude Opus 5.5
Claude generated the testcases and assisted with some of the code.
gcc/ChangeLog:
* config/gcn/gcn.cc (gcn_simt_can_duplicate_calls): New function.
(TARGET_SIMT_CAN_DUPLICATE_CALLS): Define.
* config/nvptx/nvptx.cc (TARGET_SIMT_CAN_DUPLICATE_CALLS): Define.
* doc/tm.texi.in (TARGET_SIMT_CAN_DUPLICATE_CALLS): Add hook
placeholder.
* doc/tm.texi: Regenerate.
* omp-builtins.def (BUILT_IN_GOMP_SINGLE_COPY_WAIT): New.
* omp-low.cc (lower_omp_single_simple): Document the "simt" mode
lowering of copyprivate.
(lower_omp_single_copy): In "simt" offload mode, use
GOMP_single_start and GOMP_single_copy_wait.
* target.def (can_duplicate_calls): New SIMT target hook.
* tree-cfg.cc (simt_no_duplicate_call_p): New function.
(gimple_can_duplicate_bb_p): Use it.
libgomp/ChangeLog:
* libgomp.map (GOMP_6.0.2): Add GOMP_single_copy_wait.
* libgomp_g.h (GOMP_single_copy_wait): Declare.
* single.c (GOMP_single_copy_wait): New function.
* testsuite/libgomp.c-c++-common/simt-single-copyprivate-amdgcn-1.c:
New test.
---
gcc/config/gcn/gcn.cc | 11 +++
gcc/config/nvptx/nvptx.cc | 5 ++
gcc/doc/tm.texi | 9 ++
gcc/doc/tm.texi.in | 2 +
gcc/omp-builtins.def | 2 +
gcc/omp-low.cc | 70 +++++++++++++++
gcc/target.def | 10 +++
gcc/tree-cfg.cc | 41 +++++++++
libgomp/libgomp.map | 1 +
libgomp/libgomp_g.h | 1 +
libgomp/single.c | 31 +++++++
.../simt-single-copyprivate-amdgcn-1.c | 86 +++++++++++++++++++
12 files changed, 269 insertions(+)
create mode 100644
libgomp/testsuite/libgomp.c-c++-common/simt-single-copyprivate-amdgcn-1.c
diff --git a/gcc/config/gcn/gcn.cc b/gcc/config/gcn/gcn.cc
index 2c93b009b4a3a..4854edcc72bb2 100644
--- a/gcc/config/gcn/gcn.cc
+++ b/gcc/config/gcn/gcn.cc
@@ -6418,6 +6418,15 @@ gcn_simt_vf (void)
return 1;
}
+/* Implement TARGET_SIMT_CAN_DUPLICATE_CALLS. In SIMT mode both arms of a
+ divergent branch are executed in turn by the wavefront. */
+
+static bool
+gcn_simt_can_duplicate_calls (void)
+{
+ return !flag_simt_mode;
+}
+
/* Implement TARGET_STACK_VARIABLE_PADDING. */
poly_uint64
@@ -8993,6 +9002,8 @@ gcn_output_ensure_scalar (rtx *operands, machine_mode
mode)
#define TARGET_SIMD_CLONE_USABLE gcn_simd_clone_usable
#undef TARGET_SIMT_VF
#define TARGET_SIMT_VF gcn_simt_vf
+#undef TARGET_SIMT_CAN_DUPLICATE_CALLS
+#define TARGET_SIMT_CAN_DUPLICATE_CALLS gcn_simt_can_duplicate_calls
#undef TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P
#define TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P \
gcn_small_register_classes_for_mode_p
diff --git a/gcc/config/nvptx/nvptx.cc b/gcc/config/nvptx/nvptx.cc
index 305b2bd5fe687..dd326d9319c4c 100644
--- a/gcc/config/nvptx/nvptx.cc
+++ b/gcc/config/nvptx/nvptx.cc
@@ -7956,6 +7956,11 @@ nvptx_asm_output_def_from_decls (FILE *stream, tree name,
#undef TARGET_SIMT_VF
#define TARGET_SIMT_VF nvptx_simt_vf
+/* All code may run SIMT-fashion: the threads of a warp execute both arms of
+ a divergent branch in turn. */
+#undef TARGET_SIMT_CAN_DUPLICATE_CALLS
+#define TARGET_SIMT_CAN_DUPLICATE_CALLS hook_bool_void_false
+
#undef TARGET_OMP_DEVICE_KIND_ARCH_ISA
#define TARGET_OMP_DEVICE_KIND_ARCH_ISA nvptx_omp_device_kind_arch_isa
diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi
index bcddd9b898f91..181f58a872ef9 100644
--- a/gcc/doc/tm.texi
+++ b/gcc/doc/tm.texi
@@ -6643,6 +6643,15 @@ is, the more desirable it is to use it.
Return number of threads in SIMT thread group on the target.
@end deftypefn
+@deftypefn {Target Hook} bool TARGET_SIMT_CAN_DUPLICATE_CALLS (void)
+Return false if the current function may be executed in SIMT fashion,
+with the threads of a SIMT thread group executing both arms of a divergent
+branch in turn. A call that the threads must make together, such as an
+OpenMP barrier or a function that uses cross-lane operations, must then not
+be duplicated into the arms of a branch, because the group would make it once
+for each arm. The default returns true.
+@end deftypefn
+
@deftypefn {Target Hook} int TARGET_OMP_DEVICE_KIND_ARCH_ISA (enum
omp_device_kind_arch_isa @var{trait}, const char *@var{name})
Return 1 if @var{trait} @var{name} is present in the OpenMP context's
device trait set, return 0 if not present in any OpenMP context in the
diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in
index 35c658a46473b..b5997b05e369f 100644
--- a/gcc/doc/tm.texi.in
+++ b/gcc/doc/tm.texi.in
@@ -4369,6 +4369,8 @@ address; but often a machine-dependent strategy can
generate better code.
@hook TARGET_SIMT_VF
+@hook TARGET_SIMT_CAN_DUPLICATE_CALLS
+
@hook TARGET_OMP_DEVICE_KIND_ARCH_ISA
@hook TARGET_GOACC_VALIDATE_DIMS
diff --git a/gcc/omp-builtins.def b/gcc/omp-builtins.def
index 192ff414e9170..abebe69de94ff 100644
--- a/gcc/omp-builtins.def
+++ b/gcc/omp-builtins.def
@@ -461,6 +461,8 @@ DEF_GOMP_BUILTIN (BUILT_IN_GOMP_SINGLE_COPY_START,
"GOMP_single_copy_start",
BT_FN_PTR, ATTR_NOTHROW_LEAF_LIST)
DEF_GOMP_BUILTIN (BUILT_IN_GOMP_SINGLE_COPY_END, "GOMP_single_copy_end",
BT_FN_VOID_PTR, ATTR_NOTHROW_LEAF_LIST)
+DEF_GOMP_BUILTIN (BUILT_IN_GOMP_SINGLE_COPY_WAIT, "GOMP_single_copy_wait",
+ BT_FN_PTR_PTR, ATTR_NOTHROW_LEAF_LIST)
DEF_GOMP_BUILTIN (BUILT_IN_GOMP_SCOPE_START, "GOMP_scope_start",
BT_FN_VOID_PTR, ATTR_NOTHROW_LEAF_LIST)
DEF_GOMP_BUILTIN (BUILT_IN_GOMP_SCOPE_START_WITH_END,
"GOMP_scope_start_with_end",
diff --git a/gcc/omp-low.cc b/gcc/omp-low.cc
index 2502967c699ee..f600cccff25dd 100644
--- a/gcc/omp-low.cc
+++ b/gcc/omp-low.cc
@@ -8846,6 +8846,34 @@ lower_omp_single_simple (gomp_single *single_stmt,
gimple_seq *pre_p)
GOMP_barrier ();
}
+ In "simt" offload mode each lane of a wavefront is a thread, and a lane
+ cannot wait for another lane of its own wavefront, so the threads that
+ don't execute BODY cannot wait for it inside GOMP_single_copy_start.
+ Instead emit:
+
+ {
+ copyout_p = NULL;
+ if (GOMP_single_start ())
+ {
+ BODY;
+ copyout.a = a;
+ copyout.b = b;
+ copyout.c = c;
+ copyout_p = ©out;
+ }
+ if ((copyin_p = GOMP_single_copy_wait (copyout_p)) != NULL)
+ {
+ a = copyin_p->a;
+ b = copyin_p->b;
+ c = copyin_p->c;
+ }
+ GOMP_barrier ();
+ }
+
+ The test after GOMP_single_copy_wait must depend only on its result, not
+ on COPYOUT_P, or jump threading duplicates the call into both arms of the
+ first IF, and a wavefront that executes both arms calls it twice.
+
FIXME. It may be better to delay expanding the logic of this until
pass_expand_omp. The expanded logic may make the job more difficult
to a synchronization analysis pass. */
@@ -8863,6 +8891,48 @@ lower_omp_single_copy (gomp_single *single_stmt,
gimple_seq *pre_p,
ptr_type = build_pointer_type (ctx->record_type);
ctx->receiver_decl = create_tmp_var (ptr_type, ".omp_copy_i");
+ if (param_offload_gpu_mode == OFFLOAD_GPU_MODE_SIMT
+ && omp_maybe_offloaded_ctx (ctx))
+ {
+ tree copyout_p = create_tmp_var (ptr_type, ".omp_copy_p");
+ gimplify_assign (copyout_p, build_int_cst (ptr_type, 0), pre_p);
+
+ l0 = create_artificial_label (loc);
+ l1 = create_artificial_label (loc);
+ l2 = create_artificial_label (loc);
+
+ bfn_decl = builtin_decl_explicit (BUILT_IN_GOMP_SINGLE_START);
+ t = build_call_expr_loc (loc, bfn_decl, 0);
+ t = build3 (COND_EXPR, void_type_node, t,
+ build_and_jump (&l0), build_and_jump (&l1));
+ gimplify_and_add (t, pre_p);
+
+ gimple_seq_add_stmt (pre_p, gimple_build_label (l0));
+ gimple_seq_add_seq (pre_p, gimple_omp_body (single_stmt));
+ copyin_seq = NULL;
+ lower_copyprivate_clauses (gimple_omp_single_clauses (single_stmt),
+ pre_p, ©in_seq, ctx);
+ t = build_fold_addr_expr_loc (loc, ctx->sender_decl);
+ gimplify_assign (copyout_p, t, pre_p);
+
+ gimple_seq_add_stmt (pre_p, gimple_build_label (l1));
+ bfn_decl = builtin_decl_explicit (BUILT_IN_GOMP_SINGLE_COPY_WAIT);
+ t = build_call_expr_loc (loc, bfn_decl, 1, copyout_p);
+ t = fold_convert_loc (loc, ptr_type, t);
+ gimplify_assign (ctx->receiver_decl, t, pre_p);
+
+ l0 = create_artificial_label (loc);
+ t = build2 (EQ_EXPR, boolean_type_node, ctx->receiver_decl,
+ build_int_cst (ptr_type, 0));
+ t = build3 (COND_EXPR, void_type_node, t,
+ build_and_jump (&l2), build_and_jump (&l0));
+ gimplify_and_add (t, pre_p);
+ gimple_seq_add_stmt (pre_p, gimple_build_label (l0));
+ gimple_seq_add_seq (pre_p, copyin_seq);
+ gimple_seq_add_stmt (pre_p, gimple_build_label (l2));
+ return;
+ }
+
l0 = create_artificial_label (loc);
l1 = create_artificial_label (loc);
l2 = create_artificial_label (loc);
diff --git a/gcc/target.def b/gcc/target.def
index c470b38a481f8..5a82e9b410a7c 100644
--- a/gcc/target.def
+++ b/gcc/target.def
@@ -1679,6 +1679,16 @@ DEFHOOK
"Return number of threads in SIMT thread group on the target.",
int, (void), NULL)
+DEFHOOK
+(can_duplicate_calls,
+"Return false if the current function may be executed in SIMT fashion,\n\
+with the threads of a SIMT thread group executing both arms of a divergent\n\
+branch in turn. A call that the threads must make together, such as an\n\
+OpenMP barrier or a function that uses cross-lane operations, must then not\n\
+be duplicated into the arms of a branch, because the group would make it
once\n\
+for each arm. The default returns true.",
+bool, (void), hook_bool_void_true)
+
/* Allow the target to pad stack variables. */
DEFHOOK
(stack_variable_padding,
diff --git a/gcc/tree-cfg.cc b/gcc/tree-cfg.cc
index f9da02ca21394..67728d98e52f6 100644
--- a/gcc/tree-cfg.cc
+++ b/gcc/tree-cfg.cc
@@ -6452,6 +6452,44 @@ gimple_split_block_before_cond_jump (basic_block bb)
}
+/* Return true if the call G must not be duplicated into the arms of a
+ branch because the current function may be executed SIMT-fashion: the
+ threads of a SIMT thread group execute both arms of a divergent branch in
+ turn, so a call that the threads must make together (a libgomp barrier,
+ work-sharing or SINGLE call, or any function that uses cross-lane
+ operations) would be made once per arm. Internal functions and ordinary
+ builtins are exempt. */
+
+static bool
+simt_no_duplicate_call_p (const gimple *g)
+{
+ if (gimple_call_internal_p (g))
+ return false;
+ tree fndecl = gimple_call_fndecl (g);
+ if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
+ {
+ const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
+ if (!startswith (name, "__builtin_GOMP_")
+ && !startswith (name, "__builtin_GOACC_"))
+ return false;
+ }
+ if (!targetm.simt.can_duplicate_calls ())
+ return true;
+#ifndef ACCEL_COMPILER
+ /* The host compiler optimizes offloaded code before it is streamed to
+ the offload compiler, which uses SIMT only in the SIMT offload modes. */
+ if (ENABLE_OFFLOADING && current_function_decl
+ && (param_offload_gpu_mode == OFFLOAD_GPU_MODE_SIMT_DIRECTED
+ || param_offload_gpu_mode == OFFLOAD_GPU_MODE_SIMT))
+ {
+ cgraph_node *node = cgraph_node::get (current_function_decl);
+ if (node && node->offloadable)
+ return true;
+ }
+#endif
+ return false;
+}
+
/* Return true if basic_block can be duplicated. */
static bool
@@ -6499,6 +6537,9 @@ gimple_can_duplicate_bb_p (const_basic_block bb)
|| gimple_call_internal_p (g, IFN_GOMP_SIMT_REDUCE)
|| gimple_call_internal_p (g, IFN_GOMP_SIMT_XCHG_IDX)))
return false;
+
+ if (is_gimple_call (g) && simt_no_duplicate_call_p (g))
+ return false;
}
return true;
diff --git a/libgomp/libgomp.map b/libgomp/libgomp.map
index 68e10d5bb7dd9..8c6f4601a3de3 100644
--- a/libgomp/libgomp.map
+++ b/libgomp/libgomp.map
@@ -490,6 +490,7 @@ GOMP_6.0.1 {
GOMP_6.0.2 {
global:
+ GOMP_single_copy_wait;
GOMP_has_masked_thread_num;
GOMP_has_masked_thread_num_with_end;
GOMP_masked_end;
diff --git a/libgomp/libgomp_g.h b/libgomp/libgomp_g.h
index 9caf9c3571bf9..2d7440d0615ee 100644
--- a/libgomp/libgomp_g.h
+++ b/libgomp/libgomp_g.h
@@ -350,6 +350,7 @@ extern bool GOMP_single_start_with_end (void);
extern void GOMP_single_end (void);
extern void *GOMP_single_copy_start (void);
extern void GOMP_single_copy_end (void *);
+extern void *GOMP_single_copy_wait (void *);
/* scope.c */
diff --git a/libgomp/single.c b/libgomp/single.c
index cc5d27b84f7da..e6d4736dbcb70 100644
--- a/libgomp/single.c
+++ b/libgomp/single.c
@@ -150,3 +150,34 @@ GOMP_single_copy_end (void *data)
gomp_work_share_end_nowait ();
}
+
+/* This routine is called by every thread at the end of a SINGLE construct
+ with a COPYPRIVATE clause, when the construct was compiled for "simt"
+ offload mode. There GOMP_single_start chooses the thread that executes
+ the body, because a lane can't wait for another lane of its own
+ wavefront in GOMP_single_copy_start. DATA is the copyout structure in
+ the thread that executed the body, and NULL in every other thread.
+ Returns NULL in the thread that executed the body, and its copyout
+ structure in every other thread. */
+
+void *
+GOMP_single_copy_wait (void *data)
+{
+ struct gomp_thread *thr = gomp_thread ();
+ struct gomp_team *team = thr->ts.team;
+ void *ret;
+
+ if (team == NULL)
+ return NULL;
+
+ if (gomp_work_share_start (0))
+ gomp_work_share_init_done ();
+
+ if (data != NULL)
+ thr->ts.work_share->copyprivate = data;
+ gomp_team_barrier_wait (&team->barrier, thr->ts.team_id);
+
+ ret = thr->ts.work_share->copyprivate;
+ gomp_work_share_end_nowait ();
+ return data != NULL ? NULL : ret;
+}
diff --git
a/libgomp/testsuite/libgomp.c-c++-common/simt-single-copyprivate-amdgcn-1.c
b/libgomp/testsuite/libgomp.c-c++-common/simt-single-copyprivate-amdgcn-1.c
new file mode 100644
index 0000000000000..2a4821c344ac4
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/simt-single-copyprivate-amdgcn-1.c
@@ -0,0 +1,86 @@
+/* Check "#pragma omp single copyprivate" in a parallel region on an AMD GCN
+ device in "simt" offload mode, where each lane of a wavefront is an
+ OpenMP thread. The body must run exactly once per construct, and every
+ thread must receive the values, including the other lanes of the
+ executing thread's own wavefront, which can't wait for it inside
+ GOMP_single_copy_start. In this mode the construct is lowered to
+ GOMP_single_start plus GOMP_single_copy_wait instead.
+
+ Each construct also contains an atomic, and the received values are
+ tested after it, because those once caused the compiler to duplicate
+ GOMP_single_copy_wait into both arms of the branch around the body. */
+
+/* { dg-do run } */
+/* { dg-require-effective-target offload_device_gcn } */
+/* { dg-additional-options "--param=offload-gpu-mode=simt" { target
offload_target_amdgcn } } */
+/* { dg-additional-options "-foffload=-fdump-tree-optimized" { target
offload_target_amdgcn } } */
+
+#include <stdlib.h>
+#include <omp.h>
+
+#define K 32
+#define MAXT 1024
+
+struct S
+{
+ int i;
+ double d;
+};
+
+int
+main (void)
+{
+ int count[K], bad = 0, nthr = 0;
+
+ for (int k = 0; k < K; k++)
+ count[k] = 0;
+
+ #pragma omp target teams num_teams(1) map(tofrom: count, bad, nthr)
+ #pragma omp parallel
+ {
+ #pragma omp single
+ nthr = omp_get_num_threads ();
+
+ for (int k = 0; k < K; k++)
+ {
+ int v = -1;
+ long w = -1;
+ struct S s = { -1, -1.0 };
+
+ #pragma omp single copyprivate(v, w, s)
+ {
+ #pragma omp atomic
+ count[k]++;
+ v = 1000 + k;
+ w = 2000L + omp_get_thread_num ();
+ s.i = 3000 + k;
+ s.d = k + 0.5;
+ }
+
+ /* W carries the executing thread's number, so it must be in range
+ and the same in every thread. */
+ if (v != 1000 + k
+ || w < 2000L || w >= 2000L + MAXT
+ || s.i != 3000 + k || s.d != k + 0.5)
+ {
+ #pragma omp atomic
+ bad++;
+ }
+ }
+ }
+
+ if (nthr < 1 || nthr > MAXT)
+ abort ();
+ for (int k = 0; k < K; k++)
+ if (count[k] != 1)
+ abort ();
+ if (bad != 0)
+ abort ();
+
+ return 0;
+}
+
+/* The lowering must make exactly one GOMP_single_copy_wait call per
+ construct (one here), never duplicated. */
+/* { dg-final { scan-offload-tree-dump-times "GOMP_single_copy_wait \\(" 1
"optimized" { target offload_target_amdgcn } } } */
+/* { dg-final { scan-offload-tree-dump-not "GOMP_single_copy_start"
"optimized" { target offload_target_amdgcn } } } */
--
2.56.0