public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [RFA/RFC] C++/OpenMP: Supporting (first)private for member variables [PR110347] - or VALUE_EXPR and gimplify
@ 2024-02-16 15:40 Tobias Burnus
  0 siblings, 0 replies; 4+ messages in thread
From: Tobias Burnus @ 2024-02-16 15:40 UTC (permalink / raw)
  To: Jakub Jelinek, gcc-patches

[-- 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();
}

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFA/RFC] C++/OpenMP: Supporting (first)private for member variables [PR110347] - or VALUE_EXPR and gimplify
  2024-02-16 15:42 ` Jakub Jelinek
@ 2024-02-16 15:57   ` Jakub Jelinek
  0 siblings, 0 replies; 4+ messages in thread
From: Jakub Jelinek @ 2024-02-16 15:57 UTC (permalink / raw)
  To: Tobias Burnus, gcc-patches

Hi!

Ah, and the reason why it doesn't work on target is that it has the
everything is mapped assumption:
  if ((ctx->region_type & ORT_TARGET) != 0)
    {
      if (ctx->region_type & ORT_ACC)
        /* For OpenACC, as remarked above, defer expansion.  */
        shared = false;
      else
        shared = true;
         
      ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);

Perhaps shared = true; should be shared = (flags & GOVD_MAPPED) != 0;
now that we have private/firstprivate clauses on target?

	Jakub


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFA/RFC] C++/OpenMP: Supporting (first)private for member variables [PR110347] - or VALUE_EXPR and gimplify
  2024-02-16 15:15 Tobias Burnus
@ 2024-02-16 15:42 ` Jakub Jelinek
  2024-02-16 15:57   ` Jakub Jelinek
  0 siblings, 1 reply; 4+ messages in thread
From: Jakub Jelinek @ 2024-02-16 15:42 UTC (permalink / raw)
  To: Tobias Burnus; +Cc: gcc-patches

On Fri, Feb 16, 2024 at 04:15:05PM +0100, Tobias Burnus wrote:
> I have no idea whether that would - nor whether that would be
> the way forward. - Thoughts?

Don't have time to dive through this now in detail, just want to point out
why we ignore DECL_VALUE_EXPR on the magic var during gimplification for
the parallel case - gimplify_var_or_parm_decl has
  /* When within an OMP context, notice uses of variables.  */
  if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
    return GS_ALL_DONE;

  /* If the decl is an alias for another expression, substitute it now.  */
  if (DECL_HAS_VALUE_EXPR_P (decl))
    {
      *expr_p = unshare_expr (DECL_VALUE_EXPR (decl));
      return GS_OK;
    }

  return GS_ALL_DONE;
and the trick is to make sure omp_notice_variable returns true if it is
undesirable to expand the DECL_VALUE_EXPR of decl at that point.
And whether omp_notice_variable returns true or false depends on
lang_hooks.decls.omp_disregard_value_expr langhook.
And that one has
  if (VAR_P (decl)
      && DECL_HAS_VALUE_EXPR_P (decl)
      && DECL_ARTIFICIAL (decl)
      && DECL_LANG_SPECIFIC (decl)
      && DECL_OMP_PRIVATIZED_MEMBER (decl))
    return true;
to deal with this.

	Jakub


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [RFA/RFC] C++/OpenMP: Supporting (first)private for member variables [PR110347] - or VALUE_EXPR and gimplify
@ 2024-02-16 15:15 Tobias Burnus
  2024-02-16 15:42 ` Jakub Jelinek
  0 siblings, 1 reply; 4+ messages in thread
From: Tobias Burnus @ 2024-02-16 15:15 UTC (permalink / raw)
  To: Jakub Jelinek, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 2691 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 handledby 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 theg++ 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); 
will turn the 'A' in the body 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 now. */ 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();
}

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-02-16 15:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-16 15:40 [RFA/RFC] C++/OpenMP: Supporting (first)private for member variables [PR110347] - or VALUE_EXPR and gimplify Tobias Burnus
  -- 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

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).