Skip to content

Commit 1a8691c

Browse files
committed
[VPlan] Compute cost for binary op VPInstruction with underlying values.
1 parent 675ba85 commit 1a8691c

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -972,10 +972,7 @@ class VPInstruction : public VPRecipeWithIRFlags,
972972

973973
/// Return the cost of this VPInstruction.
974974
InstructionCost computeCost(ElementCount VF,
975-
VPCostContext &Ctx) const override {
976-
// TODO: Compute accurate cost after retiring the legacy cost model.
977-
return 0;
978-
}
975+
VPCostContext &Ctx) const override;
979976

980977
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
981978
/// Print the VPInstruction to \p O.

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,27 @@ void VPInstruction::execute(VPTransformState &State) {
768768
/*IsScalar*/ GeneratesPerFirstLaneOnly);
769769
}
770770

771+
InstructionCost VPInstruction::computeCost(ElementCount VF,
772+
VPCostContext &Ctx) const {
773+
if (Instruction::isBinaryOp(getOpcode())) {
774+
if (!getUnderlyingValue())
775+
return 0;
776+
777+
assert(!doesGeneratePerAllLanes() &&
778+
"Should only generate a vector value or single scalar, not scalars "
779+
"for all lanes.");
780+
Type *ResTy = Ctx.Types.inferScalarType(this);
781+
if (!vputils::onlyFirstLaneUsed(this))
782+
ResTy = toVectorTy(ResTy, VF);
783+
784+
return Ctx.TTI.getArithmeticInstrCost(getOpcode(), ResTy, Ctx.CostKind);
785+
}
786+
787+
assert(!getUnderlyingValue() &&
788+
"unexpected VPInstruction without underlying value");
789+
return 0;
790+
}
791+
771792
bool VPInstruction::opcodeMayReadOrWriteFromMemory() const {
772793
if (Instruction::isBinaryOp(getOpcode()))
773794
return false;

llvm/test/Transforms/LoopVectorize/X86/CostModel/vpinstruction-cost.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ define void @wide_or_replaced_with_add_vpinstruction(ptr %src, ptr noalias %dst)
1818
; CHECK: Cost of 1 for VF 2: WIDEN ir<%l> = load vp<%5>
1919
; CHECK: Cost of 1 for VF 2: WIDEN ir<%iv.4> = add ir<%iv>, ir<4>
2020
; CHECK: Cost of 1 for VF 2: WIDEN ir<%c> = icmp ule ir<%l>, ir<128>
21-
; CHECK: Cost of 0 for VF 2: EMIT ir<%or> = add ir<%iv.4>, ir<1>
21+
; CHECK: Cost of 1 for VF 2: EMIT ir<%or> = add ir<%iv.4>, ir<1>
2222
; CHECK: Cost of 0 for VF 2: CLONE ir<%g.dst> = getelementptr ir<%dst>, ir<%or>
2323
; CHECK: Cost of 0 for VF 2: vp<%6> = vector-pointer ir<%g.dst>
2424
; CHECK: Cost of 1 for VF 2: WIDEN store vp<%6>, ir<%iv.4>, ir<%c>
@@ -36,7 +36,7 @@ define void @wide_or_replaced_with_add_vpinstruction(ptr %src, ptr noalias %dst)
3636
; CHECK: Cost of 1 for VF 4: WIDEN ir<%l> = load vp<%5>
3737
; CHECK: Cost of 1 for VF 4: WIDEN ir<%iv.4> = add ir<%iv>, ir<4>
3838
; CHECK: Cost of 1 for VF 4: WIDEN ir<%c> = icmp ule ir<%l>, ir<128>
39-
; CHECK: Cost of 0 for VF 4: EMIT ir<%or> = add ir<%iv.4>, ir<1>
39+
; CHECK: Cost of 1 for VF 4: EMIT ir<%or> = add ir<%iv.4>, ir<1>
4040
; CHECK: Cost of 0 for VF 4: CLONE ir<%g.dst> = getelementptr ir<%dst>, ir<%or>
4141
; CHECK: Cost of 0 for VF 4: vp<%6> = vector-pointer ir<%g.dst>
4242
; CHECK: Cost of 1 for VF 4: WIDEN store vp<%6>, ir<%iv.4>, ir<%c>

0 commit comments

Comments
 (0)