>From d57604516cacb19b9286e0298881f10b4b5266bc Mon Sep 17 00:00:00 2001 From: Minh-Quan Ho <[email protected]> Date: Mon, 16 Nov 2026 10:33:05 +0100 Subject: [PATCH] aarch64: Zero the upper bits of 64-bit partial-ptrue loads [PR128773]
aarch64_expand_maskloadstore turns an SVE masked load whose predicate is a partial PTRUE covering at most 128 bits into an Advanced SIMD load. PR125148 changed aarch64_emit_load_store_through_mode so that scalar (1, 2 and 4 byte) loads are inserted into an explicitly zeroed SVE register. Loads of 64 bits still use V8QImode, which goes through the vector path of that function and reaches the SVE register through a paradoxical subreg. The bits above the loaded 64 are then undefined in RTL even though the masked load requires them to be zero, and DSE can forward the upper bytes of an earlier wider store into them: vf init = svdupq_f64 (12.0, 24.0); svst1 (svptrue_b64 (), arr, init); vf r = svld1 (svdupq_b64 (1, 0), arr); /* lane 1 must be 0 */ gives lane 1 == 24 at -O1 and above, for any vector length (-msve-vector-bits=128/256/512 and VLA). The same holds for every 64-bit partial PTRUE: .d VL1, .s VL2, .h VL4 and .b VL8. Fix it by using DImode for 64-bit loads, so that they take the same zero-extending path as the narrower scalar cases. The existing *aarch64_vec_shl_insert_from_load_<mode> pattern still emits a single LDR Dn when the value comes straight from memory. Stores only read the low 64 bits and keep using V8QImode. gcc/ChangeLog: PR target/128773 * config/aarch64/aarch64.cc (aarch64_expand_maskloadstore): Use DImode rather than V8QImode for 64-bit loads. gcc/testsuite/ChangeLog: PR target/128773 * gcc.target/aarch64/sve/maskload_zero_upper_run.c: New test. Signed-off-by: Minh-Quan Ho <[email protected]> AI-assisted by: Claude Opus 5.5 --- gcc/config/aarch64/aarch64.cc | 10 +++- .../aarch64/sve/maskload_zero_upper_run.c | 55 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.target/aarch64/sve/maskload_zero_upper_run.c diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc index 5a0bc214f..b23ac1004 100644 --- a/gcc/config/aarch64/aarch64.cc +++ b/gcc/config/aarch64/aarch64.cc @@ -25286,7 +25286,15 @@ aarch64_expand_maskloadstore (rtx *operands, machine_mode mode) if (known_eq (width, 128)) new_mode = V16QImode; else if (known_eq (width, 64)) - new_mode = V8QImode; + /* A V8QImode load would reach the SVE register through a paradoxical + subreg, leaving the bits above the loaded 64 undefined in RTL even + though the masked load requires them to be zero. Passes such as + DSE may then forward the bytes of an earlier wider store into those + bits. Load through DImode instead so that + aarch64_emit_load_store_through_mode inserts the value into an + explicitly zeroed register. Stores only read the low 64 bits and + can keep using V8QImode. */ + new_mode = MEM_P (operands[1]) ? DImode : V8QImode; else new_mode = int_mode_for_size (width, 0).require (); aarch64_emit_load_store_through_mode (operands[0], operands[1], diff --git a/gcc/testsuite/gcc.target/aarch64/sve/maskload_zero_upper_run.c b/gcc/testsuite/gcc.target/aarch64/sve/maskload_zero_upper_run.c new file mode 100644 index 000000000..d2c0af7fe --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/sve/maskload_zero_upper_run.c @@ -0,0 +1,55 @@ +/* PR target/128773 + A predicated load whose constant predicate enables exactly the low + 64 bits must zero the inactive lanes, even when the loaded memory was + just written by a wider store. The load used to be expanded into a + V8QImode load and a paradoxical subreg, and DSE then forwarded the + upper bytes of the store. */ +/* { dg-do run { target aarch64_sve_hw } } */ +/* { dg-options "-O2" } */ + +#include <arm_sve.h> +#include <string.h> + +extern void abort (void) __attribute__ ((noreturn)); + +typedef unsigned char v64qi __attribute__ ((vector_size (64))); + +/* Fill a 64-byte buffer with nonzero bytes using a plain store that DSE + can see, load it back with predicate PRED and check that every byte + beyond the first 8 is zero. */ +#define TEST_LOAD(NAME, TYPE, PRED, LOAD) \ + void __attribute__ ((noipa)) \ + test_##NAME (void) \ + { \ + unsigned char buf[64] __attribute__ ((aligned (64))); \ + unsigned char out[256]; \ + v64qi init; \ + for (int i = 0; i < 64; ++i) \ + init[i] = i + 1; \ + memcpy (buf, &init, sizeof (init)); \ + svst1_u8 (svptrue_b8 (), out, \ + svreinterpret_u8 (LOAD (PRED, (const TYPE *) buf))); \ + for (int i = 0; i < 8; ++i) \ + if (out[i] != i + 1) \ + abort (); \ + for (int i = 8; i < (int) svcntb (); ++i) \ + if (out[i] != 0) \ + abort (); \ + } + +TEST_LOAD (u64_vl1, uint64_t, svptrue_pat_b64 (SV_VL1), svld1_u64) +TEST_LOAD (f64_vl1, float64_t, svptrue_pat_b64 (SV_VL1), svld1_f64) +TEST_LOAD (u32_vl2, uint32_t, svptrue_pat_b32 (SV_VL2), svld1_u32) +TEST_LOAD (u16_vl4, uint16_t, svptrue_pat_b16 (SV_VL4), svld1_u16) +TEST_LOAD (u8_vl8, uint8_t, svptrue_pat_b8 (SV_VL8), svld1_u8) + +int +main (void) +{ + test_u64_vl1 (); + test_f64_vl1 (); + test_u32_vl2 (); + test_u16_vl4 (); + test_u8_vl8 (); + return 0; +} -- 2.43.0
