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

            Bug ID: 128773
           Summary: [16 Regression] aarch64 SVE: svld1 with a constant
                    predicate covering the low 8 bytes drops the zeroing
                    of inactive lanes (PR125148 fix incomplete for the
                    64-bit case)
           Product: gcc
           Version: 16.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hmquan89 at gmail dot com
  Target Milestone: ---

Created attachment 66181
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66181&action=edit
Reproducer

Description:
A zeroing predicated load (svld1) whose predicate is a compile-time constant
that enables exactly the low 8 bytes of a 128-bit VLS vector returns the
previous memory contents in the inactive lanes instead of zero.

Testcase (gcc16-sve-dse-masked-load.c):

#include <arm_sve.h>
#include <stdio.h>

typedef svbool_t    vb __attribute__((arm_sve_vector_bits(128)));
typedef svfloat64_t vf __attribute__((arm_sve_vector_bits(128)));

int main(void) {
  double arr[2];

  /* init vector */
  vf init = svdupq_f64(12.0, 24.0);

  /* store to arr */
  svst1(svptrue_b64(), arr, init);

  /* lane 0 active, lane 1 inactive */
  vb m = svdupq_b64(1, 0);

  /* predicated load, zeroing the inactive lane */
  vf r = svld1(m, arr);

  double lane1 = svlastb(svptrue_b64(), r);

  double lane0 = svlasta(svpfalse(), r);
  printf("lane0 = %f  lane1 = %f\n", lane0, lane1);

  /* lane1 must be zero */
  return lane1 != 0.0;
}


  $ gcc -O2 -mcpu=neoverse-v2 -msve-vector-bits=128 gcc16-sve-dse-masked-load.c
&& ./a.out
  lane0 = 12, lane1 = 24        (expected: lane1 = 0)

Bug present in trunk GCC on Compiler explorer : https://godbolt.org/z/89eP8nTGz
Compiled with `-fno-dse` workaround the bug.

Analysis:
- .optimized still has the correct
    r_6 = .MASK_LOAD (&arr, 64B, { -1, 0 }, { 0.0, 0.0 });
- expand lowers it to an 8-byte Adv. SIMD load plus a paradoxical subreg:
    (set (reg:V8QI 113) (mem/c:V8QI ...arr...))
    (set (reg:VNx2DI 114) (subreg:VNx2DI (reg:V8QI 113) 0))
- dse1 then forwards the preceding 16-byte store into reg 113, so the
  "zero" upper half of the paradoxical subreg becomes the stored data.
  -fno-dse hides the problem.
- This is the SVE->SIMD lowering fixed by PR125148
  (g:a6ee91793b9f4d28ccd3fcc6f607f646d305a39e, present in 16.2.0).
  That fix covers 1/2/4-byte predicates but not the 8-byte one. With a
  constant first-N-bytes predicate (attached gcc16-sve-8byte-pred-shapes.c):
      u64 VL1, u32 VL2, u16 VL4, u8 VL8  (8 bytes)  -> wrong in 16.2.0
      u8 VL1, u8 VL4, u32 VL1            (1/4 bytes) -> correct
  GCC 14.2 is correct for all of them.

Found via the Kokkos SIMD unit test simd.host_loadstore on NVIDIA Grace.

Bugfix should be backported to any version >= 16.2.0 please.

Reply via email to