-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[SLP]: Introduce and use getDataFlowCost #112999
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -9044,6 +9044,51 @@ static SmallVector<Type *> buildIntrinsicArgTypes(const CallInst *CI, | |||||
| return ArgTys; | ||||||
| } | ||||||
|
|
||||||
| // The cost model may determine that vectorizing and eliminating a series of | ||||||
| // ExtractElements is beneficial. However, if the input vector is a function | ||||||
| // argument, the calling convention may require extractions in the geneerated | ||||||
| // code. In this scenario, vectorizaino would then not eliminate the | ||||||
| // ExtractElement sequence, but would add additional vectorization code. | ||||||
| // getCCCostFromScalars does the proper accounting for this. | ||||||
| static unsigned getCCCostFromScalars(ArrayRef<Value *> &Scalars, | ||||||
jrbyrnes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| unsigned ScalarSize, | ||||||
| TargetTransformInfo *TTI) { | ||||||
jrbyrnes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| SetVector<Value *> ArgRoots; | ||||||
jrbyrnes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| for (unsigned I = 0; I < ScalarSize; I++) { | ||||||
| auto *Scalar = Scalars[I]; | ||||||
jrbyrnes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| if (!Scalar) | ||||||
| continue; | ||||||
| auto *EE = dyn_cast<ExtractElementInst>(Scalar); | ||||||
| if (!EE) | ||||||
| continue; | ||||||
jrbyrnes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| auto *Vec = EE->getOperand(0); | ||||||
| if (!Vec->getType()->isVectorTy()) | ||||||
| continue; | ||||||
|
|
||||||
| auto F = EE->getFunction(); | ||||||
| auto FoundIt = find_if( | ||||||
| F->args(), [&Vec](Argument &I) { return Vec == cast<Value>(&I); }); | ||||||
|
|
||||||
| if (FoundIt == F->arg_end()) | ||||||
| continue; | ||||||
jrbyrnes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| if (!ArgRoots.contains(Vec)) | ||||||
| ArgRoots.insert(Vec); | ||||||
jrbyrnes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| } | ||||||
|
|
||||||
| if (!ArgRoots.size()) | ||||||
| return 0; | ||||||
|
|
||||||
| unsigned Cost = 0; | ||||||
| for (auto ArgOp : ArgRoots) { | ||||||
jrbyrnes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| Cost += TTI->getDataFlowCost(ArgOp->getType(), /*IsCallingConv*/ true) | ||||||
jrbyrnes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| .getValue() | ||||||
| .value_or(0); | ||||||
| } | ||||||
| return Cost; | ||||||
| } | ||||||
|
|
||||||
| InstructionCost | ||||||
| BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals, | ||||||
| SmallPtrSetImpl<Value *> &CheckedExtracts) { | ||||||
|
|
@@ -9075,15 +9120,16 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals, | |||||
| auto *FinalVecTy = FixedVectorType::get(ScalarTy, EntryVF); | ||||||
|
|
||||||
| bool NeedToShuffleReuses = !E->ReuseShuffleIndices.empty(); | ||||||
| InstructionCost CommonCost = getCCCostFromScalars(VL, VL.size(), TTI); | ||||||
| if (E->State == TreeEntry::NeedToGather) { | ||||||
| if (allConstant(VL)) | ||||||
| return 0; | ||||||
| return CommonCost; | ||||||
| if (isa<InsertElementInst>(VL[0])) | ||||||
| return InstructionCost::getInvalid(); | ||||||
| return processBuildVector<ShuffleCostEstimator, InstructionCost>( | ||||||
| E, ScalarTy, *TTI, VectorizedVals, *this, CheckedExtracts); | ||||||
| return CommonCost + | ||||||
| processBuildVector<ShuffleCostEstimator, InstructionCost>( | ||||||
| E, ScalarTy, *TTI, VectorizedVals, *this, CheckedExtracts); | ||||||
| } | ||||||
| InstructionCost CommonCost = 0; | ||||||
| SmallVector<int> Mask; | ||||||
| bool IsReverseOrder = isReverseOrder(E->ReorderIndices); | ||||||
| if (!E->ReorderIndices.empty() && | ||||||
|
|
@@ -10241,6 +10287,31 @@ InstructionCost BoUpSLP::getTreeCost(ArrayRef<Value *> VectorizedVals) { | |||||
|
|
||||||
| InstructionCost C = getEntryCost(&TE, VectorizedVals, CheckedExtracts); | ||||||
| Cost += C; | ||||||
|
|
||||||
| // Calculate the cost difference of propagating a vector vs series of scalars | ||||||
| // across blocks. This may be nonzero in the case of illegal vectors. | ||||||
| Instruction *VL0 = TE.getMainOp(); | ||||||
| bool IsAPhi = VL0 && isa<PHINode>(VL0); | ||||||
| bool HasNextEntry = VL0 && ((I + 1) < VectorizableTree.size()); | ||||||
jrbyrnes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| bool LiveThru = false; | ||||||
| if (HasNextEntry) { | ||||||
| Instruction *VL1 = VectorizableTree[I + 1]->getMainOp(); | ||||||
| LiveThru = VL1 && (VL0->getParent() != VL1->getParent()); | ||||||
| } | ||||||
| if (IsAPhi || LiveThru) { | ||||||
| VectorType *VTy = dyn_cast<VectorType>(VL0->getType()); | ||||||
| Type *ScalarTy = VTy ? VTy->getElementType() : VL0->getType(); | ||||||
| if (ScalarTy && isValidElementType(ScalarTy)) { | ||||||
jrbyrnes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| InstructionCost ScalarDFlow = | ||||||
| TTI->getDataFlowCost(ScalarTy, | ||||||
| /*IsCallingConv*/ false) * | ||||||
jrbyrnes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| TE.getVectorFactor(); | ||||||
| InstructionCost VectorDFlow = | ||||||
| TTI->getDataFlowCost(FixedVectorType::get(ScalarTy, TE.getVectorFactor()), /*IsCallingConv*/ false); | ||||||
jrbyrnes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| Cost += (VectorDFlow - ScalarDFlow); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| LLVM_DEBUG(dbgs() << "SLP: Adding cost " << C << " for bundle " | ||||||
| << shortBundleName(TE.Scalars) << ".\n" | ||||||
| << "SLP: Current total cost = " << Cost << "\n"); | ||||||
|
|
@@ -10257,15 +10328,24 @@ InstructionCost BoUpSLP::getTreeCost(ArrayRef<Value *> VectorizedVals) { | |||||
| for (ExternalUser &EU : ExternalUses) { | ||||||
| // We only add extract cost once for the same scalar. | ||||||
| if (!isa_and_nonnull<InsertElementInst>(EU.User) && | ||||||
| !ExtractCostCalculated.insert(EU.Scalar).second) | ||||||
| !ExtractCostCalculated.insert(EU.Scalar).second) { | ||||||
| continue; | ||||||
| } | ||||||
jrbyrnes marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| // Uses by ephemeral values are free (because the ephemeral value will be | ||||||
| // removed prior to code generation, and so the extraction will be | ||||||
| // removed as well). | ||||||
| if (EphValues.count(EU.User)) | ||||||
| continue; | ||||||
|
|
||||||
| // Account for any additional costs required by CallingConvention for the | ||||||
| // type. | ||||||
| if (isa_and_nonnull<ReturnInst>(EU.User)) { | ||||||
| Cost += | ||||||
| TTI->getDataFlowCost(EU.Scalar->getType(), /*IsCallingConv*/ true); | ||||||
|
||||||
| TTI->getDataFlowCost(EU.Scalar->getType(), /*IsCallingConv*/ true); | |
| TTI->getDataFlowCost(EU.Scalar->getType(), /*IsCallingConv=*/ true); |
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.
No need to add it here, you can check it in getVectorInstrCost, just need to pass an extra info about the user instruction, if needed
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.
The case we care about is vector return types. These will be skipped by the early continue check and won't fall to the getVectorInstrCost. It seems cleaner to just add getDataFlowCost rather than removing the continue check.
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 code does not check for vector return type, it is externally used scalar. What exactly do you want to calculate here: the cost of passing value, returned by extractelement instruction, or the cost of vectorized buildvector (replaced by shuffles or just original vectors in case of the identity shuffle)?
Uh oh!
There was an error while loading. Please reload this page.