llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[LoopVectorize] Missed vectorization of if/else with cross-iteration dependence through mutually exclusive branches

Open kartcq opened this issue 2 months ago • 4 comments

The loop vectorizer rejects a loop containing an if/else diamond where one branch writes c[i+1] and the other reads c[i]. LAA reports a backward dependence with distance 1 between these accesses. However, the accesses are in mutually exclusive branches — for any given iteration, exactly one of the two executes — so vectorization with masked memory operations is safe (with correct ordering of the masked store before the masked load in the linearized vector body). GCC 16 vectorizes this loop using masked loads/stores with complementary predicates. LLVM rejects it on all targets (RISC-V V, AArch64 SVE, x86 AVX-512).

Reproducer

void conditional_update(int n,
                        int *__restrict__ a,
                        int *__restrict__ b,
                        int *__restrict__ c,
                        int *__restrict__ d,
                        int *__restrict__ e)
{
    for (int i = 0; i < n - 1; i++) {
        if (b[i] < 0) {
            c[i + 1] = a[i] + d[i] * d[i];   // Path A: writes c[i+1]
        } else {
            a[i] = c[i] + d[i] * e[i];        // Path B: reads c[i]
        }
    }
}

Compile with:

clang -O3 --target=riscv64 -march=rv64gcv -Rpass-missed=loop-vectorize -S reduced.c
clang -O3 --target=aarch64 -march=armv9-a+sve -Rpass-missed=loop-vectorize -S reduced.c

Diagnostic:

reduced.c:8:22: remark: loop not vectorized: unsafe dependent memory operations in loop.
  Backward loop carried data dependence. Memory location is the same as accessed at reduced.c:10:20

Godbolt: https://godbolt.org/z/dr7dM1sxr

Why LAA rejects this

LoopAccessAnalysis::isDependent() computes the SCEV distance between the store to c[i+1] (write) and the load from c[i] (read). The distance is 1 element (4 bytes for int). Since 1 < VF, LAA classifies this as an unsafe backward dependence and returns Dependence::Backward. LAA's dependence analysis is purely SCEV-based and has no concept of control flow — it never checks whether the two accesses are in mutually exclusive basic blocks.

Why vectorization is actually safe

In the scalar loop, for any iteration i, exactly one of the two paths executes:

  • Path A (b[i] < 0): writes c[i+1]
  • Path B (b[i] >= 0): reads c[i] When vectorized with predication, each path gets a complementary mask. For the dependence to matter, we'd need iteration i to take Path A (writing c[i+1]) AND iteration i+1 to take Path B (reading c[i+1]). Even when this happens, vectorization is safe provided the masked store from Path A is executed before the masked load from Path B in the linearized vector body. Within a vector group, lane k's store to c[i+k+1] must be visible to lane k+1's load from c[i+k+1]. GCC 16 handles this correctly: it places the masked store (Path A) before the masked load (Path B) in the vectorized code, using complementary predicate masks.

kartcq avatar Sep 21 '26 10:09 kartcq

It looks like we need some changes in both LoopVectorize legality and codegen; I will take a look unless you're already on it.

madhur13490 avatar Sep 21 '26 15:09 madhur13490

The pattern looks a bit common but is there a benchmark which demonstrates this?

madhur13490 avatar Sep 21 '26 15:09 madhur13490

It looks like we need some changes in both LoopVectorize legality and codegen; I will take a look unless you're already on it.

Yes, You can look into this.

kartcq avatar Sep 22 '26 08:09 kartcq

The pattern looks a bit common but is there a benchmark which demonstrates this?

This benchmark is derived out of TSVC s161

kartcq avatar Sep 22 '26 08:09 kartcq