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

commit r15-10727-geaf6cfabe542e8bd919495fb784e04d455fdd4c3
Author: Richard Biener <[email protected]>
Date:   Thu Jan 22 13:12:43 2026 +0100

    tree-optimization/123741 - fix segfault with BB vect and masked stores
    
    When vectorizing the body of an if-converted loop with BB vectorization
    we can end up vectorizing masked stores.  But the code tracking whether
    a loop used masked stores failed to check we're doing loop
    vectorization.
    
            PR tree-optimization/123741
            * tree-vect-stmts.cc (vectorizable_store): Add missing check
            on loop vectorization.
    
            * gfortran.dg/vect/vect-pr123741.f90: New testcase.
    
    (cherry picked from commit c7c5112fa19eeb01656e000c2b7732e86be8782e)

Diff:
---
 gcc/testsuite/gfortran.dg/vect/vect-pr123741.f90 | 50 ++++++++++++++++++++++++
 gcc/tree-vect-stmts.cc                           |  2 +-
 2 files changed, 51 insertions(+), 1 deletion(-)

diff --git a/gcc/testsuite/gfortran.dg/vect/vect-pr123741.f90 
b/gcc/testsuite/gfortran.dg/vect/vect-pr123741.f90
new file mode 100644
index 000000000000..5288ea6df63a
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/vect/vect-pr123741.f90
@@ -0,0 +1,50 @@
+! { dg-do compile }
+! { dg-additional-options "-Ofast" }
+! { dg-additional-options "-mavx2" { target avx2 } }
+
+module m1
+parameter( n1=5,n2=5,n3=5,n4=5 )
+integer:: p(n1,n2,n3,n4)
+
+contains
+subroutine s1(i,nn2,nn4)
+integer:: i(n1,n2,n3,n4)
+i= p
+forall (k2=1:nn2,k4=1:nn4,mod(k2,3)/=0 .and. mod(k4,3)/=0) 
+where( mod(i(:,k2,:,k4),4) /=0 ) &
+ i(:,k2,:,k4) = i(:,k2,:,k4) + 1.0 / real(k2+k4 -1)
+end forall
+end subroutine
+
+subroutine s2(i,nn2,nn4)
+integer:: i(n1,n2,n3,n4)
+integer,allocatable:: temp(:,:,:,:)
+i= p
+allocate(temp(n1,n2,n3,n4))
+do k4=1,nn4
+do k2=1,nn2
+if (mod(k2,3)/=0 .and. mod(k4,3)/=0)then
+where( mod(i(:,k2,:,k4),4) /=0 ) &
+ temp(:,k2,:,k4) = i(:,k2,:,k4) +1.0 / real(k2+k4 -1)
+end if
+end do
+end do
+do k4=1,nn4
+do k2=1,nn2
+if (mod(k2,3)/=0 .and. mod(k4,3)/=0)then
+where( mod(i(:,k2,:,k4),4) /=0 ) &
+ i(:,k2,:,k4) = temp(:,k2,:,k4)
+end if
+end do
+end do
+end subroutine
+end
+
+use m1
+integer:: i1(n1,n2,n3,n4),i2(n1,n2,n3,n4)
+p= reshape([(k,k=1,(n1)*(n2)*(n3)*(n4))],[n1,n2,n3,n4])
+call s1(i1,n2,n4)
+call s2(i2,n2,n4)
+if (any(i1/=i2)) print *,101
+print *,'OK'
+end
diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
index e711f940173d..94d5d4e3b11f 100644
--- a/gcc/tree-vect-stmts.cc
+++ b/gcc/tree-vect-stmts.cc
@@ -9165,7 +9165,7 @@ vectorizable_store (vec_info *vinfo,
                                            memory_access_type, loop_lens);
     }
 
-  if (mask && !costing_p)
+  if (loop_vinfo && mask && !costing_p)
     LOOP_VINFO_HAS_MASK_STORE (loop_vinfo) = true;
 
   /* In case the vectorization factor (VF) is bigger than the number

Reply via email to