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

Pragma `vectorize(disable)` could not avoid vectorization when `-scalable-vectorization=on`

Open yetingk opened this issue 4 years ago • 6 comments

The loop will be vectorized with command clang -target riscv64 -Ofast -S -march=rv64gcv -mllvm -scalable-vectorization=on.

void foo(const int N, const float *restrict a, float *restrict b) {
#pragma clang loop vectorize(disable)
  for (int i = 0; i < N; ++i) {
    b[i] = a[i] + 1;
  }
}

Since vectorize(disable) mark VF = 1 to the loop, but -scalable-vectorization=on make VF = 1 x vscale. The -scalable-vectorization= seems stronger than pragma hints. Does vectorize(disable) only work without -scalable-vectorization=on?

yetingk avatar Jul 10 '22 01:07 yetingk

@preames @sdesmalen-arm

topperc avatar Jul 10 '22 02:07 topperc

This sounds like a bug. @david-arm @sdesmalen-arm what do you think?

fhahn avatar Jul 10 '22 02:07 fhahn

I have an idea that we could make -scalable-vectorization= only modify Scalable hint if Scalable hint is SK_Unspecified. And we could generate vectorize(disable) to vectorize_width(1, fixed).

yetingk avatar Jul 11 '22 06:07 yetingk

I have an idea that we could make -scalable-vectorization= only modify Scalable hint if Scalable hint is SK_Unspecified. And we could generate vectorize(disable) to vectorize_width(1, fixed).

Address my comment by the revision https://reviews.llvm.org/D129473.

yetingk avatar Jul 11 '22 08:07 yetingk

There is definitely a bug here, but it's a bit nuanced.

At the moment the LoopVectorizer seems to be doing the right thing for the IR that it is given. It's asked to vectorize a loop for a llvm.loop.vectorize.width = 1. When there's no llvm.loop.vectorize.scalable value specified, it takes the scalable value instead from the target's default behaviour (TTI) or from a flag that forces it, i.e. -scalable-vectorization=<value>. For the reproducer here, it means it will choose VF=vscale x 1 as the vectorization factor.

Clang should have set llvm.loop.vectorize.scalable to false instead of leaving it unspecified, but that still won't fix the issue reported here because the -mllvm -scalable-vectorization=on overrides the scalable property from the loop hints.

Another approach would be for Clang to set llvm.loop.vectorize.enable to false, but that disables the loop vectorizer entirely including for interleaving (if that is requested through a hint), which is probably why it sets the VF to 1 rather than setting the enable flag to false. That leads me to think that setting the vectorization factor to 1 isn't sufficient to disable vectorization. We want to set the enable flag to false, but still allow the LV to do interleaving when that is requested.

sdesmalen-arm avatar Jul 11 '22 10:07 sdesmalen-arm

I have an idea, but I am not sure if it is right. Add a warning that vectorize(disable) and -scalable-vectorization=on cannot be used at the same time.

ChunyuLiao avatar Jul 12 '22 04:07 ChunyuLiao