-
Notifications
You must be signed in to change notification settings - Fork 14.4k
AMDGPU/GlobalISel: Improve readanylane combines in regbanklegalize #142789
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
petar-avramovic
wants to merge
1
commit into
users/petar-avramovic/ral-tests
from
users/petar-avramovic/ral-combine
Closed
Changes from all commits
Commits
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -23,6 +23,7 @@ | |||||||||
#include "GCNSubtarget.h" | ||||||||||
#include "llvm/CodeGen/GlobalISel/CSEInfo.h" | ||||||||||
#include "llvm/CodeGen/GlobalISel/CSEMIRBuilder.h" | ||||||||||
#include "llvm/CodeGen/GlobalISel/GenericMachineInstrs.h" | ||||||||||
#include "llvm/CodeGen/MachineFunctionPass.h" | ||||||||||
#include "llvm/CodeGen/MachineUniformityAnalysis.h" | ||||||||||
#include "llvm/CodeGen/TargetPassConfig.h" | ||||||||||
|
@@ -137,7 +138,111 @@ class AMDGPURegBankLegalizeCombiner { | |||||||||
return {MatchMI, MatchMI->getOperand(1).getReg()}; | ||||||||||
} | ||||||||||
|
||||||||||
std::pair<GUnmerge *, int> tryMatchRALFromUnmerge(Register Src) { | ||||||||||
MachineInstr *ReadAnyLane = MRI.getVRegDef(Src); | ||||||||||
if (ReadAnyLane->getOpcode() != AMDGPU::G_AMDGPU_READANYLANE) | ||||||||||
return {nullptr, -1}; | ||||||||||
|
||||||||||
Register RALSrc = ReadAnyLane->getOperand(1).getReg(); | ||||||||||
if (auto *UnMerge = getOpcodeDef<GUnmerge>(RALSrc, MRI)) | ||||||||||
return {UnMerge, UnMerge->findRegisterDefOperandIdx(RALSrc, nullptr)}; | ||||||||||
|
||||||||||
return {nullptr, -1}; | ||||||||||
} | ||||||||||
|
||||||||||
Register getReadAnyLaneSrc(Register Src) { | ||||||||||
// Src = G_AMDGPU_READANYLANE RALSrc | ||||||||||
auto [RAL, RALSrc] = tryMatch(Src, AMDGPU::G_AMDGPU_READANYLANE); | ||||||||||
if (RAL) | ||||||||||
return RALSrc; | ||||||||||
|
||||||||||
// LoVgpr, HiVgpr = G_UNMERGE_VALUES UnmergeSrc | ||||||||||
// LoSgpr = G_AMDGPU_READANYLANE LoVgpr | ||||||||||
// HiSgpr = G_AMDGPU_READANYLANE HiVgpr | ||||||||||
// Src G_MERGE_VALUES LoSgpr, HiSgpr | ||||||||||
auto *Merge = getOpcodeDef<GMergeLikeInstr>(Src, MRI); | ||||||||||
if (Merge) { | ||||||||||
unsigned NumElts = Merge->getNumSources(); | ||||||||||
auto [Unmerge, Idx] = tryMatchRALFromUnmerge(Merge->getSourceReg(0)); | ||||||||||
if (!Unmerge || Unmerge->getNumDefs() != NumElts || Idx != 0) | ||||||||||
return {}; | ||||||||||
|
||||||||||
// check if all elements are from same unmerge and there is no shuffling | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Suggested change
|
||||||||||
for (unsigned i = 1; i < NumElts; ++i) { | ||||||||||
auto [UnmergeI, IdxI] = tryMatchRALFromUnmerge(Merge->getSourceReg(i)); | ||||||||||
if (UnmergeI != Unmerge || (unsigned)IdxI != i) | ||||||||||
return {}; | ||||||||||
} | ||||||||||
return Unmerge->getSourceReg(); | ||||||||||
} | ||||||||||
|
||||||||||
// ..., VgprI, ... = G_UNMERGE_VALUES VgprLarge | ||||||||||
// SgprI = G_AMDGPU_READANYLANE VgprI | ||||||||||
// SgprLarge G_MERGE_VALUES ..., SgprI, ... | ||||||||||
// ..., Src, ... = G_UNMERGE_VALUES SgprLarge | ||||||||||
auto *UnMerge = getOpcodeDef<GUnmerge>(Src, MRI); | ||||||||||
if (UnMerge) { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Early return and reduce indent |
||||||||||
int Idx = UnMerge->findRegisterDefOperandIdx(Src, nullptr); | ||||||||||
auto *Merge = getOpcodeDef<GMergeLikeInstr>(UnMerge->getSourceReg(), MRI); | ||||||||||
if (Merge) { | ||||||||||
auto [RAL, RALSrc] = | ||||||||||
tryMatch(Merge->getSourceReg(Idx), AMDGPU::G_AMDGPU_READANYLANE); | ||||||||||
if (RAL) | ||||||||||
return RALSrc; | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
return {}; | ||||||||||
} | ||||||||||
|
||||||||||
void replaceRegWithOrBuildCopy(Register Dst, Register Src) { | ||||||||||
if (Dst.isVirtual()) | ||||||||||
MRI.replaceRegWith(Dst, Src); | ||||||||||
else | ||||||||||
B.buildCopy(Dst, Src); | ||||||||||
} | ||||||||||
|
||||||||||
bool tryEliminateReadAnyLane(MachineInstr &Copy) { | ||||||||||
Register Dst = Copy.getOperand(0).getReg(); | ||||||||||
Register Src = Copy.getOperand(1).getReg(); | ||||||||||
if (!Src.isVirtual()) | ||||||||||
return false; | ||||||||||
|
||||||||||
Register RALDst = Src; | ||||||||||
MachineInstr &SrcMI = *MRI.getVRegDef(Src); | ||||||||||
if (SrcMI.getOpcode() == AMDGPU::G_BITCAST) | ||||||||||
RALDst = SrcMI.getOperand(1).getReg(); | ||||||||||
|
||||||||||
Register RALSrc = getReadAnyLaneSrc(RALDst); | ||||||||||
if (!RALSrc) | ||||||||||
return false; | ||||||||||
|
||||||||||
B.setInstr(Copy); | ||||||||||
if (SrcMI.getOpcode() != AMDGPU::G_BITCAST) { | ||||||||||
// Src = READANYLANE RALSrc Src = READANYLANE RALSrc | ||||||||||
// Dst = Copy Src $Dst = Copy Src | ||||||||||
// -> -> | ||||||||||
// Dst = RALSrc $Dst = Copy RALSrc | ||||||||||
replaceRegWithOrBuildCopy(Dst, RALSrc); | ||||||||||
} else { | ||||||||||
// RALDst = READANYLANE RALSrc RALDst = READANYLANE RALSrc | ||||||||||
// Src = G_BITCAST RALDst Src = G_BITCAST RALDst | ||||||||||
// Dst = Copy Src Dst = Copy Src | ||||||||||
// -> -> | ||||||||||
// NewVgpr = G_BITCAST RALDst NewVgpr = G_BITCAST RALDst | ||||||||||
// Dst = NewVgpr $Dst = Copy NewVgpr | ||||||||||
auto Bitcast = B.buildBitcast({VgprRB, MRI.getType(Src)}, RALSrc); | ||||||||||
replaceRegWithOrBuildCopy(Dst, Bitcast.getReg(0)); | ||||||||||
} | ||||||||||
|
||||||||||
eraseInstr(Copy, MRI, nullptr); | ||||||||||
return true; | ||||||||||
} | ||||||||||
|
||||||||||
void tryCombineCopy(MachineInstr &MI) { | ||||||||||
if (tryEliminateReadAnyLane(MI)) | ||||||||||
return; | ||||||||||
|
||||||||||
Register Dst = MI.getOperand(0).getReg(); | ||||||||||
Register Src = MI.getOperand(1).getReg(); | ||||||||||
// Skip copies of physical registers. | ||||||||||
|
@@ -160,24 +265,7 @@ class AMDGPURegBankLegalizeCombiner { | |||||||||
auto One = B.buildConstant({SgprRB, S32}, 1); | ||||||||||
auto BoolSrc = B.buildAnd({SgprRB, S32}, TruncS32Src, One); | ||||||||||
B.buildInstr(AMDGPU::G_AMDGPU_COPY_VCC_SCC, {Dst}, {BoolSrc}); | ||||||||||
cleanUpAfterCombine(MI, Trunc); | ||||||||||
return; | ||||||||||
} | ||||||||||
|
||||||||||
// Src = G_AMDGPU_READANYLANE RALSrc | ||||||||||
// Dst = COPY Src | ||||||||||
// -> | ||||||||||
// Dst = RALSrc | ||||||||||
if (MRI.getRegBankOrNull(Dst) == VgprRB && | ||||||||||
MRI.getRegBankOrNull(Src) == SgprRB) { | ||||||||||
auto [RAL, RALSrc] = tryMatch(Src, AMDGPU::G_AMDGPU_READANYLANE); | ||||||||||
if (!RAL) | ||||||||||
return; | ||||||||||
|
||||||||||
assert(MRI.getRegBank(RALSrc) == VgprRB); | ||||||||||
MRI.replaceRegWith(Dst, RALSrc); | ||||||||||
cleanUpAfterCombine(MI, RAL); | ||||||||||
return; | ||||||||||
eraseInstr(MI, MRI, nullptr); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
|
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.
Move these out of line and reduce indent