Skip to content
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

Add proper cost computation for llvm.vector.reduce.add #77764

Closed
wants to merge 2 commits into from

Conversation

dominik-steenken
Copy link
Contributor

WIP

Copy link

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
Collaborator

llvmbot commented Jan 11, 2024

@llvm/pr-subscribers-llvm-analysis

Author: None (dominik-steenken)

Changes

WIP


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

2 Files Affected:

  • (modified) llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp (+14-2)
  • (added) llvm/test/Analysis/CostModel/SystemZ/reduce-add.ll (+32)
diff --git a/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp b/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp
index 1f97e0f761c04d..d8a0923ab980b2 100644
--- a/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp
@@ -1244,9 +1244,21 @@ InstructionCost SystemZTTIImpl::getInterleavedMemoryOpCost(
   return NumVectorMemOps + NumPermutes;
 }
 
-static int getVectorIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy) {
+static int getVectorIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy, const SmallVectorImpl<Type *>& ParamTys) {
+  dbgs() << "getVectorIntrinsicInstrCost entry, ID == " << ID << ", name = " << Intrinsic::getName(ID) << ", compare with " << Intrinsic::getName(Intrinsic::vector_reduce_add) << "(" << Intrinsic::vector_reduce_add << ")\n";
   if (RetTy->isVectorTy() && ID == Intrinsic::bswap)
     return getNumVectorRegs(RetTy); // VPERM
+  else if (ID == Intrinsic::vector_reduce_add) {
+    dbgs() << "getVectorIntrinsicInstrCost is reduce_add intrinsic\n";
+    auto *VTy = cast<FixedVectorType>(ParamTys.front());
+    switch (VTy->getNumElements()) {
+      default: return -1;
+      case 2: return 3;
+      case 4: return 5;
+      case 8: return 7;
+      case 16: return 11;
+    }
+  }
   return -1;
 }
 
@@ -1254,7 +1266,7 @@ InstructionCost
 SystemZTTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
                                       TTI::TargetCostKind CostKind) {
   InstructionCost Cost =
-      getVectorIntrinsicInstrCost(ICA.getID(), ICA.getReturnType());
+      getVectorIntrinsicInstrCost(ICA.getID(), ICA.getReturnType(), ICA.getArgTypes());
   if (Cost != -1)
     return Cost;
   return BaseT::getIntrinsicInstrCost(ICA, CostKind);
diff --git a/llvm/test/Analysis/CostModel/SystemZ/reduce-add.ll b/llvm/test/Analysis/CostModel/SystemZ/reduce-add.ll
new file mode 100644
index 00000000000000..b62feb50212d50
--- /dev/null
+++ b/llvm/test/Analysis/CostModel/SystemZ/reduce-add.ll
@@ -0,0 +1,32 @@
+; RUN: opt < %s -mtriple=systemz-unknown -mcpu=z13 -passes="print<cost-model>" -cost-kind=throughput 2>&1 -disable-output | FileCheck %s
+
+define void @reduce(ptr %src, ptr %dst) {
+; CHECK-LABEL: 'reduce'
+; CHECK:  Cost Model: Found an estimated cost of 3 for instruction: %R2 = call i32 @llvm.vector.reduce.add.v2i32(<2 x i32> %V2)
+; CHECK:  Cost Model: Found an estimated cost of 5 for instruction: %R4 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %V4)
+; CHECK:  Cost Model: Found an estimated cost of 7 for instruction: %R8 = call i32 @llvm.vector.reduce.add.v8i32(<8 x i32> %V8)
+; CHECK:  Cost Model: Found an estimated cost of 11 for instruction: %R16 = call i32 @llvm.vector.reduce.add.v16i32(<16 x i32> %V16)
+;
+  %V2 = load <2 x i32>, ptr %src, align 8
+  %R2 = call i32 @llvm.vector.reduce.add.v2i32(<2 x i32> %V2)
+  store volatile i32 %R2, ptr %dst, align 4
+
+  %V4 = load <4 x i32>, ptr %src, align 8
+  %R4 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %V4)
+  store volatile i32 %R4, ptr %dst, align 4
+
+  %V8 = load <8 x i32>, ptr %src, align 8
+  %R8 = call i32 @llvm.vector.reduce.add.v8i32(<8 x i32> %V8)
+  store volatile i32 %R8, ptr %dst, align 4
+
+  %V16 = load <16 x i32>, ptr %src, align 8
+  %R16 = call i32 @llvm.vector.reduce.add.v16i32(<16 x i32> %V16)
+  store volatile i32 %R16, ptr %dst, align 4
+
+  ret void
+}
+
+declare i32 @llvm.vector.reduce.add.v2i32(<2 x i32>)
+declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>)
+declare i32 @llvm.vector.reduce.add.v8i32(<8 x i32>)
+declare i32 @llvm.vector.reduce.add.v16i32(<16 x i32>)
\ No newline at end of file

Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff 40d5c2bcd41a534e6bab98fedf1a930d9bd165e7 65811b9c06670a6f544ac1363ebaac33a189b8ab -- llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp
View the diff from clang-format here.
diff --git a/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp b/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp
index d8a0923ab9..ad7684a1c2 100644
--- a/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp
@@ -1244,19 +1244,29 @@ InstructionCost SystemZTTIImpl::getInterleavedMemoryOpCost(
   return NumVectorMemOps + NumPermutes;
 }
 
-static int getVectorIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy, const SmallVectorImpl<Type *>& ParamTys) {
-  dbgs() << "getVectorIntrinsicInstrCost entry, ID == " << ID << ", name = " << Intrinsic::getName(ID) << ", compare with " << Intrinsic::getName(Intrinsic::vector_reduce_add) << "(" << Intrinsic::vector_reduce_add << ")\n";
+static int
+getVectorIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
+                            const SmallVectorImpl<Type *> &ParamTys) {
+  dbgs() << "getVectorIntrinsicInstrCost entry, ID == " << ID
+         << ", name = " << Intrinsic::getName(ID) << ", compare with "
+         << Intrinsic::getName(Intrinsic::vector_reduce_add) << "("
+         << Intrinsic::vector_reduce_add << ")\n";
   if (RetTy->isVectorTy() && ID == Intrinsic::bswap)
     return getNumVectorRegs(RetTy); // VPERM
   else if (ID == Intrinsic::vector_reduce_add) {
     dbgs() << "getVectorIntrinsicInstrCost is reduce_add intrinsic\n";
     auto *VTy = cast<FixedVectorType>(ParamTys.front());
     switch (VTy->getNumElements()) {
-      default: return -1;
-      case 2: return 3;
-      case 4: return 5;
-      case 8: return 7;
-      case 16: return 11;
+    default:
+      return -1;
+    case 2:
+      return 3;
+    case 4:
+      return 5;
+    case 8:
+      return 7;
+    case 16:
+      return 11;
     }
   }
   return -1;
@@ -1265,8 +1275,8 @@ static int getVectorIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy, const Smal
 InstructionCost
 SystemZTTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
                                       TTI::TargetCostKind CostKind) {
-  InstructionCost Cost =
-      getVectorIntrinsicInstrCost(ICA.getID(), ICA.getReturnType(), ICA.getArgTypes());
+  InstructionCost Cost = getVectorIntrinsicInstrCost(
+      ICA.getID(), ICA.getReturnType(), ICA.getArgTypes());
   if (Cost != -1)
     return Cost;
   return BaseT::getIntrinsicInstrCost(ICA, CostKind);

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.

None yet

2 participants