[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 18:38:09 PDT 2024
https://github.com/ElvisWang123 created https://github.com/llvm/llvm-project/pull/91158
Cmp and select instructinos will lower to SELECT_CC and lower to PseudoCCMOVGPR which will generate a conditional branch inst and a move inst. Remove the instruction cost of the cmp instruction because of the generated branch instruction has 0 cost in the cost model.
>From c82986f1ada1d0454ffddb6cee862cfcd02e9347 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 fbd83941f635f9c67eb410a88099e3541037a946 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.
Cmp and select instructinos will lower to SELECT_CC and lower to PseudoCCMOVGPR
which will generate a conditional branch inst and a move inst.
Remove the instruction cost of the cmp instruction because of the
generated branch instruction has 0 cost in the cost model.
---
llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp | 8 ++++++++
llvm/test/Analysis/CostModel/RISCV/cmp-select.ll | 3 ++-
.../Transforms/LoopVectorize/RISCV/strided-accesses.ll | 2 +-
3 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
index ce26e61880fd05..885697e48b59ad 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
@@ -1436,6 +1436,14 @@ InstructionCost RISCVTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
}
}
+ // The cmp + select instructions will lower to SELECT_CC and lower to
+ // PseudoCCMOVGPR which will generate a conditional branch + mv. Remove
+ // the cost of the cmp instruction because the estimated cost of
+ // branch instruction is 0.
+ if (I && I->hasOneUser() && isa<SelectInst>(I->user_back()) &&
+ 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..6c88467afe6351 100644
--- a/llvm/test/Analysis/CostModel/RISCV/cmp-select.ll
+++ b/llvm/test/Analysis/CostModel/RISCV/cmp-select.ll
@@ -2,8 +2,9 @@
; 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 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
;
diff --git a/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll b/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
index 12fdf2149daf47..af62aad84d6be3 100644
--- a/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
+++ b/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
@@ -699,7 +699,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