-
Notifications
You must be signed in to change notification settings - Fork 16k
[AMDGPU] Verify dominance when rewriting spills to registers #167347
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
Open
kerbowa
wants to merge
1
commit into
main
Choose a base branch
from
users/kerbowa/rewrite-agpr-copy-crash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |||||
| /// | ||||||
| //===----------------------------------------------------------------------===// | ||||||
|
|
||||||
| #include "AMDGPURewriteAGPRCopyMFMA.h" | ||||||
| #include "AMDGPU.h" | ||||||
| #include "GCNSubtarget.h" | ||||||
| #include "SIMachineFunctionInfo.h" | ||||||
|
|
@@ -30,6 +31,7 @@ | |||||
| #include "llvm/CodeGen/LiveIntervals.h" | ||||||
| #include "llvm/CodeGen/LiveRegMatrix.h" | ||||||
| #include "llvm/CodeGen/LiveStacks.h" | ||||||
| #include "llvm/CodeGen/MachineDominators.h" | ||||||
| #include "llvm/CodeGen/MachineFrameInfo.h" | ||||||
| #include "llvm/CodeGen/MachineFunctionPass.h" | ||||||
| #include "llvm/CodeGen/VirtRegMap.h" | ||||||
|
|
@@ -39,6 +41,72 @@ using namespace llvm; | |||||
|
|
||||||
| #define DEBUG_TYPE "amdgpu-rewrite-agpr-copy-mfma" | ||||||
|
|
||||||
| namespace llvm { | ||||||
| namespace AMDGPU { | ||||||
| bool checkAGPRCopyMFMAJointDominance( | ||||||
| const MachineFunction &MF, const MachineDominatorTree &MDT, | ||||||
| const SmallVectorImpl<MachineInstr *> &Stores, | ||||||
| const SmallVectorImpl<MachineInstr *> &Loads, int Slot) { | ||||||
| SmallPtrSet<MachineBasicBlock *, 4> StoreBlocks; | ||||||
| for (MachineInstr *S : Stores) | ||||||
| if (MDT.isReachableFromEntry(S->getParent())) | ||||||
| StoreBlocks.insert(S->getParent()); | ||||||
|
|
||||||
| if (StoreBlocks.empty()) | ||||||
| return false; | ||||||
|
|
||||||
| // Compute blocks reachable from entry without passing through a store | ||||||
| // block. | ||||||
| SmallPtrSet<MachineBasicBlock *, 16> StoreFreeReachable; | ||||||
| SmallVector<MachineBasicBlock *, 16> Worklist; | ||||||
|
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.
Suggested change
|
||||||
|
|
||||||
| MachineBasicBlock &EntryMBB = const_cast<MachineBasicBlock &>(MF.front()); | ||||||
| Worklist.push_back(&EntryMBB); | ||||||
| StoreFreeReachable.insert(&EntryMBB); | ||||||
|
|
||||||
| while (!Worklist.empty()) { | ||||||
| MachineBasicBlock *MBB = Worklist.pop_back_val(); | ||||||
| if (StoreBlocks.contains(MBB)) | ||||||
| continue; | ||||||
|
|
||||||
| for (MachineBasicBlock *Succ : MBB->successors()) { | ||||||
| if (StoreFreeReachable.insert(Succ).second) | ||||||
| Worklist.push_back(Succ); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| auto IsLoadJointlyDominatedByStores = [&](MachineInstr *LoadMI) -> bool { | ||||||
| MachineBasicBlock *LoadMBB = LoadMI->getParent(); | ||||||
| if (!MDT.isReachableFromEntry(LoadMBB)) | ||||||
| return true; | ||||||
|
|
||||||
| // Check if every path passed through a store block. | ||||||
| if (!StoreFreeReachable.contains(LoadMBB)) | ||||||
| return true; | ||||||
|
|
||||||
| // Otherwise, there exists a path to this block that has not seen any | ||||||
| // store yet. We must ensure that within this block there is a store to | ||||||
| // this slot before the load. | ||||||
| for (MachineInstr &MI : *LoadMBB) { | ||||||
| if (&MI == LoadMI) | ||||||
| break; | ||||||
| if (MI.mayStore()) { | ||||||
| for (MachineOperand &MO : MI.operands()) { | ||||||
| if (MO.isFI() && MO.getIndex() == Slot) | ||||||
| return true; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return false; | ||||||
| }; | ||||||
|
|
||||||
| return llvm::all_of(Loads, IsLoadJointlyDominatedByStores); | ||||||
| } | ||||||
|
|
||||||
| } // namespace AMDGPU | ||||||
| } // namespace llvm | ||||||
|
|
||||||
| namespace { | ||||||
|
|
||||||
| STATISTIC(NumMFMAsRewrittenToAGPR, | ||||||
|
|
@@ -58,6 +126,7 @@ class AMDGPURewriteAGPRCopyMFMAImpl { | |||||
| LiveIntervals &LIS; | ||||||
| LiveStacks &LSS; | ||||||
| const RegisterClassInfo &RegClassInfo; | ||||||
| MachineDominatorTree &MDT; | ||||||
|
|
||||||
| bool attemptReassignmentsToAGPR(SmallSetVector<Register, 4> &InterferingRegs, | ||||||
| MCPhysReg PrefPhysReg) const; | ||||||
|
|
@@ -66,10 +135,11 @@ class AMDGPURewriteAGPRCopyMFMAImpl { | |||||
| AMDGPURewriteAGPRCopyMFMAImpl(MachineFunction &MF, VirtRegMap &VRM, | ||||||
| LiveRegMatrix &LRM, LiveIntervals &LIS, | ||||||
| LiveStacks &LSS, | ||||||
| const RegisterClassInfo &RegClassInfo) | ||||||
| const RegisterClassInfo &RegClassInfo, | ||||||
| MachineDominatorTree &MDT) | ||||||
| : MF(MF), ST(MF.getSubtarget<GCNSubtarget>()), TII(*ST.getInstrInfo()), | ||||||
| TRI(*ST.getRegisterInfo()), MRI(MF.getRegInfo()), VRM(VRM), LRM(LRM), | ||||||
| LIS(LIS), LSS(LSS), RegClassInfo(RegClassInfo) {} | ||||||
| LIS(LIS), LSS(LSS), RegClassInfo(RegClassInfo), MDT(MDT) {} | ||||||
|
|
||||||
| bool isRewriteCandidate(const MachineInstr &MI) const { | ||||||
| return TII.isMAI(MI) && AMDGPU::getMFMASrcCVDstAGPROp(MI.getOpcode()) != -1; | ||||||
|
|
@@ -515,6 +585,37 @@ void AMDGPURewriteAGPRCopyMFMAImpl::eliminateSpillsOfReassignedVGPRs() const { | |||||
| if (SpillReferences == SpillSlotReferences.end()) | ||||||
| continue; | ||||||
|
|
||||||
| // For each spill reload, every path from entry to the reload must pass | ||||||
| // through at least one spill store to the same stack slot. | ||||||
| SmallVector<MachineInstr *, 4> Stores, Loads; | ||||||
| Stores.reserve(SpillReferences->second.size()); | ||||||
| Loads.reserve(SpillReferences->second.size()); | ||||||
| for (MachineInstr *MI : SpillReferences->second) { | ||||||
| if (MI->mayStore()) | ||||||
| Stores.push_back(MI); | ||||||
| else if (MI->mayLoad()) | ||||||
| Loads.push_back(MI); | ||||||
| } | ||||||
|
|
||||||
| SmallPtrSet<MachineBasicBlock *, 4> StoreBlocks; | ||||||
| for (MachineInstr *S : Stores) | ||||||
| if (MDT.isReachableFromEntry(S->getParent())) | ||||||
| StoreBlocks.insert(S->getParent()); | ||||||
|
|
||||||
| if (StoreBlocks.empty()) { | ||||||
| LLVM_DEBUG(dbgs() << "Skipping " << printReg(Slot, &TRI) | ||||||
| << ": no reachable stores\n"); | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| if (!AMDGPU::checkAGPRCopyMFMAJointDominance(MF, MDT, Stores, Loads, | ||||||
| Slot)) { | ||||||
| LLVM_DEBUG( | ||||||
| dbgs() << "Skipping " << printReg(Slot, &TRI) | ||||||
| << ": some reachable load not jointly dominated by stores\n"); | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| const TargetRegisterClass *RC = LSS.getIntervalRegClass(Slot); | ||||||
|
|
||||||
| LLVM_DEBUG(dbgs() << "Trying to eliminate " << printReg(Slot, &TRI) | ||||||
|
|
@@ -603,11 +704,13 @@ class AMDGPURewriteAGPRCopyMFMALegacy : public MachineFunctionPass { | |||||
| AU.addRequired<VirtRegMapWrapperLegacy>(); | ||||||
| AU.addRequired<LiveRegMatrixWrapperLegacy>(); | ||||||
| AU.addRequired<LiveStacksWrapperLegacy>(); | ||||||
| AU.addRequired<MachineDominatorTreeWrapperPass>(); | ||||||
|
|
||||||
| AU.addPreserved<LiveIntervalsWrapperPass>(); | ||||||
| AU.addPreserved<VirtRegMapWrapperLegacy>(); | ||||||
| AU.addPreserved<LiveRegMatrixWrapperLegacy>(); | ||||||
| AU.addPreserved<LiveStacksWrapperLegacy>(); | ||||||
| AU.addPreserved<MachineDominatorTreeWrapperPass>(); | ||||||
|
|
||||||
| AU.setPreservesAll(); | ||||||
| MachineFunctionPass::getAnalysisUsage(AU); | ||||||
|
|
@@ -622,6 +725,7 @@ INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass) | |||||
| INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy) | ||||||
| INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperLegacy) | ||||||
| INITIALIZE_PASS_DEPENDENCY(LiveStacksWrapperLegacy) | ||||||
| INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass) | ||||||
| INITIALIZE_PASS_END(AMDGPURewriteAGPRCopyMFMALegacy, DEBUG_TYPE, | ||||||
| "AMDGPU Rewrite AGPR-Copy-MFMA", false, false) | ||||||
|
|
||||||
|
|
@@ -641,7 +745,8 @@ bool AMDGPURewriteAGPRCopyMFMALegacy::runOnMachineFunction( | |||||
| auto &LRM = getAnalysis<LiveRegMatrixWrapperLegacy>().getLRM(); | ||||||
| auto &LIS = getAnalysis<LiveIntervalsWrapperPass>().getLIS(); | ||||||
| auto &LSS = getAnalysis<LiveStacksWrapperLegacy>().getLS(); | ||||||
| AMDGPURewriteAGPRCopyMFMAImpl Impl(MF, VRM, LRM, LIS, LSS, RegClassInfo); | ||||||
| auto &MDT = getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree(); | ||||||
| AMDGPURewriteAGPRCopyMFMAImpl Impl(MF, VRM, LRM, LIS, LSS, RegClassInfo, MDT); | ||||||
| return Impl.run(MF); | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -652,10 +757,11 @@ AMDGPURewriteAGPRCopyMFMAPass::run(MachineFunction &MF, | |||||
| LiveRegMatrix &LRM = MFAM.getResult<LiveRegMatrixAnalysis>(MF); | ||||||
| LiveIntervals &LIS = MFAM.getResult<LiveIntervalsAnalysis>(MF); | ||||||
| LiveStacks &LSS = MFAM.getResult<LiveStacksAnalysis>(MF); | ||||||
| MachineDominatorTree &MDT = MFAM.getResult<MachineDominatorTreeAnalysis>(MF); | ||||||
| RegisterClassInfo RegClassInfo; | ||||||
| RegClassInfo.runOnMachineFunction(MF); | ||||||
|
|
||||||
| AMDGPURewriteAGPRCopyMFMAImpl Impl(MF, VRM, LRM, LIS, LSS, RegClassInfo); | ||||||
| AMDGPURewriteAGPRCopyMFMAImpl Impl(MF, VRM, LRM, LIS, LSS, RegClassInfo, MDT); | ||||||
| if (!Impl.run(MF)) | ||||||
| return PreservedAnalyses::all(); | ||||||
| auto PA = getMachineFunctionPassPreservedAnalyses(); | ||||||
|
|
||||||
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 |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| //===- AMDGPURewriteAGPRCopyMFMA.h ------------------------------*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUREWRITEAGPRCOPYMFMA_H | ||
| #define LLVM_LIB_TARGET_AMDGPU_AMDGPUREWRITEAGPRCOPYMFMA_H | ||
|
|
||
| #include "llvm/ADT/SmallVector.h" | ||
|
|
||
| namespace llvm { | ||
|
|
||
| class MachineFunction; | ||
| class MachineDominatorTree; | ||
| class MachineInstr; | ||
|
|
||
| namespace AMDGPU { | ||
|
|
||
| /// Returns true if every reload in \p Loads is jointly dominated by at least | ||
| /// one store in \p Stores to the same stack slot \p Slot. | ||
| bool checkAGPRCopyMFMAJointDominance( | ||
| const MachineFunction &MF, const MachineDominatorTree &MDT, | ||
| const SmallVectorImpl<MachineInstr *> &Stores, | ||
| const SmallVectorImpl<MachineInstr *> &Loads, int Slot); | ||
|
|
||
| } // end namespace AMDGPU | ||
| } // end namespace llvm | ||
|
|
||
| #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUREWRITEAGPRCOPYMFMA_H |
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.
It feels like this is reinventing LiveStacks, which the pass already depends on