]> git.ipfire.org Git - thirdparty/gcc.git/commit
vect: Fix insufficient alignment requirement for speculative loads [PR121190]
authorPengfei Li <Pengfei.Li2@arm.com>
Wed, 30 Jul 2025 09:51:11 +0000 (10:51 +0100)
committerTamar Christina <tamar.christina@arm.com>
Wed, 30 Jul 2025 09:53:10 +0000 (10:53 +0100)
commit46a862ef08f3c44780c8d9043ce16382d8a70ada
tree05d9979dc4ed4cfa4201fc1c61a0e3af1f1a4566
parent483326615ee2c9b33b7a1690faf95bcd31167219
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.
gcc/testsuite/gcc.dg/vect/vect-early-break_137-pr121190.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/vect/vect-early-break_52.c
gcc/tree-vect-data-refs.cc