-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[CodeGen][MachineLoop] Fix getLoopID #137820
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
Changes from 2 commits
c0d1161
a4bb987
a31fac8
7f5a5f4
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 |
|---|---|---|
|
|
@@ -181,48 +181,32 @@ MachineLoopInfo::findLoopPreheader(MachineLoop *L, bool SpeculativePreheader, | |
|
|
||
| MDNode *MachineLoop::getLoopID() const { | ||
| MDNode *LoopID = nullptr; | ||
| if (const auto *MBB = findLoopControlBlock()) { | ||
| // If there is a single latch block, then the metadata | ||
| // node is attached to its terminating instruction. | ||
|
|
||
| // Go through the latch blocks and check the terminator for the metadata | ||
| SmallVector<MachineBasicBlock *, 4> LatchesBlocks; | ||
| getLoopLatches(LatchesBlocks); | ||
| for (const auto *MBB : LatchesBlocks) { | ||
| const auto *BB = MBB->getBasicBlock(); | ||
| if (!BB) | ||
| return nullptr; | ||
| if (const auto *TI = BB->getTerminator()) | ||
| LoopID = TI->getMetadata(LLVMContext::MD_loop); | ||
| } else if (const auto *MBB = getHeader()) { | ||
| // There seem to be multiple latch blocks, so we have to | ||
| // visit all predecessors of the loop header and check | ||
| // their terminating instructions for the metadata. | ||
| if (const auto *Header = MBB->getBasicBlock()) { | ||
| // Walk over all blocks in the loop. | ||
| for (const auto *MBB : this->blocks()) { | ||
| const auto *BB = MBB->getBasicBlock(); | ||
| if (!BB) | ||
| return nullptr; | ||
| const auto *TI = BB->getTerminator(); | ||
| if (!TI) | ||
| return nullptr; | ||
| MDNode *MD = nullptr; | ||
| // Check if this terminating instruction jumps to the loop header. | ||
| for (const auto *Succ : successors(TI)) { | ||
| if (Succ == Header) { | ||
| // This is a jump to the header - gather the metadata from it. | ||
| MD = TI->getMetadata(LLVMContext::MD_loop); | ||
| break; | ||
| } | ||
| } | ||
| if (!MD) | ||
| continue; | ||
| if (!LoopID) | ||
| LoopID = MD; | ||
| else if (MD != LoopID) | ||
| return nullptr; | ||
| } | ||
| } | ||
| const auto *TI = BB->getTerminator(); | ||
| if (!TI) | ||
| return nullptr; | ||
| MDNode *MD = TI->getMetadata(LLVMContext::MD_loop); | ||
|
|
||
| if (!MD) | ||
| return nullptr; | ||
|
|
||
| if (!LoopID) | ||
| LoopID = MD; | ||
| else if (MD != LoopID) | ||
| return nullptr; | ||
| } | ||
| if (LoopID && | ||
| (LoopID->getNumOperands() == 0 || LoopID->getOperand(0) != LoopID)) | ||
| LoopID = nullptr; | ||
|
|
||
| if (!LoopID || LoopID->getNumOperands() == 0 || | ||
|
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. Is the getNumOperands() == 0 check really necessary? I would hope the verifier rejects cases with missing operands (but you are just moving existing code)
Contributor
Author
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 was indeed just moving code. However, I don't find any checks for this in the verifier, but I'm not too familiar with it. If I just experimentally remove the I do agree that it feels a bit out of place to check the validity of the metadata here, but since
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. Unrelated to this pr, the IR verifier should be verifying these. The MIR verifier should also, but that is more awkward |
||
| LoopID->getOperand(0) != LoopID) | ||
| return nullptr; | ||
|
|
||
| return LoopID; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.