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.
Changes since v1, most of them found by an automated review of v1.
Each fix is in the patch whose code it changes, with a new compile test,
and the commit messages and ChangeLogs say what changed.
1/10 A BLOCK construct with a variable of static storage (SAVE, a
DATA statement, -fmax-stack-var-size or -fno-automatic) and an
ASSIGN statement keep the construct serial. The ERR=, END= and
EOR= labels of every input/output statement and alternate
return labels now count as branches into the construct.
2/10 A construct stays serial if a SHARED or REDUCE variable is an
associate name, a Cray pointee or threadprivate, or if a REDUCE
variable is a character variable or a pointer. REDUCE(MAX:) on
a character variable ICEd, and a threadprivate one gave wrong
results.
3/10 Wrong-code fixes in the privatization. A variable set from an
index-name and then by an actual argument no longer counts as
indexed; an allocated or pointer-assigned component of a shared
scalar is a shared part; STAT= is a definition. References to a
function result are tracked, a procedure passed as an actual
argument counts as a call that may see the host's variables, a
COMMON or EQUIVALENCE member is not privatized, and a
definition inside a BLOCK construct that an EXIT may leave early
no longer counts after it.
4/10 Assumed-type variables run on the host, and the outer variable of
a LOCAL_INIT spec is mapped 'to': implicitly mapped tofrom, it
faulted for a constant actual argument.
6/10 Warnings for the new reasons.
7/10 A masked body counts as conditional, so on a device no variable
it defines gets plain lastprivate. The mask must not reference
what the body defines through an associate name or a pointer, a
REDUCE variable, or a procedure passed as an argument.
8/10 The host translation re-queues the deferred BLOCK entities and
gets back the attributes that the device translation changed; a
character(len=n) variable or an associate name of a character
expression in a BLOCK construct failed with "size of variable is
too large".
10/10 The presence check uses the symtree of the reference (a renamed
use-associated variable ICEd), leaves out firstprivate scalars,
and is skipped when the default device is the initial device, on
which omp_target_is_present is always true.
9/10 is unchanged, and 5/10 only in wording: in 4/10 to 8/10, the
comments, invoke.texi, the commit messages and the 6/10 warning now say
"a declare target procedure" where v1 said that a procedure "is declare
target".
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, _13, libgomp ..._1.
2/10 [PR128590]-2 Map DO CONCURRENT locality specs to OpenMP
trans-openmp.cc. gomp/..._3, _14, libgomp ..._2.
3/10 [PR128590]-3 Privatize DO CONCURRENT iteration-local variables
trans-openmp.cc. gomp/..._4, _5, _15, 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, _16, 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, _17.
7/10 [PR128590]-7 Lower masked DO CONCURRENT to OpenMP
trans-stmt.cc, trans-openmp.cc. gomp/..._10, _18, libgomp ..._7.
8/10 [PR128590]-8 Offload DO CONCURRENT in PURE procedures
invoke.texi, trans-openmp.cc. gomp/..._11, _19, 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, _20, 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, also to master 56c7766c4ee. In v2, each
patch was built with its own and the earlier tests passing; the series
was tested as a whole.
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 unless an EXIT
may leave the construct first, 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, and the attributes that dc_save_code_attrs saved
before it are restored.
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 contains
a construct offloaded by =device and is not a PURE, ELEMENTAL or declare
target procedure 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: v2 with the gfortran and
libgomp testsuites, no new failures, and the new tests on the host and
on the GPU. The rest of this paragraph and the timings are from v1,
which v2 changes only where it keeps more constructs serial. 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, v2), 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.