[SLP] Recent copyable fadds in fmuladd commit causing unprofitable vectorization in distray
I noticed this 4% regression on the spacemit x100 from this benchmark: https://cc-perf.igalia.com/db_default/v4/nts/profile/86/844/838
I have attached images of the relevant basic block below.
After:
Before:
I believe I have traced this regression to this specific commit https://github.com/llvm/llvm-project/pull/213786. I have pasted the diff of the IR from the SLP vectorizer before and on this commit below.
diff --git a/body98-pre.ll b/body98-post.ll
index 3c9846b..226d475 100644
--- a/body98-pre.ll
+++ b/body98-post.ll
@@ -2,90 +2,90 @@ for.body98.lr.ph: ; preds = %for.cond95.preheade
%77 = tail call double @llvm.fabs.f64(double %74)
%cmp.i253 = fcmp ogt double %77, 1.000000e-05
%fneg26.i256 = fneg double %72
- %mul.i298 = fmul double %74, %73
%mul4.i300 = fmul double %74, %fneg26.i256
%mul9.i301 = fmul double %74, %mul4.i300
- %fneg12.i302 = fneg double %mul.i298
- %mul14.i303 = fmul double %74, %fneg12.i302
%neg.i304 = fmul double %mul4.i300, %fneg26.i256
- %78 = tail call double @llvm.fmuladd.f64(double %mul.i298, double %73, double %neg.i304)
%mul3.i.i262 = fmul double %73, %73
- %79 = tail call double @llvm.fmuladd.f64(double %72, double %72, double %mul3.i.i262)
- %80 = tail call double @llvm.fmuladd.f64(double %74, double %74, double %79)
- %sqrt.i.i263 = tail call double @llvm.sqrt.f64(double %80)
+ %78 = tail call double @llvm.fmuladd.f64(double %72, double %72, double %mul3.i.i262)
+ %79 = tail call double @llvm.fmuladd.f64(double %74, double %74, double %78)
+ %sqrt.i.i263 = tail call double @llvm.sqrt.f64(double %79)
%y105 = getelementptr inbounds nuw i8, ptr %NewDir2, i64 8
- %z108 = getelementptr inbounds nuw i8, ptr %NewDir2, i64 16
%sub110 = add nsw i32 %reccount, -1
%y114 = getelementptr inbounds nuw i8, ptr %TmpCol2, i64 8
%z117 = getelementptr inbounds nuw i8, ptr %TmpCol2, i64 16
%mul4.i300.fneg26.i256 = select i1 %cmp.i253, double %mul4.i300, double %fneg26.i256
+ %mul3.i67.i264 = fmul double %mul4.i300.fneg26.i256, %mul4.i300.fneg26.i256
+ %mul.i298 = fmul double %74, %73
+ %fneg12.i302 = fneg double %mul.i298
+ %mul14.i303 = fmul double %74, %fneg12.i302
+ %80 = tail call double @llvm.fmuladd.f64(double %mul.i298, double %73, double %neg.i304)
%mul.i298. = select i1 %cmp.i253, double %mul.i298, double %73
- %.330 = select i1 %cmp.i253, double %78, double 1.000000e+00
+ %.330 = select i1 %cmp.i253, double %80, double 1.000000e+00
%mul14.i303. = select i1 %cmp.i253, double %mul14.i303, double 0.000000e+00
%mul9.i301. = select i1 %cmp.i253, double %mul9.i301, double 0.000000e+00
- %mul3.i67.i264 = fmul double %mul4.i300.fneg26.i256, %mul4.i300.fneg26.i256
- %81 = tail call double @llvm.fmuladd.f64(double %mul.i298., double %mul.i298., double %mul3.i67.i264)
- %82 = fadd double %81, 0.000000e+00
- %sqrt.i69.i265 = tail call double @llvm.sqrt.f64(double %82)
- %div.i266 = fdiv double %sqrt.i.i263, %sqrt.i69.i265
%mul3.i74.i278 = fmul double %mul14.i303., %mul14.i303.
- %83 = tail call double @llvm.fmuladd.f64(double %mul9.i301., double %mul9.i301., double %mul3.i74.i278)
- %84 = tail call double @llvm.fmuladd.f64(double %.330, double %.330, double %83)
- %sqrt.i76.i279 = tail call double @llvm.sqrt.f64(double %84)
- %div37.i280 = fdiv double %sqrt.i.i263, %sqrt.i76.i279
+ %81 = tail call double @llvm.fmuladd.f64(double %mul9.i301., double %mul9.i301., double %mul3.i74.i278)
+ %82 = tail call double @llvm.fmuladd.f64(double %mul.i298., double %mul.i298., double %mul3.i67.i264)
+ %83 = tail call double @llvm.fmuladd.f64(double %.330, double %.330, double %81)
+ %84 = fadd double %82, 0.000000e+00
+ %85 = insertelement <2 x double> poison, double %83, i64 0
+ %86 = insertelement <2 x double> %85, double %84, i64 1
+ %87 = call <2 x double> @llvm.sqrt.v2f64(<2 x double> %86)
+ %88 = insertelement <2 x double> poison, double %sqrt.i.i263, i64 0
+ %89 = shufflevector <2 x double> %88, <2 x double> poison, <2 x i32> zeroinitializer
+ %90 = fdiv <2 x double> %89, %87
+ %91 = insertelement <2 x double> <double poison, double 0.000000e+00>, double %mul14.i303., i64 0
+ %92 = insertelement <2 x double> poison, double %.330, i64 0
+ %93 = insertelement <2 x double> %92, double %mul4.i300.fneg26.i256, i64 1
br label %for.body98
for.body98: ; preds = %for.body98.lr.ph, %for.body98
%i.1325 = phi i32 [ 0, %for.body98.lr.ph ], [ %inc121, %for.body98 ]
- %85 = phi double [ 0.000000e+00, %for.body98.lr.ph ], [ %add113, %for.body98 ]
+ %94 = phi double [ 0.000000e+00, %for.body98.lr.ph ], [ %add113, %for.body98 ]
%add116320324 = phi double [ 0.000000e+00, %for.body98.lr.ph ], [ %add116, %for.body98 ]
%add119321323 = phi double [ 0.000000e+00, %for.body98.lr.ph ], [ %add119, %for.body98 ]
- %86 = load double, ptr %roughness, align 8, !tbaa !45
- %mul33.i267 = fmul double %86, %div.i266
- %87 = load i64, ptr @rnd, align 8, !tbaa !26
- %mul.i.i268 = mul i64 %87, 1103515245
+ %95 = load double, ptr %roughness, align 8, !tbaa !45
+ %96 = load i64, ptr @rnd, align 8, !tbaa !26
+ %mul.i.i268 = mul i64 %96, 1103515245
+ call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %NewDir2, ptr noundef nonnull align 8 dereferenceable(24) %NewDir, i64 24, i1 false), !tbaa.struct !36
+ %97 = load double, ptr %NewDir2, align 8, !tbaa !22
%add.i.i269 = add i64 %mul.i.i268, 12345
- %and.i.i270 = and i64 %add.i.i269, 2147483647
- %conv.i.i271 = uitofp nneg i64 %and.i.i270 to double
- %div.i.i272 = fdiv nnan double %conv.i.i271, f0x41CFFFFFFF800000
- %sub.i.i273 = fsub nnan double 1.000000e+00, %div.i.i272
- %mul35.i274 = fmul double %mul33.i267, %sub.i.i273
- %mul.i70.i275 = fmul double %mul.i298., %mul35.i274
- %mul1.i.i276 = fmul double %mul4.i300.fneg26.i256, %mul35.i274
- %mul2.i.i277 = fmul double %mul35.i274, 0.000000e+00
- %mul38.i281 = fmul double %86, %div37.i280
+ %98 = insertelement <2 x double> poison, double %95, i64 0
+ %99 = shufflevector <2 x double> %98, <2 x double> poison, <2 x i32> zeroinitializer
+ %100 = fmul <2 x double> %99, %90
%mul.i77.i282 = mul i64 %add.i.i269, 1103515245
%add.i78.i283 = add i64 %mul.i77.i282, 12345
+ %101 = insertelement <2 x i64> poison, i64 %add.i78.i283, i64 0
+ %102 = insertelement <2 x i64> %101, i64 %add.i.i269, i64 1
+ %103 = and <2 x i64> %102, splat (i64 2147483647)
%and.i79.i284 = and i64 %add.i78.i283, 2147483647
+ %104 = uitofp <2 x i64> %103 to <2 x double>
+ %105 = fdiv nnan <2 x double> %104, splat (double f0x41CFFFFFFF800000)
+ %106 = fsub nnan <2 x double> splat (double 1.000000e+00), %105
+ %107 = fmul <2 x double> %100, %106
+ %108 = extractelement <2 x double> %107, i64 1
+ %mul.i70.i275 = fmul double %mul.i298., %108
store i64 %and.i79.i284, ptr @rnd, align 8, !tbaa !26
- %conv.i80.i285 = uitofp nneg i64 %and.i79.i284 to double
- %div.i81.i286 = fdiv nnan double %conv.i80.i285, f0x41CFFFFFFF800000
- %sub.i82.i287 = fsub nnan double 1.000000e+00, %div.i81.i286
- %mul40.i288 = fmul double %mul38.i281, %sub.i82.i287
- %mul.i83.i289 = fmul double %mul9.i301., %mul40.i288
- %mul1.i85.i290 = fmul double %mul14.i303., %mul40.i288
- %mul2.i87.i291 = fmul double %.330, %mul40.i288
+ %109 = extractelement <2 x double> %107, i64 0
+ %mul.i83.i289 = fmul double %mul9.i301., %109
+ %110 = fmul <2 x double> %91, %107
+ %111 = fmul <2 x double> %93, %107
+ %112 = shufflevector <2 x double> %111, <2 x double> poison, <2 x i32> <i32 1, i32 0>
%add.i292 = fadd double %mul.i70.i275, %mul.i83.i289
- %add46.i293 = fadd double %mul1.i.i276, %mul1.i85.i290
- %add50.i295 = fadd double %mul2.i.i277, %mul2.i87.i291
- call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %NewDir2, ptr noundef nonnull align 8 dereferenceable(24) %NewDir, i64 24, i1 false), !tbaa.struct !36
- %88 = load double, ptr %NewDir2, align 8, !tbaa !22
- %add103 = fadd double %88, %add.i292
+ %113 = fadd <2 x double> %110, %112
+ %add103 = fadd double %97, %add.i292
store double %add103, ptr %NewDir2, align 8, !tbaa !22
- %89 = load double, ptr %y105, align 8, !tbaa !28
- %add106 = fadd double %89, %add46.i293
- store double %add106, ptr %y105, align 8, !tbaa !28
- %90 = load double, ptr %z108, align 8, !tbaa !29
- %add109 = fadd double %90, %add50.i295
- store double %add109, ptr %z108, align 8, !tbaa !29
+ %114 = load <2 x double>, ptr %y105, align 8, !tbaa !25
+ %115 = fadd <2 x double> %114, %113
+ store <2 x double> %115, ptr %y105, align 8, !tbaa !25
call fastcc void @TraceLine(ptr noundef nonnull %Pnt, ptr noundef %NewDir2, ptr noundef %TmpCol2, i32 noundef signext %sub110)
- %91 = load double, ptr %TmpCol2, align 8, !tbaa !22
- %add113 = fadd double %91, %85
- %92 = load double, ptr %y114, align 8, !tbaa !28
- %add116 = fadd double %92, %add116320324
- %93 = load double, ptr %z117, align 8, !tbaa !29
- %add119 = fadd double %93, %add119321323
+ %116 = load double, ptr %TmpCol2, align 8, !tbaa !22
+ %add113 = fadd double %116, %94
+ %117 = load double, ptr %y114, align 8, !tbaa !28
+ %add116 = fadd double %117, %add116320324
+ %118 = load double, ptr %z117, align 8, !tbaa !29
+ %add119 = fadd double %118, %add119321323
%inc121 = add nuw nsw i32 %i.1325, 1
- %94 = load i32, ptr @DISTRIB, align 4, !tbaa !20
- %cmp96 = icmp slt i32 %inc121, %94
+ %119 = load i32, ptr @DISTRIB, align 4, !tbaa !20
+ %cmp96 = icmp slt i32 %inc121, %119
br i1 %cmp96, label %for.body98, label %for.end122, !llvm.loop !46
cc @alexey-bataev
Still investigating the logs but it seems that the Spill Cost may be too cheap here?
Scale 2 For entry 34
SLP: Spill Cost = 2.
SLP: Extract Cost = 8.
SLP: Total Cost = -12.
SLP: Found cost = -12 for VF=2
SLP: Decided to vectorize cost = -12
My tentative understanding so far is as there is a recursive call to TraceLine in the looping basic block for.body98 and v8 does not get preserved across calls, the value of v8 must be stored onto the stack in the for.body98.lr.ph, and loaded in for.body98.
There are five values produced in for.body98.lr.ph that are being consumed in for.body98, hence the five vs1r.v/vl1r.v instructions.
Hey @Alexey-bataev, the overall benchmark is no longer regressing but the corresponding basic blocks are stlil being vectorized, hence the stack spill instructions, although there are now less stack instructions. I have included relevant photos of the new code, which you can compare to the scalar version above.
https://cc-perf.igalia.com/db_default/v4/nts/profile/86/937/724
I think I may have traced why this is still getting vectorized. My suspicion is that the Spill Cost here is being undercounted.
SLP: Spill Cost = 6.
SLP: Extract Cost = 12.
SLP: Total Cost = -4.
SLP: Found cost = -4 for VF=2
SLP: Decided to vectorize cost = -4
I noticed that, for example, this instruction
mul33.i267 = fmul double %61, %div.i266
The IR above triggers this code:
InstructionCost BoUpSLP::getSpillCost() {
...
// 3. Check the predecessors of entry's block till op's block.
if (!CheckPredecessors(Parent, Pred, OpParent)) {
AddCosts(Op);
continue;
}
getScaleToLoopIterations called by AddCosts returns a Scale = 1. Since U is not provided, and this entry is not a Gather node nor a SplitVectorize, Parent in getScaleToLoopIterations resolve to Op's own parent block, for.body98.lr.ph. This is the pre-header, so getScaleToLoopIterations would immediately return 1.
Is my logic sound, shouldn't this function refer to loop.body98 for loop info? I have a patch that modifies AddCost to include an optional U to pass in LastInst as the U parameter to getScaleToLoopIterations, so that Parent can resolve to for.body98.
I noticed this still doesn't fix the issue immediately. findInnermostNonInvariantLoop would call getLoopNestScale with a null value because the operands are loop-invariant. I addressed this by calling getLoopNestScale directly in AddCosts if U is provided. This returns a Scale value of 2, the fall back value since the upper-bound value of this loop in the source code is a run-time value.
Overall, these changes do not cause vectorization in the block, keeping the scalar code as before.
SLP: Spill Cost = 12.
SLP: Extract Cost = 0.
SLP: Total Cost = 8.
SLP: Found cost = 8 for VF=2
Would like to try to fix this if it seems a valid issue.
Hey @alexey-bataev, the overall benchmark is no longer regressing but the corresponding basic blocks are stlil being vectorized, hence the stack spill instructions, although there are now less stack instructions. I have included relevant photos of the new code, which you can compare to the scalar version above.
https://cc-perf.igalia.com/db_default/v4/nts/profile/86/937/724
I think I may have traced why this is still getting vectorized. My suspicion is that the Spill Cost here is being undercounted.
SLP: Spill Cost = 6. SLP: Extract Cost = 12. SLP: Total Cost = -4. SLP: Found cost = -4 for VF=2 SLP: Decided to vectorize cost = -4I noticed that, for example, this instruction
mul33.i267 = fmul double %61, %div.i266 The IR above triggers this code:
InstructionCost BoUpSLP::getSpillCost() {
... // 3. Check the predecessors of entry's block till op's block. if (!CheckPredecessors(Parent, Pred, OpParent)) { AddCosts(Op); continue; }
getScaleToLoopIterationscalled byAddCostsreturns a Scale = 1. SinceUis not provided, and this entry is not a Gather node nor a SplitVectorize,ParentingetScaleToLoopIterationsresolve toOp's own parent block,for.body98.lr.ph. This is the pre-header, sogetScaleToLoopIterationswould immediately return 1.Is my logic sound, shouldn't this function refer to
loop.body98for loop info? I have a patch that modifiesAddCostto include an optionalUto pass inLastInstas theUparameter togetScaleToLoopIterations, so thatParentcan resolve tofor.body98.I noticed this still doesn't fix the issue immediately.
findInnermostNonInvariantLoopwould callgetLoopNestScalewith a null value because the operands are loop-invariant. I addressed this by callinggetLoopNestScaledirectly inAddCostsifUis provided. This returns aScalevalue of 2, the fall back value since the upper-bound value of this loop in the source code is a run-time value.Overall, these changes do not cause vectorization in the block, keeping the scalar code as before.
SLP: Spill Cost = 12. SLP: Extract Cost = 0. SLP: Total Cost = 8. SLP: Found cost = 8 for VF=2Would like to try to fix this if it seems a valid issue.
If it is not regressing, what shall be fixed here?
Isn't the scalar version of the basic block (prior to this commit) cheaper than with the vectorized version ? The benchmark improved (as in there's less cycles) but this seems to be mainly due to other basic blocks consuming less cycles from a different commit. With the SLP vectorized codegen, the header + loop body sums up to 54.47 M cycles while the scalar code, the same basic blocks produces 32.6 M cycles.
I think I may have traced why this is still getting vectorized. My suspicion is that the Spill Cost here is being undercounted.