Skip to content

[RISCV][CostModel] Remove cost of icmp inst in icmp+select with SFB. #91158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 20, 2024

Conversation

ElvisWang123
Copy link
Contributor

@ElvisWang123 ElvisWang123 commented May 6, 2024

With ShortFowrardBranchOpt(SFB) or ConditionalMoveFusion, scalar
ICmp and scalar Select instructions will lower to SELECT_CC
and lower to PseudoCCMOVGPR which will generate a conditional
branch instruction and a move instruction.
The cost of scalar (ICmp + Select) = (0 + Select instruction cost)

Copy link

github-actions bot commented May 6, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented May 6, 2024

@llvm/pr-subscribers-llvm-analysis

@llvm/pr-subscribers-llvm-transforms

Author: Elvis Wang (ElvisWang123)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/91158.diff

3 Files Affected:

  • (modified) llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp (+8)
  • (added) llvm/test/Analysis/CostModel/RISCV/cmp-select.ll (+14)
  • (modified) llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll (+1-1)
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
new file mode 100644
index 00000000000000..6c88467afe6351
--- /dev/null
+++ b/llvm/test/Analysis/CostModel/RISCV/cmp-select.ll
@@ -0,0 +1,14 @@
+; 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 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
+;
+  %cmp1 = icmp slt i64 0, 1
+  %select1 = select i1 %cmp1, 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 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:

@llvmbot
Copy link
Member

llvmbot commented May 6, 2024

@llvm/pr-subscribers-backend-risc-v

Author: Elvis Wang (ElvisWang123)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/91158.diff

3 Files Affected:

  • (modified) llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp (+8)
  • (added) llvm/test/Analysis/CostModel/RISCV/cmp-select.ll (+14)
  • (modified) llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll (+1-1)
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
new file mode 100644
index 00000000000000..6c88467afe6351
--- /dev/null
+++ b/llvm/test/Analysis/CostModel/RISCV/cmp-select.ll
@@ -0,0 +1,14 @@
+; 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 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
+;
+  %cmp1 = icmp slt i64 0, 1
+  %select1 = select i1 %cmp1, 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 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:

@ElvisWang123 ElvisWang123 force-pushed the eliminete-compare-cost branch from fbd8394 to 35a8ba5 Compare May 6, 2024 01:52
@arcbbb arcbbb requested review from preames, lukel97 and topperc May 6, 2024 01:56
@ElvisWang123 ElvisWang123 force-pushed the eliminete-compare-cost branch from 35a8ba5 to 74460bc Compare May 6, 2024 02:05
@ElvisWang123 ElvisWang123 force-pushed the eliminete-compare-cost branch 3 times, most recently from 7e7f150 to f7da97d Compare May 6, 2024 03:32
@ElvisWang123 ElvisWang123 force-pushed the eliminete-compare-cost branch from f7da97d to 1f8e9d2 Compare May 6, 2024 08:56
@ElvisWang123 ElvisWang123 force-pushed the eliminete-compare-cost branch 2 times, most recently from 0550d47 to 7f59916 Compare May 6, 2024 13:19
@ElvisWang123 ElvisWang123 changed the title [RISCV][CostModel] Remove inst cost of cmp inst in cmp-select sequence. [RISCV][CostModel] Remove cost of icmp inst in icmp+select with SFB. May 10, 2024
With ShortFowrardBranchOpt(SFB) or ConditionalMoveFusion, 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).
@ElvisWang123 ElvisWang123 force-pushed the eliminete-compare-cost branch from 7f59916 to 2e85de7 Compare May 13, 2024 14:57
@ElvisWang123 ElvisWang123 force-pushed the eliminete-compare-cost branch from 2e85de7 to 0668031 Compare May 13, 2024 15:00
Copy link
Member

@kito-cheng kito-cheng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@arcbbb arcbbb merged commit b60e628 into llvm:main May 20, 2024
4 checks passed
Copy link

@ElvisWang123 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested
by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants