[llvm] [AArch64][CostModel] Consider the cost of const vector (PR #117539)

Sushant Gokhale via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 15 22:58:47 PST 2024


================
@@ -3785,12 +3785,116 @@ bool AArch64TTIImpl::useNeonVector(const Type *Ty) const {
   return isa<FixedVectorType>(Ty) && !ST->useSVEForFixedLengthVectors();
 }
 
-InstructionCost AArch64TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Ty,
-                                                MaybeAlign Alignment,
-                                                unsigned AddressSpace,
-                                                TTI::TargetCostKind CostKind,
-                                                TTI::OperandValueInfo OpInfo,
-                                                const Instruction *I) {
+template <typename T>
+static bool HaveIdenticalVectVals(ArrayRef<Constant *> A,
+                                  ArrayRef<Constant *> B) {
+  auto R = zip(A, B);
+  return all_of(R, [&](std::tuple<Constant *, Constant *> P) {
+    return cast<T>(get<0>(P))->getValue() == cast<T>(get<1>(P))->getValue();
+  });
+}
+
+template <typename T>
+static bool HaveIdenticalVectTy(ArrayRef<Constant *> A,
+                                ArrayRef<Constant *> B) {
+  auto R1 = all_of(A, [&](Constant *C) { return isa<T>(C); });
+  auto R2 = all_of(B, [&](Constant *C) { return isa<T>(C); });
+  return R1 & R2;
+}
+
+template <typename T>
+static bool AreIdenticalVects(ArrayRef<Constant *> A, ArrayRef<Constant *> B) {
+  if (A.empty() || B.empty() || !HaveIdenticalVectTy<T>(A, B))
+    return false;
+  return HaveIdenticalVectVals<T>(A, B);
+}
+
+InstructionCost AArch64TTIImpl::getConstantMaterializationCost(
+    ArrayRef<Constant *> VL, Type *SrcTy, TTI::TargetCostKind CostKind,
+    ArrayRef<SmallVector<Constant *>> ConstVectsPerTree,
+    ArrayRef<SmallVector<Constant *>> MaterializedConstVectsPerFunc) {
+  // Compute the scalar cost.
+  InstructionCost Cost;
+  if (!SrcTy->isVectorTy()) {
+    // FIXME: Consider floating point types as well.
+    auto *C = dyn_cast<ConstantInt>(VL[0]);
+    return C ? getIntImmCost(C->getValue(), C->getType(), CostKind)
+             : InstructionCost::getInvalid();
+  } else // Vector cost.
+  {
----------------
sushgokh wrote:

I need to add extra check if `CostKind == RecipThroughput`. Forgot this. Will make it next revision

https://github.com/llvm/llvm-project/pull/117539


More information about the llvm-commits mailing list