-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[AMDGPU] Move S_BFE lowering into RegBankCombiner #141589
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: users/pierre-vh/gi-kb-sbfe
Are you sure you want to change the base?
Changes from all commits
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 | ||
---|---|---|---|---|
|
@@ -89,6 +89,8 @@ class AMDGPURegBankCombinerImpl : public Combiner { | |||
|
||||
void applyCanonicalizeZextShiftAmt(MachineInstr &MI, MachineInstr &Ext) const; | ||||
|
||||
bool lowerUniformBFX(MachineInstr &MI) const; | ||||
|
||||
private: | ||||
SIModeRegisterDefaults getMode() const; | ||||
bool getIEEE() const; | ||||
|
@@ -392,6 +394,56 @@ void AMDGPURegBankCombinerImpl::applyCanonicalizeZextShiftAmt( | |||
MI.eraseFromParent(); | ||||
} | ||||
|
||||
bool AMDGPURegBankCombinerImpl::lowerUniformBFX(MachineInstr &MI) const { | ||||
assert(MI.getOpcode() == TargetOpcode::G_UBFX || | ||||
MI.getOpcode() == TargetOpcode::G_SBFX); | ||||
const bool Signed = (MI.getOpcode() == TargetOpcode::G_SBFX); | ||||
|
||||
Register DstReg = MI.getOperand(0).getReg(); | ||||
const RegisterBank *RB = RBI.getRegBank(DstReg, MRI, TRI); | ||||
assert(RB && "No RB?"); | ||||
if (RB->getID() != AMDGPU::SGPRRegBankID) | ||||
return false; | ||||
|
||||
Register SrcReg = MI.getOperand(1).getReg(); | ||||
Register OffsetReg = MI.getOperand(2).getReg(); | ||||
Register WidthReg = MI.getOperand(3).getReg(); | ||||
|
||||
const LLT S32 = LLT::scalar(32); | ||||
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. nit: is it necessary to use 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. Do you mean why is the Or why using a 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. Having |
||||
LLT Ty = MRI.getType(DstReg); | ||||
|
||||
const unsigned Opc = Ty == S32 | ||||
? (Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32) | ||||
: (Signed ? AMDGPU::S_BFE_I64 : AMDGPU::S_BFE_U64); | ||||
|
||||
// Pack the offset and width of a BFE into | ||||
// the format expected by the S_BFE_I32 / S_BFE_U32. In the second | ||||
// source, bits [5:0] contain the offset and bits [22:16] the width. | ||||
|
||||
// Ensure the high bits are clear to insert the offset. | ||||
auto OffsetMask = B.buildConstant(S32, maskTrailingOnes<unsigned>(6)); | ||||
auto ClampOffset = B.buildAnd(S32, OffsetReg, OffsetMask); | ||||
Comment on lines
+423
to
+425
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. I don't think you need to do this, since the original G_*BFX node would have undefined result if the offset was out of range. The valid ranges are mentioned here:
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. I'm doing it so the high bits are zero and the width doesn't get overwritten, not really to sanitize the BFX's operand Do you prefer if I remove the AND ? 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. Yes I'd prefer removing the AND. |
||||
|
||||
// Zeros out the low bits, so don't bother clamping the input value. | ||||
auto ShiftAmt = B.buildConstant(S32, 16); | ||||
auto ShiftWidth = B.buildShl(S32, WidthReg, ShiftAmt); | ||||
|
||||
auto MergedInputs = B.buildOr(S32, ClampOffset, ShiftWidth); | ||||
|
||||
MRI.setRegBank(OffsetMask.getReg(0), *RB); | ||||
MRI.setRegBank(ClampOffset.getReg(0), *RB); | ||||
MRI.setRegBank(ShiftAmt.getReg(0), *RB); | ||||
MRI.setRegBank(ShiftWidth.getReg(0), *RB); | ||||
MRI.setRegBank(MergedInputs.getReg(0), *RB); | ||||
|
||||
auto MIB = B.buildInstr(Opc, {DstReg}, {SrcReg, MergedInputs}); | ||||
if (!constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI)) | ||||
llvm_unreachable("failed to constrain BFE"); | ||||
|
||||
MI.eraseFromParent(); | ||||
return true; | ||||
} | ||||
|
||||
SIModeRegisterDefaults AMDGPURegBankCombinerImpl::getMode() const { | ||||
return MF.getInfo<SIMachineFunctionInfo>()->getMode(); | ||||
} | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py | ||
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1010 -run-pass=amdgpu-regbank-combiner -verify-machineinstrs %s -o - | FileCheck %s | ||
|
||
--- | ||
name: test_s_bfe_i32__constants | ||
legalized: true | ||
regBankSelected: true | ||
tracksRegLiveness: true | ||
body: | | ||
bb.1: | ||
liveins: $sgpr0 | ||
|
||
; CHECK-LABEL: name: test_s_bfe_i32__constants | ||
; CHECK: liveins: $sgpr0 | ||
; CHECK-NEXT: {{ $}} | ||
; CHECK-NEXT: %reg:sreg_32(s32) = COPY $sgpr0 | ||
; CHECK-NEXT: %width:sgpr(s32) = G_CONSTANT i32 5 | ||
; CHECK-NEXT: %offset:sgpr(s32) = G_CONSTANT i32 7 | ||
; CHECK-NEXT: [[C:%[0-9]+]]:sgpr(s32) = G_CONSTANT i32 16 | ||
; CHECK-NEXT: [[SHL:%[0-9]+]]:sgpr(s32) = G_SHL %width, [[C]](s32) | ||
; CHECK-NEXT: [[OR:%[0-9]+]]:sreg_32(s32) = G_OR %offset, [[SHL]] | ||
; CHECK-NEXT: %bfx:sreg_32(s32) = S_BFE_I32 %reg(s32), [[OR]](s32), implicit-def $scc | ||
; CHECK-NEXT: $sgpr0 = COPY %bfx(s32) | ||
%reg:sgpr(s32) = COPY $sgpr0 | ||
%width:sgpr(s32) = G_CONSTANT i32 5 | ||
%offset:sgpr(s32) = G_CONSTANT i32 7 | ||
%bfx:sgpr(s32) = G_SBFX %reg, %offset, %width | ||
$sgpr0 = COPY %bfx | ||
... | ||
--- | ||
name: test_s_bfe_u32__constants | ||
legalized: true | ||
regBankSelected: true | ||
tracksRegLiveness: true | ||
body: | | ||
bb.1: | ||
liveins: $sgpr0 | ||
|
||
; CHECK-LABEL: name: test_s_bfe_u32__constants | ||
; CHECK: liveins: $sgpr0 | ||
; CHECK-NEXT: {{ $}} | ||
; CHECK-NEXT: %reg:sreg_32(s32) = COPY $sgpr0 | ||
; CHECK-NEXT: %width:sgpr(s32) = G_CONSTANT i32 5 | ||
; CHECK-NEXT: %offset:sgpr(s32) = G_CONSTANT i32 7 | ||
; CHECK-NEXT: [[C:%[0-9]+]]:sgpr(s32) = G_CONSTANT i32 16 | ||
; CHECK-NEXT: [[SHL:%[0-9]+]]:sgpr(s32) = G_SHL %width, [[C]](s32) | ||
; CHECK-NEXT: [[OR:%[0-9]+]]:sreg_32(s32) = G_OR %offset, [[SHL]] | ||
; CHECK-NEXT: %bfx:sreg_32(s32) = S_BFE_U32 %reg(s32), [[OR]](s32), implicit-def $scc | ||
; CHECK-NEXT: $sgpr0 = COPY %bfx(s32) | ||
%reg:sgpr(s32) = COPY $sgpr0 | ||
%width:sgpr(s32) = G_CONSTANT i32 5 | ||
%offset:sgpr(s32) = G_CONSTANT i32 7 | ||
%bfx:sgpr(s32) = G_UBFX %reg, %offset, %width | ||
$sgpr0 = COPY %bfx | ||
... | ||
--- | ||
name: test_s_bfe_i64__constants | ||
legalized: true | ||
regBankSelected: true | ||
tracksRegLiveness: true | ||
body: | | ||
bb.1: | ||
liveins: $sgpr0_sgpr1 | ||
|
||
; CHECK-LABEL: name: test_s_bfe_i64__constants | ||
; CHECK: liveins: $sgpr0_sgpr1 | ||
; CHECK-NEXT: {{ $}} | ||
; CHECK-NEXT: %reg:sreg_64(s64) = COPY $sgpr0_sgpr1 | ||
; CHECK-NEXT: %width:sgpr(s32) = G_CONSTANT i32 5 | ||
; CHECK-NEXT: %offset:sgpr(s32) = G_CONSTANT i32 7 | ||
; CHECK-NEXT: [[C:%[0-9]+]]:sgpr(s32) = G_CONSTANT i32 16 | ||
; CHECK-NEXT: [[SHL:%[0-9]+]]:sgpr(s32) = G_SHL %width, [[C]](s32) | ||
; CHECK-NEXT: [[OR:%[0-9]+]]:sreg_32(s32) = G_OR %offset, [[SHL]] | ||
; CHECK-NEXT: %bfx:sreg_64(s64) = S_BFE_I64 %reg(s64), [[OR]](s32), implicit-def $scc | ||
; CHECK-NEXT: $sgpr0_sgpr1 = COPY %bfx(s64) | ||
%reg:sgpr(s64) = COPY $sgpr0_sgpr1 | ||
%width:sgpr(s32) = G_CONSTANT i32 5 | ||
%offset:sgpr(s32) = G_CONSTANT i32 7 | ||
%bfx:sgpr(s64) = G_SBFX %reg, %offset, %width | ||
$sgpr0_sgpr1 = COPY %bfx | ||
... | ||
--- | ||
name: test_s_bfe_u64__constants | ||
legalized: true | ||
regBankSelected: true | ||
tracksRegLiveness: true | ||
body: | | ||
bb.1: | ||
liveins: $sgpr0_sgpr1 | ||
|
||
; CHECK-LABEL: name: test_s_bfe_u64__constants | ||
; CHECK: liveins: $sgpr0_sgpr1 | ||
; CHECK-NEXT: {{ $}} | ||
; CHECK-NEXT: %reg:sreg_64(s64) = COPY $sgpr0_sgpr1 | ||
; CHECK-NEXT: %width:sgpr(s32) = G_CONSTANT i32 5 | ||
; CHECK-NEXT: %offset:sgpr(s32) = G_CONSTANT i32 7 | ||
; CHECK-NEXT: [[C:%[0-9]+]]:sgpr(s32) = G_CONSTANT i32 16 | ||
; CHECK-NEXT: [[SHL:%[0-9]+]]:sgpr(s32) = G_SHL %width, [[C]](s32) | ||
; CHECK-NEXT: [[OR:%[0-9]+]]:sreg_32(s32) = G_OR %offset, [[SHL]] | ||
; CHECK-NEXT: %bfx:sreg_64(s64) = S_BFE_U64 %reg(s64), [[OR]](s32), implicit-def $scc | ||
; CHECK-NEXT: $sgpr0_sgpr1 = COPY %bfx(s64) | ||
%reg:sgpr(s64) = COPY $sgpr0_sgpr1 | ||
%width:sgpr(s32) = G_CONSTANT i32 5 | ||
%offset:sgpr(s32) = G_CONSTANT i32 7 | ||
%bfx:sgpr(s64) = G_UBFX %reg, %offset, %width | ||
$sgpr0_sgpr1 = COPY %bfx | ||
... |
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.
This needs more elaboration; needs to be clear that this can't be a mandatory lowering performed in a combiner
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.
What do you mean? I don't understand
Do you mean the combine name should avoid "lower" and be named something like
bfx_to_s_bfe
instead?I just thought about this but should I also make sure ISel doesn't crash on scalar BFXs by making it select the vector version all the time & inserting copies if the inputs are SGPRs? It's not a good result but at least our ISel wouldn't crash if that combine is skipped for some reason.