llvm / llvm/llvm-project

[SimplifyCFG] Sinking of stores accesses affects vectorization

Open
#222,516 3 comments 0 reactions 0 assignees View on GitHub
llvm:transforms missed-optimization vectorizers
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

SimplifyCFG may sink conditional stores to different base pointers into a common block, replacing them with a single store through a pointer PHI. This hides consecutive memory accesses from the loop vectorizer.

#### Reproducer

```c
// issue.c
void foo(int *A, int *B, int *C, int *X, int *Y, int *Z, unsigned len) {
for (unsigned i = 0; i < len; i++)
if (B[i] > 0)
A[i] = B[i] + C[i];
else
X[i] = Y[i] * Z[i];
}
```

```
clang issue.c -O2 -fno-unroll-loops -march=znver5 -emit-llvm -S -o -
clang issue.c -O2 -fno-unroll-loops -mavx2 -S -o -
```

SimplifyCFG merges the two stores through a pointer PHI on both targets:

```llvm
%A.sink = phi ptr [ %A, %if.then ], [ %X, %if.else ]
%arrayidx7 = getelementptr inbounds nuw [4 x i8], ptr %A.sink, i64 %indvars.iv
store i32 %add.sink, ptr %arrayidx7, align 4
```

AVX-512: the vectorizer generates a costly llvm.masked.scatter instead of two efficient llvm.masked.store operations.
AVX2: since scatter is unavailable, the vectorizer scalarizes the store (REPLICATE), producing multiple scalar stores instead of masked vector stores.

Stores to the same address are not affected, as they only create a value PHI and still vectorize as a single vector store.

As a result, sinking stores before loop vectorization can significantly increase vectorization cost and generate less efficient code.

Contributor guide

Open the contributing guide

Research direction

Reproduce the behavior with issue.c using the two provided clang commands, then inspect the SimplifyCFG store-sinking path and the loop-vectorizer output. Done means conditional stores to different base pointers no longer become a pointer-PHI store that causes an unnecessary masked.scatter or scalarized stores, while the same-address case remains unaffected.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.