[llvm] [CodeGen][MachineLoop] Fix getLoopID (PR #137820)
Vito Kortbeek via llvm-commits
llvm-commits at lists.llvm.org
Fri May 23 02:50:15 PDT 2025
https://github.com/kortbeek-snps updated https://github.com/llvm/llvm-project/pull/137820
>From c0d11615319de3e5bc12de7b5cc5e5628888ad10 Mon Sep 17 00:00:00 2001
From: Vito Kortbeek <kortbeek at synopsys.com>
Date: Tue, 29 Apr 2025 16:13:20 +0200
Subject: [PATCH 1/4] [CodeGen][MachineLoop] Fix getLoopID
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/lib/CodeGen/MachineLoopInfo.cpp | 60 ++++++++++------------------
1 file changed, 22 insertions(+), 38 deletions(-)
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;
}
>From a4bb98714e904cd71164403b11d1962f44fa1851 Mon Sep 17 00:00:00 2001
From: Vito Kortbeek <kortbeek at synopsys.com>
Date: Tue, 29 Apr 2025 16:20:15 +0200
Subject: [PATCH 2/4] [MachineBlockPlacement][X86] Add test for loops with
single latch that's not exiting
---
llvm/test/CodeGen/X86/code-align-loops.ll | 42 +++++++++++++++++++++++
1 file changed, 42 insertions(+)
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()
>From a31fac8d078c281adf10327cc847d2b166bacc6f Mon Sep 17 00:00:00 2001
From: Vito Kortbeek <kortbeek at synopsys.com>
Date: Fri, 23 May 2025 11:48:01 +0200
Subject: [PATCH 3/4] [MachineBlockPlacement][X86] Address test comments
---
llvm/test/CodeGen/X86/code-align-loops.ll | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/llvm/test/CodeGen/X86/code-align-loops.ll b/llvm/test/CodeGen/X86/code-align-loops.ll
index f3f36f575a37c..cd2bac54fee14 100644
--- a/llvm/test/CodeGen/X86/code-align-loops.ll
+++ b/llvm/test/CodeGen/X86/code-align-loops.ll
@@ -192,7 +192,7 @@ exit: ; preds = %bb2, %bb3, %bb4
; CHECK-LABEL: test5:
; ALIGN: .p2align 6
; ALIGN-NEXT: .LBB4_1: # %while.cond
-define dso_local i32 @test5(i32 %n) #0 {
+define i32 @test5(i32 %n) #0 {
entry:
%retval = alloca i32, align 4
%n.addr = alloca i32, align 4
@@ -202,20 +202,20 @@ entry:
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
+ %i.val = load i32, ptr %i, align 4
+ %n.val = load i32, ptr %n.addr, align 4
+ %cmp = icmp slt i32 %i.val, %n.val
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
+ %tmp = load i32, ptr %i, align 4
+ %inc = add nsw i32 %tmp, 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
+ %val = load i32, ptr %retval, align 4
+ ret i32 %val
}
>From 7f5a5f43f02ce23e35df115c1d1122d1acdd2322 Mon Sep 17 00:00:00 2001
From: Vito Kortbeek <kortbeek at synopsys.com>
Date: Fri, 23 May 2025 11:48:51 +0200
Subject: [PATCH 4/4] [CodeGen][MachineLoop] Address code comment
---
llvm/lib/CodeGen/MachineLoopInfo.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/CodeGen/MachineLoopInfo.cpp b/llvm/lib/CodeGen/MachineLoopInfo.cpp
index 4fc7fe4d52417..1c97e5c9063e4 100644
--- a/llvm/lib/CodeGen/MachineLoopInfo.cpp
+++ b/llvm/lib/CodeGen/MachineLoopInfo.cpp
@@ -192,8 +192,8 @@ MDNode *MachineLoop::getLoopID() const {
const auto *TI = BB->getTerminator();
if (!TI)
return nullptr;
- MDNode *MD = TI->getMetadata(LLVMContext::MD_loop);
+ MDNode *MD = TI->getMetadata(LLVMContext::MD_loop);
if (!MD)
return nullptr;
More information about the llvm-commits
mailing list