-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[VectorCombine] Handle shuffle of selects #128032
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2a4dfdf
add pre-test
ParkHanbum 4c989b9
[VectorCombine] Handle shuffle of selects
ParkHanbum 4a551ed
Preserve Fast-Math Flags
ParkHanbum 17a6939
NewSel the transformed result append to Worklist
ParkHanbum f784d41
add comments to notice that restriction of FMF
ParkHanbum bf1d91d
add new shuffles to worklist
ParkHanbum 4e9e6a8
add debug message
ParkHanbum 42b4bf4
add early return when if type of C1 != C2
ParkHanbum 575a897
Eliminate NewSel value push to Worklist
ParkHanbum d85f8df
Fix logic to compare and substitute FPM of the Select
ParkHanbum 82aeff7
add tests for selects with different FPMs
ParkHanbum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -119,6 +119,7 @@ class VectorCombine { | |
| bool foldConcatOfBoolMasks(Instruction &I); | ||
| bool foldPermuteOfBinops(Instruction &I); | ||
| bool foldShuffleOfBinops(Instruction &I); | ||
| bool foldShuffleOfSelects(Instruction &I); | ||
| bool foldShuffleOfCastops(Instruction &I); | ||
| bool foldShuffleOfShuffles(Instruction &I); | ||
| bool foldShuffleOfIntrinsics(Instruction &I); | ||
|
|
@@ -1899,6 +1900,73 @@ bool VectorCombine::foldShuffleOfBinops(Instruction &I) { | |
| return true; | ||
| } | ||
|
|
||
| /// Try to convert, | ||
| /// (shuffle(select(c1,t1,f1)), (select(c2,t2,f2)), m) into | ||
| /// (select (shuffle c1,c2,m), (shuffle t1,t2,m), (shuffle f1,f2,m)) | ||
| bool VectorCombine::foldShuffleOfSelects(Instruction &I) { | ||
| ArrayRef<int> Mask; | ||
| Value *C1, *T1, *F1, *C2, *T2, *F2; | ||
| if (!match(&I, m_Shuffle( | ||
| m_OneUse(m_Select(m_Value(C1), m_Value(T1), m_Value(F1))), | ||
| m_OneUse(m_Select(m_Value(C2), m_Value(T2), m_Value(F2))), | ||
| m_Mask(Mask)))) | ||
| return false; | ||
|
|
||
| auto *DstVecTy = dyn_cast<FixedVectorType>(I.getType()); | ||
| auto *C1VecTy = dyn_cast<FixedVectorType>(C1->getType()); | ||
| auto *C2VecTy = dyn_cast<FixedVectorType>(C2->getType()); | ||
| if (!C1VecTy || !C2VecTy || C1VecTy != C2VecTy) | ||
| return false; | ||
|
|
||
| // SelectInsts must have the same FMF. | ||
| auto *Select0 = cast<Instruction>(I.getOperand(0)); | ||
| if (auto *SI0FOp = dyn_cast<FPMathOperator>(Select0)) | ||
| if (auto *SI1FOp = dyn_cast<FPMathOperator>((I.getOperand(1)))) | ||
| if (SI0FOp->getFastMathFlags() != SI1FOp->getFastMathFlags()) | ||
| return false; | ||
|
|
||
| auto SK = TargetTransformInfo::SK_PermuteTwoSrc; | ||
| auto SelOp = Instruction::Select; | ||
| InstructionCost OldCost = TTI.getCmpSelInstrCost( | ||
| SelOp, T1->getType(), C1VecTy, CmpInst::BAD_ICMP_PREDICATE, CostKind); | ||
| OldCost += TTI.getCmpSelInstrCost(SelOp, T2->getType(), C2VecTy, | ||
| CmpInst::BAD_ICMP_PREDICATE, CostKind); | ||
| OldCost += TTI.getShuffleCost(SK, DstVecTy, Mask, CostKind, 0, nullptr, | ||
| {I.getOperand(0), I.getOperand(1)}, &I); | ||
|
|
||
| auto *C1C2VecTy = cast<FixedVectorType>( | ||
| toVectorTy(Type::getInt1Ty(I.getContext()), DstVecTy->getNumElements())); | ||
| InstructionCost NewCost = | ||
| TTI.getShuffleCost(SK, C1C2VecTy, Mask, CostKind, 0, nullptr, {C1, C2}); | ||
| NewCost += | ||
| TTI.getShuffleCost(SK, DstVecTy, Mask, CostKind, 0, nullptr, {T1, T2}); | ||
| NewCost += | ||
| TTI.getShuffleCost(SK, DstVecTy, Mask, CostKind, 0, nullptr, {F1, F2}); | ||
| NewCost += TTI.getCmpSelInstrCost(SelOp, DstVecTy, DstVecTy, | ||
| CmpInst::BAD_ICMP_PREDICATE, CostKind); | ||
|
|
||
| LLVM_DEBUG(dbgs() << "Found a shuffle feeding two selects: " << I | ||
| << "\n OldCost: " << OldCost << " vs NewCost: " << NewCost | ||
| << "\n"); | ||
| if (NewCost > OldCost) | ||
RKSimon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return false; | ||
|
|
||
| Value *ShuffleCmp = Builder.CreateShuffleVector(C1, C2, Mask); | ||
| Value *ShuffleTrue = Builder.CreateShuffleVector(T1, T2, Mask); | ||
| Value *ShuffleFalse = Builder.CreateShuffleVector(F1, F2, Mask); | ||
| Value *NewSel = Builder.CreateSelect(ShuffleCmp, ShuffleTrue, ShuffleFalse); | ||
|
|
||
| // We presuppose that the SelectInsts have the same FMF. | ||
| if (isa<FPMathOperator>(NewSel)) | ||
|
||
| cast<Instruction>(NewSel)->setFastMathFlags(Select0->getFastMathFlags()); | ||
RKSimon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Worklist.pushValue(ShuffleCmp); | ||
| Worklist.pushValue(ShuffleTrue); | ||
| Worklist.pushValue(ShuffleFalse); | ||
| replaceValue(I, *NewSel); | ||
| return true; | ||
| } | ||
|
|
||
| /// Try to convert "shuffle (castop), (castop)" with a shared castop operand | ||
| /// into "castop (shuffle)". | ||
| bool VectorCombine::foldShuffleOfCastops(Instruction &I) { | ||
|
|
@@ -3352,6 +3420,7 @@ bool VectorCombine::run() { | |
| case Instruction::ShuffleVector: | ||
| MadeChange |= foldPermuteOfBinops(I); | ||
| MadeChange |= foldShuffleOfBinops(I); | ||
| MadeChange |= foldShuffleOfSelects(I); | ||
| MadeChange |= foldShuffleOfCastops(I); | ||
| MadeChange |= foldShuffleOfShuffles(I); | ||
| MadeChange |= foldShuffleOfIntrinsics(I); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we assert that both/neither of the selects are FPMathOperator ?
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.
I'm not sure, since we're trying to combine two Select statements, I thought they should have the same FMF. do I need to think more about FMFs?
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.
I'd probably just replace it with: