-
Notifications
You must be signed in to change notification settings - Fork 15.2k
VirtRegRewriter: Add implicit register defs for live out undef lanes #112679
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 4 commits into
main
from
users/arsenm/issue98474-register-coalescer-verifier-error
Oct 29, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3bdaf20
VirtRegRewriter: Add super register defs for live out undef lanes
arsenm 41d4717
Try to fix clobbering lanes in other assigned lanes of physreg
arsenm f80bb29
Use LiveRegMatrix and only add necessary impdefs
arsenm 4456624
More comments
arsenm 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
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
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 |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| #include "llvm/CodeGen/LiveDebugVariables.h" | ||
| #include "llvm/CodeGen/LiveInterval.h" | ||
| #include "llvm/CodeGen/LiveIntervals.h" | ||
| #include "llvm/CodeGen/LiveRegMatrix.h" | ||
| #include "llvm/CodeGen/LiveStacks.h" | ||
| #include "llvm/CodeGen/MachineBasicBlock.h" | ||
| #include "llvm/CodeGen/MachineFrameInfo.h" | ||
|
|
@@ -203,6 +204,7 @@ class VirtRegRewriter : public MachineFunctionPass { | |
| MachineRegisterInfo *MRI = nullptr; | ||
| SlotIndexes *Indexes = nullptr; | ||
| LiveIntervals *LIS = nullptr; | ||
| LiveRegMatrix *LRM = nullptr; | ||
| VirtRegMap *VRM = nullptr; | ||
| LiveDebugVariables *DebugVars = nullptr; | ||
| DenseSet<Register> RewriteRegs; | ||
|
|
@@ -215,6 +217,9 @@ class VirtRegRewriter : public MachineFunctionPass { | |
| void handleIdentityCopy(MachineInstr &MI); | ||
| void expandCopyBundle(MachineInstr &MI) const; | ||
| bool subRegLiveThrough(const MachineInstr &MI, MCRegister SuperPhysReg) const; | ||
| LaneBitmask liveOutUndefPhiLanesForUndefSubregDef( | ||
| const LiveInterval &LI, const MachineBasicBlock &MBB, unsigned SubReg, | ||
| MCPhysReg PhysReg, const MachineInstr &MI) const; | ||
|
|
||
| public: | ||
| static char ID; | ||
|
|
@@ -247,6 +252,7 @@ INITIALIZE_PASS_BEGIN(VirtRegRewriter, "virtregrewriter", | |
| INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass) | ||
| INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass) | ||
| INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables) | ||
| INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperLegacy) | ||
| INITIALIZE_PASS_DEPENDENCY(LiveStacks) | ||
| INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy) | ||
| INITIALIZE_PASS_END(VirtRegRewriter, "virtregrewriter", | ||
|
|
@@ -262,6 +268,7 @@ void VirtRegRewriter::getAnalysisUsage(AnalysisUsage &AU) const { | |
| AU.addRequired<LiveStacks>(); | ||
| AU.addPreserved<LiveStacks>(); | ||
| AU.addRequired<VirtRegMapWrapperLegacy>(); | ||
| AU.addRequired<LiveRegMatrixWrapperLegacy>(); | ||
|
|
||
| if (!ClearVirtRegs) | ||
| AU.addPreserved<LiveDebugVariables>(); | ||
|
|
@@ -276,6 +283,7 @@ bool VirtRegRewriter::runOnMachineFunction(MachineFunction &fn) { | |
| MRI = &MF->getRegInfo(); | ||
| Indexes = &getAnalysis<SlotIndexesWrapperPass>().getSI(); | ||
| LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS(); | ||
| LRM = &getAnalysis<LiveRegMatrixWrapperLegacy>().getLRM(); | ||
| VRM = &getAnalysis<VirtRegMapWrapperLegacy>().getVRM(); | ||
| DebugVars = &getAnalysis<LiveDebugVariables>(); | ||
| LLVM_DEBUG(dbgs() << "********** REWRITE VIRTUAL REGISTERS **********\n" | ||
|
|
@@ -548,6 +556,40 @@ bool VirtRegRewriter::subRegLiveThrough(const MachineInstr &MI, | |
| return false; | ||
| } | ||
|
|
||
| /// Compute a lanemask for undef lanes which need to be preserved out of the | ||
| /// defining block for a register assignment for a subregister def. \p PhysReg | ||
| /// is assigned to \p LI, which is the main range. | ||
| LaneBitmask VirtRegRewriter::liveOutUndefPhiLanesForUndefSubregDef( | ||
| const LiveInterval &LI, const MachineBasicBlock &MBB, unsigned SubReg, | ||
| MCPhysReg PhysReg, const MachineInstr &MI) const { | ||
| LaneBitmask UndefMask = ~TRI->getSubRegIndexLaneMask(SubReg); | ||
| LaneBitmask LiveOutUndefLanes; | ||
|
|
||
| for (const LiveInterval::SubRange &SR : LI.subranges()) { | ||
| // Figure out which lanes are undef live into a successor. | ||
| LaneBitmask NeedImpDefLanes = UndefMask & SR.LaneMask; | ||
|
Collaborator
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. Add a comment that SubReg is an undef definition otherwise it is unclear (at least to me) why we would look at the other lanes. |
||
| if (NeedImpDefLanes.any() && !LIS->isLiveOutOfMBB(SR, &MBB)) { | ||
| for (const MachineBasicBlock *Succ : MBB.successors()) { | ||
| if (LIS->isLiveInToMBB(SR, Succ)) | ||
| LiveOutUndefLanes |= NeedImpDefLanes; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| SlotIndex MIIndex = LIS->getInstructionIndex(MI); | ||
| SlotIndex BeforeMIUses = MIIndex.getBaseIndex(); | ||
| LaneBitmask InterferingLanes = | ||
| LRM->checkInterferenceLanes(BeforeMIUses, MIIndex.getRegSlot(), PhysReg); | ||
| LiveOutUndefLanes &= ~InterferingLanes; | ||
|
|
||
| LLVM_DEBUG(if (LiveOutUndefLanes.any()) { | ||
| dbgs() << "Need live out undef defs for " << printReg(PhysReg) | ||
| << LiveOutUndefLanes << " from " << printMBBReference(MBB) << '\n'; | ||
| }); | ||
|
|
||
| return LiveOutUndefLanes; | ||
| } | ||
|
|
||
| void VirtRegRewriter::rewrite() { | ||
| bool NoSubRegLiveness = !MRI->subRegLivenessEnabled(); | ||
| SmallVector<Register, 8> SuperDeads; | ||
|
|
@@ -602,6 +644,32 @@ void VirtRegRewriter::rewrite() { | |
| MO.setIsUndef(true); | ||
| } else if (!MO.isDead()) { | ||
| assert(MO.isDef()); | ||
| if (MO.isUndef()) { | ||
| const LiveInterval &LI = LIS->getInterval(VirtReg); | ||
|
|
||
| LaneBitmask LiveOutUndefLanes = | ||
| liveOutUndefPhiLanesForUndefSubregDef(LI, *MBBI, SubReg, | ||
| PhysReg, MI); | ||
| if (LiveOutUndefLanes.any()) { | ||
| SmallVector<unsigned, 16> CoveringIndexes; | ||
|
|
||
| // TODO: Just use one super register def if none of the lanes | ||
| // are needed? | ||
| if (!TRI->getCoveringSubRegIndexes( | ||
| *MRI, MRI->getRegClass(VirtReg), LiveOutUndefLanes, | ||
| CoveringIndexes)) | ||
| llvm_unreachable( | ||
| "cannot represent required subregister defs"); | ||
|
|
||
| // Try to represent the minimum needed live out def as a | ||
| // sequence of subregister defs. | ||
| // | ||
| // FIXME: It would be better if we could directly represent | ||
| // liveness with a lanemask instead of spamming operands. | ||
| for (unsigned SubIdx : CoveringIndexes) | ||
| SuperDefs.push_back(TRI->getSubReg(PhysReg, SubIdx)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
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
Oops, something went wrong.
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.
Assert that SubReg is not 0.
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.
But I think it works correctly and returns a full mask for 0?
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.
Probably, but then there is nothing to do here.
Up to you.