-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[AMDGPU] Insert copy when only one register can be constrained #161435
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?
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-backend-amdgpu Author: Nico Núñez (niconunezz) ChangesResolve TODO: insert copy when only one register can be constrained. Full diff: https://github.com/llvm/llvm-project/pull/161435.diff 2 Files Affected:
diff --git a/llvm/lib/Target/AMDGPU/SILoadStoreOptimizer.cpp b/llvm/lib/Target/AMDGPU/SILoadStoreOptimizer.cpp
index f0d1117664983..21bd75c229042 100644
--- a/llvm/lib/Target/AMDGPU/SILoadStoreOptimizer.cpp
+++ b/llvm/lib/Target/AMDGPU/SILoadStoreOptimizer.cpp
@@ -1352,12 +1352,21 @@ SILoadStoreOptimizer::checkAndPrepareMerge(CombineInfo &CI,
DataRC1, SubReg);
}
- if (!MRI->constrainRegClass(Data0->getReg(), DataRC0) ||
- !MRI->constrainRegClass(Data1->getReg(), DataRC1))
+ bool constrainData0 = MRI->constrainRegClass(Data0->getReg(), DataRC0);
+ bool constrainData1 = MRI->constrainRegClass(Data1->getReg(), DataRC1);
+ if (!constrainData0 && !constrainData1) {
return nullptr;
-
- // TODO: If one register can be constrained, and not the other, insert a
- // copy.
+ } else if (!constrainData0 || !constrainData1) {
+ MachineBasicBlock::iterator InsertBefore = CI.I;
+ MachineBasicBlock *MBB = CI.I->getParent();
+ DebugLoc DL = CI.I->getDebugLoc();
+ const MachineOperand *activeData = !constrainData0 ? Data0 : Data1;
+ Register BaseReg = MRI->createVirtualRegister(&AMDGPU::VGPR_32RegClass);
+ const MCInstrDesc &CopyDesc = TII->get(TargetOpcode::COPY);
+ BuildMI(*MBB, InsertBefore, DL, CopyDesc, BaseReg)
+ .addReg(activeData->getReg(), 0);
+ const_cast<MachineOperand *>(activeData)->setReg(BaseReg);
+ }
}
return Where;
diff --git a/llvm/test/CodeGen/AMDGPU/load-store-opt-ds-regclass-constrain.mir b/llvm/test/CodeGen/AMDGPU/load-store-opt-ds-regclass-constrain.mir
index 33f210533e10b..7a505f599254e 100644
--- a/llvm/test/CodeGen/AMDGPU/load-store-opt-ds-regclass-constrain.mir
+++ b/llvm/test/CodeGen/AMDGPU/load-store-opt-ds-regclass-constrain.mir
@@ -206,5 +206,42 @@ body: |
%2:av_64_align2 = COPY $vgpr4_vgpr5
DS_WRITE_B64_gfx9 %0, %1, 512, 0, implicit $exec :: (store (s64), addrspace 3)
DS_WRITE_B64_gfx9 %0, %2, 1536, 0, implicit $exec :: (store (s64), addrspace 3)
+...
+
+---
+name: ds_write_b32__av32_physical
+body: |
+ bb.0:
+ liveins: $vgpr0, $vgpr1, $vgpr2
+
+ ; CHECK-LABEL: name: ds_write_b32__av32_physical
+ ; CHECK: liveins: $vgpr0, $vgpr1, $vgpr2
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:vgpr_32 = COPY $vgpr0
+ ; CHECK-NEXT: [[COPY1:%[0-9]+]]:vgpr_32 = COPY $vgpr1
+ ; CHECK-NEXT: [[COPY2:%[0-9]+]]:vgpr_32 = COPY $vgpr2
+ ; CHECK-NEXT: DS_WRITE2_B32_gfx9 [[COPY]], [[COPY1]], [[COPY2]], 10, 24, 0, implicit $exec :: (store (s32), addrspace 3)
+ %0:vgpr_32 = COPY $vgpr0
+ %1:av_32 = COPY $vgpr1
+ DS_WRITE_B32_gfx9 %0, %1, 40, 0, implicit $exec :: (store (s32), addrspace 3)
+ DS_WRITE_B32_gfx9 %0, $vgpr2, 96, 0, implicit $exec :: (store (s32), addrspace 3)
...
+
+---
+name: ds_write_b32__physical_av32
+body: |
+ bb.0:
+ liveins: $vgpr0, $vgpr1, $vgpr2
+
+ ; CHECK-LABEL: name: ds_write_b32__physical_av32
+ ; CHECK: liveins: $vgpr0, $vgpr1, $vgpr2
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:vgpr_32 = COPY $vgpr0
+ ; CHECK-NEXT: [[COPY1:%[0-9]+]]:vgpr_32 = COPY $vgpr2
+ ; CHECK-NEXT: [[COPY2:%[0-9]+]]:vgpr_32 = COPY $vgpr1
+ ; CHECK-NEXT: DS_WRITE2_B32_gfx9 [[COPY]], [[COPY2]], [[COPY1]], 10, 24, 0, implicit $exec :: (store (s32), addrspace 3)
+ %0:vgpr_32 = COPY $vgpr0
+ %2:av_32 = COPY $vgpr2
+ DS_WRITE_B32_gfx9 %0, $vgpr1, 40, 0, implicit $exec :: (store (s32), addrspace 3)
+ DS_WRITE_B32_gfx9 %0, %2, 96, 0, implicit $exec :: (store (s32), addrspace 3)
\ No newline at end of file
|
const MCInstrDesc &CopyDesc = TII->get(TargetOpcode::COPY); | ||
BuildMI(*MBB, InsertBefore, DL, CopyDesc, BaseReg) | ||
.addReg(activeData->getReg(), 0); | ||
const_cast<MachineOperand *>(activeData)->setReg(BaseReg); |
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.
No const_cast. Can you defer this until the transform is performed
llvm/test/CodeGen/AMDGPU/load-store-opt-ds-regclass-constrain.mir
Outdated
Show resolved
Hide resolved
llvm/test/CodeGen/AMDGPU/load-store-opt-ds-regclass-constrain.mir
Outdated
Show resolved
Hide resolved
@arsenm Thanks for the feedback! I've implemented the suggested changes. Let me know if you'd like me to adjust anything else. |
llvm/test/CodeGen/AMDGPU/load-store-opt-ds-regclass-constrain.mir
Outdated
Show resolved
Hide resolved
llvm/test/CodeGen/AMDGPU/load-store-opt-ds-regclass-constrain.mir
Outdated
Show resolved
Hide resolved
llvm/test/CodeGen/AMDGPU/load-store-opt-ds-regclass-constrain.mir
Outdated
Show resolved
Hide resolved
llvm/test/CodeGen/AMDGPU/load-store-opt-ds-regclass-constrain.mir
Outdated
Show resolved
Hide resolved
Co-authored-by: Matt Arsenault <[email protected]>
Co-authored-by: Matt Arsenault <[email protected]>
Co-authored-by: Matt Arsenault <[email protected]>
Co-authored-by: Matt Arsenault <[email protected]>
Co-authored-by: Matt Arsenault <[email protected]>
@arsenm done! |
Resolve TODO: insert copy when only one register can be constrained.
Helps with #155769 .