public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tobias Burnus <tburnus@baylibre.com>
To: Jakub Jelinek <jakub@redhat.com>, gcc-patches <gcc-patches@gcc.gnu.org>
Subject: [RFA/RFC] C++/OpenMP: Supporting (first)private for member variables [PR110347] - or VALUE_EXPR and gimplify
Date: Fri, 16 Feb 2024 16:40:13 +0100	[thread overview]
Message-ID: <4d72cb5b-a0d5-4a38-adf1-754a069c4a2c@baylibre.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2669 bytes --]

The following works with PARALLEL but not with TARGET.

OpenMP states the following is supposed to work:

    A = 5;  // == this->A
    B = 6;  // == this->B
    C[44] = 7; // == this->C; assume 'int C[100]'

    #pragma <parallel|target> firstprivate(A,C) private(B)
    {
      A += 5;  // Now: A is 10.
      B = 7;
      C[44] += 7; // Now C is 14
      // It is unspecified what value this->{A,B,C} has
    }
    // {A,B,C[44]} == this->{A,B,C[44]} are still {5,6,7}

* * *

In the C++ FE, that's handled by creating a temporary variable:
       v = create_temporary_var (TREE_TYPE (m));
with
       SET_DECL_VALUE_EXPR (v, m);
       DECL_OMP_PRIVATIZED_MEMBER(v)
where 'm' is, e.g., 'this->A' - and a bunch of
   'if (DECL_OMP_PRIVATIZED_MEMBER(decl))'
in the g++ FE, only.

For PARALLEL, the VALUE_EXPR survives until omp-low.cc, which handles 
this for (first)privatizing.

But for TARGET, in gimplify.cc, after the following call in 
gimplify_omp_workshare

16813  gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);

the 'A' in the body will be turned into 'this->A'.

* * *

Thus, while there is after omplower the expected

   #pragma omp target ... firstprivate(A)

and also

    D.3081 = .omp_data_i->A; A= ...;

what actually gets used is

    D.3084 = .omp_data_i->D.3046;
    this = D.3084;
    D.2996 = this->A;

which unsurprisingly breaks.

* * *

This can be "fixed" by using the following patch.

With that patch, the -fdump-tree-omplower looks fine. But it does then 
fail with:

  during RTL pass: expand
  g2.cpp:11:7: internal compiler error: in make_decl_rtl, at varasm.cc:1443

for the 'A' with 'B = A' (where B is a non-member var) and 'A' is still 
as the value expr 'this->A'.

--- a/gcc/gimplify.cc
+++ b/gcc/gimplify.cc
@@ -3285,12 +3285,15 @@ gimplify_var_or_parm_decl (tree *expr_p)
    if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, 
decl, true))
      return GS_ALL_DONE;

+ if (!flag_openmp) // Assume: C++'s DECL_OMP_PRIVATIZED_MEMBER (decl)
+ {
    /* If the decl is an alias for another expression, substitute it. */
    if (DECL_HAS_VALUE_EXPR_P (decl))
      {
        *expr_p = unshare_expr (DECL_VALUE_EXPR (decl));
        return GS_OK;
      }
+ }

    return GS_ALL_DONE;
  }


* * *

Any idea / suggestion how to handle this best?

One way I see would be to add a lang-hook here to check for 
DECL_OMP_PRIVATIZED_MEMBER, similar to the hack above. And
then ensure that the DECL_VALUE_EXPR points to the var decl
in the target region (i.e. some hacking in omp-low.cc).

I have no idea whether that would - nor whether that would be
the way forward. - Thoughts?

Tobias

[-- Attachment #2: testcase.cpp --]
[-- Type: text/x-c++src, Size: 1559 bytes --]

#if TEMPL
template <typename T>
#else
#define T int
#endif
#if PRIVATE
#define firstprivate private
#endif
struct t {
  T A;
void f()
{
  T B = 49;
  A = 7;
  #pragma omp parallel firstprivate(A) if(0) shared(B) default(none)
  {
    if (A != 7) __builtin_printf("ERROR 1b: %d (!= 7) inside omp parallel\n", A);
    A = 5;
    B = A;
  }
  if (A != 7) __builtin_printf("ERROR 1: %d (!= 7) omp parallel\n", A);
  if (B != 5) __builtin_printf("ERROR 1a: %d\n", B);
  A = 8; B = 49;
  #pragma omp parallel firstprivate(A)if(0) shared(B) default(none)
  {
    if (A != 8) __builtin_printf("ERROR 1b: %d (!= 8) inside omp parallel\n", A);
    A = 6;
    B = A;
  }
  if (A != 8) __builtin_printf("ERROR 2: %d (!= 8) omp parallel\n", A);
  if (B != 6) __builtin_printf("ERROR 2a: %d\n", B);
  A = 8; B = 49;
  #pragma omp target firstprivate(A) map(from:B) defaultmap(none)
  {
    if (A != 7) __builtin_printf("ERROR 2b: %d (!= 7) inside omp target\n", A);
    A = 7;
    B = A;
  }
  if (A != 8) __builtin_printf("ERROR 3: %d (!= 8) omp target\n", A);
  if (B != 7) __builtin_printf("ERROR 3a: %d\n", B);
  A = 9; B = 49;
  #pragma omp target firstprivate(A) map(from:B) defaultmap(none)
  {
    if (A != 7) __builtin_printf("ERROR 3b: %d (!= 7) inside omp target\n", A);
    A = 8;
    B = A;
  }
  if (A != 9) __builtin_printf("ERROR 4: %d (!= 9) omp target\n", A); else __builtin_printf("OK\n");
  if (B != 8) __builtin_printf("ERROR 4a: %d\n", B);
}
};

void bar() {
#if TEMPL
  struct t<int> x;
#else
  struct t x;
#endif
  x.f();
}

int main()
{
  bar();
}

             reply	other threads:[~2024-02-16 15:40 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-16 15:40 Tobias Burnus [this message]
  -- strict thread matches above, loose matches on Subject: below --
2024-02-16 15:15 Tobias Burnus
2024-02-16 15:42 ` Jakub Jelinek
2024-02-16 15:57   ` Jakub Jelinek

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=4d72cb5b-a0d5-4a38-adf1-754a069c4a2c@baylibre.com \
    --to=tburnus@baylibre.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).