-
Notifications
You must be signed in to change notification settings - Fork 15k
[RISCV][TTI] Fix shift VV opcode mapping in getArithmeticInstrCost #156408
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
base: main
Are you sure you want to change the base?
Conversation
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 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. |
@llvm/pr-subscribers-backend-risc-v @llvm/pr-subscribers-llvm-analysis Author: None (steven-studio) ChangesSummaryFix incorrect opcode mapping in
Previously, all three were mapped to MotivationCorrect mapping ensures the cost model reflects the real instructions, Changes
Scope
Follow-upA separate PR will address the incorrect mapping of Full diff: https://github.com/llvm/llvm-project/pull/156408.diff 3 Files Affected:
diff --git a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
index 1ca513214f67c..adc4acaf0058f 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
@@ -2522,9 +2522,13 @@ InstructionCost RISCVTTIImpl::getArithmeticInstrCost(
Op = RISCV::VADD_VV;
break;
case ISD::SHL:
+ Op = RISCV::VSLL_VV;
+ break;
case ISD::SRL:
+ Op = RISCV::VSRL_VV;
+ break;
case ISD::SRA:
- Op = RISCV::VSLL_VV;
+ Op = RISCV::VSRA_VV;
break;
case ISD::AND:
case ISD::OR:
diff --git a/llvm/test/Analysis/CostModel/RISCV/rvv-shift-cost.ll b/llvm/test/Analysis/CostModel/RISCV/rvv-shift-cost.ll
new file mode 100644
index 0000000000000..75ae916c0cfe7
--- /dev/null
+++ b/llvm/test/Analysis/CostModel/RISCV/rvv-shift-cost.ll
@@ -0,0 +1,26 @@
+; REQUIRES: riscv-registered-target
+; RUN: opt -mtriple=riscv64 -mattr=+v -passes='print<cost-model>' -disable-output < %s 2>&1 | FileCheck %s
+
+define <8 x i32> @shl_cost(<8 x i32> %a, <8 x i32> %b) {
+; CHECK-LABEL: 'shl_cost'
+; CHECK: Cost Model: Found an estimated cost of
+; CHECK: shl <8 x i32>
+ %r = shl <8 x i32> %a, %b
+ ret <8 x i32> %r
+}
+
+define <8 x i32> @srl_cost(<8 x i32> %a, <8 x i32> %b) {
+; CHECK-LABEL: 'srl_cost'
+; CHECK: Cost Model: Found an estimated cost of
+; CHECK: lshr <8 x i32>
+ %r = lshr <8 x i32> %a, %b
+ ret <8 x i32> %r
+}
+
+define <8 x i32> @sra_cost(<8 x i32> %a, <8 x i32> %b) {
+; CHECK-LABEL: 'sra_cost'
+; CHECK: Cost Model: Found an estimated cost of
+; CHECK: ashr <8 x i32>
+ %r = ashr <8 x i32> %a, %b
+ ret <8 x i32> %r
+}
diff --git a/llvm/test/CodeGen/RISCV/rvv-shift-vv.ll b/llvm/test/CodeGen/RISCV/rvv-shift-vv.ll
new file mode 100644
index 0000000000000..8c13f63fd2fd7
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/rvv-shift-vv.ll
@@ -0,0 +1,27 @@
+; REQUIRES: riscv-registered-target
+
+; RUN: llc -mtriple=riscv64 -mattr=+v -O2 -verify-machineinstrs < %s | FileCheck %s
+
+; 我們用 <8 x i32> 並讓位移量來自第二個向量參數,確保走 VV 形式
+;(不是 vx/vi)。每個函式只做一次對應的 shift。
+
+; CHECK-LABEL: shl_vv
+; CHECK: vsll.vv
+define <8 x i32> @shl_vv(<8 x i32> %a, <8 x i32> %b) {
+ %r = shl <8 x i32> %a, %b
+ ret <8 x i32> %r
+}
+
+; CHECK-LABEL: srl_vv
+; CHECK: vsrl.vv
+define <8 x i32> @srl_vv(<8 x i32> %a, <8 x i32> %b) {
+ %r = lshr <8 x i32> %a, %b
+ ret <8 x i32> %r
+}
+
+; CHECK-LABEL: sra_vv
+; CHECK: vsra.vv
+define <8 x i32> @sra_vv(<8 x i32> %a, <8 x i32> %b) {
+ %r = ashr <8 x i32> %a, %b
+ ret <8 x i32> %r
+}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't have any difference because the RISCV opcodes all have the same cost and are generally expected to. There's not really any benefit here to making it more granular
Summary
Fix incorrect opcode mapping in
RISCVTTIImpl::getArithmeticInstrCost
for vector shift operations:Previously, all three were mapped to
VSLL_VV
.Motivation
Correct mapping ensures the cost model reflects the real instructions,
preventing vectorization and scheduling decisions from being based on wrong costs.
Changes
RISCVTargetTransformInfo.cpp
to use proper opcodes.Verifies
shl/lshr/ashr
generatevsll.vv / vsrl.vv / vsra.vv
.Ensures cost model reports costs for
shl/lshr/ashr
.Scope
Follow-up
A separate PR will address the incorrect mapping of
ssub_sat
to useVSSUB_VV
instead ofVSSUBU_VV
.