-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Move SI Lower Control Flow Up #159557
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
Draft
linuxrocks123
wants to merge
8
commits into
llvm:main
Choose a base branch
from
linuxrocks123:regalloc-1
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.
Draft
Move SI Lower Control Flow Up #159557
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
508836b
In-progress work
linuxrocks123 ca2af95
Finish initial implementation ... I hope.
linuxrocks123 301f997
Now to write the pass to get rid of the bundles
linuxrocks123 df60d5a
For my next act, I'll actually make this SSA form by creating differe…
linuxrocks123 c701bea
More work
linuxrocks123 39c58b7
Works on simple testcase
linuxrocks123 37ac452
Must delete from all predecessors if lowering.
linuxrocks123 07f9460
clang-format and camelcaser
linuxrocks123 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
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,245 @@ | ||
#pragma once | ||
|
||
#include "GCNSubtarget.h" | ||
#include "MCTargetDesc/AMDGPUMCTargetDesc.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/CodeGen/MachineBasicBlock.h" | ||
#include "llvm/CodeGen/MachineInstr.h" | ||
#include "llvm/Support/ErrorHandling.h" | ||
|
||
#include "SIInstrInfo.h" | ||
|
||
#include <cassert> | ||
#include <unordered_set> | ||
|
||
using namespace llvm; | ||
|
||
using std::unordered_set; | ||
using std::vector; | ||
|
||
static inline MachineInstr &getBranchWithDest(MachineBasicBlock &BranchingMBB, | ||
MachineBasicBlock &DestMBB) { | ||
auto &TII = | ||
*BranchingMBB.getParent()->getSubtarget<GCNSubtarget>().getInstrInfo(); | ||
for (MachineInstr &BranchMI : reverse(BranchingMBB.instrs())) | ||
if (BranchMI.isBranch() && TII.getBranchDestBlock(BranchMI) == &DestMBB) | ||
return BranchMI; | ||
|
||
llvm_unreachable("Don't call this if there's no branch to the destination."); | ||
} | ||
|
||
static inline void moveInsBeforePhis(MachineInstr &MI) { | ||
MachineBasicBlock &MBB = *MI.getParent(); | ||
MachineFunction &MF = *MBB.getParent(); | ||
auto &TII = *MF.getSubtarget<GCNSubtarget>().getInstrInfo(); | ||
auto &MRI = MF.getRegInfo(); | ||
|
||
bool PhiSeen = false; | ||
MachineBasicBlock::iterator FirstPhi; | ||
for (FirstPhi = MBB.begin(); FirstPhi != MBB.end(); FirstPhi++) | ||
if (FirstPhi->getOpcode() == AMDGPU::PHI) { | ||
PhiSeen = true; | ||
break; | ||
} | ||
|
||
if (!PhiSeen) { | ||
MI.removeFromParent(); | ||
MBB.insert(MBB.begin(), &MI); | ||
} else { | ||
auto Phi = BuildMI(MBB, FirstPhi, MI.getDebugLoc(), TII.get(AMDGPU::PHI), | ||
MI.getOperand(0).getReg()); | ||
for (auto *PredMBB : MBB.predecessors()) { | ||
Register ClonedReg = MRI.cloneVirtualRegister(MI.getOperand(0).getReg()); | ||
MachineInstr &BranchMI = getBranchWithDest(*PredMBB, MBB); | ||
MachineInstr *ClonedMI = MF.CloneMachineInstr(&MI); | ||
ClonedMI->getOperand(0).setReg(ClonedReg); | ||
Phi.addReg(ClonedReg).addMBB(PredMBB); | ||
PredMBB->insertAfterBundle(BranchMI.getIterator(), ClonedMI); | ||
ClonedMI->bundleWithPred(); | ||
} | ||
MI.eraseFromParent(); | ||
} | ||
} | ||
|
||
struct EpilogIterator { | ||
MachineBasicBlock::instr_iterator InternalIt; | ||
EpilogIterator(MachineBasicBlock::instr_iterator I) : InternalIt(I) {} | ||
|
||
bool operator==(const EpilogIterator &Other) { | ||
return InternalIt == Other.InternalIt; | ||
} | ||
bool isEnd() { return InternalIt.isEnd(); } | ||
MachineInstr &operator*() { return *InternalIt; } | ||
MachineBasicBlock::instr_iterator operator->() { return InternalIt; } | ||
EpilogIterator &operator++() { | ||
++InternalIt; | ||
if (!InternalIt.isEnd() && InternalIt->isBranch()) | ||
InternalIt = InternalIt->getParent()->instr_end(); | ||
return *this; | ||
} | ||
EpilogIterator operator++(int Ignored) { | ||
EpilogIterator ToReturn = *this; | ||
++*this; | ||
return ToReturn; | ||
} | ||
}; | ||
|
||
static inline EpilogIterator getEpilogForSuccessor(MachineBasicBlock &PredMBB, | ||
MachineBasicBlock &SuccMBB) { | ||
MachineFunction &MF = *PredMBB.getParent(); | ||
auto &TII = *MF.getSubtarget<GCNSubtarget>().getInstrInfo(); | ||
|
||
for (MachineInstr &BranchMI : reverse(PredMBB.instrs())) | ||
if (BranchMI.isBranch() && TII.getBranchDestBlock(BranchMI) == &SuccMBB) | ||
return ++EpilogIterator(BranchMI.getIterator()); | ||
|
||
llvm_unreachable("There should always be a branch to succ_MBB."); | ||
} | ||
|
||
static inline bool epilogsAreIdentical(const vector<MachineInstr *> Left, | ||
const vector<MachineInstr *> Right, | ||
const MachineBasicBlock &SuccMBB) { | ||
if (Left.size() != Right.size()) | ||
return false; | ||
|
||
for (unsigned I = 0; I < Left.size(); I++) | ||
if (!Left[I]->isIdenticalTo(*Right[I])) | ||
return false; | ||
return true; | ||
} | ||
|
||
static inline void moveBody(vector<MachineInstr *> &Body, | ||
MachineBasicBlock &DestMBB) { | ||
for (auto RevIt = Body.rbegin(); RevIt != Body.rend(); RevIt++) { | ||
MachineInstr &BodyIns = **RevIt; | ||
BodyIns.removeFromBundle(); | ||
DestMBB.insert(DestMBB.begin(), &BodyIns); | ||
} | ||
} | ||
|
||
static inline void normalizeIrPostPhiElimination(MachineFunction &MF) { | ||
auto &TII = *MF.getSubtarget<GCNSubtarget>().getInstrInfo(); | ||
|
||
struct CFGRewriteEntry { | ||
unordered_set<MachineBasicBlock *> PredMBBs; | ||
MachineBasicBlock *SuccMBB; | ||
vector<MachineInstr *> Body; | ||
}; | ||
|
||
vector<CFGRewriteEntry> CfgRewriteEntries; | ||
for (MachineBasicBlock &MBB : MF) { | ||
CFGRewriteEntry ToInsert = {{}, &MBB, {}}; | ||
for (MachineBasicBlock *PredMBB : MBB.predecessors()) { | ||
EpilogIterator EpIt = getEpilogForSuccessor(*PredMBB, MBB); | ||
|
||
vector<MachineInstr *> Epilog; | ||
while (!EpIt.isEnd()) | ||
Epilog.push_back(&*EpIt++); | ||
|
||
if (!epilogsAreIdentical(ToInsert.Body, Epilog, MBB)) { | ||
if (ToInsert.PredMBBs.size() && ToInsert.Body.size()) { | ||
// Potentially, we need to insert a new entry. But first see if we | ||
// can find an existing entry with the same epilog. | ||
bool ExistingEntryFound = false; | ||
for (auto RevIt = CfgRewriteEntries.rbegin(); | ||
RevIt != CfgRewriteEntries.rend() && RevIt->SuccMBB == &MBB; | ||
RevIt++) | ||
if (epilogsAreIdentical(RevIt->Body, Epilog, MBB)) { | ||
RevIt->PredMBBs.insert(PredMBB); | ||
ExistingEntryFound = true; | ||
break; | ||
} | ||
|
||
if (!ExistingEntryFound) | ||
CfgRewriteEntries.push_back(ToInsert); | ||
} | ||
ToInsert.PredMBBs.clear(); | ||
ToInsert.Body = Epilog; | ||
} | ||
|
||
ToInsert.PredMBBs.insert(PredMBB); | ||
} | ||
|
||
// Handle the last potential rewrite entry. Lower instead of journaling a | ||
// rewrite entry if all predecessor MBBs are in this single entry. | ||
if (ToInsert.PredMBBs.size() == MBB.pred_size()) { | ||
moveBody(ToInsert.Body, MBB); | ||
for (MachineBasicBlock *PredMBB : ToInsert.PredMBBs) { | ||
// Delete instructions that were lowered from epilog | ||
MachineInstr &BranchIns = | ||
getBranchWithDest(*PredMBB, *ToInsert.SuccMBB); | ||
auto EpilogIt = ++EpilogIterator(BranchIns.getIterator()); | ||
while (!EpilogIt.isEnd()) | ||
EpilogIt++->eraseFromBundle(); | ||
} | ||
|
||
} else if (ToInsert.Body.size()) | ||
CfgRewriteEntries.push_back(ToInsert); | ||
} | ||
|
||
// Perform the journaled rewrites. | ||
for (auto &Entry : CfgRewriteEntries) { | ||
MachineBasicBlock *MezzanineMBB = MF.CreateMachineBasicBlock(); | ||
MF.insert(MF.end(), MezzanineMBB); | ||
|
||
// Deal with mezzanine to successor succession. | ||
BuildMI(MezzanineMBB, DebugLoc(), TII.get(AMDGPU::S_BRANCH)) | ||
.addMBB(Entry.SuccMBB); | ||
MezzanineMBB->addSuccessor(Entry.SuccMBB); | ||
|
||
// Move instructions to mezzanine block. | ||
moveBody(Entry.Body, *MezzanineMBB); | ||
|
||
for (MachineBasicBlock *PredMBB : Entry.PredMBBs) { | ||
// Deal with predecessor to mezzanine succession. | ||
MachineInstr &BranchIns = getBranchWithDest(*PredMBB, *Entry.SuccMBB); | ||
assert(BranchIns.getOperand(0).isMBB() && "Branch instruction isn't."); | ||
BranchIns.getOperand(0).setMBB(MezzanineMBB); | ||
PredMBB->replaceSuccessor(Entry.SuccMBB, MezzanineMBB); | ||
|
||
// Delete instructions that were lowered from epilog | ||
auto EpilogIt = ++EpilogIterator(BranchIns.getIterator()); | ||
while (!EpilogIt.isEnd()) | ||
EpilogIt++->eraseFromBundle(); | ||
} | ||
} | ||
} | ||
|
||
namespace std { | ||
template <> struct hash<Register> { | ||
std::size_t operator()(const Register &R) const { | ||
return hash<unsigned>()(R); | ||
} | ||
}; | ||
} // namespace std | ||
|
||
static inline void hoistUnrelatedCopies(MachineFunction &MF) { | ||
for (MachineBasicBlock &MBB : MF) | ||
for (MachineInstr &BranchMI : MBB) { | ||
if (!BranchMI.isBranch()) | ||
continue; | ||
|
||
unordered_set<Register> RelatedCopySources; | ||
EpilogIterator EpilogIt = BranchMI.getIterator(); | ||
EpilogIterator CopyMoveIt = ++EpilogIt; | ||
while (!EpilogIt.isEnd()) { | ||
if (EpilogIt->getOpcode() != AMDGPU::COPY) | ||
RelatedCopySources.insert(EpilogIt->getOperand(0).getReg()); | ||
++EpilogIt; | ||
} | ||
|
||
while (!CopyMoveIt.isEnd()) { | ||
EpilogIterator Next = CopyMoveIt; | ||
++Next; | ||
if (CopyMoveIt->getOpcode() == AMDGPU::COPY && | ||
!RelatedCopySources.count(CopyMoveIt->getOperand(1).getReg()) || | ||
CopyMoveIt->getOpcode() == AMDGPU::IMPLICIT_DEF) { | ||
MachineInstr &MIToMove = *CopyMoveIt; | ||
MIToMove.removeFromBundle(); | ||
MBB.insert(BranchMI.getIterator(), &MIToMove); | ||
} | ||
|
||
CopyMoveIt = Next; | ||
} | ||
} | ||
} |
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
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.
What is about fall through?
bb.1: successors: %bb.2(0x80000000)
bb.2: successors: %bb.5(0x40000000), %bb.3(0x40000000)
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.
It looked like the IR wasn't allowed to have fall-throughs at this stage, because I didn't see any and I did see unconditional-branch-to-next. Are fall-throughs instead allowed and just not common at this stage?
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.
The fall-throughs are not prohibited and I have seen enough valid MIR in SSA with fall-through CF.