-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[GlobalIsel] Combine G_UNMERGE_VALUES from opaque vectors into scalars #113040
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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
169 changes: 169 additions & 0 deletions
169
llvm/lib/CodeGen/GlobalISel/CombinerHelperArtifacts.cpp
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 |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| //===- CombinerHelperArtifacts.cpp-----------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This file implements CombinerHelper for legalization artifacts. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // G_UNMERGE_VALUES | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| #include "llvm/CodeGen/GlobalISel/CombinerHelper.h" | ||
| #include "llvm/CodeGen/GlobalISel/LegalizerHelper.h" | ||
| #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h" | ||
| #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" | ||
| #include "llvm/CodeGen/GlobalISel/Utils.h" | ||
| #include "llvm/CodeGen/LowLevelTypeUtils.h" | ||
| #include "llvm/CodeGen/MachineOperand.h" | ||
| #include "llvm/CodeGen/MachineRegisterInfo.h" | ||
| #include "llvm/CodeGen/TargetOpcodes.h" | ||
| #include "llvm/Support/Casting.h" | ||
|
|
||
| #define DEBUG_TYPE "gi-combiner" | ||
|
|
||
| using namespace llvm; | ||
|
|
||
| bool CombinerHelper::matchUnmergeValuesAnyExtBuildVector(const MachineInstr &MI, | ||
| BuildFnTy &MatchInfo) { | ||
| const GUnmerge *Unmerge = cast<GUnmerge>(&MI); | ||
|
|
||
| if (!MRI.hasOneNonDBGUse(Unmerge->getSourceReg())) | ||
| return false; | ||
|
|
||
| const MachineInstr *Source = MRI.getVRegDef(Unmerge->getSourceReg()); | ||
|
|
||
| LLT DstTy = MRI.getType(Unmerge->getReg(0)); | ||
|
|
||
| // $bv:_(<8 x s8>) = G_BUILD_VECTOR .... | ||
| // $any:_(<8 x s16>) = G_ANYEXT $bv | ||
| // $uv:_(<4 x s16>), $uv1:_(<4 x s16>) = G_UNMERGE_VALUES $any | ||
| // | ||
| // -> | ||
| // | ||
| // $any:_(s16) = G_ANYEXT $bv[0] | ||
| // $any1:_(s16) = G_ANYEXT $bv[1] | ||
| // $any2:_(s16) = G_ANYEXT $bv[2] | ||
| // $any3:_(s16) = G_ANYEXT $bv[3] | ||
| // $any4:_(s16) = G_ANYEXT $bv[4] | ||
| // $any5:_(s16) = G_ANYEXT $bv[5] | ||
| // $any6:_(s16) = G_ANYEXT $bv[6] | ||
| // $any7:_(s16) = G_ANYEXT $bv[7] | ||
| // $uv:_(<4 x s16>) = G_BUILD_VECTOR $any, $any1, $any2, $any3 | ||
| // $uv1:_(<4 x s16>) = G_BUILD_VECTOR $any4, $any5, $any6, $any7 | ||
|
|
||
| // We want to unmerge into vectors. | ||
| if (!DstTy.isFixedVector()) | ||
| return false; | ||
|
|
||
| const GAnyExt *Any = dyn_cast<GAnyExt>(Source); | ||
| if (!Any) | ||
| return false; | ||
|
|
||
| const MachineInstr *NextSource = MRI.getVRegDef(Any->getSrcReg()); | ||
|
|
||
| if (const GBuildVector *BV = dyn_cast<GBuildVector>(NextSource)) { | ||
| // G_UNMERGE_VALUES G_ANYEXT G_BUILD_VECTOR | ||
|
|
||
| if (!MRI.hasOneNonDBGUse(BV->getReg(0))) | ||
| return false; | ||
|
|
||
| // FIXME: check element types? | ||
| if (BV->getNumSources() % Unmerge->getNumDefs() != 0) | ||
| return false; | ||
|
|
||
| LLT BigBvTy = MRI.getType(BV->getReg(0)); | ||
| LLT SmallBvTy = DstTy; | ||
| LLT SmallBvElemenTy = SmallBvTy.getElementType(); | ||
|
|
||
| if (!isLegalOrBeforeLegalizer( | ||
| {TargetOpcode::G_BUILD_VECTOR, {SmallBvTy, SmallBvElemenTy}})) | ||
| return false; | ||
|
|
||
| // We check the legality of scalar anyext. | ||
| if (!isLegalOrBeforeLegalizer( | ||
| {TargetOpcode::G_ANYEXT, | ||
| {SmallBvElemenTy, BigBvTy.getElementType()}})) | ||
| return false; | ||
|
|
||
| MatchInfo = [=](MachineIRBuilder &B) { | ||
| // Build into each G_UNMERGE_VALUES def | ||
| // a small build vector with anyext from the source build vector. | ||
| for (unsigned I = 0; I < Unmerge->getNumDefs(); ++I) { | ||
| SmallVector<Register> Ops; | ||
| for (unsigned J = 0; J < SmallBvTy.getNumElements(); ++J) { | ||
| Register SourceArray = | ||
| BV->getSourceReg(I * SmallBvTy.getNumElements() + J); | ||
| auto AnyExt = B.buildAnyExt(SmallBvElemenTy, SourceArray); | ||
| Ops.push_back(AnyExt.getReg(0)); | ||
| } | ||
| B.buildBuildVector(Unmerge->getOperand(I).getReg(), Ops); | ||
| }; | ||
| }; | ||
| return true; | ||
| }; | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| bool CombinerHelper::matchUnmergeValuesOfScalarAndVector(const MachineInstr &MI, | ||
| BuildFnTy &MatchInfo) { | ||
|
|
||
| constexpr unsigned MAX_NUM_DEFS_LIMIT = 4; | ||
|
|
||
| // %opaque:_(<2 x s64>) = G_OPAQUE | ||
| // %un1:_(s64), %un2:_(s64) = G_UNMERGE_VALUES %opaque(<2 x s64>) | ||
| // | ||
| // -> | ||
| // | ||
| // %zero:_(s64) = G_CONSTANT i64 0 | ||
| // %one:_(s64) = G_CONSTANT i64 1 | ||
| // %un1:_(s64) = G_EXTRACT_VECTOR_ELT %opaque, $zero | ||
| // %un2:_(s64) = G_EXTRACT_VECTOR_ELT %opaque, $one | ||
|
|
||
| const GUnmerge *Unmerge = cast<GUnmerge>(&MI); | ||
|
|
||
| if (Unmerge->getNumDefs() > MAX_NUM_DEFS_LIMIT) | ||
| return false; | ||
|
|
||
| LLT DstTy = MRI.getType(Unmerge->getReg(0)); | ||
| LLT SrcTy = MRI.getType(Unmerge->getSourceReg()); | ||
|
|
||
| // We want to unmerge a vector into scalars. | ||
| if (!DstTy.isScalar() || !SrcTy.isFixedVector() || DstTy.getSizeInBits() > 64) | ||
| return false; | ||
|
|
||
| if (DstTy != SrcTy.getElementType()) | ||
| return false; | ||
|
|
||
| // We want to unmerge from an opaque vector. | ||
| const MachineInstr *Source = MRI.getVRegDef(Unmerge->getSourceReg()); | ||
| if (isa<GBuildVector>(Source)) | ||
| return false; | ||
|
|
||
| unsigned PreferredVecIdxWidth = | ||
| getTargetLowering().getVectorIdxTy(getDataLayout()).getSizeInBits(); | ||
|
|
||
| LLT IdxTy = LLT::scalar(PreferredVecIdxWidth); | ||
|
|
||
| if (!isLegalOrBeforeLegalizer( | ||
| {TargetOpcode::G_EXTRACT_VECTOR_ELT, {DstTy, SrcTy, IdxTy}})) | ||
| return false; | ||
|
|
||
| if (!isConstantLegalOrBeforeLegalizer(IdxTy)) | ||
| return false; | ||
|
|
||
| MatchInfo = [=](MachineIRBuilder &B) { | ||
| for (unsigned I = 0; I < Unmerge->getNumDefs(); ++I) { | ||
| auto Index = B.buildConstant(IdxTy, I); | ||
| B.buildExtractVectorElement(Unmerge->getOperand(I).getReg(), | ||
| Unmerge->getSourceReg(), Index); | ||
| } | ||
| }; | ||
|
|
||
| return true; | ||
| } |
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
Oops, something went wrong.
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.
It would be easier to review if the diff was local to the file, and this code wasn't moved to another file