public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Frederik Harwath <frederik@codesourcery.com>
To: <gcc-patches@gcc.gnu.org>
Cc: <thomas@codesourcery.com>, <rguenther@suse.de>
Subject: [PATCH 27/40] openacc: Handle internal function calls in pass_lim
Date: Wed, 15 Dec 2021 16:54:34 +0100	[thread overview]
Message-ID: <20211215155447.19379-28-frederik@codesourcery.com> (raw)
In-Reply-To: <20211215155447.19379-1-frederik@codesourcery.com>

The loop invariant motion pass correctly refuses to move statements
out of a loop if any other statement in the loop is unanalyzable.  The
pass does not know how to handle the OpenACC internal function calls
which was not necessary until recently when the OpenACC device
lowering pass was moved to a later position in the pass pipeline.

This commit changes pass_lim to ignore the OpenACC internal function
calls which do not contain any memory references. The hoisting enabled
by this change can be useful for the data-dependence analysis in
Graphite; for instance, in the outlined functions for OpenACC regions,
all invariant accesses to the ".omp_data_i" struct should be hoisted
out of the OpenACC loop.  This is particularly important for variables
that were scalars in the original loop and which have been turned into
accesses to the struct by the outlining process.  Not hoisting those
can prevent scalar evolution analysis which is crucial for Graphite.
Since any hoisting that introduces intermediate names - and hence,
"fake" dependences - inside the analyzed nest can be harmful to
data-dependence analysis, a flag to restrict the hoisting in OpenACC
functions is added to the pass. The pass instance that executes before
Graphite now runs with this flag set to true and the pass instance
after Graphite runs unrestricted.

A more precise way of selecting the statements for which hoisting
should be enabled is left for a future improvement.

gcc/ChangeLog:
        * passes.def: Set restrict_oacc_hoisting to true for the early
        pass_lim instance.
        * tree-ssa-loop-im.c (movement_possibility): Add
        restrict_oacc_hoisting flag to function; restrict movement if set.
        (compute_invariantness): Add restrict_oacc_hoisting flag and pass it on.
        (gather_mem_refs_stmt): Skip IFN_GOACC_LOOP and IFN_UNIQUE
        calls.
        (loop_invariant_motion_in_fun): Add restrict_oacc_hoisting flag and
        pass it on.
        (pass_lim::execute): Pass on new flags.
        * tree-ssa-loop-manip.h (loop_invariant_motion_in_fun): Adjust
        declaration.
        * gimple-loop-interchange.cc (pass_linterchange::execute): Adjust call to
        loop_invariant_motion_in_fun.
---
 gcc/gimple-loop-interchange.cc |  2 +-
 gcc/passes.def                 |  2 +-
 gcc/tree-ssa-loop-im.c         | 57 ++++++++++++++++++++++++++++------
 gcc/tree-ssa-loop-manip.h      |  2 +-
 4 files changed, 51 insertions(+), 12 deletions(-)

diff --git a/gcc/gimple-loop-interchange.cc b/gcc/gimple-loop-interchange.cc
index ccd5083145f8..7c9b7b2345fa 100644
--- a/gcc/gimple-loop-interchange.cc
+++ b/gcc/gimple-loop-interchange.cc
@@ -2107,7 +2107,7 @@ pass_linterchange::execute (function *fun)
   if (changed_p)
     {
       unsigned todo = TODO_update_ssa_only_virtuals;
-      todo |= loop_invariant_motion_in_fun (cfun, false);
+      todo |= loop_invariant_motion_in_fun (cfun, false, false);
       scev_reset ();
       return todo;
     }
diff --git a/gcc/passes.def b/gcc/passes.def
index 681392f8f79f..1da9382bac53 100644
--- a/gcc/passes.def
+++ b/gcc/passes.def
@@ -250,7 +250,7 @@ along with GCC; see the file COPYING3.  If not see
       NEXT_PASS (pass_cse_sincos);
       NEXT_PASS (pass_optimize_bswap);
       NEXT_PASS (pass_laddress);
-      NEXT_PASS (pass_lim);
+      NEXT_PASS (pass_lim, true /* restrict_oacc_hoisting */);
       NEXT_PASS (pass_walloca, false);
       NEXT_PASS (pass_pre);
       NEXT_PASS (pass_sink_code);
diff --git a/gcc/tree-ssa-loop-im.c b/gcc/tree-ssa-loop-im.c
index 4b187c2cdafe..466dc494fb52 100644
--- a/gcc/tree-ssa-loop-im.c
+++ b/gcc/tree-ssa-loop-im.c
@@ -47,6 +47,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "builtins.h"
 #include "tree-dfa.h"
 #include "dbgcnt.h"
+#include "graphite-oacc.h"
+#include "internal-fn.h"

 /* TODO:  Support for predicated code motion.  I.e.

@@ -327,11 +329,23 @@ enum move_pos
    Otherwise return MOVE_IMPOSSIBLE.  */

 enum move_pos
-movement_possibility (gimple *stmt)
+movement_possibility (gimple *stmt, bool restrict_oacc_hoisting)
 {
   tree lhs;
   enum move_pos ret = MOVE_POSSIBLE;

+  if (restrict_oacc_hoisting && oacc_get_fn_attrib (cfun->decl)
+      && gimple_code (stmt) == GIMPLE_ASSIGN)
+    {
+      tree rhs = gimple_assign_rhs1 (stmt);
+
+      if (TREE_CODE (rhs) == VIEW_CONVERT_EXPR)
+       rhs = TREE_OPERAND (rhs, 0);
+
+      if (TREE_CODE (rhs) == ARRAY_REF)
+         return MOVE_IMPOSSIBLE;
+    }
+
   if (flag_unswitch_loops
       && gimple_code (stmt) == GIMPLE_COND)
     {
@@ -981,7 +995,7 @@ rewrite_bittest (gimple_stmt_iterator *bsi)
    statements.  */

 static void
-compute_invariantness (basic_block bb)
+compute_invariantness (basic_block bb, bool restrict_oacc_hoisting)
 {
   enum move_pos pos;
   gimple_stmt_iterator bsi;
@@ -1009,7 +1023,7 @@ compute_invariantness (basic_block bb)
       {
        stmt = gsi_stmt (bsi);

-       pos = movement_possibility (stmt);
+       pos = movement_possibility (stmt, restrict_oacc_hoisting);
        if (pos == MOVE_IMPOSSIBLE)
          continue;

@@ -1040,7 +1054,7 @@ compute_invariantness (basic_block bb)
     {
       stmt = gsi_stmt (bsi);

-      pos = movement_possibility (stmt);
+      pos = movement_possibility (stmt, restrict_oacc_hoisting);
       if (pos == MOVE_IMPOSSIBLE)
        {
          if (nonpure_call_p (stmt))
@@ -1465,6 +1479,13 @@ gather_mem_refs_stmt (class loop *loop, gimple *stmt)
   if (!gimple_vuse (stmt))
     return;

+  /* The expansion of those OpenACC internal function calls which occurs in a
+   * later pass does not introduce any memory references. Hence it is safe to
+   * ignore them. */
+  if (gimple_call_internal_p (stmt, IFN_GOACC_LOOP)
+      || gimple_call_internal_p (stmt, IFN_UNIQUE))
+    return;
+
   mem = simple_mem_ref_in_stmt (stmt, &is_stored);
   if (!mem && is_gimple_assign (stmt))
     {
@@ -1506,7 +1527,7 @@ gather_mem_refs_stmt (class loop *loop, gimple *stmt)
       ao_ref_alias_set (&aor);
       HOST_WIDE_INT offset, size, max_size;
       poly_int64 saved_maxsize = aor.max_size, mem_off;
-      tree mem_base;
+      tree mem_base = NULL;
       bool ref_decomposed;
       if (aor.max_size_known_p ()
          && aor.offset.is_constant (&offset)
@@ -3244,7 +3265,8 @@ tree_ssa_lim_finalize (void)
    Only perform store motion if STORE_MOTION is true.  */

 unsigned int
-loop_invariant_motion_in_fun (function *fun, bool store_motion)
+loop_invariant_motion_in_fun (function *fun, bool store_motion,
+                             bool restrict_oacc_hoisting)
 {
   unsigned int todo = 0;

@@ -3262,7 +3284,7 @@ loop_invariant_motion_in_fun (function *fun, bool store_motion)
   /* For each statement determine the outermost loop in that it is
      invariant and cost for computing the invariant.  */
   for (int i = 0; i < n; ++i)
-    compute_invariantness (BASIC_BLOCK_FOR_FN (fun, rpo[i]));
+    compute_invariantness (BASIC_BLOCK_FOR_FN (fun, rpo[i]), restrict_oacc_hoisting);

   /* Execute store motion.  Force the necessary invariants to be moved
      out of the loops as well.  */
@@ -3309,13 +3331,21 @@ class pass_lim : public gimple_opt_pass
 {
 public:
   pass_lim (gcc::context *ctxt)
-    : gimple_opt_pass (pass_data_lim, ctxt)
+    : gimple_opt_pass (pass_data_lim, ctxt), restrict_oacc_hoisting (false)
   {}

+  void set_pass_param (unsigned int n, bool param)
+    {
+      gcc_assert (n == 0);
+      restrict_oacc_hoisting = param;
+    }
+
   /* opt_pass methods: */
   opt_pass * clone () { return new pass_lim (m_ctxt); }
   virtual bool gate (function *) { return flag_tree_loop_im != 0; }
   virtual unsigned int execute (function *);
+private:
+  bool restrict_oacc_hoisting;

 }; // class pass_lim

@@ -3328,7 +3358,16 @@ pass_lim::execute (function *fun)

   if (number_of_loops (fun) <= 1)
     return 0;
-  unsigned int todo = loop_invariant_motion_in_fun (fun, flag_move_loop_stores);
+
+  bool store_motion = flag_move_loop_stores;
+  /* TODO Enabling store motion in OpenACC kernel functions requires further
+     handling of the OpenACC internal function calls.  It can also be harmful
+     to data-dependence analysis. Keep it disabled for now. */
+  if (oacc_function_p (cfun) && graphite_analyze_oacc_target_region_type_p (cfun))
+    store_motion = false;
+
+  unsigned int todo = loop_invariant_motion_in_fun (fun, store_motion,
+                                                   restrict_oacc_hoisting);

   if (!in_loop_pipeline)
     loop_optimizer_finalize ();
diff --git a/gcc/tree-ssa-loop-manip.h b/gcc/tree-ssa-loop-manip.h
index 4f604e1bd24a..864fb9f1d355 100644
--- a/gcc/tree-ssa-loop-manip.h
+++ b/gcc/tree-ssa-loop-manip.h
@@ -53,7 +53,7 @@ extern void tree_transform_and_unroll_loop (class loop *, unsigned,
                                            transform_callback, void *);
 extern void tree_unroll_loop (class loop *, unsigned, tree_niter_desc *);
 extern tree canonicalize_loop_ivs (class loop *, tree *, bool);
-extern unsigned int loop_invariant_motion_in_fun (function *, bool);
+extern unsigned int loop_invariant_motion_in_fun (function *, bool, bool);


 #endif /* GCC_TREE_SSA_LOOP_MANIP_H */
--
2.33.0

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

  parent reply	other threads:[~2021-12-15 15:56 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-15 15:54 [PATCH 00/40] OpenACC "kernels" Improvements Frederik Harwath
2021-12-15 15:54 ` [PATCH 01/40] Kernels loops annotation: C and C++ Frederik Harwath
2021-12-15 15:54 ` [PATCH 02/40] Add -fno-openacc-kernels-annotate-loops option to more testcases Frederik Harwath
2021-12-15 15:54 ` [PATCH 03/40] Kernels loops annotation: Fortran Frederik Harwath
2021-12-15 15:54 ` [PATCH 04/40] Additional Fortran testsuite fixes for kernels loops annotation pass Frederik Harwath
2021-12-15 15:54 ` [PATCH 05/40] Fix bug in processing of array dimensions in data clauses Frederik Harwath
2021-12-15 15:54 ` [PATCH 06/40] Add a "combined" flag for "acc kernels loop" etc directives Frederik Harwath
2021-12-15 15:54 ` [PATCH 07/40] Annotate inner loops in "acc kernels loop" directives (C/C++) Frederik Harwath
2021-12-15 15:54 ` [PATCH 08/40] Annotate inner loops in "acc kernels loop" directives (Fortran) Frederik Harwath
2021-12-15 15:54 ` [PATCH 09/40] Permit calls to builtins and intrinsics in kernels loops Frederik Harwath
2021-12-15 15:54 ` [PATCH 10/40] Fix patterns in Fortran tests for kernels loop annotation Frederik Harwath
2021-12-15 15:54 ` [PATCH 11/40] Clean up loop variable extraction in OpenACC " Frederik Harwath
2021-12-15 15:54 ` [PATCH 12/40] Relax some restrictions on the loop bound in " Frederik Harwath
2021-12-15 15:54 ` [PATCH 13/40] Fortran: Delinearize array accesses Frederik Harwath
2021-12-15 15:54 ` [PATCH 14/40] openacc: Move pass_oacc_device_lower after pass_graphite Frederik Harwath
2021-12-15 15:54 ` [PATCH 15/40] graphite: Extend SCoP detection dump output Frederik Harwath
2022-05-16 12:49   ` Tobias Burnus
2022-05-17  8:21     ` Richard Biener
2022-05-18 12:19       ` Harwath, Frederik
2022-05-18 12:21         ` Richard Biener
2021-12-15 15:54 ` [PATCH 16/40] graphite: Rename isl_id_for_ssa_name Frederik Harwath
2022-05-16 12:49   ` Tobias Burnus
2022-05-17  8:22     ` Richard Biener
2021-12-15 15:54 ` [PATCH 17/40] graphite: Fix minor mistakes in comments Frederik Harwath
2022-05-16 12:49   ` Tobias Burnus
2022-05-17  8:22     ` Richard Biener
2021-12-15 15:54 ` [PATCH 18/40] Move compute_alias_check_pairs to tree-data-ref.c Frederik Harwath
2021-12-15 15:54 ` [PATCH 19/40] graphite: Add runtime alias checking Frederik Harwath
2021-12-15 15:54 ` [PATCH 20/40] openacc: Use Graphite for dependence analysis in "kernels" regions Frederik Harwath
2021-12-15 15:54 ` [PATCH 21/40] openacc: Add "can_be_parallel" flag info to "graph" dumps Frederik Harwath
2021-12-15 15:54 ` [PATCH 22/40] openacc: Remove unused partitioning in "kernels" regions Frederik Harwath
2021-12-15 15:54 ` [PATCH 23/40] Add function for printing a single OMP_CLAUSE Frederik Harwath
2021-12-15 15:54 ` [PATCH 24/40] openacc: Add data optimization pass Frederik Harwath
2021-12-15 15:54 ` [PATCH 25/40] openacc: Add runtime alias checking for OpenACC kernels Frederik Harwath
2021-12-15 15:54 ` [PATCH 26/40] openacc: Warn about "independent" "kernels" loops with data-dependences Frederik Harwath
2021-12-15 15:54 ` Frederik Harwath [this message]
2021-12-15 15:54 ` [PATCH 28/40] openacc: Disable pass_pre on outlined functions analyzed by Graphite Frederik Harwath
2021-12-15 15:54 ` [PATCH 29/40] graphite: Tune parameters for OpenACC use Frederik Harwath
2021-12-15 15:54 ` [PATCH 30/40] graphite: Adjust scop loop-nest choice Frederik Harwath
2021-12-15 15:54 ` [PATCH 31/40] graphite: Accept loops without data references Frederik Harwath
2021-12-15 15:54 ` [PATCH 32/40] Reference reduction localization Frederik Harwath
2021-12-15 15:54 ` [PATCH 33/40] Fix tree check failure with " Frederik Harwath
2021-12-15 15:54 ` [PATCH 34/40] Use more appropriate var in localize_reductions call Frederik Harwath
2021-12-15 15:54 ` [PATCH 35/40] Handle references in OpenACC "private" clauses Frederik Harwath
2021-12-15 15:54 ` [PATCH 36/40] openacc: Enable reduction variable localization for "kernels" Frederik Harwath
2021-12-15 15:54 ` [PATCH 37/40] Fix for is_gimple_reg vars to 'data kernels' Frederik Harwath
2021-12-15 15:54 ` [PATCH 38/40] openacc: fix privatization of by-reference arrays Frederik Harwath
2021-12-15 15:54 ` [PATCH 39/40] openacc: Check type for references in reduction lowering Frederik Harwath
2021-12-16 12:00 ` [PATCH 40/40] openacc: Adjust testsuite to new "kernels" handling Frederik Harwath

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=20211215155447.19379-28-frederik@codesourcery.com \
    --to=frederik@codesourcery.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=rguenther@suse.de \
    --cc=thomas@codesourcery.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).