-
Notifications
You must be signed in to change notification settings - Fork 14.3k
AMDGPU: Extract lambda used in foldImmediate into a helper function #127484
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
arsenm
merged 2 commits into
main
from
users/arsenm/amdgpu/add-extractSubregFromImm-helper
Feb 18, 2025
Merged
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3437,6 +3437,30 @@ void SIInstrInfo::removeModOperands(MachineInstr &MI) const { | |
} | ||
} | ||
|
||
std::optional<int64_t> SIInstrInfo::extractSubregFromImm(int64_t Imm, | ||
unsigned SubRegIndex) { | ||
switch (SubRegIndex) { | ||
case AMDGPU::NoSubRegister: | ||
return Imm; | ||
case AMDGPU::sub0: | ||
return Lo_32(Imm); | ||
case AMDGPU::sub1: | ||
return Hi_32(Imm); | ||
case AMDGPU::lo16: | ||
return SignExtend64<16>(Imm); | ||
case AMDGPU::hi16: | ||
return SignExtend64<16>(Imm >> 16); | ||
case AMDGPU::sub1_lo16: | ||
return SignExtend64<16>(Imm >> 32); | ||
case AMDGPU::sub1_hi16: | ||
return SignExtend64<16>(Imm >> 48); | ||
default: | ||
return std::nullopt; | ||
} | ||
|
||
llvm_unreachable("covered subregister switch"); | ||
} | ||
|
||
bool SIInstrInfo::foldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, | ||
Register Reg, MachineRegisterInfo *MRI) const { | ||
if (!MRI->hasOneNonDBGUse(Reg)) | ||
|
@@ -3446,25 +3470,6 @@ bool SIInstrInfo::foldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, | |
if (!getConstValDefinedInReg(DefMI, Reg, Imm)) | ||
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. [Re: line +3469] nit: I'd still prefer to initialize it even though if See this comment inline on Graphite. |
||
return false; | ||
|
||
auto getImmFor = [=](const MachineOperand &UseOp) -> int64_t { | ||
switch (UseOp.getSubReg()) { | ||
default: | ||
return Imm; | ||
case AMDGPU::sub0: | ||
return Lo_32(Imm); | ||
case AMDGPU::sub1: | ||
return Hi_32(Imm); | ||
case AMDGPU::lo16: | ||
return SignExtend64<16>(Imm); | ||
case AMDGPU::hi16: | ||
return SignExtend64<16>(Imm >> 16); | ||
case AMDGPU::sub1_lo16: | ||
return SignExtend64<16>(Imm >> 32); | ||
case AMDGPU::sub1_hi16: | ||
return SignExtend64<16>(Imm >> 48); | ||
} | ||
}; | ||
|
||
assert(!DefMI.getOperand(0).getSubReg() && "Expected SSA form"); | ||
|
||
unsigned Opc = UseMI.getOpcode(); | ||
|
@@ -3480,7 +3485,11 @@ bool SIInstrInfo::foldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, | |
: AMDGPU::V_MOV_B32_e32 | ||
: Is64Bit ? AMDGPU::S_MOV_B64_IMM_PSEUDO | ||
: AMDGPU::S_MOV_B32; | ||
APInt Imm(Is64Bit ? 64 : 32, getImmFor(UseMI.getOperand(1)), | ||
|
||
std::optional<int64_t> SubRegImm = | ||
extractSubregFromImm(Imm, UseMI.getOperand(1).getSubReg()); | ||
|
||
APInt Imm(Is64Bit ? 64 : 32, *SubRegImm, | ||
/*isSigned=*/true, /*implicitTrunc=*/true); | ||
|
||
if (RI.isAGPR(*MRI, DstReg)) { | ||
|
@@ -3591,7 +3600,8 @@ bool SIInstrInfo::foldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, | |
if (NewOpc == AMDGPU::V_FMAMK_F16_fake16) | ||
return false; | ||
|
||
const int64_t Imm = getImmFor(RegSrc == Src1 ? *Src0 : *Src1); | ||
const std::optional<int64_t> SubRegImm = extractSubregFromImm( | ||
Imm, RegSrc == Src1 ? Src0->getSubReg() : Src1->getSubReg()); | ||
|
||
// FIXME: This would be a lot easier if we could return a new instruction | ||
// instead of having to modify in place. | ||
|
@@ -3608,7 +3618,7 @@ bool SIInstrInfo::foldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, | |
UseMI.untieRegOperand( | ||
AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)); | ||
|
||
Src1->ChangeToImmediate(Imm); | ||
Src1->ChangeToImmediate(*SubRegImm); | ||
|
||
removeModOperands(UseMI); | ||
UseMI.setDesc(get(NewOpc)); | ||
|
@@ -3679,8 +3689,11 @@ bool SIInstrInfo::foldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, | |
UseMI.untieRegOperand( | ||
AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)); | ||
|
||
const std::optional<int64_t> SubRegImm = | ||
extractSubregFromImm(Imm, Src2->getSubReg()); | ||
|
||
// ChangingToImmediate adds Src2 back to the instruction. | ||
Src2->ChangeToImmediate(getImmFor(*Src2)); | ||
Src2->ChangeToImmediate(*SubRegImm); | ||
|
||
// These come before src2. | ||
removeModOperands(UseMI); | ||
|
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
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.
do we really need this to avoid compiler warning?
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.
Yes, this should be the version that makes msvc, gcc, and clang happy