Skip to content

Commit 0a5d52a

Browse files
authored
[RISCV][CostModel] Add getCFInstrCost RISC-V implementation (#65599)
This patch implements getCFInstrCost TTI hook that mostly affects LoopVectorizer decisions. It sets zero cost for PHI nodes and zero throughput cost for branches (assuming that branches are likely to be predicted). The implementation is similar to X86/AArch64/PowerPC targets and reduces loop cost by excluding induction PHIs/loop latch branches, which in turn leads to selecting smaller vectorization factor.
1 parent b4858c6 commit 0a5d52a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+707
-686
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,15 @@ InstructionCost RISCVTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
14371437
return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind, I);
14381438
}
14391439

1440+
InstructionCost RISCVTTIImpl::getCFInstrCost(unsigned Opcode,
1441+
TTI::TargetCostKind CostKind,
1442+
const Instruction *I) {
1443+
if (CostKind != TTI::TCK_RecipThroughput)
1444+
return Opcode == Instruction::PHI ? 0 : 1;
1445+
// Branches are assumed to be predicted.
1446+
return 0;
1447+
}
1448+
14401449
InstructionCost RISCVTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
14411450
TTI::TargetCostKind CostKind,
14421451
unsigned Index, Value *Op0,

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ class RISCVTTIImpl : public BasicTTIImplBase<RISCVTTIImpl> {
169169
TTI::TargetCostKind CostKind,
170170
const Instruction *I = nullptr);
171171

172+
InstructionCost getCFInstrCost(unsigned Opcode, TTI::TargetCostKind CostKind,
173+
const Instruction *I = nullptr);
174+
172175
using BaseT::getVectorInstrCost;
173176
InstructionCost getVectorInstrCost(unsigned Opcode, Type *Val,
174177
TTI::TargetCostKind CostKind,

llvm/test/Analysis/CostModel/RISCV/abs.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ define i32 @abs(i32 %arg) {
7575
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %30 = call <vscale x 16 x i8> @llvm.abs.nxv16i8(<vscale x 16 x i8> undef, i1 false)
7676
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %31 = call <vscale x 32 x i8> @llvm.abs.nxv32i8(<vscale x 32 x i8> undef, i1 false)
7777
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %32 = call <vscale x 64 x i8> @llvm.abs.nxv64i8(<vscale x 64 x i8> undef, i1 false)
78-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 undef
78+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 undef
7979
;
8080
call <2 x i64> @llvm.abs.v2i64(<2 x i64> undef, i1 false)
8181
call <4 x i64> @llvm.abs.v4i64(<4 x i64> undef, i1 false)
@@ -114,4 +114,4 @@ define i32 @abs(i32 %arg) {
114114
call <vscale x 64 x i8> @llvm.abs.nxv64i8(<vscale x 64 x i8> undef, i1 false)
115115

116116
ret i32 undef
117-
}
117+
}

llvm/test/Analysis/CostModel/RISCV/active_lane_mask.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ define void @get_lane_mask() {
2525
; CHECK-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %mask_v2i1_i32 = call <2 x i1> @llvm.get.active.lane.mask.v2i1.i32(i32 undef, i32 undef)
2626
; CHECK-NEXT: Cost Model: Found an estimated cost of 22 for instruction: %mask_v32i1_i64 = call <32 x i1> @llvm.get.active.lane.mask.v32i1.i64(i64 undef, i64 undef)
2727
; CHECK-NEXT: Cost Model: Found an estimated cost of 5 for instruction: %mask_v16i1_i16 = call <16 x i1> @llvm.get.active.lane.mask.v16i1.i16(i16 undef, i16 undef)
28-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void
28+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
2929
;
3030
%mask_nxv16i1_i64 = call <vscale x 16 x i1> @llvm.get.active.lane.mask.nxv16i1.i64(i64 undef, i64 undef)
3131
%mask_nxv8i1_i64 = call <vscale x 8 x i1> @llvm.get.active.lane.mask.nxv8i1.i64(i64 undef, i64 undef)

llvm/test/Analysis/CostModel/RISCV/arith-fp.ll

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ define i32 @fadd() {
3838
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %NXV2F64 = fadd <vscale x 2 x double> undef, undef
3939
; CHECK-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %NXV4F64 = fadd <vscale x 4 x double> undef, undef
4040
; CHECK-NEXT: Cost Model: Found an estimated cost of 8 for instruction: %NXV8F64 = fadd <vscale x 8 x double> undef, undef
41-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 undef
41+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 undef
4242
;
4343
%F16 = fadd half undef, undef
4444
%F32 = fadd float undef, undef
@@ -118,7 +118,7 @@ define i32 @fsub() {
118118
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %NXV2F64 = fsub <vscale x 2 x double> undef, undef
119119
; CHECK-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %NXV4F64 = fsub <vscale x 4 x double> undef, undef
120120
; CHECK-NEXT: Cost Model: Found an estimated cost of 8 for instruction: %NXV8F64 = fsub <vscale x 8 x double> undef, undef
121-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 undef
121+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 undef
122122
;
123123
%F16 = fsub half undef, undef
124124
%F32 = fsub float undef, undef
@@ -198,7 +198,7 @@ define i32 @fmul() {
198198
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %NXV2F64 = fmul <vscale x 2 x double> undef, undef
199199
; CHECK-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %NXV4F64 = fmul <vscale x 4 x double> undef, undef
200200
; CHECK-NEXT: Cost Model: Found an estimated cost of 8 for instruction: %NXV8F64 = fmul <vscale x 8 x double> undef, undef
201-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 undef
201+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 undef
202202
;
203203
%F16 = fmul half undef, undef
204204
%F32 = fmul float undef, undef
@@ -278,7 +278,7 @@ define i32 @fdiv() {
278278
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %NXV2F64 = fdiv <vscale x 2 x double> undef, undef
279279
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %NXV4F64 = fdiv <vscale x 4 x double> undef, undef
280280
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %NXV8F64 = fdiv <vscale x 8 x double> undef, undef
281-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 undef
281+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 undef
282282
;
283283
%F16 = fdiv half undef, undef
284284
%F32 = fdiv float undef, undef
@@ -358,7 +358,7 @@ define i32 @frem() {
358358
; CHECK-NEXT: Cost Model: Invalid cost for instruction: %NXV2F64 = frem <vscale x 2 x double> undef, undef
359359
; CHECK-NEXT: Cost Model: Invalid cost for instruction: %NXV4F64 = frem <vscale x 4 x double> undef, undef
360360
; CHECK-NEXT: Cost Model: Invalid cost for instruction: %NXV8F64 = frem <vscale x 8 x double> undef, undef
361-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 undef
361+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 undef
362362
;
363363
%F16 = frem half undef, undef
364364
%F32 = frem float undef, undef
@@ -438,7 +438,7 @@ define i32 @fneg() {
438438
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %NXV2F64 = fneg <vscale x 2 x double> undef
439439
; CHECK-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %NXV4F64 = fneg <vscale x 4 x double> undef
440440
; CHECK-NEXT: Cost Model: Found an estimated cost of 8 for instruction: %NXV8F64 = fneg <vscale x 8 x double> undef
441-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 undef
441+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 undef
442442
;
443443
%F16 = fneg half undef
444444
%F32 = fneg float undef
@@ -518,7 +518,7 @@ define i32 @fcopysign() {
518518
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %NXV2F64 = call <vscale x 2 x double> @llvm.copysign.nxv2f64(<vscale x 2 x double> undef, <vscale x 2 x double> undef)
519519
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %NXV4F64 = call <vscale x 4 x double> @llvm.copysign.nxv4f64(<vscale x 4 x double> undef, <vscale x 4 x double> undef)
520520
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %NXV8F64 = call <vscale x 8 x double> @llvm.copysign.nxv8f64(<vscale x 8 x double> undef, <vscale x 8 x double> undef)
521-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 undef
521+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 undef
522522
;
523523
%F16 = call half @llvm.copysign.f16(half undef, half undef)
524524
%F32 = call float @llvm.copysign.f32(float undef, float undef)
@@ -598,7 +598,7 @@ define i32 @fma() {
598598
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %NXV2F64 = call <vscale x 2 x double> @llvm.fma.nxv2f64(<vscale x 2 x double> undef, <vscale x 2 x double> undef, <vscale x 2 x double> undef)
599599
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %NXV4F64 = call <vscale x 4 x double> @llvm.fma.nxv4f64(<vscale x 4 x double> undef, <vscale x 4 x double> undef, <vscale x 4 x double> undef)
600600
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %NXV8F64 = call <vscale x 8 x double> @llvm.fma.nxv8f64(<vscale x 8 x double> undef, <vscale x 8 x double> undef, <vscale x 8 x double> undef)
601-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 undef
601+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 undef
602602
;
603603
%F16 = call half @llvm.fma.f16(half undef, half undef, half undef)
604604
%F32 = call float @llvm.fma.f32(float undef, float undef, float undef)
@@ -675,7 +675,7 @@ define void @fmuladd() {
675675
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %28 = call <vscale x 4 x double> @llvm.fmuladd.nxv4f64(<vscale x 4 x double> undef, <vscale x 4 x double> undef, <vscale x 4 x double> undef)
676676
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %29 = call <vscale x 8 x double> @llvm.fmuladd.nxv8f64(<vscale x 8 x double> undef, <vscale x 8 x double> undef, <vscale x 8 x double> undef)
677677
; CHECK-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %30 = call <vscale x 16 x double> @llvm.fmuladd.nxv16f64(<vscale x 16 x double> undef, <vscale x 16 x double> undef, <vscale x 16 x double> undef)
678-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void
678+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
679679
;
680680
call half @llvm.fmuladd.f16(half undef, half undef, half undef)
681681
call float @llvm.fmuladd.f32(float undef, float undef, float undef)

0 commit comments

Comments
 (0)