https://gcc.gnu.org/g:bec1b0a2f0c64ee462f6ba4f093803d0308d5a82

commit r17-5959-gbec1b0a2f0c64ee462f6ba4f093803d0308d5a82
Author: Iain Sandoe <[email protected]>
Date:   Wed Sep 16 23:51:44 2026 +0100

    D: Correct vector compares for targets with mask types different from data 
size.
    
    build_boolop types the VEC_COND_EXPR result with the comparison's mask type
    instead of a same-width integral vector. Where the target has AVX512 mask 
registers
    (avx512vl) that type is 32x narrower than the operands, and D's same-size 
rule then
    rejects the reinterpretation.
    
    The solution is to copy the approach used in the C/C++ frontends, where a 
separate
    return type is computed, the patch here essentially duplicates those 
actions.
    
    gcc/d/ChangeLog:
    
            * d-codegen.cc (build_vec_cmp): New.
            (build_boolop): Where required compute a separate return type for 
vector
            boolean ops.
    
    gcc/testsuite/ChangeLog:
    
            * gdc.dg/pr108945.d: Actully test for the absence of the floating
            point constant, instead of using VEC_COND_EXPR; the latter is now
            validly emitted.
    
    Signed-off-by: Iain Sandoe <[email protected]>

Diff:
---
 gcc/d/d-codegen.cc              | 46 +++++++++++++++++++++++++++++------------
 gcc/testsuite/gdc.dg/pr108945.d |  2 +-
 2 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/gcc/d/d-codegen.cc b/gcc/d/d-codegen.cc
index 0f51fd70eb64..ff4b006fb26c 100644
--- a/gcc/d/d-codegen.cc
+++ b/gcc/d/d-codegen.cc
@@ -1514,41 +1514,61 @@ warn_for_null_address (tree arg)
             TREE_OPERAND (arg, 0));
 }
 
+static tree
+build_vec_cmp (location_t loc, tree_code code, tree type,
+              tree arg0, tree arg1)
+{
+  tree zero_vec = build_zero_cst (type);
+  tree minus_one_vec = build_minus_one_cst (type);
+  tree cmp_type = truth_type_for (TREE_TYPE (arg0));
+  tree cmp = fold_build2_loc (loc, code, cmp_type, arg0, arg1);
+  return fold_build3_loc (loc, VEC_COND_EXPR, type, cmp, minus_one_vec, 
zero_vec);
+}
+
 /* Build a boolean ARG0 op ARG1 expression.  */
 
 tree
 build_boolop (tree_code code, tree arg0, tree arg1)
 {
+  tree type0 = TREE_TYPE (arg0);
+  tree type1 = TREE_TYPE (arg1);
   /* Aggregate comparisons may get lowered to a call to builtin memcmp,
      so need to remove all side effects in case its address is taken.  */
-  if (AGGREGATE_TYPE_P (TREE_TYPE (arg0)))
+  if (AGGREGATE_TYPE_P (type0))
     arg0 = d_save_expr (arg0);
-  if (AGGREGATE_TYPE_P (TREE_TYPE (arg1)))
+  if (AGGREGATE_TYPE_P (type1))
     arg1 = d_save_expr (arg1);
 
-  if (VECTOR_TYPE_P (TREE_TYPE (arg0)) && VECTOR_TYPE_P (TREE_TYPE (arg1)))
+  tree res_type = type0;
+  if (VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
     {
-      /* Build a vector comparison.
-        VEC_COND_EXPR <e1 op e2, { -1, -1, -1, -1 }, { 0, 0, 0, 0 }>; */
-      tree cmptype = truth_type_for (TREE_TYPE (arg0));
-      tree cmp = fold_build2_loc (input_location, code, cmptype, arg0, arg1);
-
-      return fold_build3_loc (input_location, VEC_COND_EXPR, cmptype, cmp,
-                             build_minus_one_cst (cmptype),
-                             build_zero_cst (cmptype));
+      if (!VECTOR_BOOLEAN_TYPE_P (type0) || !VECTOR_BOOLEAN_TYPE_P (type1))
+       {
+         tree intt = lang_hooks.types.type_for_size
+           (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
+         if (intt == NULL_TREE)
+           {
+             error_at (input_location, "could not find an integer type "
+                       "of the same size as %qT", TREE_TYPE (type0));
+             return error_mark_node;
+           }
+         res_type
+           = build_opaque_vector_type (intt, TYPE_VECTOR_SUBPARTS (type0));
+       }
+      return build_vec_cmp (input_location, code, res_type, arg0, arg1);
     }
 
   if (code == EQ_EXPR || code == NE_EXPR)
     {
       /* Check if comparing the address of a variable to null.  */
-      if (POINTER_TYPE_P (TREE_TYPE (arg0)) && integer_zerop (arg1))
+      if (POINTER_TYPE_P (type0) && integer_zerop (arg1))
        warn_for_null_address (arg0);
       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && integer_zerop (arg0))
        warn_for_null_address (arg1);
     }
 
   return fold_build2_loc (input_location, code, d_bool_type,
-                         arg0, d_convert (TREE_TYPE (arg0), arg1));
+                         arg0, d_convert (type0, arg1));
 }
 
 /* Return a COND_EXPR.  ARG0, ARG1, and ARG2 are the three
diff --git a/gcc/testsuite/gdc.dg/pr108945.d b/gcc/testsuite/gdc.dg/pr108945.d
index 03b9de8e7587..837cc3f34c3f 100644
--- a/gcc/testsuite/gdc.dg/pr108945.d
+++ b/gcc/testsuite/gdc.dg/pr108945.d
@@ -9,4 +9,4 @@ auto pr108945(f4 a, f4 b)
     return a < b;
 }
 
-// { dg-final { scan-tree-dump-not "VEC_COND_EXPR" "gimple" } }
+// { dg-final { scan-tree-dump-not "1\.0e\+0" "gimple" } }

Reply via email to