Skip to content

Commit f70cdc5

Browse files
committed
[NPM] Properly reset parent loop after loop passes
This fixes https://bugs.llvm.org/show_bug.cgi?id=49185 When `NDEBUG` is not set, `LPMUpdater` checks if the added loops have the same parent loop as the current one in `addSiblingLoops`. If multiple loop passes are executed through `LoopPassManager`, `U.ParentL` will be the same across all passes. However, the parent loop might change after running a loop pass, resulting in assertion failures in subsequent passes. This patch resets `U.ParentL` after running individual loop passes in `LoopPassManager`. Reviewed By: asbirlea, ychen Differential Revision: https://reviews.llvm.org/D96727
1 parent a77e918 commit f70cdc5

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

llvm/include/llvm/Transforms/Scalar/LoopPassManager.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ class LPMUpdater {
271271
SkipCurrentLoop = true;
272272
}
273273

274+
void setParentLoop(Loop *L) {
275+
#ifndef NDEBUG
276+
ParentL = L;
277+
#endif
278+
}
279+
274280
/// Loop passes should use this method to indicate they have added new child
275281
/// loops of the current loop.
276282
///

llvm/lib/Transforms/Scalar/LoopPassManager.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ LoopPassManager::runWithLoopNestPasses(Loop &L, LoopAnalysisManager &AM,
114114
// Check if the current pass preserved the loop-nest object or not.
115115
IsLoopNestPtrValid &= PassPA->getChecker<LoopNestAnalysis>().preserved();
116116

117+
// After running the loop pass, the parent loop might change and we need to
118+
// notify the updater, otherwise U.ParentL might gets outdated and triggers
119+
// assertion failures in addSiblingLoops and addChildLoops.
120+
U.setParentLoop(L.getParentLoop());
121+
117122
// FIXME: Historically, the pass managers all called the LLVM context's
118123
// yield function here. We don't have a generic way to acquire the
119124
// context and it isn't yet clear what the right pattern is for yielding
@@ -158,6 +163,11 @@ LoopPassManager::runWithoutLoopNestPasses(Loop &L, LoopAnalysisManager &AM,
158163
// aggregate preserved set for this pass manager.
159164
PA.intersect(std::move(*PassPA));
160165

166+
// After running the loop pass, the parent loop might change and we need to
167+
// notify the updater, otherwise U.ParentL might gets outdated and triggers
168+
// assertion failures in addSiblingLoops and addChildLoops.
169+
U.setParentLoop(L.getParentLoop());
170+
161171
// FIXME: Historically, the pass managers all called the LLVM context's
162172
// yield function here. We don't have a generic way to acquire the
163173
// context and it isn't yet clear what the right pattern is for yielding

llvm/test/Transforms/Util/pr49185.ll

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
; REQUIRES: asserts
2+
; RUN: opt -S -passes='default<O3>' %s
3+
%struct0 = type { i64, i64, i32, i64, i32 }
4+
%struct1 = type { i32 }
5+
%union0 = type { i32 }
6+
%union1 = type { i16 }
7+
%union2 = type { i32 }
8+
9+
@g_6 = external dso_local global i32, align 1
10+
@g_60 = external dso_local global i16, align 1
11+
@g_79 = external dso_local global { i16, i16 }, align 1
12+
@g_315 = external dso_local global %struct0, align 1
13+
@g_359 = external dso_local global %struct0, align 1
14+
15+
define dso_local i16 @main(i16 %argc, i16** %argv) #0 {
16+
entry:
17+
%call2 = call i16 @func_1()
18+
unreachable
19+
}
20+
21+
define internal i16 @func_1() #0 {
22+
entry:
23+
%call = call i16 @func_21(i32* undef, i32 undef, i32* undef)
24+
ret i16 undef
25+
}
26+
27+
define internal i16 @func_21(i32* %p_22, i32 %p_23, i32* %p_24) #0 {
28+
entry:
29+
call void @func_34(%struct0* align 1 undef, i32 undef, i32 undef, i32* @g_6, %union0* byval(%union0) align 1 undef)
30+
unreachable
31+
}
32+
33+
define internal void @func_34(%struct0* %agg.result, i32 %p_35, i32 %p_36, i32* %p_37, %union0* %p_38) #0 {
34+
entry:
35+
%p_37.addr = alloca i32*, align 1
36+
%cleanup.dest.slot = alloca i32, align 1
37+
store i32* %p_37, i32** %p_37.addr, align 1
38+
br label %lbl_898
39+
40+
lbl_898: ; preds = %cleanup3097, %entry
41+
br label %lbl_1111
42+
43+
lbl_1111: ; preds = %cleanup3097, %lbl_898
44+
%0 = load i32, i32* getelementptr inbounds (%struct0, %struct0* @g_359, i32 0, i32 4), align 1
45+
%tobool1833 = icmp ne i32 %0, 0
46+
br i1 %tobool1833, label %land.rhs1834, label %land.end1851
47+
48+
land.rhs1834: ; preds = %lbl_1111
49+
store i16 0, i16* @g_60, align 1
50+
br label %land.end1851
51+
52+
land.end1851: ; preds = %land.rhs1834, %lbl_1111
53+
%1 = load i32*, i32** %p_37.addr, align 1
54+
%2 = load i32, i32* %1, align 1
55+
%tobool2351 = icmp ne i32 %2, 0
56+
br i1 %tobool2351, label %if.then2352, label %if.else3029
57+
58+
if.then2352: ; preds = %land.end1851
59+
%3 = load i16, i16* getelementptr inbounds ({ i16, i16 }, { i16, i16 }* @g_79, i32 0, i32 0), align 1, !tbaa !1
60+
%tobool3011 = icmp ne i16 %3, 0
61+
call void @llvm.assume(i1 %tobool3011)
62+
store i32 11, i32* %cleanup.dest.slot, align 1
63+
br label %cleanup3097
64+
65+
if.else3029: ; preds = %land.end1851
66+
store i32 3, i32* getelementptr inbounds (%struct0, %struct0* @g_315, i32 0, i32 4), align 1
67+
store i32 132, i32* %cleanup.dest.slot, align 1
68+
br label %cleanup3097
69+
70+
cleanup3097: ; preds = %if.else3029, %if.then2352
71+
%cleanup.dest3113 = load i32, i32* %cleanup.dest.slot, align 1
72+
switch i32 %cleanup.dest3113, label %cleanup3402 [
73+
i32 132, label %lbl_1111
74+
i32 11, label %lbl_898
75+
]
76+
77+
cleanup3402: ; preds = %cleanup3097
78+
ret void
79+
}
80+
81+
; Function Attrs: nofree nosync nounwind willreturn
82+
declare void @llvm.assume(i1 noundef) #4
83+
84+
attributes #0 = { "use-soft-float"="false" }
85+
attributes #1 = { argmemonly nofree nosync nounwind willreturn }
86+
attributes #2 = { noinline }
87+
attributes #3 = { argmemonly nofree nosync nounwind willreturn writeonly }
88+
attributes #4 = { nofree nosync nounwind willreturn }
89+
90+
!llvm.ident = !{!0}
91+
92+
!0 = !{!"clang version 13.0.0"}
93+
!1 = !{!2, !2, i64 0}
94+
!2 = !{!"omnipotent char", !3, i64 0}
95+
!3 = !{!"Simple C/C++ TBAA"}

0 commit comments

Comments
 (0)