Skip to content

Commit e03b1b8

Browse files
[CodeGen][MachineLoop] Fix getLoopID (#137820)
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 (#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 #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 !
1 parent b5663d0 commit e03b1b8

File tree

2 files changed

+64
-38
lines changed

2 files changed

+64
-38
lines changed

llvm/lib/CodeGen/MachineLoopInfo.cpp

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -181,48 +181,32 @@ MachineLoopInfo::findLoopPreheader(MachineLoop *L, bool SpeculativePreheader,
181181

182182
MDNode *MachineLoop::getLoopID() const {
183183
MDNode *LoopID = nullptr;
184-
if (const auto *MBB = findLoopControlBlock()) {
185-
// If there is a single latch block, then the metadata
186-
// node is attached to its terminating instruction.
184+
185+
// Go through the latch blocks and check the terminator for the metadata
186+
SmallVector<MachineBasicBlock *, 4> LatchesBlocks;
187+
getLoopLatches(LatchesBlocks);
188+
for (const auto *MBB : LatchesBlocks) {
187189
const auto *BB = MBB->getBasicBlock();
188190
if (!BB)
189191
return nullptr;
190-
if (const auto *TI = BB->getTerminator())
191-
LoopID = TI->getMetadata(LLVMContext::MD_loop);
192-
} else if (const auto *MBB = getHeader()) {
193-
// There seem to be multiple latch blocks, so we have to
194-
// visit all predecessors of the loop header and check
195-
// their terminating instructions for the metadata.
196-
if (const auto *Header = MBB->getBasicBlock()) {
197-
// Walk over all blocks in the loop.
198-
for (const auto *MBB : this->blocks()) {
199-
const auto *BB = MBB->getBasicBlock();
200-
if (!BB)
201-
return nullptr;
202-
const auto *TI = BB->getTerminator();
203-
if (!TI)
204-
return nullptr;
205-
MDNode *MD = nullptr;
206-
// Check if this terminating instruction jumps to the loop header.
207-
for (const auto *Succ : successors(TI)) {
208-
if (Succ == Header) {
209-
// This is a jump to the header - gather the metadata from it.
210-
MD = TI->getMetadata(LLVMContext::MD_loop);
211-
break;
212-
}
213-
}
214-
if (!MD)
215-
continue;
216-
if (!LoopID)
217-
LoopID = MD;
218-
else if (MD != LoopID)
219-
return nullptr;
220-
}
221-
}
192+
const auto *TI = BB->getTerminator();
193+
if (!TI)
194+
return nullptr;
195+
196+
MDNode *MD = TI->getMetadata(LLVMContext::MD_loop);
197+
if (!MD)
198+
return nullptr;
199+
200+
if (!LoopID)
201+
LoopID = MD;
202+
else if (MD != LoopID)
203+
return nullptr;
222204
}
223-
if (LoopID &&
224-
(LoopID->getNumOperands() == 0 || LoopID->getOperand(0) != LoopID))
225-
LoopID = nullptr;
205+
206+
if (!LoopID || LoopID->getNumOperands() == 0 ||
207+
LoopID->getOperand(0) != LoopID)
208+
return nullptr;
209+
226210
return LoopID;
227211
}
228212

llvm/test/CodeGen/X86/code-align-loops.ll

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,48 @@ exit: ; preds = %bb2, %bb3, %bb4
177177
ret void
178178
}
179179

180+
; test5 is to check if .p2align can be correctly set on loops with a single
181+
; latch that's not the exiting block.
182+
; The test IR is generated from below simple C file:
183+
; $ clang -O0 -S -emit-llvm loop.c
184+
; $ cat loop.c
185+
; int test5(int n) {
186+
; int i = 0;
187+
; [[clang::code_align(64)]]
188+
; while (i < n) {
189+
; i++;
190+
; }
191+
; }
192+
; CHECK-LABEL: test5:
193+
; ALIGN: .p2align 6
194+
; ALIGN-NEXT: .LBB4_1: # %while.cond
195+
define i32 @test5(i32 %n) #0 {
196+
entry:
197+
%retval = alloca i32, align 4
198+
%n.addr = alloca i32, align 4
199+
%i = alloca i32, align 4
200+
store i32 %n, ptr %n.addr, align 4
201+
store i32 0, ptr %i, align 4
202+
br label %while.cond
203+
204+
while.cond: ; preds = %while.body, %entry
205+
%i.val = load i32, ptr %i, align 4
206+
%n.val = load i32, ptr %n.addr, align 4
207+
%cmp = icmp slt i32 %i.val, %n.val
208+
br i1 %cmp, label %while.body, label %while.end
209+
210+
while.body: ; preds = %while.cond
211+
%tmp = load i32, ptr %i, align 4
212+
%inc = add nsw i32 %tmp, 1
213+
store i32 %inc, ptr %i, align 4
214+
br label %while.cond, !llvm.loop !0
215+
216+
while.end: ; preds = %while.cond
217+
%val = load i32, ptr %retval, align 4
218+
ret i32 %val
219+
}
220+
221+
180222
declare void @bar()
181223
declare void @var()
182224

0 commit comments

Comments
 (0)