openmp - Fix up && and || reductions [PR94366]
authorJakub Jelinek <jakub@redhat.com>
Thu, 1 Jul 2021 06:55:49 +0000 (08:55 +0200)
committerJakub Jelinek <jakub@redhat.com>
Thu, 1 Jul 2021 06:55:49 +0000 (08:55 +0200)
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  <jakub@redhat.com>

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.

gcc/omp-low.c
libgomp/testsuite/libgomp.c-c++-common/pr94366.c [new file with mode: 0644]

index 410a4a6..e7049c8 100644 (file)
@@ -6505,11 +6505,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))
                    {
@@ -6560,17 +6557,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]);
@@ -6592,19 +6590,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);
@@ -7548,12 +7546,7 @@ lower_reduction_clauses (tree clauses, gimple_seq *stmt_seqp,
       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);
@@ -7562,17 +7555,17 @@ lower_reduction_clauses (tree clauses, gimple_seq *stmt_seqp,
          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;
@@ -7680,16 +7673,16 @@ lower_reduction_clauses (tree clauses, gimple_seq *stmt_seqp,
            {
              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);
@@ -7726,16 +7719,16 @@ lower_reduction_clauses (tree clauses, gimple_seq *stmt_seqp,
        {
          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 (file)
index 0000000..5837cd0
--- /dev/null
@@ -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;
+}