This series adds -fdo-concurrent-to-openmp=none|host|device, the option
flang has under the same name.  With -fopenmp, the front end translates
a DO CONCURRENT construct into an OpenMP 'parallel do simd' nest (host)
or 'target teams distribute parallel do simd' nest (device), one
collapsed loop per concurrent-control.  It builds the gfc_code of that
construct and hands it to the existing gfc_trans_omp_parallel_do_simd and
gfc_trans_omp_target, so all clause splitting, mapping and privatization
of compiler temporaries are the OpenMP paths' own.  The option defaults
to none, which leaves the serial lowering untouched; the only change
there is a function factored out in 1/10.

 1/10  [PR128590]-1 Add -fdo-concurrent-to-openmp=host
       flag-types.h, lang.opt, options.cc, invoke.texi, trans.cc,
       trans-stmt.cc, trans-openmp.cc.  do_concurrent_to_openmp_1,
       gomp/do_concurrent_to_openmp_1, _2, libgomp ..._1.

 2/10  [PR128590]-2 Map DO CONCURRENT locality specs to OpenMP
       trans-openmp.cc.  gomp/..._3, libgomp ..._2.

 3/10  [PR128590]-3 Privatize DO CONCURRENT iteration-local variables
       trans-openmp.cc.  gomp/..._4, _5, libgomp ..._3.

 4/10  [PR128590]-4 Add -fdo-concurrent-to-openmp=device
       flag-types.h, lang.opt, invoke.texi, trans-stmt.cc,
       trans-openmp.cc.  gomp/..._6, _7, libgomp ..._4, _5.

 5/10  [PR128590]-5 Allow declare target calls in offloaded loops
       trans-openmp.cc.  gomp/..._8, libgomp ..._6.

 6/10  [PR128590]-6 Add -Wdo-concurrent-to-openmp
       lang.opt, invoke.texi, trans-openmp.cc.  gomp/..._9.

 7/10  [PR128590]-7 Lower masked DO CONCURRENT to OpenMP
       trans-stmt.cc, trans-openmp.cc.  gomp/..._10, libgomp ..._7.

 8/10  [PR128590]-8 Offload DO CONCURRENT in PURE procedures
       invoke.texi, trans-openmp.cc.  gomp/..._11, libgomp ..._8.

 9/10  [PR128590]-9 Clean up the CFG after resolving variant cookies
       omp-offload.cc (middle end).

10/10  [PR128590]-10 Run DO CONCURRENT with few iterations on the host
       invoke.texi, trans-decl.cc, trans.h, trans-openmp.cc.
       gomp/..._12, libgomp ..._9.

The series applies on top of the PR128791 patch, sent separately: an
independent wrong-code fix in existing OpenMP code (also in GCC 16),
where PARALLEL DO SIMD COLLAPSE(2) with a step that is not a constant
races on the DO variable.  The series needs it for runtime negative
strides.

The patches apply in order.  1/10 to 7/10 were each built and tested
with the tests added up to them; 8/10 to 10/10 were tested together.

Reading guide.  In 1/10, start at gfc_trans_omp_do_concurrent at the end
of trans-openmp.cc: it shadows the index-names, builds the EXEC_DO
chain, and puts one "iteration" node -- a DO CONCURRENT without
concurrent-controls -- in the innermost loop.  gfc_trans_do_concurrent
in trans-stmt.cc translates that node with
gfc_trans_do_concurrent_iteration, which is the DO CONCURRENT branch of
gfc_trans_forall_1 moved out unchanged; that is why LOCAL and LOCAL_INIT
need nothing in 2/10.  The flag-types.h, lang.opt and options.cc hunks
are mechanical, and the trans.cc hunk only counts enclosing OpenMP and
OpenACC constructs.

3/10 is the part to review closely.  dc_scan_body walks the body with
dc_scan_stmt, which handles a statement without the statements it
contains and then walks those, keeping the set of variables whose whole
definition precedes that point on every path through the iteration.  A
definition inside an IF, SELECT or loop construct counts only inside
it, one inside a BLOCK or ASSOCIATE construct also after it, and a label
clears the set.  For each variable of unspecified locality that the body
defines as a whole, struct dc_use records whether some reference is
outside that set and whether every iteration defines it; after a CYCLE,
a GOTO or an EXIT from a BLOCK construct, no definition counts as made
by every iteration.  F2018 11.1.7.5 lets a variable defined by several
iterations become undefined but requires one defined by a single
iteration to keep that value, and one defined by none to keep its old
value; conditional lastprivate gives exactly that.  A reference before
the definition is conforming only if no other iteration defines the
variable; in practice it is an accumulation that lacks REDUCE, so the
construct stays serial rather than silently giving a different sum.
dc_scan_index_def and the loop in dc_scan_body let b(k) stay shared
after k = 2*i.

4/10: dc_device_ok lists when a construct runs on the host instead.  Its
PURE/ELEMENTAL/declare-target rule, which 8/10 replaces, is there
because on amdgcn a target region nested in device code returns without
running its body (libgomp/config/gcn/target.c); dc_has_iterations exists
because plain lastprivate, which DISTRIBUTE requires, leaves the
variable unspecified after zero iterations.  dc_build_clauses maps 'to'
what the body does not define, because copying a constant actual
argument back faults on the GPU: its storage is read-only.  6/10 only
threads a reason through the existing fallback points.

8/10: in a PURE, ELEMENTAL or declare target procedure, which may itself
run on the device, gfc_trans_omp_do_concurrent translates the construct
twice, as a target construct and as with =host, and selects between
them with an OMP_TARGET_DEVICE_MATCHES cookie for device={kind(host)},
which ompdevlow resolves in each compiler.  dc_reset_body_decls clears
the decls that the first translation gave to labels and to BLOCK and
ASSOCIATE entities.

9/10 needs a middle-end review.  Without optimization, the branch that
a resolved cookie makes unreachable stays, and the offload compiler
must still link what it calls.  In 10/10 that branch calls
omp_target_is_present, which the device libgomp lacks, so its libgomp
tests fail to link at -O0 without 9/10.

10/10: offloading costs a kernel launch and copies, a few hundred
microseconds on this GPU.  A construct with fewer than 32 iterations,
none of whose mapped variables is present on the default device, now
runs in the encountering thread, through an if clause on the target
construct and on the parallel construct inside it; with the clause on
target alone, each host fallback starts a team of threads (560 us per
construct).  A construct that calls a procedure other than an intrinsic
one is always offloaded, since the callee may use declare target
variables.  32 is a constant for now; a --param can follow.

Known restriction, documented in invoke.texi: a procedure that is not
PURE, ELEMENTAL or declare target and contains a construct offloaded by
=device must not be called in a target region.  The front end cannot
see that call in general; on amdgcn the result is the silent skip
described above.

Of the 40 constructs in the formal package, 37 are in PURE procedures,
as are the 5 that the fiats package compiles by default; before 8/10
=device ran all of those on the host.  fiats then showed the cost that
10/10 avoids: its tests run a 3-iteration construct about 500,000
times, which took 217 s under =device, against 0.77 s serially.

Testing.  x86_64-pc-linux-gnu with amdgcn-amdhsa offloading (RX 6600
XT, gfx10-3-generic), on master 968c1131042: gfortran and libgomp
testsuites and C/C++ gomp.exp (for 9/10), no new failures; the new
tests on the host and on the GPU.  The 58 existing DO CONCURRENT/FORALL
run tests in gfortran.dg were also compiled at -O0 and -O2 with =host
and =device and compared with the serial lowering; the only difference
is do_concurrent_13.f90, which says itself that it assumes no
concurrency.  So were, with 1/10 to 7/10, the 341 tests of the Fujitsu
compiler test suite that use DO CONCURRENT, against -fopenmp alone: no
ICE, and the only new mismatch is 0269_0044, which defines the same
element in several iterations.  formal's 31 tests and fiats' 23 pass
with all three settings, at -O0 and in release builds, apart from a
fiats test that starts from random weights and fails about one run in
three with any setting.  For =device they need four independent OpenMP
fixes, sent separately: PR128977 (strided assumed-shape dummy),
PR129002 (scalar associate name), PR129003 (OpenACC firstprivate size)
and PR129004 (array-valued function result).

Relation to PR125717.  That PR asks for annot_expr_parallel_kind on DO
CONCURRENT for -ftree-parallelize-loops, which still gives wrong results
on the PR83064 test.  This series leaves that path alone and offers
-fopenmp -fdo-concurrent-to-openmp=host instead, which parallelizes the
whole collapsed nest; do_concurrent_5.f90, the PR83064 test, passes
with it.

Timings, -O3, Ryzen 7 5800X (16 threads) and RX 6600 XT, 3 runs each:
                       4096^2 Jacobi (100 it)   compute-bound kernel
  serial                      1.24 s                  2.38 s
  =host                       1.33 s                  0.158 s
  =device                     4.52 s                  0.083 s
  =device in target data      0.48 s                  0.035 s
The stencil is memory-bound on this machine; hand-written OpenMP gets
the same 1.3 s.

The formal and fiats test drivers, release builds, median of 3 runs:
                serial     =host      =device
  formal        0.036 s    0.041 s    0.22 s
  fiats         0.77 s     1.47 s     1.17 s
formal's =device time is mostly GPU start-up.

OK for mainline?

Regards,

Jerry

Reply via email to