[llvm] [RISCV][CostModel] Remove inst cost of cmp inst in cmp-select sequence. (PR #91158)
Elvis Wang via llvm-commits
llvm-commits at lists.llvm.org
Sun May 5 20:33:01 PDT 2024
https://github.com/ElvisWang123 updated https://github.com/llvm/llvm-project/pull/91158
>From c577ebd493726717f407a77c15df523263d2d32d Mon Sep 17 00:00:00 2001
From: Elvis Wang <elvis.wang at sifive.com>
Date: Sun, 5 May 2024 18:23:14 -0700
Subject: [PATCH 1/2] Pre-commit test case for instruction cost of cmp instr
(nfc)
---
llvm/test/Analysis/CostModel/RISCV/cmp-select.ll | 13 +++++++++++++
1 file changed, 13 insertions(+)
create mode 100644 llvm/test/Analysis/CostModel/RISCV/cmp-select.ll
diff --git a/llvm/test/Analysis/CostModel/RISCV/cmp-select.ll b/llvm/test/Analysis/CostModel/RISCV/cmp-select.ll
new file mode 100644
index 00000000000000..d50bd824bcfac6
--- /dev/null
+++ b/llvm/test/Analysis/CostModel/RISCV/cmp-select.ll
@@ -0,0 +1,13 @@
+; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py
+; RUN: opt < %s -mtriple=riscv64 -passes="print<cost-model>" -cost-kind=throughput 2>&1 -disable-output | FileCheck %s --check-prefixes=CHECK
+
+define void @cmp-select() {
+; CHECK-LABEL: 'cmp-select'
+; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %cmp1 = icmp slt i64 0, 1
+; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %select1 = select i1 %cmp1, i32 5, i32 4
+; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
+;
+ %cmp1 = icmp slt i64 0, 1
+ %select1 = select i1 %cmp1, i32 5, i32 4
+ ret void
+}
>From f7da97d17b49c648337620250fb3b841dfe5434e Mon Sep 17 00:00:00 2001
From: Elvis Wang <elvis.wang at sifive.com>
Date: Sun, 5 May 2024 18:28:24 -0700
Subject: [PATCH 2/2] [RISCV][CostModel] Remove inst cost of cmp inst in
cmp-select sequence.
Scalar ICmp and scalar Select instructinos will lower to SELECT_CC and lower to PseudoCCMOVGPR
which will generate a conditional branch instr and a move instr.
The cost of scalar (ICmp + Select) = (0 + Select instr cost).
---
.../Target/RISCV/RISCVTargetTransformInfo.cpp | 9 +++++++++
.../test/Analysis/CostModel/RISCV/cmp-select.ll | 17 ++++++++++++++---
.../LoopVectorize/RISCV/strided-accesses.ll | 2 +-
3 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
index 5f84175da703d6..7128bc7cb7479c 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
@@ -1443,6 +1443,15 @@ InstructionCost RISCVTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
}
}
+ // The scalar icmp + select instructions will lower to SELECT_CC and lower to
+ // PseudoCCMOVGPR which will generate a conditional branch + mv. The
+ // cost of (icmp + select) will be (0 + select instr cost).
+ if (I && isa<ICmpInst>(I) && !I->getType()->isVectorTy() && I->hasOneUser() &&
+ isa<SelectInst>(I->user_back()) &&
+ !I->user_back()->getType()->isVectorTy() &&
+ I->user_back()->getOperand(0) == I)
+ return 0;
+
// TODO: Add cost for scalar type.
return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind, I);
diff --git a/llvm/test/Analysis/CostModel/RISCV/cmp-select.ll b/llvm/test/Analysis/CostModel/RISCV/cmp-select.ll
index d50bd824bcfac6..42b346ab72324a 100644
--- a/llvm/test/Analysis/CostModel/RISCV/cmp-select.ll
+++ b/llvm/test/Analysis/CostModel/RISCV/cmp-select.ll
@@ -1,9 +1,9 @@
; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py
; RUN: opt < %s -mtriple=riscv64 -passes="print<cost-model>" -cost-kind=throughput 2>&1 -disable-output | FileCheck %s --check-prefixes=CHECK
-define void @cmp-select() {
-; CHECK-LABEL: 'cmp-select'
-; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %cmp1 = icmp slt i64 0, 1
+define void @icmp-select() {
+; CHECK-LABEL: 'icmp-select'
+; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %cmp1 = icmp slt i64 0, 1
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %select1 = select i1 %cmp1, i32 5, i32 4
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
;
@@ -11,3 +11,14 @@ define void @cmp-select() {
%select1 = select i1 %cmp1, i32 5, i32 4
ret void
}
+
+define void @fcmp-select() {
+; CHECK-LABEL: 'fcmp-select'
+; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %fcmp1 = fcmp ogt float 0.000000e+00, 1.000000e+00
+; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %select1 = select i1 %fcmp1, i32 5, i32 4
+; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
+;
+ %fcmp1 = fcmp ogt float 0.0, 1.0
+ %select1 = select i1 %fcmp1, i32 5, i32 4
+ ret void
+}
diff --git a/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll b/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
index 6936887cd166c3..a02c7130963f85 100644
--- a/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
+++ b/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
@@ -715,7 +715,7 @@ define void @double_stride_ptr_iv(ptr %p, ptr %p2, i64 %stride) {
; STRIDED-NEXT: entry:
; STRIDED-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
; STRIDED-NEXT: [[TMP1:%.*]] = mul i64 [[TMP0]], 4
-; STRIDED-NEXT: [[TMP2:%.*]] = call i64 @llvm.umax.i64(i64 32, i64 [[TMP1]])
+; STRIDED-NEXT: [[TMP2:%.*]] = call i64 @llvm.umax.i64(i64 24, i64 [[TMP1]])
; STRIDED-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 1024, [[TMP2]]
; STRIDED-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_MEMCHECK:%.*]]
; STRIDED: vector.memcheck:
More information about the llvm-commits
mailing list