From patchwork Wed Nov 17 16:03:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Frederik Harwath X-Patchwork-Id: 1556268 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HvSnY1Zblz9s1l for ; Thu, 18 Nov 2021 03:20:01 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0D6BC385AC3E for ; Wed, 17 Nov 2021 16:19:59 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa3.mentor.iphmx.com (esa3.mentor.iphmx.com [68.232.137.180]) by sourceware.org (Postfix) with ESMTPS id 043AA3858410 for ; Wed, 17 Nov 2021 16:04:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 043AA3858410 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com IronPort-SDR: 7cE7xKm8DO8rpkX8Xvf3QIDjhPrBIVjof7oPSmoTqyvkZaX1XGmnM1hgu7EcUDIC2GJz6YfG5U TZzsLqVYTupqFkSA7bIHDrtrNSc6VjmmW+Vpp4Taf2xyHpxrkFiR5PajJjD49LIbaCf6Pd0t1b WwqdEVnRJA1RDKfufZdV6leI+E43VQLzPodk8b/FLcSWZnkqQ8/pNuXGTKJYDmmyyhYWU5lFvH EQazfgG6yk52kEt8T8kbsEhrKFhZoVHU6GW1re5voRuGjESNYC9FbYyLF9Vck3A5hLukrzkhXg O2QZoMHWIGJ6C8gCJ9LYFUgh X-IronPort-AV: E=Sophos;i="5.87,241,1631606400"; d="scan'208";a="68445390" Received: from orw-gwy-01-in.mentorg.com ([192.94.38.165]) by esa3.mentor.iphmx.com with ESMTP; 17 Nov 2021 08:04:51 -0800 IronPort-SDR: 1FaprDfkpS1mA6KpGea7QxS44N4GM9U5Un88/+pls3kIymSo50HPt6bVLXzPXuI+4ZwU4yAeFJ gJ9rHsGvyuWb5RO85exIbbVbUq+VQObnzzWqLmvX4SSd3cQYZhS0fPPWZuy911dl1xM+QzXgkZ QJg//Wgt6x3atg3JH1pyf5ttV3CEl2sAqjhiLom6QXmgLUSV4vVMXfAimDsuGzG8UyB0bu7zUQ ojyOoaDeG8IhfvIBhiEPW/2yVeK1Yj0F2HouuUUC92pTPqp+Bi5w56QaQ1qMCa3H4urwm+FqBD s+A= From: Frederik Harwath To: Subject: [OG11][committed][PATCH 17/22] openacc: Handle internal function calls in pass_lim Date: Wed, 17 Nov 2021 17:03:25 +0100 Message-ID: <20211117160330.20029-17-frederik@codesourcery.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211117160330.20029-1-frederik@codesourcery.com> References: <20211117160330.20029-1-frederik@codesourcery.com> MIME-Version: 1.0 X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: svr-ies-mbx-13.mgc.mentorg.com (139.181.222.13) To SVR-IES-MBX-04.mgc.mentorg.com (139.181.222.4) X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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 | 58 ++++++++++++++++++++++++++++------ gcc/tree-ssa-loop-manip.h | 2 +- 4 files changed, 52 insertions(+), 12 deletions(-) -- 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 diff --git a/gcc/gimple-loop-interchange.cc b/gcc/gimple-loop-interchange.cc index 7b799eca805c..d617438910fd 100644 --- a/gcc/gimple-loop-interchange.cc +++ b/gcc/gimple-loop-interchange.cc @@ -2096,7 +2096,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 48c9821011f0..d1dedbc287e2 100644 --- a/gcc/passes.def +++ b/gcc/passes.def @@ -247,7 +247,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 7de47edbcb30..b392ae609aaf 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. @@ -320,11 +322,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) { @@ -974,7 +988,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; @@ -1002,7 +1016,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; @@ -1033,7 +1047,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)) @@ -1458,7 +1472,15 @@ 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) { /* We use the shared mem_ref for all unanalyzable refs. */ @@ -1484,7 +1506,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) @@ -3155,7 +3177,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; @@ -3173,7 +3196,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. */ @@ -3220,13 +3243,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 @@ -3239,7 +3270,16 @@ pass_lim::execute (function *fun) if (number_of_loops (fun) <= 1) return 0; - unsigned int todo = loop_invariant_motion_in_fun (fun, true); + + bool store_motion = true; + /* 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 86fc118b6bef..e0beb624aec9 100644 --- a/gcc/tree-ssa-loop-manip.h +++ b/gcc/tree-ssa-loop-manip.h @@ -55,7 +55,7 @@ extern void tree_transform_and_unroll_loop (class loop *, unsigned, extern void tree_unroll_loop (class loop *, unsigned, edge, class 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 */