https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121190

--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Tamar Christina <tnfch...@gcc.gnu.org>:

https://gcc.gnu.org/g:46a862ef08f3c44780c8d9043ce16382d8a70ada

commit r16-2632-g46a862ef08f3c44780c8d9043ce16382d8a70ada
Author: Pengfei Li <pengfei....@arm.com>
Date:   Wed Jul 30 10:51:11 2025 +0100

    vect: Fix insufficient alignment requirement for speculative loads
[PR121190]

    This patch fixes a segmentation fault issue that can occur in vectorized
    loops with an early break. When GCC vectorizes such loops, it may insert
    a versioning check to ensure that data references (DRs) with speculative
    loads are aligned. The check normally requires DRs to be aligned to the
    vector mode size, which prevents generated vector load instructions from
    crossing page boundaries.

    However, this is not sufficient when a single scalar load is vectorized
    into multiple loads within the same iteration. In such cases, even if
    none of the vector loads crosses page boundaries, subsequent loads after
    the first one may still access memory beyond current valid page.

    Consider the following loop as an example:

            while (i < MAX_COMPARE) {
              if (*(p + i) != *(q + i))
                return i;
              i++;
            }

    When compiled with "-O3 -march=znver2" on x86, the vectorized loop may
    include instructions like:

            vmovdqa (%rcx,%rax), %ymm0
            vmovdqa 32(%rcx,%rax), %ymm1
            vpcmpeqq (%rdx,%rax), %ymm0, %ymm0
            vpcmpeqq 32(%rdx,%rax), %ymm1, %ymm1

    Note two speculative vector loads are generated for each DR (p and q).
    The first vmovdqa and vpcmpeqq are safe due to the vector size (32-byte)
    alignment, but the following ones (at offset 32) may not be safe because
    they could read from the beginning of the next memory page, potentially
    leading to segmentation faults.

    To avoid the issue, this patch increases the alignment requirement for
    speculative loads to DR_TARGET_ALIGNMENT. It ensures all vector loads in
    the same vector iteration access memory within the same page.

    gcc/ChangeLog:

            PR tree-optimization/121190
            * tree-vect-data-refs.cc (vect_enhance_data_refs_alignment):
            Increase alignment requirement for speculative loads.

    gcc/testsuite/ChangeLog:

            PR tree-optimization/121190
            * gcc.dg/vect/vect-early-break_52.c: Update an unsafe test.
            * gcc.dg/vect/vect-early-break_137-pr121190.c: New test.

Reply via email to