-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[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
Conversation
Mirror the getLoopID implementation of LoopInfo in MachineLoopInfo. getLoopID used findLoopControlBlock to detect the special case where there is a single latch. However, findLoopControlBlock returns the exiting block if the latch is not an exiting block. The middle end places the LoopID metadata on the latch regardless of if it's an exiting block or not.
@llvm/pr-subscribers-backend-x86 Author: Vito Kortbeek (kortbeek-snps) ChangesMirror the
I raised this issue in the PR that introduced the I've mirrored the implementation of I've also updated the test associated with #71026 ( Let me know if you have any comments, @FreddyLeaf ! Full diff: https://github.com/llvm/llvm-project/pull/137820.diff 2 Files Affected:
diff --git a/llvm/lib/CodeGen/MachineLoopInfo.cpp b/llvm/lib/CodeGen/MachineLoopInfo.cpp
index d6906bacde0e2..4fc7fe4d52417 100644
--- a/llvm/lib/CodeGen/MachineLoopInfo.cpp
+++ b/llvm/lib/CodeGen/MachineLoopInfo.cpp
@@ -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 ||
+ LoopID->getOperand(0) != LoopID)
+ return nullptr;
+
return LoopID;
}
diff --git a/llvm/test/CodeGen/X86/code-align-loops.ll b/llvm/test/CodeGen/X86/code-align-loops.ll
index 68ae49792ed18..f3f36f575a37c 100644
--- a/llvm/test/CodeGen/X86/code-align-loops.ll
+++ b/llvm/test/CodeGen/X86/code-align-loops.ll
@@ -177,6 +177,48 @@ exit: ; preds = %bb2, %bb3, %bb4
ret void
}
+; test5 is to check if .p2align can be correctly set on loops with a single
+; latch that's not the exiting block.
+; The test IR is generated from below simple C file:
+; $ clang -O0 -S -emit-llvm loop.c
+; $ cat loop.c
+; int test5(int n) {
+; int i = 0;
+; [[clang::code_align(64)]]
+; while (i < n) {
+; i++;
+; }
+; }
+; CHECK-LABEL: test5:
+; ALIGN: .p2align 6
+; ALIGN-NEXT: .LBB4_1: # %while.cond
+define dso_local i32 @test5(i32 %n) #0 {
+entry:
+ %retval = alloca i32, align 4
+ %n.addr = alloca i32, align 4
+ %i = alloca i32, align 4
+ store i32 %n, ptr %n.addr, align 4
+ store i32 0, ptr %i, align 4
+ br label %while.cond
+
+while.cond: ; preds = %while.body, %entry
+ %0 = load i32, ptr %i, align 4
+ %1 = load i32, ptr %n.addr, align 4
+ %cmp = icmp slt i32 %0, %1
+ br i1 %cmp, label %while.body, label %while.end
+
+while.body: ; preds = %while.cond
+ %2 = load i32, ptr %i, align 4
+ %inc = add nsw i32 %2, 1
+ store i32 %inc, ptr %i, align 4
+ br label %while.cond, !llvm.loop !0
+
+while.end: ; preds = %while.cond
+ %3 = load i32, ptr %retval, align 4
+ ret i32 %3
+}
+
+
declare void @bar()
declare void @var()
|
Ping @phoebewang @KanRobert @FreddyLeaf Any thoughts on this? Or any other people I can tag to review this? |
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.
LGTM.
Changes look good to me. Sorry just back from a long leave. |
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.
This is a lot simpler and the old code definitely shouldn't have been trying to look at the underlying IR
(LoopID->getNumOperands() == 0 || LoopID->getOperand(0) != LoopID)) | ||
LoopID = nullptr; | ||
|
||
if (!LoopID || LoopID->getNumOperands() == 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.
Is the getNumOperands() == 0 check really necessary? I would hope the verifier rejects cases with missing operands (but you are just moving existing code)
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.
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 LoopID
operands and run opt -passes=verify code-align-loops.ll -disable-output
(or llc
), I get an assert during the LLVMIR parsing:
llvm/lib/IR/DebugInfo.cpp:536: MDNode *stripDebugLocFromLoopID(MDNode *): Assertion !N->operands().empty() && "Missing self reference?"' failed.
I do agree that it feels a bit out of place to check the validity of the metadata here, but since LoopInfo.cpp::Loop::getLoopID()
does the same, It might not be too strange?
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.
Unrelated to this pr, the IR verifier should be verifying these. The MIR verifier should also, but that is more awkward
Mirror the `getLoopID()` implementation of `LoopInfo` in `MachineLoopInfo`. `getLoopID` used `findLoopControlBlock` to detect the special case where there is a single latch. However, `findLoopControlBlock` returns the exiting block if the latch is not an exiting block. The middle end places the `LoopID` metadata on the latch regardless of if it's an exiting block or not. I raised this issue in the PR that introduced the `getLoopID()` helper (llvm#71026 (comment)) and @FreddyLeaf confirmed this is a bug and asked me to help implement a refinement. I've mirrored the implementation of `LoopInfo` instead of simply changing `findLoopControlBlock()` to `findLoopControlBlock()` to keep the two implementations consistent. The only difference between the two is that `MachineLoopInfo::getLoopID` initially starts out with a `MachineBacisBlock` and attempts to retrieve the `BasicBlock` (if it wasn't for this difference, I would have moved it to `genericLoopInfo`). I've also updated the test associated with llvm#71026 (`test5`) that check the alignment for a loop with a single latch that's not the exit. This test will fail for the current implementation. I'm not sure if we want to include this test upstream (it might look out of place after we remove the 'single-latch-specialization' from `getLoopID()`). Let me know if you have any comments, @FreddyLeaf !
Mirror the
getLoopID()
implementation ofLoopInfo
inMachineLoopInfo
.getLoopID
usedfindLoopControlBlock
to detect the special case where there is a single latch. However,findLoopControlBlock
returns the exiting block if the latch is not an exiting block. The middle end places theLoopID
metadata on thelatch regardless of if it's an exiting block or not.
I raised this issue in the PR that introduced the
getLoopID()
helper (#71026 (comment)) and @FreddyLeaf confirmed this is a bug and asked me to help implement a refinement.I've mirrored the implementation of
LoopInfo
instead of simply changingfindLoopControlBlock()
tofindLoopControlBlock()
to keep the two implementations consistent. The only difference between the two is thatMachineLoopInfo::getLoopID
initially starts out with aMachineBacisBlock
and attempts to retrieve theBasicBlock
(if it wasn't for this difference, I would have moved it togenericLoopInfo
).I've also updated the test associated with #71026 (
test5
) that check the alignment for a loop with a single latch that's not the exit. This test will fail for the current implementation. I'm not sure if we want to include this test upstream (it might look out of place after we remove the 'single-latch-specialization' fromgetLoopID()
).Let me know if you have any comments, @FreddyLeaf !