Skip to content

Commit ac9a466

Browse files
committed
[LoopPeel] Insert new phis before first non-PHI when peeling last iter.
Make sure the new phis are inserted before any non-phi instructions. This fixes a crash when dbg_value instructions are present in the original exit block.
1 parent 6da8f3b commit ac9a466

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

llvm/lib/Transforms/Utils/LoopPeel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ static void cloneLoopBlocks(
922922
// InsertTop, using the incoming value from the preheader for the original
923923
// preheader (when skipping the main loop) and the incoming value from the
924924
// latch for the latch (when continuing from the main loop).
925-
IRBuilder<> B(InsertTop->getTerminator());
925+
IRBuilder<> B(InsertTop, InsertTop->getFirstNonPHIIt());
926926
for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
927927
PHINode *NewPHI = cast<PHINode>(VMap[&*I]);
928928
PHINode *PN = B.CreatePHI(NewPHI->getType(), 2);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -p loop-unroll -unroll-full-max-count=0 -S %s | FileCheck %s
3+
4+
declare void @foo(i32)
5+
6+
define void @test_dbg_value_in_exit_block() {
7+
; CHECK-LABEL: define void @test_dbg_value_in_exit_block() {
8+
; CHECK-NEXT: [[ENTRY:.*]]:
9+
; CHECK-NEXT: br label %[[LOOP:.*]]
10+
; CHECK: [[LOOP]]:
11+
; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[INC:%.*]], %[[LOOP]] ]
12+
; CHECK-NEXT: call void @foo(i32 0)
13+
; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[IV]], 1
14+
; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[INC]], 20
15+
; CHECK-NEXT: br i1 [[EXITCOND]], label %[[LOOP]], label %[[EXIT_PEEL_BEGIN:.*]], !llvm.loop [[LOOP4:![0-9]+]]
16+
; CHECK: [[EXIT_PEEL_BEGIN]]:
17+
; CHECK-NEXT: [[TMP0:%.*]] = phi i32 [ [[INC]], %[[LOOP]] ]
18+
; CHECK-NEXT: #dbg_value(float poison, [[META6:![0-9]+]], !DIExpression(), [[META11:![0-9]+]])
19+
; CHECK-NEXT: br label %[[LOOP_PEEL:.*]]
20+
; CHECK: [[LOOP_PEEL]]:
21+
; CHECK-NEXT: [[CMP2_PEEL:%.*]] = icmp eq i32 [[TMP0]], 20
22+
; CHECK-NEXT: [[SEL_PEEL:%.*]] = select i1 [[CMP2_PEEL]], i32 1, i32 0
23+
; CHECK-NEXT: call void @foo(i32 [[SEL_PEEL]])
24+
; CHECK-NEXT: [[INC_PEEL:%.*]] = add i32 [[TMP0]], 1
25+
; CHECK-NEXT: [[EXITCOND_PEEL:%.*]] = icmp ne i32 [[INC_PEEL]], 21
26+
; CHECK-NEXT: br i1 [[EXITCOND_PEEL]], label %[[EXIT_PEEL_NEXT:.*]], label %[[EXIT_PEEL_NEXT]]
27+
; CHECK: [[EXIT_PEEL_NEXT]]:
28+
; CHECK-NEXT: br label %[[LOOP_PEEL_NEXT:.*]]
29+
; CHECK: [[LOOP_PEEL_NEXT]]:
30+
; CHECK-NEXT: br label %[[EXIT:.*]]
31+
; CHECK: [[EXIT]]:
32+
; CHECK-NEXT: ret void
33+
;
34+
entry:
35+
br label %loop
36+
37+
loop:
38+
%iv = phi i32 [ 0, %entry ], [ %inc, %loop ]
39+
%cmp2 = icmp eq i32 %iv, 20
40+
%sel = select i1 %cmp2, i32 1, i32 0
41+
call void @foo(i32 %sel)
42+
%inc = add i32 %iv, 1
43+
%exitcond = icmp ne i32 %inc, 21
44+
br i1 %exitcond, label %loop, label %exit
45+
46+
exit:
47+
#dbg_value(float poison, !4, !DIExpression(), !9)
48+
ret void
49+
}
50+
51+
!llvm.dbg.cu = !{!0}
52+
!llvm.module.flags = !{!3}
53+
54+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 21.0.0git", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: !2, splitDebugInlining: false, nameTableKind: None)
55+
!1 = !DIFile(filename: "repro.c", directory: "/", checksumkind: CSK_MD5, checksum: "3a182b2d218eb10b64164b06427cef3c")
56+
!2 = !{}
57+
!3 = !{i32 2, !"Debug Info Version", i32 3}
58+
!4 = !DILocalVariable(name: "m_2", scope: !5, file: !1, line: 4, type: !8)
59+
!5 = distinct !DISubprogram(name: "test_dbg_value_in_exit_block", scope: !1, file: !1, line: 3, type: !6, scopeLine: 3, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
60+
!6 = !DISubroutineType(types: !7)
61+
!7 = !{null}
62+
!8 = !DIBasicType(name: "float", size: 32, encoding: DW_ATE_float)
63+
!9 = !DILocation(line: 0, scope: !5)
64+
;.
65+
; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], producer: "{{.*}}clang version {{.*}}", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: [[META2:![0-9]+]], splitDebugInlining: false, nameTableKind: None)
66+
; CHECK: [[META1]] = !DIFile(filename: "{{.*}}repro.c", directory: {{.*}})
67+
; CHECK: [[META2]] = !{}
68+
; CHECK: [[LOOP4]] = distinct !{[[LOOP4]], [[META5:![0-9]+]]}
69+
; CHECK: [[META5]] = !{!"llvm.loop.peeled.count", i32 1}
70+
; CHECK: [[META6]] = !DILocalVariable(name: "m_2", scope: [[META7:![0-9]+]], file: [[META1]], line: 4, type: [[META10:![0-9]+]])
71+
; CHECK: [[META7]] = distinct !DISubprogram(name: "test_dbg_value_in_exit_block", scope: [[META1]], file: [[META1]], line: 3, type: [[META8:![0-9]+]], scopeLine: 3, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]], retainedNodes: [[META2]])
72+
; CHECK: [[META8]] = !DISubroutineType(types: [[META9:![0-9]+]])
73+
; CHECK: [[META9]] = !{null}
74+
; CHECK: [[META10]] = !DIBasicType(name: "float", size: 32, encoding: DW_ATE_float)
75+
; CHECK: [[META11]] = !DILocation(line: 0, scope: [[META7]])
76+
;.

0 commit comments

Comments
 (0)