From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1729) id 3230E385DC19; Tue, 25 Jan 2022 20:36:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3230E385DC19 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Kwok Yeung To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/omp/gcc-11] openmp: Add support for streaming metadirectives and resolving them after LTO X-Act-Checkin: gcc X-Git-Author: Kwok Cheung Yeung X-Git-Refname: refs/heads/devel/omp/gcc-11 X-Git-Oldrev: 7e672d2ba146ca55dfffc36b198fbb3f3200f8f2 X-Git-Newrev: b6fd3d1a54736c87fcd29a4ed294b31346b3af75 Message-Id: <20220125203632.3230E385DC19@sourceware.org> Date: Tue, 25 Jan 2022 20:36:32 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 25 Jan 2022 20:36:32 -0000 https://gcc.gnu.org/g:b6fd3d1a54736c87fcd29a4ed294b31346b3af75 commit b6fd3d1a54736c87fcd29a4ed294b31346b3af75 Author: Kwok Cheung Yeung Date: Tue Jan 25 10:49:44 2022 -0800 openmp: Add support for streaming metadirectives and resolving them after LTO This patch adds support for streaming metadirective Gimple statements during LTO, and adds a metadirective expansion pass that runs after LTO. This is required for metadirectives with selectors that can only be resolved from within the accel compiler. 2022-01-25 Kwok Cheung Yeung gcc/ * Makefile.in (OBJS): Add omp-expand-metadirective.o. * gimple-streamer-in.c (input_gimple_stmt): Add case for GIMPLE_OMP_METADIRECTIVE. Handle metadirective labels. * gimple-streamer-out.c (output_gimple_stmt): Likewise. * omp-expand-metadirective.cc: New. * passes.def: Add pass_omp_expand_metadirective. * tree-pass.h (make_pass_omp_expand_metadirective): New prototype. Diff: --- gcc/ChangeLog.omp | 10 +++ gcc/Makefile.in | 1 + gcc/gimple-streamer-in.c | 10 +++ gcc/gimple-streamer-out.c | 6 ++ gcc/omp-expand-metadirective.cc | 191 ++++++++++++++++++++++++++++++++++++++++ gcc/passes.def | 1 + gcc/tree-pass.h | 1 + 7 files changed, 220 insertions(+) diff --git a/gcc/ChangeLog.omp b/gcc/ChangeLog.omp index bb9f45493fd..8fa5827c642 100644 --- a/gcc/ChangeLog.omp +++ b/gcc/ChangeLog.omp @@ -1,3 +1,13 @@ +2022-01-25 Kwok Cheung Yeung + + * Makefile.in (OBJS): Add omp-expand-metadirective.o. + * gimple-streamer-in.c (input_gimple_stmt): Add case for + GIMPLE_OMP_METADIRECTIVE. Handle metadirective labels. + * gimple-streamer-out.c (output_gimple_stmt): Likewise. + * omp-expand-metadirective.cc: New. + * passes.def: Add pass_omp_expand_metadirective. + * tree-pass.h (make_pass_omp_expand_metadirective): New prototype. + 2022-01-25 Kwok Cheung Yeung * gimplify.c (expand_omp_metadirective): New. diff --git a/gcc/Makefile.in b/gcc/Makefile.in index 8c02b85d2a9..321ef959f43 100644 --- a/gcc/Makefile.in +++ b/gcc/Makefile.in @@ -1507,6 +1507,7 @@ OBJS = \ omp-low.o \ omp-oacc-kernels-decompose.o \ omp-simd-clone.o \ + omp-expand-metadirective.o \ omp-data-optimize.o \ opt-problem.o \ optabs.o \ diff --git a/gcc/gimple-streamer-in.c b/gcc/gimple-streamer-in.c index 1c979f438a5..b821aa3ca30 100644 --- a/gcc/gimple-streamer-in.c +++ b/gcc/gimple-streamer-in.c @@ -151,6 +151,7 @@ input_gimple_stmt (class lto_input_block *ib, class data_in *data_in, case GIMPLE_COND: case GIMPLE_GOTO: case GIMPLE_DEBUG: + case GIMPLE_OMP_METADIRECTIVE: for (i = 0; i < num_ops; i++) { tree *opp, op = stream_read_tree (ib, data_in); @@ -188,6 +189,15 @@ input_gimple_stmt (class lto_input_block *ib, class data_in *data_in, else gimple_call_set_fntype (call_stmt, stream_read_tree (ib, data_in)); } + if (gomp_metadirective *metadirective_stmt + = dyn_cast (stmt)) + { + gimple_alloc_omp_metadirective (metadirective_stmt); + for (i = 0; i < num_ops; i++) + gimple_omp_metadirective_set_label (metadirective_stmt, i, + stream_read_tree (ib, + data_in)); + } break; case GIMPLE_NOP: diff --git a/gcc/gimple-streamer-out.c b/gcc/gimple-streamer-out.c index fcbf92300d4..c19dff74261 100644 --- a/gcc/gimple-streamer-out.c +++ b/gcc/gimple-streamer-out.c @@ -127,6 +127,7 @@ output_gimple_stmt (struct output_block *ob, struct function *fn, gimple *stmt) case GIMPLE_COND: case GIMPLE_GOTO: case GIMPLE_DEBUG: + case GIMPLE_OMP_METADIRECTIVE: for (i = 0; i < gimple_num_ops (stmt); i++) { tree op = gimple_op (stmt, i); @@ -169,6 +170,11 @@ output_gimple_stmt (struct output_block *ob, struct function *fn, gimple *stmt) else stream_write_tree (ob, gimple_call_fntype (stmt), true); } + if (gimple_code (stmt) == GIMPLE_OMP_METADIRECTIVE) + for (i = 0; i < gimple_num_ops (stmt); i++) + stream_write_tree (ob, gimple_omp_metadirective_label (stmt, i), + true); + break; case GIMPLE_NOP: diff --git a/gcc/omp-expand-metadirective.cc b/gcc/omp-expand-metadirective.cc new file mode 100644 index 00000000000..aaf048a699a --- /dev/null +++ b/gcc/omp-expand-metadirective.cc @@ -0,0 +1,191 @@ +/* Expand an OpenMP metadirective. + + Copyright (C) 2021 Free Software Foundation, Inc. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "backend.h" +#include "target.h" +#include "tree.h" +#include "langhooks.h" +#include "gimple.h" +#include "tree-pass.h" +#include "cgraph.h" +#include "fold-const.h" +#include "gimplify.h" +#include "gimple-iterator.h" +#include "gimple-walk.h" +#include "gomp-constants.h" +#include "omp-general.h" +#include "diagnostic-core.h" +#include "tree-cfg.h" +#include "cfganal.h" +#include "ssa.h" +#include "tree-into-ssa.h" +#include "cfghooks.h" + +static void +omp_expand_metadirective (function *fun, basic_block bb) +{ + gimple *stmt = last_stmt (bb); + vec candidates + = omp_resolve_metadirective (stmt); + + /* This is the last chance for the metadirective to be resolved. */ + if (candidates.is_empty ()) + gcc_unreachable (); + + auto_vec labels; + + for (unsigned int i = 0; i < candidates.length (); i++) + labels.safe_push (candidates[i].directive); + + /* Delete BBs for all variants not in the candidate list. */ + for (unsigned i = 0; i < gimple_num_ops (stmt); i++) + { + tree label = gimple_omp_metadirective_label (stmt, i); + if (!labels.contains (label)) + { + edge e = find_edge (bb, label_to_block (fun, label)); + remove_edge_and_dominated_blocks (e); + } + } + + /* Remove the metadirective statement. */ + gimple_stmt_iterator gsi = gsi_last_bb (bb); + gsi_remove (&gsi, true); + + if (candidates.length () == 1) + { + /* Special case if there is only one selector - there should be one + remaining edge from BB to the selected variant. */ + edge e = find_edge (bb, label_to_block (fun, + candidates.last ().directive)); + e->flags |= EDGE_FALLTHRU; + + return; + } + + basic_block cur_bb = bb; + + /* For each candidate, create a conditional that checks the dynamic + condition, branching to the candidate directive if true, to the + next candidate check if false. */ + for (unsigned i = 0; i < candidates.length () - 1; i++) + { + basic_block next_bb = NULL; + gcond *cond_stmt = gimple_build_cond_from_tree (candidates[i].selector, + NULL_TREE, NULL_TREE); + gsi = gsi_last_bb (cur_bb); + gsi_insert_seq_after (&gsi, cond_stmt, GSI_NEW_STMT); + + if (i < candidates.length () - 2) + { + edge e_false = split_block (cur_bb, cond_stmt); + e_false->flags &= ~EDGE_FALLTHRU; + e_false->flags |= EDGE_FALSE_VALUE; + e_false->probability = profile_probability::uninitialized (); + + next_bb = e_false->dest; + } + + /* Redirect the source of the edge from BB to the candidate directive + to the conditional. Reusing the edge avoids disturbing phi nodes in + the destination BB. */ + edge e = find_edge (bb, label_to_block (fun, candidates[i].directive)); + redirect_edge_pred (e, cur_bb); + e->flags |= EDGE_TRUE_VALUE; + + if (next_bb) + cur_bb = next_bb; + } + + /* The last of the candidates is always static. */ + edge e = find_edge (cur_bb, label_to_block (fun, + candidates.last ().directive)); + e->flags |= EDGE_FALSE_VALUE; +} + +namespace { + +const pass_data pass_data_omp_expand_metadirective = +{ + GIMPLE_PASS, /* type */ + "omp_expand_metadirective", /* name */ + OPTGROUP_OMP, /* optinfo_flags */ + TV_NONE, /* tv_id */ + PROP_gimple_lcf, /* properties_required */ + 0, /* properties_provided */ + 0, /* properties_destroyed */ + 0, /* todo_flags_start */ + TODO_update_ssa | TODO_cleanup_cfg, /* todo_flags_finish */ +}; + +class pass_omp_expand_metadirective : public gimple_opt_pass +{ +public: + pass_omp_expand_metadirective (gcc::context *ctxt) + : gimple_opt_pass (pass_data_omp_expand_metadirective, ctxt) + {} + + /* opt_pass methods: */ + virtual bool gate (function *) + { + return (flag_openmp); + } + + virtual unsigned int execute (function *fun); +}; // class pass_omp_oacc_kernels_decompose + +unsigned int +pass_omp_expand_metadirective::execute (function *fun) +{ + basic_block bb; + auto_vec metadirective_bbs; + + FOR_EACH_BB_FN (bb, fun) + { + gimple *stmt = last_stmt (bb); + if (stmt && is_a (stmt)) + metadirective_bbs.safe_push (bb); + } + + if (metadirective_bbs.is_empty ()) + return 0; + + calculate_dominance_info (CDI_DOMINATORS); + + for (unsigned i = 0; i < metadirective_bbs.length (); i++) + omp_expand_metadirective (fun, metadirective_bbs[i]); + + free_dominance_info (fun, CDI_DOMINATORS); + mark_virtual_operands_for_renaming (fun); + + return 0; +} + +} // anon namespace + + +gimple_opt_pass * +make_pass_omp_expand_metadirective (gcc::context *ctxt) +{ + return new pass_omp_expand_metadirective (ctxt); +} diff --git a/gcc/passes.def b/gcc/passes.def index d1dedbc287e..d30442d343d 100644 --- a/gcc/passes.def +++ b/gcc/passes.def @@ -186,6 +186,7 @@ along with GCC; see the file COPYING3. If not see NEXT_PASS (pass_lower_eh_dispatch); NEXT_PASS (pass_omp_device_lower); NEXT_PASS (pass_omp_target_link); + NEXT_PASS (pass_omp_expand_metadirective); NEXT_PASS (pass_adjust_alignment); NEXT_PASS (pass_all_optimizations); PUSH_INSERT_PASSES_WITHIN (pass_all_optimizations) diff --git a/gcc/tree-pass.h b/gcc/tree-pass.h index 6fde66bdd8d..f7812c28eae 100644 --- a/gcc/tree-pass.h +++ b/gcc/tree-pass.h @@ -418,6 +418,7 @@ extern gimple_opt_pass *make_pass_lower_switch_O0 (gcc::context *ctxt); extern gimple_opt_pass *make_pass_lower_vector (gcc::context *ctxt); extern gimple_opt_pass *make_pass_lower_vector_ssa (gcc::context *ctxt); extern gimple_opt_pass *make_pass_omp_oacc_kernels_decompose (gcc::context *ctxt); +extern gimple_opt_pass *make_pass_omp_expand_metadirective (gcc::context *ctxt); extern gimple_opt_pass *make_pass_lower_omp (gcc::context *ctxt); extern gimple_opt_pass *make_pass_omp_data_optimize (gcc::context *ctxt); extern gimple_opt_pass *make_pass_diagnose_omp_blocks (gcc::context *ctxt);