-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[AMDGPU] Analyze REG_SEQUENCE To Remove Redundant CMP Instructions #167364
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
base: main
Are you sure you want to change the base?
Changes from all commits
65e8164
40a6671
960bdd3
51956c9
de0102e
221099c
9c017a9
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 |
|---|---|---|
|
|
@@ -1313,6 +1313,34 @@ Register SIInstrInfo::insertNE(MachineBasicBlock *MBB, | |
| return Reg; | ||
| } | ||
|
|
||
| MachineInstr * | ||
| SIInstrInfo::pierceThroughRegSequence(const MachineInstr &MI) const { | ||
| if (MI.getOpcode() != AMDGPU::REG_SEQUENCE || MI.getNumOperands() != 5) | ||
| return nullptr; | ||
|
|
||
|
Contributor
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. Check if MI has wrong number of operands.
Contributor
Author
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. Done |
||
| const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); | ||
| int64_t SubRegValues[2]; | ||
| bool SubRegIsConst[2]; | ||
| MachineInstr *RealDefs[2]; | ||
| for (unsigned I : {2, 4}) { | ||
| unsigned ArrayIdx = MI.getOperand(I).getImm() == AMDGPU::sub0 ? 0 : 1; | ||
| Register Subreg = MI.getOperand(I - 1).getReg(); | ||
| RealDefs[ArrayIdx] = MRI.getUniqueVRegDef(Subreg); | ||
|
Contributor
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. Since this is operating on SSA form, getVRegDef is better. Also, check if return value is null. |
||
| SubRegIsConst[ArrayIdx] = getConstValDefinedInReg( | ||
| *RealDefs[ArrayIdx], Subreg, SubRegValues[ArrayIdx]); | ||
| } | ||
|
|
||
| for (unsigned I : {0, 1}) | ||
| if (SubRegIsConst[I] && !SubRegValues[I] && | ||
| MRI.getRegClass(RealDefs[(I + 1) % 2]->getOperand(0).getReg()) | ||
| ->MC->getSizeInBits() * | ||
| 2 == | ||
| MRI.getRegClass(MI.getOperand(0).getReg())->MC->getSizeInBits()) | ||
| return RealDefs[(I + 1) % 2]; | ||
|
|
||
| return nullptr; | ||
| } | ||
|
|
||
| bool SIInstrInfo::getConstValDefinedInReg(const MachineInstr &MI, | ||
| const Register Reg, | ||
| int64_t &ImmVal) const { | ||
|
|
@@ -10698,6 +10726,9 @@ bool SIInstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg, | |
| if (!Def) | ||
| return false; | ||
|
|
||
| if (MachineInstr *RegSequenceDef = pierceThroughRegSequence(*Def)) | ||
| Def = RegSequenceDef; | ||
|
Comment on lines
+10729
to
+10730
Contributor
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. You need to know the type / subregister at the def and use point, this doesn't have enough context involved to be correct. getConstValDefinedInReg should probably stick to the trivial cases
Contributor
Author
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. @arsenm I can confirm that the function |
||
|
|
||
| // For S_OP that set SCC = DST!=0, do the transformation | ||
| // | ||
| // s_cmp_lg_* (S_OP ...), 0 => (S_OP ...) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6 | ||
| ; RUN: llc -mtriple=amdgcn -mcpu=gfx900 < %s | FileCheck %s | ||
| define amdgpu_ps i64 @ordertest(i64 inreg %val0) { | ||
|
Contributor
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. What does the MIR look like at this point?
Contributor
Author
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. @arsenm here is the MIR just prior to peephole-opt: |
||
| ; CHECK-LABEL: ordertest: | ||
| ; CHECK: ; %bb.0: | ||
| ; CHECK-NEXT: s_lshr_b32 s0, s1, 2 | ||
| ; CHECK-NEXT: s_cselect_b64 s[2:3], -1, 0 | ||
| ; CHECK-NEXT: s_mov_b32 s1, 0 | ||
| ; CHECK-NEXT: v_cndmask_b32_e64 v2, 0, 1, s[2:3] | ||
| ; CHECK-NEXT: v_lshrrev_b64 v[0:1], v2, s[0:1] | ||
| ; CHECK-NEXT: v_xor_b32_e32 v0, v2, v0 | ||
| ; CHECK-NEXT: v_readfirstlane_b32 s0, v0 | ||
| ; CHECK-NEXT: ; return to shader part epilog | ||
| %shl = lshr i64 %val0, 34 | ||
| %result = and i64 %shl, 4294967295 | ||
| %cmp = icmp ne i64 %result, 0 | ||
| %zext = zext i1 %cmp to i64 | ||
| %param0 = lshr i64 %shl, %zext | ||
| %param = and i64 %param0, 4294967295 | ||
| %xory = xor i64 %zext, %param | ||
| ret i64 %xory | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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 think it would be helpful to add a comment describing what is being done. Something like:
For a 64-bit value defined by a REG_SEQUENCE with half of the result being 0, find the instruction that defines exactly the bits in the other half.