[llvm] [SystemZ] Fix Operand Retrieval for Vector Reduction Intrinsic in `shouldExpandReduction` (PR #88874)

Jonas Paulsson via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 17 02:41:21 PDT 2024


================
@@ -1323,25 +1324,43 @@ SystemZTTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
 }
 
 bool SystemZTTIImpl::shouldExpandReduction(const IntrinsicInst *II) const {
-  // Always expand on Subtargets without vector instructions
+  // Always expand on Subtargets without vector instructions.
   if (!ST->hasVector())
     return true;
 
-  // Always expand for operands that do not fill one vector reg
-  auto *Type = cast<FixedVectorType>(II->getOperand(0)->getType());
-  unsigned NumElts = Type->getNumElements();
-  unsigned ScalarSize = Type->getScalarSizeInBits();
+  // Find the type of the vector operand of the intrinsic
+  // This assumes that each vector reduction intrinsic only
+  // has one vector operand.
+  FixedVectorType *VType = 0x0;
+  for (unsigned I = 0; I < II->getNumOperands(); ++I) {
+    auto *T = II->getOperand(I)->getType();
+    if (T->isVectorTy()) {
+      VType = cast<FixedVectorType>(T);
+      break;
+    }
+  }
+
----------------
JonPsson1 wrote:

shouldExpandReduction() looks nice now.

I don't quite like getFirstOperandVectorType(), it's not robust against any future instrinsics we do not know about, and it doesn't have an assert that would discover any such future cases if they emerge.

We want to know the intrinsic type, and we know which intrinsic we are dealing with. So why make it unnecessarily general? No need to loop as we know what operand number we need to use. This could be done directly in shouldExpandReduction(), or if you want to have a function for it, you could pass the operand number to it.

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


More information about the llvm-commits mailing list