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

Potential improvement for Loop Vectorization: XMM vs YMM vs ZMM

Open serguei-katkov opened this issue 9 years ago • 10 comments

Bugzilla Link 35299
Resolution FIXED
Resolved on Nov 16, 2017 00:20
Version trunk
OS Windows NT
CC @annamthomas,@topperc,@hfinkel,@RKSimon,@rotateright

Extended Description

What is motivation to choose the lowest Vectorization Factor for equal vector cost?

Specifically in LoopVectorizationCostModel::selectVectorizationFactor(unsigned MaxVF): for (unsigned i = 2; i <= MaxVF; i *= 2) { ... if (VectorCost < Cost) { Cost = VectorCost; Width = i; } } ... VectorizationFactor Factor = {Width, (unsigned)(Width * Cost)}; return Factor;

So we update vectorization factor to higher value only if the cost is strongly less than previous computed cost.

So if the computed vectorization cost is equal for XMM, YMM and ZMM we always will use XMM.

So my question is why it is done on this way? why not to choose the maximum VF for equal cost?

serguei-katkov avatar Nov 14 '17 11:11 serguei-katkov

A couple of days happened pretty fast :) Verified!

Sanjay, Craig, thank you very much with quick resolution!

serguei-katkov avatar Nov 16 '17 08:11 serguei-katkov

I think so, let me a couple of days and verify whether it solves my original issue.

serguei-katkov avatar Nov 16 '17 03:11 serguei-katkov

After: https://reviews.llvm.org/rL318323

We have: $ ./opt -O2 35299.ll -S |grep mul %14 = mul <16 x i16> %wide.load, %broadcast.splat8 %15 = mul <16 x i16> %wide.load4, %broadcast.splat10 %16 = mul <16 x i16> %wide.load5, %broadcast.splat12 ...

$ ./opt -O2 35299.ll -S | ./llc -o - | grep mull vpmullw (%rdi,%rcx,2), %ymm0, %ymm1 vpmullw 32(%rdi,%rcx,2), %ymm0, %ymm2 vpmullw 64(%rdi,%rcx,2), %ymm0, %ymm3 ...

I think that's optimal for the target as specified in comment 2.

Resolve this bug?

rotateright avatar Nov 15 '17 19:11 rotateright

We miss all of these: https://rise4fun.com/Alive/4V https://rise4fun.com/Alive/Sgh

...but it's an easy addition to an existing InstCombine transform. Patch coming up.

rotateright avatar Nov 15 '17 17:11 rotateright

Could we combine them in InstCombine instead while they are still scalars? We should be able to do it based on Datalayout right?

Sorry, missed this comment before I sent my last one. Yep, let me try to patch this in InstCombine.

rotateright avatar Nov 15 '17 16:11 rotateright

I guess the real question / root cause is here:

%x32 = sext i16 %x16 to i32 %m = mul i32 %x32, %y32 %r = trunc i32 %m to i16 => %y16 = trunc i32 %y32 to i16 %r = mul i16 %x16, %y16

https://rise4fun.com/Alive/gZZ

rotateright avatar Nov 15 '17 16:11 rotateright

Could we combine them in InstCombine instead while they are still scalars? We should be able to do it based on Datalayout right?

topperc avatar Nov 15 '17 16:11 topperc

I agree that the cost calculation is at fault, but I'm not sure how to relay the information that the sext/trunc can be removed by the backend up to the cost model.

Simon (or any other cc's), have you seen this kind of case before?

rotateright avatar Nov 15 '17 16:11 rotateright

Hi Sanjay, thank you very much for pointer. It makes sense.

Then I tend to think that I have a problem with cost computation. I was able to reduce the reproducer:

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128-ni:1" target triple = "x86_64-unknown-linux-gnu"

define i1 @​test(i16* %arr, i32 %k, i64 %N) #​1 { entry: br label %loop loop: %iv = phi i64 [ %iv.next, %loop ], [ 0, %entry ] %gep = getelementptr inbounds i16, i16* %arr, i64 %iv %v = load i16, i16* %gep, align 2 %v.ext = sext i16 %v to i32 %res = mul i32 %v.ext, %k %res.t = trunc i32 %res to i16 store i16 %res.t, i16* %gep, align 2 %iv.next = add nuw nsw i64 %iv, 1 %cmp = icmp ult i64 %iv.next, %N br i1 %cmp, label %loop, label %end end: ret i1 0 }

attributes #​1 = { "target-cpu"="skylake" "target-features"="+sse2,+cx16,-tbm,-avx512ifma,-avx512dq,-fma4,+prfchw,+bmi2,+xsavec,+fsgsbase,+popcnt,+aes,+xsaves,-avx512er,-avx512vpopcntdq,-clwb,-avx512f,-clzero,-pku,+mmx,-lwp,-xop,+rdseed,-sse4a,-avx512bw,+clflushopt,+xsave,-avx512vl,-avx512cd,+avx,+rtm,+fma,+bmi,+rdrnd,-mwaitx,+sse4.1,+sse4.2,+avx2,+sse,+lzcnt,+pclmul,-prefetchwt1,+f16c,+ssse3,+sgx,+cmov,-avx512vbmi,+movbe,+xsaveopt,-sha,+adx,-avx512pf,+sse3" }

Running loop vectorizer $ opt --loop-vectorize test.ll --debug-only=loop-vectorize -S -o test.opt.ll we see: LV: Found an estimated cost of 0 for VF 8 For instruction: %iv = phi i64 [ %iv.next, %loop ], [ 0, %entry ] LV: Found an estimated cost of 0 for VF 8 For instruction: %gep = getelementptr inbounds i16, i16* %arr, i64 %iv LV: Found an estimated cost of 1 for VF 8 For instruction: %v = load i16, i16* %gep, align 2 LV: Found an estimated cost of 1 for VF 8 For instruction: %v.ext = sext i16 %v to i32 LV: Found an estimated cost of 1 for VF 8 For instruction: %res = mul i32 %v.ext, %k LV: Found an estimated cost of 2 for VF 8 For instruction: %res.t = trunc i32 %res to i16 LV: Found an estimated cost of 1 for VF 8 For instruction: store i16 %res.t, i16* %gep, align 2 LV: Found an estimated cost of 1 for VF 8 For instruction: %iv.next = add nuw nsw i64 %iv, 1 LV: Found an estimated cost of 1 for VF 8 For instruction: %cmp = icmp ult i64 %iv.next, %N LV: Found an estimated cost of 0 for VF 8 For instruction: br i1 %cmp, label %loop, label %end LV: Vector loop of width 8 costs: 1. LV: Found an estimated cost of 0 for VF 16 For instruction: %iv = phi i64 [ %iv.next, %loop ], [ 0, %entry ] LV: Found an estimated cost of 0 for VF 16 For instruction: %gep = getelementptr inbounds i16, i16* %arr, i64 %iv LV: Found an estimated cost of 1 for VF 16 For instruction: %v = load i16, i16* %gep, align 2 LV: Found an estimated cost of 4 for VF 16 For instruction: %v.ext = sext i16 %v to i32 LV: Found an estimated cost of 2 for VF 16 For instruction: %res = mul i32 %v.ext, %k LV: Found an estimated cost of 6 for VF 16 For instruction: %res.t = trunc i32 %res to i16 LV: Found an estimated cost of 1 for VF 16 For instruction: store i16 %res.t, i16* %gep, align 2 LV: Found an estimated cost of 1 for VF 16 For instruction: %iv.next = add nuw nsw i64 %iv, 1 LV: Found an estimated cost of 1 for VF 16 For instruction: %cmp = icmp ult i64 %iv.next, %N LV: Found an estimated cost of 0 for VF 16 For instruction: br i1 %cmp, label %loop, label %end LV: Vector loop of width 16 costs: 1.

that loop cost for VF=8 and VF=16 is the same = 1.

However if we look into generated assembler: $ llc test.opt.ll .LBB0_3: # %vector.body # =>This Inner Loop Header: Depth=1 vpmullw (%rdi,%rcx,2), %xmm0, %xmm1 vpmullw 16(%rdi,%rcx,2), %xmm0, %xmm2 vpmullw 32(%rdi,%rcx,2), %xmm0, %xmm3 vpmullw 48(%rdi,%rcx,2), %xmm0, %xmm4 vmovdqu %xmm1, (%rdi,%rcx,2) vmovdqu %xmm2, 16(%rdi,%rcx,2) vmovdqu %xmm3, 32(%rdi,%rcx,2) vmovdqu %xmm4, 48(%rdi,%rcx,2) addq $32, %rcx cmpq %rcx, %rax jne .LBB0_3

That just changing xmm to ymm we can process more elements per iteration without any additional cost.

Returning back to output of loop-vectorizer we can notice that: For VF=8: LV: Found an estimated cost of 1 for VF 8 For instruction: %v.ext = sext i16 %v to i32 LV: Found an estimated cost of 2 for VF 8 For instruction: %res.t = trunc i32 %res to i16

For VF=16: LV: Found an estimated cost of 4 for VF 16 For instruction: %v.ext = sext i16 %v to i32 LV: Found an estimated cost of 6 for VF 16 For instruction: %res.t = trunc i32 %res to i16

However as I understand sext and trunc are eliminated in combineTruncatedArithmetic and during computation of the vectorization cost this has not been taken into account.

However if we would just suggest that the cost of this two instruction is zero then cost for VF=16 would be less than cost of VF=8 and vectorizer would choose VF=16 causing generation of ymm.

Do I miss anything?

serguei-katkov avatar Nov 15 '17 08:11 serguei-katkov

I don't know if the vectorizer is choosing the narrower width purposely or if that was an unintended side effect.

But one possible reason for choosing narrower ops if they have the same theoretical cost is discussed in a current llvm-dev thread: http://lists.llvm.org/pipermail/llvm-dev/2017-November/118734.html

So there is a potentially unaccounted cost when using wider ops: power. So all (theoretical cost model) things being equal, we probably should choose the narrowest ops that get the job done.

If you have a case where this is causing a perf problem, please post an example.

rotateright avatar Nov 14 '17 21:11 rotateright