Hi Thomas, Andrew,
this patch implements reductions for arrays and structs for OpenACC. Following
the pattern for OpenACC reductions, this is mostly in the respective NVPTX/GCN
backends' *_goacc_reduction_setup/init/fini/teardown hooks, particularly in the
fini part, and [nvptx/gcn]_reduction_update routines. The code is mostly
similar between the two targets, with mostly the lack of vector mode handling
in GCN.
To Julian, there is a patch to the middle-end neutering, a hack actually, that
detects SSA_NAMEs used in reduction array MEM_REFs, and avoids single->parallel
copying (by moving those definitions before BUILT_IN_GOACC_SINGLE_COPY_START).
This appears to work because reductions do their own initializing of the
private copy.
As we discussed in our internal calls, the real proper way is to create the
private array in a more appropriate stage, but that is too long a shot for now.
The changes here are needed at least for some -O0 cases (when under
optimization, propagation of the private copies' local address eliminate the
SSA_NAME and things actually just work in that case). So please bear with this
hack.
I believe the new added libgomp testcases should be fairly complete. Though
note that one case of reduction of * for double arrays has been commented out
for now, for there appears to be a (presumably) unrelated issue causing this
case to fail (maybe has to do with the loop-based atomic form used by both
NVPTX/GCN). Maybe should XFAIL instead of comment out. Will do this in next
iteration.
Thanks,
Chung-Lin
2024-01-02 Chung-Lin Tang
gcc/c/ChangeLog:
* c-parser.cc (c_parser_omp_clause_reduction): Adjustments for
OpenACC-specific cases.
* c-typeck.cc (c_oacc_reduction_defined_type_p): New function.
(c_oacc_reduction_code_name): Likewise.
(c_finish_omp_clauses): Handle OpenACC cases using new functions.
gcc/cp/ChangeLog:
* parser.cc (cp_parser_omp_clause_reduction): Adjustments for
OpenACC-specific cases.
* semantics.cc (cp_oacc_reduction_defined_type_p): New function.
(cp_oacc_reduction_code_name): Likewise.
(finish_omp_reduction_clause): Handle OpenACC cases using new functions.
gcc/ChangeLog:
* config/gcn/gcn-tree.cc (gcn_reduction_update): Additions for
handling ARRAY_TYPE and RECORD_TYPE reductions.
(gcn_goacc_reduction_setup): Likewise.
(gcn_goacc_reduction_init): Likewise.
(gcn_goacc_reduction_fini): Likewise.
(gcn_goacc_reduction_teardown): Likewise.
* config/nvptx/nvptx.cc (nvptx_gen_shuffle): Properly generate
V2SI shuffle using vec_extract op.
(nvptx_get_shared_red_addr): Adjust type/alignment calculations to
use TYPE_SIZE/ALIGN_UNIT instead of machine mode based.
(nvptx_reduction_update): Additions for handling ARRAY_TYPE and
RECORD_TYPE reductions.
(nvptx_goacc_reduction_setup): Likewise.
(nvptx_goacc_reduction_init): Likewise.
(nvptx_goacc_reduction_fini): Likewise.
(nvptx_goacc_reduction_teardown): Likewise.
* omp-low.cc (scan_sharing_clauses): Adjust ARRAY_REF pointer type
building to use decl type, rather than generic ptr_type_node.
(omp_reduction_init_op): Add ARRAY_TYPE and RECORD_TYPE init op
construction.
(lower_oacc_reductions): Add code to teardown/recover array access
MEM_REF in OMP_CLAUSE_DECL, to accomodate for lookup requirements.
Adjust type/alignment calculations to use TYPE_SIZE/ALIGN_UNIT
instead of machine mode based.
* omp-oacc-neuter-broadcast.cc (worker_single_copy):
Add 'hash_set *array_reduction_base_vars' parameter.
Add xxx.
(neuter_worker_single): Add 'hash_set *array_reduction_base_vars'
parameter. Adjust recursive calls to self and worker_single_copy.
(oacc_do_neutering): Add 'hash_set *array_reduction_base_vars'
parameter. Adjust call to neuter_worker_single.
(execute_omp_oacc_neuter_broadcast): Add local
'hash_set array_reduction_base_vars' declaration. Collect MEM_REF
base-pointer SSA_NAMEs of arrays into array_reduction_base_vars. Add
'_reduction_base_vars' argument to call of oacc_do_neutering.
* omp-offload.cc (default_goacc_reduction): Add unshare_expr.
gcc/testsuite/ChangeLog:
* c-c++-common/goacc/reduction-9.c: New test.
* c-c++-common/goacc/reduction-10.c: New test.
* c-c++-common/goacc/reduction-11.c: New test.
* c-c++-common/goacc/reduction-12.c: New test.
* c-c++-common/goacc/reduction-13.c: New test.
libgomp/ChangeLog:
* testsuite/libgomp.oacc-c-c++-common/reduction.h
(check_reduction_array_xx): New macro.
(operator_apply): Likewise.
(check_reduction_array_op): Likewise.
(check_reduction_arraysec_op): Likewise.