https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111666

            Bug ID: 111666
           Summary: [OpenMP] Fortran FE creates too many temporaries for
                    clauses / 'omp target'+'omp team' #teams/#threads
                    optimize_target_teams optimization fails
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: missed-optimization, openmp
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: burnus at gcc dot gnu.org
                CC: andrewjenner at gcc dot gnu.org, jakub at gcc dot gnu.org
  Target Milestone: ---

I wonder whether trans-openmp.cc in general converts too much into temporaries.

Namely the following function. the 'gfc_evaluate_now' returns a constant as a
constant but otherwise creates a temporary:

static inline tree
gfc_convert_expr_to_tree (stmtblock_t *block, gfc_expr *expr)
{
  gfc_se se;
  tree result;

  gfc_init_se (&se, NULL );
  gfc_conv_expr (&se, expr);
  gfc_add_block_to_block (block, &se.pre);
  result = gfc_evaluate_now (se.expr, block);
  gfc_add_block_to_block (block, &se.post);

  return result;
}

* * *

This bites us for something similar to libgomp.fortran/examples-4/teams-2.f90
but modified:

function dotprod (..., num_teams, block_threads)

  integer, VALUE :: num_teams, block_threads

  !$omp target map(to: B, C, block_size) ...
    !$omp teams num_teams(num_teams) thread_limit(block_threads) ...


The problem is that the gfc_evaluate_now produces code like:

          D.4323 = num_teams;
          D.4324 = block_threads;
          #pragma omp teams ... num_teams(D.4323) thread_limit(D.4324)

And this is then too complex for gcc/gimplify.cc's optimize_target_teams
which no longer recognizes that num_teams and block_threads are (implicitly)
firstprivatize.

* * *

A similar issue shows up above without the VALUE attribute; this matches the
OpenMP Example document teams-2.f90 example - except that the values are passed
by reference.

(That's != libgomp.fortran/examples-4/teams-2.f90; the latter has 'map(tofrom:'
which prevents the optimize_target_teams optimization.)


In order to make that work, we would need to preserve 'num_teams(*num_teams)'
in the evaluation – and handle indirect-ref+DECL_P in optimize_target_teams.

Reply via email to