When attempting to vectorize a loop with partial vector usage and a masked
simd clone with scalar integer mask mode, get_related_vectype_for_scalar_type
can return NULL_TREE if the target does not support a vector mode with the
required number of elements (such as V2SF on 32-bit x86).  Verify that a
valid mask type was found and disable partial vectors if not, rather than
passing a NULL vectype to vect_record_loop_mask and crashing in hash-set.h.
Also assert in vect_record_loop_mask that vectype is non-null.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.
The testcase in 1/2 exposes the issue with -m32.

        PR tree-optimization/127975
        * tree-vect-stmts.cc (vectorizable_simd_clone_call): Disable
        partial vectors if get_related_vectype_for_scalar_type returns
        NULL_TREE for the mask.
---
 gcc/tree-vect-stmts.cc | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
index 3ea4f39ddcc..09d87739cea 100644
--- a/gcc/tree-vect-stmts.cc
+++ b/gcc/tree-vect-stmts.cc
@@ -4508,8 +4508,8 @@ vectorizable_simd_clone_call (vec_info *vinfo, 
stmt_vec_info stmt_info,
                  if (SCALAR_INT_MODE_P (bestn_inbranch->simdclone->mask_mode))
                    {
                      callee_nelements
-                         = exact_div (bestn_inbranch->simdclone->simdlen,
-                                      
bestn_inbranch->simdclone->args[i].linear_step);
+                       = exact_div (bestn_inbranch->simdclone->simdlen,
+                                    
bestn_inbranch->simdclone->args[i].linear_step);
                      masktype = get_related_vectype_for_scalar_type
                          (vinfo->vector_mode, TREE_TYPE (vectype),
                           callee_nelements);
@@ -4528,10 +4528,22 @@ vectorizable_simd_clone_call (vec_info *vinfo, 
stmt_vec_info stmt_info,
                           TYPE_MODE (masktype));
                      callee_nelements = TYPE_VECTOR_SUBPARTS (masktype);
                    }
-                 auto o = vector_unroll_factor (nunits, callee_nelements);
-                 vect_record_loop_mask (loop_vinfo,
-                                        &LOOP_VINFO_MASKS (loop_vinfo),
-                                        ncopies  * o, masktype, NULL_TREE);
+                 if (!masktype)
+                   {
+                     if (dump_enabled_p ())
+                       dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
+                                        "can't use a fully-masked loop because"
+                                        " no suitable vector type was found 
for"
+                                        " the mask.\n");
+                     LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
+                   }
+                 else
+                   {
+                     auto o = vector_unroll_factor (nunits, callee_nelements);
+                     vect_record_loop_mask (loop_vinfo,
+                                            &LOOP_VINFO_MASKS (loop_vinfo),
+                                            ncopies * o, masktype, NULL_TREE);
+                   }
                }
            }
          else if ((bestn->simdclone->args[i].arg_type
-- 
2.51.0

Reply via email to