From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 77B4A383940F; Wed, 11 May 2022 06:21:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 77B4A383940F MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r9-10091] openmp - Fix up && and || reductions [PR94366] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/releases/gcc-9 X-Git-Oldrev: f1b95ed55a0b2a587c315037cb2d5413af7eaa76 X-Git-Newrev: 26c58b164fd78db4ce25f0183ed0225c08018f3f Message-Id: <20220511062134.77B4A383940F@sourceware.org> Date: Wed, 11 May 2022 06:21:34 +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: Wed, 11 May 2022 06:21:34 -0000 https://gcc.gnu.org/g:26c58b164fd78db4ce25f0183ed0225c08018f3f commit r9-10091-g26c58b164fd78db4ce25f0183ed0225c08018f3f Author: Jakub Jelinek Date: Thu Jul 1 08:55:49 2021 +0200 openmp - Fix up && and || reductions [PR94366] As the testcase shows, the special treatment of && and || reduction combiners where we expand them as omp_out = (omp_out != 0) && (omp_in != 0) (or with ||) is not needed just for &&/|| on floating point or complex types, but for all &&/|| reductions - when expanded as omp_out = omp_out && omp_in (not in C but GENERIC) it is actually gimplified into NOP_EXPRs to bool from both operands, which turns non-zero values multiple of 2 into 0 rather than 1. This patch just treats all &&/|| the same and furthermore uses bool type instead of int for the comparisons. 2021-07-01 Jakub Jelinek PR middle-end/94366 gcc/ * omp-low.c (lower_rec_input_clauses): Rename is_fp_and_or to is_truth_op, set it for TRUTH_*IF_EXPR regardless of new_var's type, use boolean_type_node instead of integer_type_node as NE_EXPR type. (lower_reduction_clauses): Likewise. libgomp/ * testsuite/libgomp.c-c++-common/pr94366.c: New test. (cherry picked from commit 91c771ec8a3b649765de3e0a7b04cf946c6649ef) Diff: --- gcc/omp-low.c | 55 +++++++++++------------- libgomp/testsuite/libgomp.c-c++-common/pr94366.c | 17 ++++++++ 2 files changed, 41 insertions(+), 31 deletions(-) diff --git a/gcc/omp-low.c b/gcc/omp-low.c index 89075dd54a0..0b79500ca2c 100644 --- a/gcc/omp-low.c +++ b/gcc/omp-low.c @@ -5177,11 +5177,8 @@ lower_rec_input_clauses (tree clauses, gimple_seq *ilist, gimple_seq *dlist, if (code == MINUS_EXPR) code = PLUS_EXPR; - /* C/C++ permits FP/complex with || and &&. */ - bool is_fp_and_or - = ((code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR) - && (FLOAT_TYPE_P (TREE_TYPE (new_var)) - || TREE_CODE (TREE_TYPE (new_var)) == COMPLEX_TYPE)); + bool is_truth_op + = (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR); tree new_vard = new_var; if (is_simd && omp_is_reference (var)) { @@ -5209,17 +5206,18 @@ lower_rec_input_clauses (tree clauses, gimple_seq *ilist, gimple_seq *dlist, } tree ivar2 = ivar; tree ref2 = ref; - if (is_fp_and_or) + if (is_truth_op) { tree zero = build_zero_cst (TREE_TYPE (ivar)); ivar2 = fold_build2_loc (clause_loc, NE_EXPR, - integer_type_node, ivar, + boolean_type_node, ivar, zero); ref2 = fold_build2_loc (clause_loc, NE_EXPR, - integer_type_node, ref, zero); + boolean_type_node, ref, + zero); } x = build2 (code, TREE_TYPE (ref), ref2, ivar2); - if (is_fp_and_or) + if (is_truth_op) x = fold_convert (TREE_TYPE (ref), x); ref = build_outer_var_ref (var, ctx); gimplify_assign (ref, x, &llist[1]); @@ -5241,19 +5239,19 @@ lower_rec_input_clauses (tree clauses, gimple_seq *ilist, gimple_seq *dlist, tree ref = build_outer_var_ref (var, ctx); tree new_var2 = new_var; tree ref2 = ref; - if (is_fp_and_or) + if (is_truth_op) { tree zero = build_zero_cst (TREE_TYPE (new_var)); new_var2 = fold_build2_loc (clause_loc, NE_EXPR, - integer_type_node, new_var, + boolean_type_node, new_var, zero); ref2 = fold_build2_loc (clause_loc, NE_EXPR, - integer_type_node, ref, + boolean_type_node, ref, zero); } x = build2 (code, TREE_TYPE (ref2), ref2, new_var2); - if (is_fp_and_or) + if (is_truth_op) x = fold_convert (TREE_TYPE (new_var), x); ref = build_outer_var_ref (var, ctx); gimplify_assign (ref, x, dlist); @@ -5974,12 +5972,7 @@ lower_reduction_clauses (tree clauses, gimple_seq *stmt_seqp, omp_context *ctx) if (code == MINUS_EXPR) code = PLUS_EXPR; - /* C/C++ permits FP/complex with || and &&. */ - bool is_fp_and_or = ((code == TRUTH_ANDIF_EXPR - || code == TRUTH_ORIF_EXPR) - && (FLOAT_TYPE_P (TREE_TYPE (new_var)) - || (TREE_CODE (TREE_TYPE (new_var)) - == COMPLEX_TYPE))); + bool is_truth_op = (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR); if (count == 1) { tree addr = build_fold_addr_expr_loc (clause_loc, ref); @@ -5988,17 +5981,17 @@ lower_reduction_clauses (tree clauses, gimple_seq *stmt_seqp, omp_context *ctx) ref = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (addr)), addr); tree new_var2 = new_var; tree ref2 = ref; - if (is_fp_and_or) + if (is_truth_op) { tree zero = build_zero_cst (TREE_TYPE (new_var)); new_var2 = fold_build2_loc (clause_loc, NE_EXPR, - integer_type_node, new_var, zero); - ref2 = fold_build2_loc (clause_loc, NE_EXPR, integer_type_node, + boolean_type_node, new_var, zero); + ref2 = fold_build2_loc (clause_loc, NE_EXPR, boolean_type_node, ref, zero); } x = fold_build2_loc (clause_loc, code, TREE_TYPE (new_var2), ref2, new_var2); - if (is_fp_and_or) + if (is_truth_op) x = fold_convert (TREE_TYPE (new_var), x); x = build2 (OMP_ATOMIC, void_type_node, addr, x); OMP_ATOMIC_MEMORY_ORDER (x) = OMP_MEMORY_ORDER_RELAXED; @@ -6106,16 +6099,16 @@ lower_reduction_clauses (tree clauses, gimple_seq *stmt_seqp, omp_context *ctx) { tree out2 = out; tree priv2 = priv; - if (is_fp_and_or) + if (is_truth_op) { tree zero = build_zero_cst (TREE_TYPE (out)); out2 = fold_build2_loc (clause_loc, NE_EXPR, - integer_type_node, out, zero); + boolean_type_node, out, zero); priv2 = fold_build2_loc (clause_loc, NE_EXPR, - integer_type_node, priv, zero); + boolean_type_node, priv, zero); } x = build2 (code, TREE_TYPE (out2), out2, priv2); - if (is_fp_and_or) + if (is_truth_op) x = fold_convert (TREE_TYPE (out), x); out = unshare_expr (out); gimplify_assign (out, x, &sub_seq); @@ -6152,16 +6145,16 @@ lower_reduction_clauses (tree clauses, gimple_seq *stmt_seqp, omp_context *ctx) { tree new_var2 = new_var; tree ref2 = ref; - if (is_fp_and_or) + if (is_truth_op) { tree zero = build_zero_cst (TREE_TYPE (new_var)); new_var2 = fold_build2_loc (clause_loc, NE_EXPR, - integer_type_node, new_var, zero); - ref2 = fold_build2_loc (clause_loc, NE_EXPR, integer_type_node, + boolean_type_node, new_var, zero); + ref2 = fold_build2_loc (clause_loc, NE_EXPR, boolean_type_node, ref, zero); } x = build2 (code, TREE_TYPE (ref), ref2, new_var2); - if (is_fp_and_or) + if (is_truth_op) x = fold_convert (TREE_TYPE (new_var), x); ref = build_outer_var_ref (var, ctx); gimplify_assign (ref, x, &sub_seq); diff --git a/libgomp/testsuite/libgomp.c-c++-common/pr94366.c b/libgomp/testsuite/libgomp.c-c++-common/pr94366.c new file mode 100644 index 00000000000..5837cd01a47 --- /dev/null +++ b/libgomp/testsuite/libgomp.c-c++-common/pr94366.c @@ -0,0 +1,17 @@ +/* PR middle-end/94366 */ + +int +main () +{ + int a = 2; + #pragma omp parallel reduction(&& : a) + a = a && 1; + if (!a) + __builtin_abort (); + a = 4; + #pragma omp parallel reduction(|| : a) + a = a || 0; + if (!a) + __builtin_abort (); + return 0; +}