public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tobias Burnus <tobias@codesourcery.com>
To: Jakub Jelinek <jakub@redhat.com>
Cc: gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: [Patch][v5] OpenMP: Move omp requires checks to libgomp
Date: Fri, 1 Jul 2022 18:31:48 +0200	[thread overview]
Message-ID: <ab77de9f-29fa-5ec2-2815-2a51dde2d6a2@codesourcery.com> (raw)
In-Reply-To: <Yr8F36AD8gtcGyMl@tucnak>

On 01.07.22 16:34, Jakub Jelinek wrote:
> On Fri, Jul 01, 2022 at 03:06:05PM +0200, Tobias Burnus wrote:
> [...]
> Will Fortran diagnose:
> subroutine foo
> !$omp requires unified_shared_memory
> !$omp target
> !$omp end target
> end subroutine foo
> subroutine bar
> !$omp requires reverse_offload
> !$omp target
> !$omp end target
> end subroutine bar
>
> or just merge it from the different namespaces?

This is done in openmp.cc during parsing. The merging you quoted (in parse.cc) happens
after the whole input file has been parsed and resolved. For your test case, the
following error is shown:

test.f90:1:15:

     1 |  subroutine foo
       |               1
Error: Program unit at (1) has OpenMP device constructs/routines but does not set !$OMP REQUIRES REVERSE_OFFLOAD but other program units do
test.f90:6:14:

     6 | subroutine bar
       |              1
Error: Program unit at (1) has OpenMP device constructs/routines but does not set !$OMP REQUIRES UNIFIED_SHARED_MEMORY but other program units do


> @@ -1764,6 +1781,20 @@ input_symtab (void)
>>       }
>>   }
>>
>> +static void
>> +omp_requires_to_name (char *buf, size_t size, unsigned int requires_mask)
>> +{
>> +  char *end = buf + size, *p = buf;
>> +  if (requires_mask & GOMP_REQUIRES_UNIFIED_ADDRESS)
>> +    p += snprintf (p, end - p, "unified_address");
>> +  if (requires_mask & GOMP_REQUIRES_UNIFIED_SHARED_MEMORY)
>> +    p += snprintf (p, end - p, "%sunified_shared_memory",
>> +               (p == buf ? "" : ", "));
>> +  if (requires_mask & GOMP_REQUIRES_REVERSE_OFFLOAD)
>> +    p += snprintf (p, end - p, "%sreverse_offload",
>> +               (p == buf ? "" : ", "));
> So, what does this print if requires_mask is 0 (or just the target used bit
> set but not unified_address, unified_shared_memory nor reverse_offload)?

Well, that's what libgomp/testsuite/libgomp.c-c++-common/requires-2.c (+ *-2-aux.c)
tests:

/* { dg-error "OpenMP 'requires' directive with non-identical clauses in multiple compilation units: 'unified_shared_memory' vs. ''" "" { target *-*-* } 0 }  */

I hope the '' vs. 'unified_shared_memory' is clear - but if you have a better wording.

Note that both:
   no 'omp requires'
and
   'omp requires' with other clauses (such as the atomic ones or dynamic_allocators)
will lead to 0. Thus, if the wording is changed, it should fit for both cases.

>> @@ -1810,6 +1847,54 @@ input_offload_tables (bool do_force_output)
>>               may be no refs to var_decl in offload LTO mode.  */
>>            if (do_force_output)
>>              varpool_node::get (var_decl)->force_output = 1;
>> +          tmp_decl = var_decl;
>> +        }
>> +      else if (tag == LTO_symtab_edge)
>> +        {
>> +          static bool error_emitted = false;
>> +          HOST_WIDE_INT val = streamer_read_hwi (ib);
>> +
>> +          if (omp_requires_mask == 0)
>> +            {
>> +              omp_requires_mask = (omp_requires) val;
>> +              requires_decl = tmp_decl;
>> +              requires_fn = file_data->file_name;
> And similarly here, if some device construct is seen but requires
> directive isn't, not sure if in this version val would be 0 or something
> with the TARGET_USED bit set.  In the latter case, only what is printed
> for no requires or just atomic related requires is a problem, in the former
> case due to the == 0 check mixing of 0 with non-zero would be ignored
> but mixing of non-zero with 0 wouldn't be.

Here: 0 = "unset" in the sense that either TARGET_USE nor USM/UA/RO was
specified. If any of those is set, we get != 0.

For mkoffload, the single results are merged - and TARGET_USE is stripped,
such that it is either 0 or a combination of USM/UA/RO

>> +            }
>> +          else if (omp_requires_mask != val && !error_emitted)
>> +            {
>> +              char buf[64], buf2[64];
> Perhaps cleaner would be to size the buffers as
> sizeof ("unified_address,unified_shared_memory,reverse_offload")
> 64 is more, but just a wild guess and if further clauses are added later,
> it might be too small.

I concur – except that ',' should be ', '.
(Likewise in libgomp/target.c)

> @@ -1821,6 +1906,18 @@ input_offload_tables (bool do_force_output)
>>         lto_destroy_simple_input_block (file_data, LTO_section_offload_table,
>>                                    ib, data, len);
>>       }
>> +#ifdef ACCEL_COMPILER
>> +  char *omp_requires_file = getenv ("GCC_OFFLOAD_OMP_REQUIRES_FILE");
>> +  if (omp_requires_file == NULL || omp_requires_file[0] == '\0')
>> +    fatal_error (input_location, "GCC_OFFLOAD_OMP_REQUIRES_FILE unset");
>> +  FILE *f = fopen (omp_requires_file, "wb");
>> +  if (!f)
>> +    fatal_error (input_location, "Cannot open omp_requires file %qs",
>> +             omp_requires_file);
>> +  uint32_t req_mask = omp_requires_mask & ~OMP_REQUIRES_TARGET_USED;
> Perhaps it is better to also store the TARGET_USED bit and on the library
> side completely ignore values of 0.

For the compiler side, we need to distinguish no requires vs. some
requires when checking multiple TU (to distinguish it from TU which do
not use target constructs).

But for libgomp only the result counts: no requires or some requires.
Thus, passing 0 if there are no USM/UA/RO should be fine – and the code
does so. This 0 is then passed on to the plugin to check against it.

If we pass target_used to libgomp, we need to filter it out at some point.

>> --- a/gcc/omp-low.cc
>> +++ b/gcc/omp-low.cc
>> @@ -12701,6 +12701,11 @@ lower_omp_target (gimple_stmt_iterator *gsi_p, omp_context *ctx)
>>         gcc_unreachable ();
>>       }
>>
>> +  /* Ensure that requires map is written via output_offload_tables, even if only
>> +     'target (enter/exit) data' is used in the translation unit.  */
>> +  if (ENABLE_OFFLOADING && (omp_requires_mask & OMP_REQUIRES_TARGET_USED))
>> +    g->have_offload = true;
> Is
> c.c:
> #pragma omp requires unified_shared_memory
> d.c:
> void baz (void) {
>    #pragma omp target
>    ;
> }
> ok?

This one is *already* streamed out as it creates a symbol and entry in
in offload_functions (baz.omp_fn.0).

The code is rather for '#pragma omp target enter data map(x)' as this
only adds a library call and no symbol.

> Pedantically reading current standard probably yes, but perhaps again
> something to be discussed.  The question is what the requires directive
> in that case would do, nothing at all as there are no device constructs
> etc.?

Isn't there a device construct – which happens to be empty?

With 'omp target map(always, to: x)' it would be even observable that
the code is run.

> In that case omp_requires_mask & OMP_REQUIRES_TARGET_USED is right.
> But if it should influence the behavior anyway, the restriction should be
> Either all compilation units of a program that contain ... device
> constructs ... should include also requires directive with one of the
> unified_shared_memory, unified_address or reverse_offload clauses.
> In that case the test would be
> omp_requires_mask & (OMP_REQUIRES_TARGET_USED | OMP_REQUIRES_UNIFIED* | OMP_REQUIRES_REV*)

I think I am lost – don't we effectively test this? We filter out
everything else in output_offload_tables. Thus, in input_offload_tables,
a single '==' will do. (We additionally know that TARGET_USED is set -
as otherwise there wouldn't be a symbol in the offload table.)

Thus, it is unclear to me what you propose here.

>> +static void
>> +gomp_requires_to_name (char *buf, size_t size, int requires_mask)
>> +{
>> +  char *end = buf + size, *p = buf;
>> +  if (requires_mask & GOMP_REQUIRES_UNIFIED_ADDRESS)
>> +    p += snprintf (p, end - p, "unified_address");
>> +  if (requires_mask & GOMP_REQUIRES_UNIFIED_SHARED_MEMORY)
>> +    p += snprintf (p, end - p, "%sunified_shared_memory",
>> +               (p == buf ? "" : ", "));
>> +  if (requires_mask & GOMP_REQUIRES_REVERSE_OFFLOAD)
>> +    p += snprintf (p, end - p, "%sreverse_offload",
>> +               (p == buf ? "" : ", "));
>> +}
> Same question as earlier.

Same answer, except that in libgomp, this code is effectively only
reachable when omp_requires_mask != 0 as it reaches this code only if
either some additional flag was added (in register_ver) or when devices
were available, but those do not support a flag.

We just have to remember to update this, if we ever add additional flags.

>>   /* This function should be called from every offload image while loading.
>>      It gets the descriptor of the host func and var tables HOST_TABLE, TYPE of
>>      the target, and TARGET_DATA needed by target plugin.  */
>> @@ -2323,11 +2341,29 @@ GOMP_offload_register_ver (unsigned version, const void *host_table,
>>                         int target_type, const void *target_data)
>>   {
>>     int i;
>> +  int omp_req = omp_requires_mask;
>>
>>     if (GOMP_VERSION_LIB (version) > GOMP_VERSION)
>>       gomp_fatal ("Library too old for offload (version %u < %u)",
>>              GOMP_VERSION, GOMP_VERSION_LIB (version));
>> -
>> +
>> +  if (GOMP_VERSION_LIB (version) > 1)
>> +    {
>> +      omp_req = (int) (size_t) ((void **) target_data)[0];
>> +      target_data = &((void **) target_data)[1];
>> +      if (num_devices && (omp_req & ~omp_requires_mask))
>> +    {
>> +      char buf[64];
>> +      gomp_requires_to_name (buf, sizeof (buf),
>> +                             omp_req & ~omp_requires_mask);
>> +      gomp_error ("devices already initialized when registering additional "
>> +                  "offload images that use the additional OpenMP 'requires'"
>> +                  " directive clauses %s. Therefore, the program might not "
>> +                  "run correctly", buf);
>> +    }
>> +      omp_requires_mask |= omp_req;
>> +    }
> Both omp_requires_mask and num_devices are global vars that would be
> modified concurrently in some other thread, so the above is racy.
>
> What I'd do is int omp_req = 0; early, just the omp_req + target_data in
> if (GOMP_VERSION_LIB (version) > 1) otherwise.  That computes
> the local omp_req only.
>
>> +
>>     gomp_mutex_lock (&register_lock);
> Then under the lock, you can do the merging.
> But, IMHO the runtime library should repeat what is done in the offloading
> lto1, diagnose if there are differences between the masks in between
> different TUs, here at runtime on the program/shared library level.
> And IMHO the error you emit above is unnecessary, because (at least
> hopefully) the num_devices computation / device initialization should
> only happen on behalf of some device construct or device related OpenMP API
> routine, so at that point the shared library or program that does that
> should have its own mask and if something is dlopened later, it should
> either have compatible mask (nothing is diagnosed) or incompatible, but then
> it should be diagnosed like any other incompatibilities.

OK – I will diagnose it always.

Question: If it is not the same, should there just be a message to
stderr (gomp_error) or should libgomp abort (gomp_fatal)?

Downside is that I cannot really provide much data where it fails. But
on the other hand, it will probably only rarely occur.

> I thought I've mentioned earlier it would be nice to rename the
> get_num_devices plugin hook because its API has changed, so that
> if one mixes old plugin with new libgomp or vice versa it doesn't
> break silently.

As discussed off list, gomp_load_plugin_for_device calls     if
(device->version_func () != GOMP_VERSION) and we did bump the GOMP_VERSION.

Tobias

-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955

  reply	other threads:[~2022-07-01 16:31 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-13 15:07 [PATCH, OpenMP 5.0] More implementation of the requires directive Chung-Lin Tang
2021-01-13 15:27 ` Jakub Jelinek
2021-03-25 11:18 ` Thomas Schwinge
2022-03-29 13:42 ` Andrew Stubbs
2022-06-08  3:56 ` [Patch] OpenMP: Move omp requires checks to libgomp Tobias Burnus
2022-06-09 11:40   ` Jakub Jelinek
2022-06-09 12:46     ` Tobias Burnus
2022-06-09 14:19       ` Jakub Jelinek
2022-06-29 14:33         ` [Patch][v4] " Tobias Burnus
2022-06-29 17:02           ` Jakub Jelinek
2022-06-29 18:10             ` Tobias Burnus
2022-06-29 20:18               ` Jakub Jelinek
2022-07-01 13:06                 ` [Patch][v5] " Tobias Burnus
2022-07-01 14:34                   ` Jakub Jelinek
2022-07-01 16:31                     ` Tobias Burnus [this message]
2022-07-01 16:55                       ` Jakub Jelinek
2022-07-01 21:08                         ` Tobias Burnus
2022-07-04  8:31                           ` Jakub Jelinek
2022-07-07 13:26                           ` Fix one issue in OpenMP 'requires' directive diagnostics (was: [Patch][v5] OpenMP: Move omp requires checks to libgomp) Thomas Schwinge
2022-07-07 13:56                             ` Tobias Burnus
2022-07-08  6:59                               ` Thomas Schwinge
2022-07-06 10:42                   ` Restore 'GOMP_offload_unregister_ver' functionality " Thomas Schwinge
2022-07-06 13:59                     ` Tobias Burnus
2022-07-06 21:08                       ` Thomas Schwinge
2022-08-17 11:45                       ` Jakub Jelinek
2023-09-15  9:41                   ` [Patch][v5] OpenMP: Move omp requires checks to libgomp Thomas Schwinge
2022-07-07  8:37           ` Adjust 'libgomp.c-c++-common/requires-3.c' (was: [Patch][v4] OpenMP: Move omp requires checks to libgomp) Thomas Schwinge
2022-07-07  9:02             ` Tobias Burnus
2022-07-07  8:42           ` Enhance 'libgomp.c-c++-common/requires-4.c', 'libgomp.c-c++-common/requires-5.c' testing " Thomas Schwinge
2022-07-07  9:36             ` Tobias Burnus
2022-07-07 10:42               ` Thomas Schwinge
2022-07-06 10:30   ` Define 'OMP_REQUIRES_[...]', 'GOMP_REQUIRES_[...]' in a single place (was: [Patch] " Thomas Schwinge
2022-07-06 13:40     ` Tobias Burnus
2022-07-06 11:04   ` Fix Intel MIC 'mkoffload' for OpenMP 'requires' " Thomas Schwinge
2022-07-06 11:29     ` Tobias Burnus
2022-07-06 12:38       ` Thomas Schwinge
2022-07-06 13:30         ` Tobias Burnus
2022-07-07 10:46           ` Thomas Schwinge
2022-07-06 14:19     ` Tobias Burnus
2024-03-07 12:38   ` nvptx: 'cuDeviceGetCount' failure is fatal " Thomas Schwinge
2024-03-07 14:28     ` nvptx: 'cuDeviceGetCount' failure is fatal Tobias Burnus
2024-03-08 15:58       ` Thomas Schwinge

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ab77de9f-29fa-5ec2-2815-2a51dde2d6a2@codesourcery.com \
    --to=tobias@codesourcery.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).