Skip to content

Commit e7195d5

Browse files
authored
[LoopInterchange] Fix crash when dbg_value exists right after PHI (llvm#208147)
When enabling reduction2mem with `-loop-interchange-reduction-to-mem` and the input IR contains `dbg_value`s immediately after a certain PHI node, assertion failure could be triggered due to the insertion position of newly created PHI node. This patch fixes the issue by changing IRBuilder ctor to be called. TBH, I'm not entirely sure what the difference is between `IRBuilder::IRBuilder(Instruction *)` and `IRBuilder::IRBuilder(BasicBlock *, BasicBlock::iterator)`, but judging from the generated code, the behavior with this patch seems reasonable. Taken over from llvm#183273
1 parent 70df3cc commit e7195d5

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

llvm/lib/Transforms/Scalar/LoopInterchange.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2074,7 +2074,7 @@ void LoopInterchangeTransform::reduction2Memory() {
20742074

20752075
LoopInterchangeLegality::InnerReduction SR = InnerReductions[0];
20762076
BasicBlock *InnerLoopHeader = InnerLoop->getHeader();
2077-
IRBuilder<> Builder(&*(InnerLoopHeader->getFirstNonPHIIt()));
2077+
IRBuilder<> Builder(InnerLoopHeader, InnerLoopHeader->getFirstNonPHIIt());
20782078

20792079
// Check if it's the first iteration.
20802080
LLVMContext &Context = InnerLoopHeader->getContext();
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
2+
; RUN: opt < %s -passes=loop-interchange -loop-interchange-profitabilities=ignore -loop-interchange-reduction-to-mem -S | FileCheck %s
3+
4+
define void @f(ptr %A) {
5+
; CHECK-LABEL: define void @f(
6+
; CHECK-SAME: ptr [[A:%.*]]) {
7+
; CHECK-NEXT: [[ENTRY:.*:]]
8+
; CHECK-NEXT: br label %[[FOR_J_PREHEADER:.*]]
9+
; CHECK: [[FOR_I_HEADER_PREHEADER:.*]]:
10+
; CHECK-NEXT: br label %[[FOR_I_HEADER:.*]]
11+
; CHECK: [[FOR_I_HEADER]]:
12+
; CHECK-NEXT: [[I:%.*]] = phi i64 [ [[I_NEXT:%.*]], %[[FOR_I_LATCH:.*]] ], [ 0, %[[FOR_I_HEADER_PREHEADER]] ]
13+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr double, ptr [[A]], i64 [[I]]
14+
; CHECK-NEXT: br label %[[FOR_J_SPLIT1:.*]]
15+
; CHECK: [[FOR_J_PREHEADER]]:
16+
; CHECK-NEXT: br label %[[FOR_J:.*]]
17+
; CHECK: [[FOR_J]]:
18+
; CHECK-NEXT: [[J:%.*]] = phi i64 [ [[TMP1:%.*]], %[[FOR_J_SPLIT:.*]] ], [ 0, %[[FOR_J_PREHEADER]] ]
19+
; CHECK-NEXT: [[RED:%.*]] = phi double [ [[RED_NEXT_LCSSA2:%.*]], %[[FOR_J_SPLIT]] ], [ 0.000000e+00, %[[FOR_J_PREHEADER]] ]
20+
; CHECK-NEXT: [[FIRST_ITER:%.*]] = phi i1 [ false, %[[FOR_J_SPLIT]] ], [ true, %[[FOR_J_PREHEADER]] ]
21+
; CHECK-NEXT: br label %[[FOR_I_HEADER_PREHEADER]]
22+
; CHECK: [[FOR_J_SPLIT1]]:
23+
; CHECK-NEXT: [[TMP0:%.*]] = load double, ptr [[GEP]], align 8
24+
; CHECK-NEXT: [[NEW_VAR:%.*]] = select i1 [[FIRST_ITER]], double 0.000000e+00, double [[TMP0]]
25+
; CHECK-NEXT: #dbg_value(i64 [[J]], [[META3:![0-9]+]], !DIExpression(), [[META10:![0-9]+]])
26+
; CHECK-NEXT: [[J_NEXT:%.*]] = add i64 [[J]], 1
27+
; CHECK-NEXT: [[RED_NEXT:%.*]] = fadd reassoc double 1.000000e+00, [[NEW_VAR]]
28+
; CHECK-NEXT: store double [[RED_NEXT]], ptr [[GEP]], align 8
29+
; CHECK-NEXT: [[EC_J:%.*]] = icmp eq i64 [[J_NEXT]], 10
30+
; CHECK-NEXT: br label %[[FOR_I_LATCH]]
31+
; CHECK: [[FOR_J_SPLIT]]:
32+
; CHECK-NEXT: [[RED_NEXT_LCSSA2]] = phi double [ [[RED_NEXT]], %[[FOR_I_LATCH]] ]
33+
; CHECK-NEXT: [[RED_NEXT_LCSSA:%.*]] = phi double [ [[RED_NEXT]], %[[FOR_I_LATCH]] ]
34+
; CHECK-NEXT: [[TMP1]] = add i64 [[J]], 1
35+
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i64 [[TMP1]], 10
36+
; CHECK-NEXT: br i1 [[TMP2]], label %[[EXIT:.*]], label %[[FOR_J]]
37+
; CHECK: [[FOR_I_LATCH]]:
38+
; CHECK-NEXT: [[I_NEXT]] = add i64 [[I]], 1
39+
; CHECK-NEXT: [[EC_I:%.*]] = icmp eq i64 [[I_NEXT]], 10
40+
; CHECK-NEXT: br i1 [[EC_I]], label %[[FOR_J_SPLIT]], label %[[FOR_I_HEADER]]
41+
; CHECK: [[EXIT]]:
42+
; CHECK-NEXT: ret void
43+
;
44+
entry:
45+
br label %for.i.header
46+
47+
for.i.header:
48+
%i = phi i64 [ 0, %entry ], [ %i.next, %for.i.latch ]
49+
%gep = getelementptr double, ptr %A, i64 %i
50+
br label %for.j
51+
52+
for.j:
53+
%j = phi i64 [ 0, %for.i.header ], [ %j.next, %for.j ]
54+
%red = phi double [ 0.000000e+00, %for.i.header ], [ %red.next, %for.j ]
55+
#dbg_value(i64 %j, !3, !DIExpression(), !13)
56+
%j.next = add i64 %j, 1
57+
%red.next = fadd reassoc double 1.000000e+00, %red
58+
%ec.j = icmp eq i64 %j.next, 10
59+
br i1 %ec.j, label %for.i.latch, label %for.j
60+
61+
for.i.latch:
62+
store double %red.next, ptr %gep
63+
%i.next = add i64 %i, 1
64+
%ec.i = icmp eq i64 %i.next, 10
65+
br i1 %ec.i, label %exit, label %for.i.header
66+
67+
exit:
68+
ret void
69+
}
70+
71+
!llvm.dbg.cu = !{!0}
72+
!llvm.module.flags = !{!2}
73+
74+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug)
75+
!1 = !DIFile(filename: "test.c", directory: "/home/abc/wyy/test/FirstIter_DbgRecords", checksumkind: CSK_MD5, checksum: "69b1c47396b37bc8b48d5517efe4fb54")
76+
!2 = !{i32 2, !"Debug Info Version", i32 3}
77+
!3 = !DILocalVariable(name: "j", scope: !4, file: !1, line: 7, type: !12)
78+
!4 = distinct !DILexicalBlock(scope: !8, file: !1, line: 7, column: 5)
79+
!8 = distinct !DISubprogram(name: "func", scope: !1, file: !1, line: 2, type: !9, scopeLine: 2, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11, keyInstructions: true)
80+
!9 = !DISubroutineType(types: !10)
81+
!10 = !{null}
82+
!11 = !{}
83+
!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
84+
!13 = !DILocation(line: 0, scope: !4)
85+
;.
86+
; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug)
87+
; CHECK: [[META1]] = !DIFile(filename: "{{.*}}test.c", directory: {{.*}}, checksumkind: CSK_MD5, checksum: "69b1c47396b37bc8b48d5517efe4fb54")
88+
; CHECK: [[META3]] = !DILocalVariable(name: "j", scope: [[META4:![0-9]+]], file: [[META1]], line: 7, type: [[META9:![0-9]+]])
89+
; CHECK: [[META4]] = distinct !DILexicalBlock(scope: [[META5:![0-9]+]], file: [[META1]], line: 7, column: 5)
90+
; CHECK: [[META5]] = distinct !DISubprogram(name: "func", scope: [[META1]], file: [[META1]], line: 2, type: [[META6:![0-9]+]], scopeLine: 2, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]], retainedNodes: [[META8:![0-9]+]], keyInstructions: true)
91+
; CHECK: [[META6]] = !DISubroutineType(types: [[META7:![0-9]+]])
92+
; CHECK: [[META7]] = !{null}
93+
; CHECK: [[META8]] = !{}
94+
; CHECK: [[META9]] = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
95+
; CHECK: [[META10]] = !DILocation(line: 0, scope: [[META4]])
96+
;.

0 commit comments

Comments
 (0)