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

            Bug ID: 129093
           Summary: vect: Runtime profitability check dropped for
                    partial-vector loops without epilogue peeling
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: shitl1017 at gmail dot com
  Target Milestone: ---

When vectorizing a loop using partial vectors
(LOOP_VINFO_USING_PARTIAL_VECTORS_P) that does not require loop versioning
(LOOP_REQUIRES_VERSIONING == false) and has no epilogue peeling (epilog_peeling
== false), a required runtime profitability check (check_profitability == true)
is silently dropped and no scalar fallback loop is emitted.

Reproducer (RISC-V RVV, no --param needed on GCC 17 Trunk):

  void ssm_conv_f32(const float *s, const float *c, float *x,
                    int ir, int nc, int ncs) {
    for (int i1 = 0; i1 < ir; ++i1) {
      float sumf = 0.0f;
      for (int i0 = 0; i0 < nc; ++i0)
        sumf += s[i0 + i1 * ncs] * c[i0 + i1 * nc];
      x[i1] = sumf;
    }
  }

Compiled with GCC 17 Trunk (gcc version 17.0.0 20261126 (experimental) (GCC),
commit 7be67d007f88dc166fc890102e3a53ec25b1ffcb):

  riscv64-unknown-linux-gnu-gcc -O3 -march=rv64gcv -mabi=lp64d \
    -mrvv-max-lmul=dynamic -fdump-tree-vect-details -S ssm_conv.c

Observed behavior:

- The vect dump reports: Profitability threshold is 479 loop iterations.
- However, the generated assembly contains no runtime profitability check (nc < 
  479) and no scalar fallback loop. Short unknown trip counts (e.g. nc = 4)
enter 
  the e32, m8 ordered FP reduction (vfredosum.vs) vector loop unconditionally.
- Adding --param=min-vect-loop-bound=32 raises the logged threshold to 1024
loop 
  iterations, yet the generated assembly is identical (0 diff).

Cross-architecture confirmation:

The same middle-end issue reproduces on other targets whenever partial vectors
are enabled for the main loop and no alias/alignment versioning is needed:

  void vadd_f32(float *__restrict dst, const float *__restrict a,
                const float *__restrict b, int n) {
    for (int i = 0; i < n; ++i)
      dst[i] = a[i] + b[i];
  }

Comparing with vs without --param=min-vect-loop-bound=32, the vect dump logs
the raised threshold but the emitted assembly has 0 diff:

- AArch64 SVE (-O3 -march=armv8-a+sve): logs 128 iterations, 0 asm diff.
- x86-64 AVX-512 (-O3 -march=skylake-avx512
--param=vect-partial-vector-usage=2): 
  logs 256 iterations, 0 asm diff.

Root cause:

1. In vect_transform_loop (gcc/tree-vect-loop.cc:11463-11470), 
   vect_apply_runtime_profitability_check_p returns true (th = 479 >= VF),
   setting check_profitability = true.
2. None of the LOOP_REQUIRES_VERSIONING conditions hold, so
vect_loop_versioning
   (gcc/tree-vect-loop.cc:11485) is skipped and check_profitability = true is
   passed to vect_do_peeling (gcc/tree-vect-loop.cc:11517).
3. In vect_do_peeling (gcc/tree-vect-loop-manip.cc:3406-3429), partial-vector
   execution sets bound_epilog == 0 (epilog_peeling == false). With 
   prolog_peeling == 0, vect_do_peeling hits:

     if (!prolog_peeling && !epilog_peeling)
       return NULL;

   and returns early, silently dropping the runtime profitability check.

Expected behavior and impact:

When vect_apply_runtime_profitability_check_p requests a runtime profitability
check for a partial-vector main loop without epilogue peeling (and whose entry
lower bound is not proven >= th), a scalar fallback and runtime threshold check
should be emitted (or vectorization rejected under
-fvect-cost-model=very-cheap).

Note on PR 69873:

This is related to PR 69873 (where comment 3 noted it is difficult to hit a
runtime profitability check without peeling or versioning for full-vector
loops).  With partial-vector execution (RVV, SVE, AVX-512 masked loops),
epilogue peeling is eliminated even for arbitrary unknown trip counts, so
real-world loops such as in-order FP reductions hit this directly without any
--param flags.

This reproducer is extracted from ggml_compute_forward_ssm_conv_f32 in
llama.cpp (where nc = 4 at runtime while unknown at compile time). On a
SpacemiT K3 (X100, VLEN = 256), unconditionally entering the m8 + vfredosum.vs
loop at nc = 4 (ir = 768) takes 80.21 us vs 5.95 us for a scalar loop;
restoring the runtime profitability check reduces nc = 4 to 5.97 us (13.43x
faster,
matching scalar) while keeping the vector path for nc >= 479.

Reply via email to