I am working on a v2 of this to address comments from the gcc.sashiko.dev.

Jerry

On 11/26/26 5:56 PM, Jerry DeLisle wrote:
See attached patch, v2.  A scalar ASSOCIATE name of a variable is a
pointer to its selector, and gfc_omp_predetermined_sharing makes that
pointer firstprivate.  In a target construct the device then gets the
host address of the selector and faults when it dereferences it.

The fix treats the associate name like a by-reference dummy: privatized
by reference and predetermined shared, which OpenMP 5.2 (5.1.1)
requires anyway for an associate name whose association occurs outside
the construct.  gfc_omp_scalar_p then looks through the pointer, so the
implicit data-mapping rules make the value firstprivate on a target
construct, as for any scalar.

Changes since v1.  The associate names of SELECT TYPE and SELECT RANK
(RANK (0)) still faulted on the device, because they are artificial
decls, which gfc_omp_privatize_by_reference rejected and
gfc_omp_predetermined_mapping mapped 'to' as pointers.  v1 also ICEd in
lower_omp_target for a map of a component through an associate name,
such as map(tofrom: mc%block) or an OpenACC copy clause.  The fixes for
both touch omp-low.cc, so v2 needs a middle-end review as well.

Read the trans-openmp.cc hunks in order: gfc_omp_privatize_by_reference
accepts associate names, also artificial ones,
gfc_omp_predetermined_sharing returns shared for all of them,
gfc_omp_predetermined_mapping no longer maps them 'to', and
gfc_omp_scalar_p looks through the pointer.  The trans-decl.cc hunk
makes SELECT TYPE temporaries DECL_BY_REFERENCE like other associate
names.  In lower_omp_target, the first hunk asks
omp_privatize_by_reference about the variable of an OpenACC
firstprivate clause rather than its copy, which has lost the Fortran
flags, and the second treats a pointer privatized by reference that
does not point to an array as the base of a component map, not as a
reference.  The rest is the new tests.

libgomp.oacc-fortran/associate3.f90 uses firstprivate of an
explicit-shape dummy and so needs the PR129003 patch.

Regression tested on x86_64, and libgomp also with amdgcn offloading.

OK for mainline?

Regards,

Jerry

---

fortran: [PR129002] Treat a scalar associate name as its selector

A scalar ASSOCIATE name of a variable is a pointer to its selector, and
gfc_omp_predetermined_sharing made that pointer firstprivate.  A target
construct therefore copied the host address of the selector to the
device, which faulted when dereferencing it.  Treat the associate name
as a reference to its selector instead: privatized by reference,
predetermined shared, as OpenMP 5.2 (5.1.1) makes an associate name
whose association occurs outside the construct, and scalar for the
implicit data-mapping rules, so that a target construct makes its value
firstprivate.

The associate names of SELECT TYPE and SELECT RANK are artificial decls,
which gfc_omp_privatize_by_reference rejected and
gfc_omp_predetermined_mapping mapped 'to' as pointers.  Treat them like
other associate names, which also makes them DECL_BY_REFERENCE.  In
lower_omp_target, a pointer privatized by reference that does not point
to an array is the base of a component map, not a reference, and the
receiver of an OpenACC firstprivate copy asks about the variable itself,
since its copy has lost the Fortran flags.

Assisted-by: Claude Opus 5.5

        PR fortran/129002

gcc/ChangeLog:

        * omp-low.cc (lower_omp_target): For an OpenACC firstprivate
        variable, ask omp_privatize_by_reference about the variable rather
        than its copy.  Do not treat a pointer privatized by reference that
        does not point to an array as a reference.

gcc/fortran/ChangeLog:

        * trans-decl.cc (gfc_get_symbol_decl): Make SELECT TYPE temporaries
        DECL_BY_REFERENCE too.
        * trans-openmp.cc (gfc_omp_privatize_by_reference): Privatize
        associate names by reference, also artificial ones.
        (gfc_omp_predetermined_sharing): Make associate names shared.
        (gfc_omp_predetermined_mapping): Do not map artificial associate
        names 'to'.
        (gfc_omp_scalar_p): Look through the pointer of an associate name.

gcc/testsuite/ChangeLog:

        * gfortran.dg/gomp/associate3.f90: New test.

libgomp/ChangeLog:

        * testsuite/libgomp.fortran/associate5.f90: New test.
        * testsuite/libgomp.fortran/associate6.f90: New test.
        * testsuite/libgomp.oacc-fortran/associate3.f90: New test.
        * testsuite/libgomp.oacc-fortran/associate4.f90: New test.

Reply via email to